Title: Class Prep
1Class Prep
- Bring to Class
- Towers of Hanoi
- SuDoku Sheets
- In-Class Exercises
- Paper for Classroom Printer
- Copy Sample Programs
- Run MATLAB
- Set Current Directory
2Grab LOTS of Files
- While we are waiting for class to start, copy
towers.m, move.m, cs70ex8a.m, sudoku.m,
fillSudoku.m, cellOK.m, sudoku2.txt,
TestCountBlobs.m, CountBlobs.m, MarkBlobs.m,
findPath.m, xmaze.txt, hampton.txt to your
directory - Run MATLAB and set Current Directory
3Week 08-a(9.9-9.10)
- Advanced Recursive Applications
4Survivor Thailand Episode 3
"Day 9 and it's Immunity Challenge time. It's a
variation of the Hanoi Tower puzzle game. A model
of a Thai temple is made of several stackable
pieces, each smaller than the one below. The
model must be disassembled and re-assembled using
three platforms. They may only stack smaller
pieces on larger ones, and they must be in a
circled area around each platform to move a
piece. This is an easy one for Ted, who knows
math well being a programmer. Chuay Gahn takes an
early lead and never looks back. Shii Ann admits
later that she knew what to do, but kept her
mouth shut. She thinks it's time to get rid of
some people. Sook Jai, under guidance of Jed,
loses their first immunity challenge."
5Towers of Hanoi Game
- Object Move all disks from one platform (pin) to
another platform (pin) - Can only move one disk at a time
- Can't stack larger disk on top of a smaller disk
- Have one additional platform (pin) to use as
auxiliary - (Original problem was for Tibetan Priests to move
64 disks following these rules) - Classic Recursion
- If only one disk to move "Just Do It"
- if not, reduce to simpler problem (one less disk)
- WEB Versions (1), (2)
6Our Problem
- Todays problem is to write a program that
generates the instructions to move the disks.
Source
Auxiliary
Destination
7Design of the recursive method move
- Lets call the 3 towers Source, Auxiliary,
Destination, - Base case What is an instance of the problem
that is trivial? - n 1
Source
Auxiliary
Destination
Output the instruction to move the top disk from
Source to Destination Move top disk from Source
to Destination
8Design of the recursive method move
- Induction Step n 1
- How can recursion help us out?
a. Recursively move n-1 disks from Source to
Auxiliary.
9Design of the recursive method move
- Induction Step n 1
- How can recursion help us out?
b. Move the one remaining disk from Source to
Destination.
10Design of the Recursive Function tower()
- Induction Step n 1
- How can recursion help us out?
c. Recursively move n-1 disks from Auxiliary to
Destination
11Design of the Recursive Function tower()
- Induction Step n 1
- How can recursion help us out?
d. Were done!
12Algorithm
- We can combine these steps into the following
algorithm - 0. Receive n, Source, Auxiliary, Destination
- 1. If n 1
- a. Move n-1 disks from Source to Auxiliary
(using Destination) - b. Move 1 disk from Source to Destination
(using Auxiliary) - c. Move n-1 disks from Auxiliary to Destination
(using Source) - Else
- Display Move the top disk from , Source, to
, Destination. - End if.
13Hands-On DEMO Towers of Hanoi
- User enters number of disks, Program generates a
set of instructions for moving the disks
Towers of Hanoi by Dave Hannay Recursive
Solution using "move" function clear, clc, format
compact numDisks input('Enter the number of
disks') move(numDisks, 'A', 'C', 'B')
14Hands-On DEMO move Function
- function move(n, from, to, using)
- if (n 1)
- fprintf('Move disk d from peg s to peg
s\n', n, from, to) - else
- move(n-1, from, using, to)
- fprintf('Move disk d from peg s to peg
s\n', n, from, to) - move(n-1, using, to, from)
- end
15Analysis
- Lets see how many moves it takes to solve this
problem, as a function of n. - n Number of disk-moves required
- 1 1
- 2 3
- 3 7
- 4 15
- 5 31
- ...
- i 2i-1
- 64 264-1 (a big number)
16Analysis (Ctd)
- How big?
- Suppose that our computer and super-printer can
generate and print 1,048,576 (220)
instructions/second. - How long will it take to print the instructions
for 64 disks? - There are 264 instructions to print.
- Then it will take 264/220 244 seconds to print
them. - 1 minute 60 seconds.
- Lets take 64 26 as an approximation of 60.
- Then it will take _at_ 244 / 26 238 minutes to
print them.
17Analysis (Ctd)
- 1 hour 60 minutes.
- Lets take 64 26 as an approximation of 60.
- Then it will take _at_ 238 / 26 232 hours to print
them. - 1 day 24 hours.
- Lets take 32 25 as an approximation of 24.
- Then it will take _at_ 232 / 25 227 days to print
them. - 1 year 365 days.
- Lets take 512 29 as an approximation of 365.
- Then it will take _at_ 227 / 29 218 years to print
them. - 1 century 100 years.
- Lets take 128 27 as an approximation of 100.
- Then it will take _at_ 218 / 27 211 centuries to
print them.
18Analysis (Ctd)
- Hmm. 211 centuries is hard to grasp. Lets keep
going... - 1 millenium 10 centuries.
- Lets take 16 24 as an approximation of 10.
- Then it will take _at_ 211 / 24 27 128 millenia
just to print the instructions (assuming our
computer doesnt crash, in which case we have to
start all over again). - How fast can the disks actually be moved?
- Probably NOT one million per second!
- If 1 per second, the task will take more
than128,000,000,000 years
19Hands-On DEMO Counting "Blobs"
- See
- TestCountBlobs.m
- CountBlobs.m
- MarkBlobs.m
20Hands-On DEMO SuDoku
- Run sudoku.m
- Use sudoku2.txt
21A Simple Maze In-class Exercise 8a(recursive
traversal)
No diagonal movements, just Right, Down, Left,
Up 2-D array represents maze in program Recursive
findPath(row, col) looks for path
(data files xmaze.txt and hampton.txt)
22Maze Solving Program on Web
- See http//cs.union.edu/hannayd/csc105
23Maze Program Plan
- Read Maze design from a file
- Symbols represent Walls, Start, Finish
- Program can solve more than one maze
- Use 2-D array of some symbols to represent maze
in program - Recursive findPath(row, col) method searches for
solution - Insert Path or Visited symbols in array while
looking for solution - Display method repeatedly shows status of the maze
xmaze.txt file
- S..................
- ....
- ..................
- .........
- ................
- ...........
- ..............
- .............
- ............
- .............
- ..............
- ...........
- ............
- .............
- .............
- ...............
- .........
- ................
24findPath(int row, int col)
- Several Base Cases return false or true
- Several Inductive Cases return result of a new
call to findPath with different row column
25findPath(int row, int col) (In-Class Exercise)
BASE CASE 1 going off the edge (return
false) BASE CASE 2 already visited, or already
part of the path (return false) BASE CASE 3 had
success got to the finish square (return true)
(mark the square as part of the path) INDUCTIVE
CASE 1 try moving right, see if there is a path
from there INDUCTIVE CASE 2 if that did not work,
try moving down, see if there is a path from
there INDUCTIVE CASE 3 if that did not work, try
moving left, see if there is a path from
there INDUCTIVE CASE 4 if that did not work, try
moving up, see if there is a path from
there (mark the square as visited)
BASE CASE 4 none of the inductive cases worked,
so this space is not part of the path (return
false)
26Hampton Court Maze (see hampton.txt)
A Hedge Maze (nearly 1/3 acre in area) built in
1702 on grounds of Hampton Court Palace near
LondonYou can get lost in it!
Run program with hampton.txt, a rectangular
version of the Hampton Court Maze Reverse the S
and F to get out!
27Hands-On DEMO Sudoku
- Consider sudoku.m which reads sudoku1.txt
- More on solving sudoku recursively
- dlmread
- We could also first import into a spreadsheet,
save as an Excel file, then use xlsread to read
into MATLAB.
28Recursion Summary
- Recursion is a valuable tool that allows some
problems to be solved in an elegant and efficient
manner. - Methods can sometimes require more than one
recursive call in order to accomplish their task. - There are problems for which we can design a
solution, but the nature of the problem makes
solving it effectively uncomputable. e.g. Towers
of Hanoi for 64 disks, ack(10,10)
29Remember
- Must have a "base" case
- Must work toward the base case by simplifying the
problem each time - Use if, not while since the recursion itself is
performing a "loop"
30In-Class Exercise 8a Traverse a Maze
- Use the given cs70ex8a.m
- Complete the findPath function to find you way
through a maze given 'S' as the starting point,
and 'F' as the finish point. - NOTE You need to add Inductive Steps 1-4 and
Base Case 4 - Run with both xmaze.txt and hampton.txt
31END