Tower of Hanoi
Please view this website for the application:
Let us call the three pegs Src (Source), Aux (Auxiliary) and Dst (Destination). As you solve the problem, sooner or later the bottom disk will have to be moved from Src to Dst. Then, the remaining disks will then have to be stacked in decreasing size order on Aux. After moving the bottom disk from Src to Dst these disks will have to be moved from Aux to Dst. Therefore, for a given number N of disks, the problem appears to be solved if we know how to accomplish the following tasks:
- Move the top disk from Src to Aux (using Dst as an intermediary peg)
- Move the bottom disk from Src to Dst
- Move the smallest disk from Aux to Dst (using Src as an intermediary peg)
Suppose there is a function "Solve" with four arguments - number of disks(N) and three pegs (source, intermediary and destination - in this order). Then the body of the function might be as follows:
Solve(N, Src, Aux, Dst)
if N is 0 exit
else
Solve(N-1, Src, Dst, Aux)
Move from Src to Dst
Solve(N-1, Aux, Src, Dst)
This actually serves as the definition of the function Solve. The function is recursive in that it calls itself repeatedly with decreasing values of N until a terminating condition (in this case N=0) has been met. It is indeed amazing how this puzzle could be solve by a method of such simplicity.
When there are 3 disk, the function thus translates into
- Move from Src to Dst
- Move from Src to Aux
- Move from Dst to Aux
- Move from Src to Dst
- Move from Aux to Src
- Move from Aux to Dst
- Move from Src to Dst
Of course "Move" means moving the topmost disk. For N=4 we get the following sequence
- Move from Src to Aux
- Move from Src to Dst
- Move from Aux to Dst
- Move from Src to Aux
- Move from Dst to Src
- Move from Dst to Aux
- Move from Src to Aux
- Move from Src to Dst
- Move from Aux to Dst
- Move from Aux to Src
- Move from Dst to Src
- Move from Aux to Dst
- Move from Src to Aux
- Move from Src to Dst
- Move from Aux to Dst
Recurrence relations
Let TN be the minimum number of moves needed to solve the puzzle with N disks. From the previous section, we can see that T3 and T4 are 7 and 15. One can easily convince oneself that T2 is 3 and T1, 1 of course. It does not take one to be a trained mathematician to notice that T0=0.
A general formula:
The recursive solution above involves moving twice (N-1) disks from one peg to another and making one additional move in between. It then follows that
TN<=TN-1+1+TN-1 = 2TN-1+1
The inequality suggests that it might be possible to move N disks with fewer than 2TN-1+1 moves, which is actually not the case. Indeed, when the time comes to move the bottom disk (N-1) disks will have been moved from Src to Aux in at least TN-1 moves. Since we are trying to use as few steps as possible, we may assume that that portion of the task took exactly TN-1 moves. It takes just one move to move the biggest disk from Src to Dst. One then needs exactly TN-1 more steps to finish the task. Therefore the minimum number of moves needed to solve the puzzle with N disks equals
In other words,
TN = 2TN-1 + 1
Thus we can define the quantity TN as
From here, we may compute T1 = 2T0 + 1 = 1,
Returning to the definition of TN, define