towers of hanoi
DESCRIPTION
TRANSCRIPT
![Page 1: towers of hanoi](https://reader034.vdocuments.mx/reader034/viewer/2022052522/547be720b4af9f09278b476e/html5/thumbnails/1.jpg)
TOWERS OF HANOI
![Page 2: towers of hanoi](https://reader034.vdocuments.mx/reader034/viewer/2022052522/547be720b4af9f09278b476e/html5/thumbnails/2.jpg)
• Move: A B• For, N=1, the only move is AC• For, N=2, the moves are AB, AC, BC• For, N=3 ?
![Page 3: towers of hanoi](https://reader034.vdocuments.mx/reader034/viewer/2022052522/547be720b4af9f09278b476e/html5/thumbnails/3.jpg)
![Page 4: towers of hanoi](https://reader034.vdocuments.mx/reader034/viewer/2022052522/547be720b4af9f09278b476e/html5/thumbnails/4.jpg)
![Page 5: towers of hanoi](https://reader034.vdocuments.mx/reader034/viewer/2022052522/547be720b4af9f09278b476e/html5/thumbnails/5.jpg)
• For N=N: following steps are used:
1.Move top n-1 disks from A to B2.Move top disk from A to C3.Move top n-1 disks from B to C (recursion by changing the B and C)
![Page 6: towers of hanoi](https://reader034.vdocuments.mx/reader034/viewer/2022052522/547be720b4af9f09278b476e/html5/thumbnails/6.jpg)
Algorithm
• General Notation of Algorithm is: TOWER (N, BEG, AUX, END)• EXAMPLE MOVE: TOWER(1, BEG, AUX, END) I.E, BEGEND• 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)
![Page 7: towers of hanoi](https://reader034.vdocuments.mx/reader034/viewer/2022052522/547be720b4af9f09278b476e/html5/thumbnails/7.jpg)
![Page 8: towers of hanoi](https://reader034.vdocuments.mx/reader034/viewer/2022052522/547be720b4af9f09278b476e/html5/thumbnails/8.jpg)
• TOWER(N,BEG,AUX,END)• 1 If N=1, then• (a) Write : BEGEND• (b) Return• 2 [Move N-1 disks from BEG to AUX]• Call TOWER(N-1,BEG,END,AUX)• 3 Write: BEGEND• 4 [Move N-1 disks from AUX to END]• Call TOWER(N-1,AUX,BEG,END)• 5 Return