Tower of HanoiThe Tower of Hanoi puzzle was invented by the French mathematician Edouard Lucas in 1883. We are given a tower of eight disks (initially four in the applet below), initially stacked in increasing size on one of three pegs. The objective is to transfer the entire tower to one of the other pegs (the rightmost one in the applet below), moving only one disk at a time and never a larger one onto a smaller. The puzzle is well known to students of Computer Science since it appears in virtually any introductory text on data structures or algorithms. Its solution touches on two important topics discussed later on:
The applet has several controls that allow one to select the number of disks and observe the solution in a Fast or Slow manner. To solve the puzzle drag disks from one peg to another following the rules. You can drop a disk on to a peg when its center is sufficiently close to the center of the peg. The applet expects you to move disks from the leftmost peg to the rightmost peg.
Recursive solutionLet call the three pegs Src (Source), Aux (Auxiliary) and Dst (Destination). To better understand and appreciate the following solution you should try solving the puzzle for small number of disks, say, 2,3, and, perhaps, 4. However one solves the problem, sooner or later the bottom disk will have to be moved from Src to Dst. At this point in time all the remaining disks will 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:
Assume there is a function Solve with four arguments - number of disks and three pegs (source, intermediary and destination - in this order). Then the body of the function might look like
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 our case
Of course "Move" means moving the topmost disk. For
Recurrence relationsLet TN be the minimum number of moves needed to solve the puzzle with N disks. From the previous section The recursive solution above involves moving twice
The inequality suggests that it might be possible to move N disks with fewer than In other words,
Thus we can define the quantity TN as
We may compute T1 = 2T0 + 1 = 1, The above expression is known as a recurrence relation which, as you might have noticed, is but a recursive function. TN is defined in terms of only one of its preceding values. Other recurrence relations may be more complicated, for example, Returning to the definition of TN, define
The latter is solved easily in the closed (non-recurrent) form SN=2N. Wherefrom
Remark 1The latest implementation of the applet sports a "Suggest move" button that exploits an algorithm devised by Romek Zylla who graciously put up on the Web an explanation of his algorithm. The algorithm actually provides another, a non-recursive solution to the puzzle. Remark 2There are quite a few variations on the puzzle. For example, you may want to experiment with its bicolor or 3 colors versions. Reference
On Internet
|Contact| |Front page| |Contents| |Games| |Store| Copyright © 1996-2010 Alexander Bogomolny |
| 37201744 |

