towers of hanoi

pradyumnagujjar7 1,775 views 8 slides Sep 26, 2014
Slide 1
Slide 1 of 8
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8

About This Presentation

No description available for this slideshow.


Slide Content

TOWERS OF HANOI

•Move: A  B
•For, N=1, the only move is AC
•For, N=2, the moves are AB, AC, BC
• For, N=3 ?

•For N=N: following steps are used:
1.Move top n-1 disks from A to B
2.Move top disk from A to C
3.Move top n-1 disks from B to C (recursion by changing the B and C)

Algorithm
•General Notation of Algorithm is: TOWER (N, BEG, AUX, END)
•EXAMPLE MOVE: TOWER(1, BEG, AUX, END) I.E, BEGEND
•Steps for N disks:
•1. TOWER(N-1, BEG,END,AUX)
•2 TOWER(1, BEG,AUX,END)
•3 TOWER(N-1, AUX,BEG,END) --- (recursion)
•Example : TOWER(4,A,B,C)

•TOWER(N,BEG,AUX,END)
•1 If N=1, then
• (a) Write : BEGEND
• (b) Return
•2 [Move N-1 disks from BEG to AUX]
• Call TOWER(N-1,BEG,END,AUX)
•3 Write: BEGEND
•4 [Move N-1 disks from AUX to END]
• Call TOWER(N-1,AUX,BEG,END)
•5 Return
Tags