In: Computer Science
Describe an algorithm to solve the variant of the Towers of Hanoi in as few moves as possible. Prove that your algorithm is correct. Initially, all the n disks are on peg 1, and you need to move the disks to peg 2. You are not allowed to put a bigger disk on top of a smaller disk.
1. Suppose you are forbidden to move any disk directly between peg 1 and peg 2, and every move must involve (the third peg). Exactly (i.e., not asymptotically) how many moves does your algorithm make as a function of n?
How many moves will it take to transfer n disks from the left post to the right post?
1 disk: 1 move
2 disks: 3 moves
3 disks: 7 moves
A. Recursive pattern
Here M = the number of moves
B. Explicit Pattern
So the formula for finding the number of steps it takes to transfer n disks from post A to post B is: 2^n - 1.
Algoithm:
START Procedure Hanoi(disk, A, C, B) IF disk == 1, THEN move disk from A to C ELSE Hanoi(disk - 1, A, B, C) // Step 1 move disk from A to C // Step 2 Hanoi(disk - 1, B, C, A) // Step 3 END IF END Procedure STOP