# Tower of Hanoi

The 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:

- recursive functions and stacks
- recurrence relations

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.

What if applet does not run? |

## Recursive solution

Let 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:

- Move the top N - 1 disks from Src to Aux (using Dst as an intermediary peg)
- Move the bottom disk from Src to Dst
- Move N - 1 disks from Aux to Dst (using Src as an intermediary peg)

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

- 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

- 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 T_{N} be the minimum number of moves needed to solve the puzzle with N disks. From the previous section _{3} = 7_{4} = 15._{2} = 3_{1} = 1._{0} = 0.

The recursive solution above involves moving twice

T_{N} ≤ T_{N-1} + 1 + T_{N-1} = 2T_{N-1} + 1

The inequality suggests that it might be possible to move N disks with fewer than _{N-1} + 1_{N-1}_{N-1}_{N-1}_{N-1} + 1 + T_{N-1} = 2T_{N-1} + 1

In other words,

T_{N} = 2T_{N-1} + 1

Thus we can define the quantity T_{N} as

T_{0} = 0

T_{N} = 2T_{N-1} + 1 for N > 0

We may compute T_{1} = 2T_{0} + 1 = 1, _{2} = 2T_{1} + 1= 3,_{3} = 2T_{2} + 1 = 7

The above expression is known as a recurrence relation which, as you might have noticed, is but a recursive function. T_{N} 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 T_{N}, define _{N} = T_{N} + 1._{0} = 1_{N} = T_{N} + 1 = (2T_{N-1} + 1) + 1 = 2T_{N-1} + 2 = 2(T_{N-1} + 1) = 2S_{N-1}. Which is to say that S_{N} could be defined as

S_{0} = 1

S_{N} = 2S_{N-1} for N > 0

The latter is solved easily in the closed (non-recurrent) form S_{N}=2^{N}.
Wherefrom

T_{N} = 2^{N} - 1 for N ≥ 0.

### Remark 1

The 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 2

There are quite a few variations on the puzzle. For example, you may want to experiment with its bicolor or 3 colors versions.

### Reference

- A. Beck, M. N. Bleicher, D. W. Crowe,
*Excursions into Mathematics*, A K Peters, 2000

- Tower of Hanoi
- Tower of Hanoi, the Hard Way
- Bicolor Towers of Hanoi
- 3-Colors Tower of Hanoi
- 3-Colors Tower of Hanoi (Algorithm)
- Hanoing
- Sierpinski Gasket and Tower of Hanoi

|Contact| |Front page| |Contents| |Games|

Copyright © 1996-2017 Alexander Bogomolny62599486 |