CS101 Lecture 25 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

CS101 Lecture 25

Description:

... Only one disc at a time may be moved. ... hanoi(origin, spare, dest, how_many - 1) ... Then, the time to move all 64 disks from one peg to the other, and end the ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 26
Provided by: tomga
Category:
Tags: cs101 | lecture

less

Transcript and Presenter's Notes

Title: CS101 Lecture 25


1
Lecture 25
2
What will I learn in this lecture?
  • Recursive Functions
  • Related Chapter FER Chapter 21

3
Recursive Functions
Recursive functions are defined in terms of
themselves i.e., a function is recursive if it
contains calls back to itself or to another
function that calls the original function.
Recursive programming usually requires more
memory and runs more slowly than non-recursive
programs. This is due to the cost of implementing
the stack. However, recursion is often a
natural way to define a problem.
4
Example - Factorial Function
1. Problem Definition Write a factorial
function. 0! 1 and n! n(n-1)!. Use
recursion to implement the factorial
function. 2. Refine, Generalize, Decompose the
problem definition (i.e., identify sub-problems,
I/O, etc.) Input Non-negative integers as
input. Output return the factorial of the
input value. Note that 69! 1.711224524...
x 1098 so our function will only work for
small integer values.
It would be better to return a
value of data-type double (Why?)
5
Example - (Recursive) Factorial Function
double fact(int n) if (n 0) / Voom!
/ return 1.0 else return (n fact(n -
1)) / recursive call /
In main we can call the factorial function by the
following command printf("lf", fact(3)) or
by (if x is of data-type int and y is of
data-type double) x 3 y fact(x)
6
(STACK)
return 1.0 (n 0)return ( 1 fact(0)) (n
1) return ( 2 fact(1)) (n 2) return ( 3
fact(2)) (n 3)
(push) return ( 3 fact(2)) (n 3)
(push) return ( 2 fact(1)) (n
2) (push) return ( 1 fact(0)) (n
1) (push/pop) return 1.0
(n 0) (pop) return ( 1 1.0) (n
1) (pop) return ( 2 1.0) (n
2) (pop) return ( 3 2.0) (n 3)
return ( 1 1.0) (n 1) return ( 2
fact(1)) (n 2) return ( 3 fact(2)) (n 3)

return ( 2 1.0) (n 2) return ( 3
fact(2)) (n 3)

return ( 3 2.0) (n 3)

printf("lf", fact(3) )
The stack contains stack frames see FER
21.2.1.
7
Example - Traversing a Maze
1. Problem Definition Write a solve_maze
function. Read the maze from a file, maze.txt
and display one path that traverses the maze. Use
recursion to implement the maze function. 2.
Refine, Generalize, Decompose the problem
definition (i.e., identify sub-problems, I/O,
etc.) Input The file maze.txt contains the
following O
X
  • Your program will have to find its way through a
    10x10 maze where the symbols , O and X
    denote
  • are solid walls through which you cannot
    travel
  • "O" denotes the starting position,
  • "X" is the exit for which you are looking for

8
(No Transcript)
9
Example - Traversing a Maze
2. Refine, Generalize, Decompose the problem
definition Input (continued) Read the maze
into the 2D array,
O X


char mazeNUMROWSNUMCOLS where NUMROWS and
NUMCOLS are constants with value 10. The upper
left-handcorner of the maze has the value
maze00 . If we want to test whether the cell
in the fourth row and fourth column contains a
wall then it's enough to use an if statement
like this if (maze33 ')
10
Example - Traversing a Maze
2. Refine, Generalize, Decompose the problem
definition Output Display a solution as
follows OOOOOO O
O O X OO OO O
OO OOOO
11
Example - Traversing a Maze
3. Develop Algorithm (processing steps to solve
problem)
Step 1 Read in the maze and find the starting
Row and Column (the position of the O). Use
variables curRow and curCol to keep track of
the current position as we traverse through the
maze (the 2D matrix maze). Step 2 Display the
maze. Step 3 Check to see if current position is
an X then we are done. Otherwise first try
to go up and if not then down and if not then
left and if not then right and if you can go
up/down/left/right then go back to Step 2.
Otherwise, go back to the previous position
curRow,curCol and try another direction.
12
include ltstdio.hgt include ltstdlib.hgt define
NUMROWS 10 define NUMCOLS 10 / prototypes /
int read_maze (char, int , int ) void
display_maze (char) void solve_maze
(char, int, int)
13
void main (void) int startRow, startCol
/ Starting point in maze. / char
mazeNUMROWSNUMCOLS / Stores maze read from
input file. / if (read_maze(maze,
startRow, startCol) 0) printf
("Error reading maze from file maze.txt!\n")
return solve_maze(maze, startRow,
startCol) /end of main /
14
void solve_maze(char mazeNUMCOLS, int curRow,
int curCol) int i display_maze(maze) /
Check if solution found. / if ((mazecurRow -
1curCol 'X') (mazecurRow
1curCol 'X') (mazecurRowcurCol
1 'X') (mazecurRowcurCol - 1
'X')) exit (0) / Recurse in each
possible direction that is empty. / / Move up
/ if (mazecurRow - 1curCol ' ')
mazecurRow - 1curCol 'O'
solve_maze(maze, curRow - 1, curCol)
mazecurRow - 1curCol ' ' / continued
on next slide /
15
/ Move down / if (mazecurRow 1curCol
' ') mazecurRow 1curCol 'O'
solve_maze (maze, curRow 1, curCol)
mazecurRow 1curCol ' ' / Move left
/ if (mazecurRowcurCol - 1 ' ')
mazecurRowcurCol - 1 'O' solve_maze
(maze, curRow, curCol - 1)
mazecurRowcurCol - 1 ' ' / Move
right / if (mazecurRowcurCol 1 ' ')
mazecurRowcurCol 1 'O'
solve_maze (maze, curRow, curCol 1)
mazecurRowcurCol 1 ' ' return
16
/ Display the maze passed as a parameter to
standard output. / void display_maze (char maze
NUMCOLS) int i, row, col for (row
0 row lt NUMROWS row) for (col 0
col lt NUMCOLS col) printf ("c",
mazerowcol) printf ("\n")
usleep (600000) printf ("\n")
17
int read_maze (char maze NUMCOLS, int sRow,
int sCol) FILE fpMaze int row, col
char endofline / end of line character / /
Open maze text file, make sure it opens OK. /
if ((fpMaze fopen ("maze.txt", "r")) NULL)
return 0 for (row 0 row lt NUMROWS row)
/ Loop through the rows. /
for(col0colltNUMCOLScol) / Loop through
columns / fscanf(fpMaze,"c",m
azerowcol) if (mazerowcol 'O')
/Check if this is the starting position./
sRow row sCol
col / end of for(col... loop /
fscanf(fpMaze,"c",endofline) / end of
for(row... loop / fclose(fpMaze) return 1
18
Towers of Hanoi
According to legend, in the great temple of
Benares, beneath the dome which marks the center
of the world, rests a brass plate on which are
fixed three diamond needles. On one of these
needles at creation, there were placed 64 discs
of pure gold, the largest disc resting on the
brass plate and the others getting smaller up to
the top one. This is the TOWERS OF HANOI. Day and
night, the people on duty move the discs from one
needle to another, according to the two following
laws Law 1 Only one disc at a time may be
moved. Law 2 A larger disc may never rest on a
smaller disc. The workers labor in the belief
that once the tower has been transferred to
another needle there will be heaven on earth, so
they want to complete the task in the least
number of moves.
19
Towers of Hanoi
Actually, the Tower of Hanoi puzzle was invented
in 1883 by the French mathematician Edouard Lucas
(1842-1891), who made up the legend to accompany
it.
20
Towers of Hanoi
An elegant and efficient way to solve this
problem is to think recursively. Suppose that
you, somehow or other, have found the most
efficient way possible to transfer a tower of n-1
disks one by one from one pole to another obeying
the restriction that you never place a larger
disk on top of a smaller one. Then, what is the
most efficient way to move a tower of n disks
from one pole to another?
21
Pseudo-code
Assume we know how to move n-1 disks from one peg
to another.Then can we move n disks from peg 1 to
peg 3 ? 1. Move n-1 disks from peg 1 to peg 2,
peg 3 is just a temporary holding area 2. Move
the last disk(the largest) from peg 1 to peg 3 3.
Move the n-1 disks from peg 2 to peg 3, peg 1 is
just a temporary holding area.
1 2 3
n disks
22
Example - Animation
1 2 3
Click on Picture to start game
23
Example - Towers of Hanoi
/ function prototype/ void hanoi( int origin,
int dest, int spare, int how_many) void
main(void) int how_many printf("\n\tHow many
disks initially on peg1? ") scanf(i,
how_many) hanoi(1, 3, 2, how_many)
24
Example - Towers of Hanoi
void hanoi( int origin, int dest, int spare, int
how_many) if(how_many 1)
printf(\n\n\tMove top disk from peg i to
peg i., origin, dest) return
hanoi(origin, spare, dest, how_many -
1) printf("\n\n\t Move top disk from peg i,
to peg i.\n ", origin, dest) hanoi(spare,
dest, origin, how_many - 1)
25
Computational Complexity
Going back to the legend, suppose the workers
rapidly move one disk every second. As shown
earlier, the minimum sequence of moves must be
The minimum number of moves needed to
transfer n-1 disks from peg2 to peg3 on top
of the n th disk
The minimum number of moves needed to
transfer a tower of n disks from peg1
to peg3
The minimum number of moves needed to
transfer n-1 disks from peg1 to peg2
The minimum number of moves needed to
transfer the n th disk from peg1 to peg3



Therefore, the recurrence relation is moves(n)
2moves(n-1) 1 and initial case is moves(1) 1
second. For example, moves(2) 2moves(1) 1
3, moves(3) 2moves(2) 1 7, or in
general, moves(n) 2n-1 Then, the time to move
all 64 disks from one peg to the other, and end
the universe would be moves(64) seconds or 584.9
billion years!!
Write a Comment
User Comments (0)
About PowerShow.com