Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

BIT23 (1983), 429-135

AN ANALYSIS OF THE
GENERALIZED TOWERS OF HANOI PROBLEM

M. C. ER
Department of Computing Science, The University of Wollongong, P.O. Box 1144, Wollongong,
N.S.W. 2500 Australia

Abstract.
A state-space graph for representing the states and their transitions of n discs on three pegs is
formulated. It is then transformed to a shortest-path tree for representing the shortest paths in
transferring n discs in any configurations to a specified peg. The shortest-path tree clearly
characterizes the generalized Towers of Hanoi problem ; and its use leads to a very simple analysis
of the generalized problem. The best-case, the worst-case and the average-case complexities are
analyzed.

Keywords: State-space graph, shortest-path tree, analysis, complexity, Towers of Hanoi.

1. Introduction.

In a recent paper [4], Er proposes a generalization to the Towers of Hanoi


problem [t-3, 6]. In this generalization, n discs of different sizes are legally
distributed on three pegs initially; and the task is to transfer them to a specified
peg subject to the restrictions of the standard problem:
(i) Only the topmost discs may be moved, one at a time;
(ii) No disc may ever stack on a disc smaller than itself.
A recursive solution 1-4] and an iterative solution [5] to this generalized
problem are also reported.
In this paper, we provide a complete analysis of the complexity of the
generalized Towers of Hanoi problem, independent of any particular
implementations. The best-case, the worst-case and the average-case
complexities of the generalized problem are.discussed, using a shortest-path tree,
which is a transformation of a state-space graph.

2. State-space graph.

A configuration of discs on the pegs can be conceptualized as a state in the


solution space. The movement of a disc from one peg to another peg is therefore

Received March 5, 1983. Revised May 16, 1983.


430 M.C. ER

a transition of a state to another state. If a state is represented as a node, a


transition of states could be represented as a link joining two states. Since a
transition is reversible in the generalized problem, the corresponding link is
undirected. By representing all possible states and their transitions in this way,
the result is an undirected graph, which is called the state-space graph. An
example of such a state-space graph for the three-disc Towers of Hanoi problem
may be seen in figure 1. Here we denote the three pegs as A, B and C. We
further assume that the discs of increasing sizes are numbered successively with
the smallest disc being numbered 1. Suppose discs 1, 2, and 3 are on pegs A, C,
and B respectively; we may write ACB as a shorthand for representing this
state. Define an admissible configuration as a configuration of the discs on the
three pegs such that none of the restrictions described in the previous section is
violated. From the discussion above, any admissible configuration of n discs has
a unique name. Conversely, any letter sequence of length n composed of A, B,
and C describes an admissible configuration uniquely. Consequently, the number
of states in a state-space graph is equal to the number of different letter
sequences, i.e. 3".

AAA

J-- L
CCA
ACA

ABBAC~Cd :BAB

cGB ckB A-A8 AAC B.~C BCC CCC


Figure 1. A state-space graph for the generalized Towers of Hanoi problem with three discs.
AN ANALYSIS OF THE GENERALIZED TOWERS OF HANOI PROBLEM 431

Further, the number of links connecting to a node is either two or three.


There are two links when all discs are stacked on one peg corresponding to two
possible moves of the smallest disc. Three links arise from a possible move of
the smaller topmost disc among the two pegs not occupied by the smallest disc,
in addition to two possible moves of the smallest disc. Any other links will lead
to a violation of the restrictions, and therefore should be prohibited. Thus, each
of the three vertices of a state-space graph has two links, and each of other
nodes has three links.
Suppose disc 1 is removed; then disc 2 becomes the smallest disc. All possible
moves of disc 2 in a configuration must be identical to that of disc 1 in the
similar situation. Equivalently, the topology of links representing the
movements of disc 2 when all smallest triangles representing the movements of
disc 1 are shrinked into nodes ought to be similar to the topology of a smallest
triangle, because disc 2 now takes the place of disc 1. Such a topology can be
recursively applied to the movements of other discs; and hence the structure of
a state-space graph is recursive and simple.
Next, we discuss a symmetry property. Suppose B and C are interchanged for
all state names in the state-space graph but keeping A invariant, then half of the
states could be derived from another half. If a vertical line is drawn passing
through vertex A A A (see figure 1), the state-space graph is symmetric with
respect to this vertical line. The same arguments apply to the similar axes
passing through the other two vertices; so the mirror symmetries embedded in
the state-space graph may be easily seen. Further, suppose A, B, and C are
replaced by B, C, and A respectively for all state names, and the new state-space
graph could be obtained by a 120 degree rotation of the previous state-space
graph (see figure 1). Thus the state-space graph maintains the rotational
symmetry as well.
The minimum number of disc moves in transforming a configuration to
another configuration is given by the shortest path between two nodes
representing these two configurations. The shortest path between two nodes
may not be unique. An example is the shortest paths between nodes B C C and
C B C (see figure 1) - both B C C - A C C - A B C - C B C and B C C - B A C - C A C -
C B C are equally possible.

3. Shortest-path tree.

The goal of the generalised problem is to move all discs to a specified peg;
such a goal node is one of the three extreme vertices of the state-space graph. If
such a goal node is treated as a root and all non-shortest paths between the
goal node and other nodes are removed, the result is a tree - we call it the
shortest-path t r e e . It is a tree because the shortest paths between the goal node
and any nodes are unique. The uniqueness follows from the mirror-symmetry
432 M . C . ER

property of the state-space graph - the mirror-symmetry axes passing through


the vertices do not pass through any other nodes. In a further generalization, if
any one of the nodes could be the goal node, the uniqueness property of the
shortest-path between a node and the goal node does not hold. But we shall not
consider this extension further.
Because of the symmetry property embedded in the original state-space
graph, the shortest-path tree is also symmetric. An example of the shortest-path
tree, a transformation of the state-space graph shown in figure 1, for
representing the state-changes in moving three discs to peg A with the minimum
numbers of steps is displayed in figure 2. Comparing figures 1 and 2, we see that
the non-shortest-path links that have been removed are precisely those
horizontal links. This is not accidental - to move from a node of the right
subtree of the shortest-path tree to its root, it is faster by ascending the right
subtree directly than by ascending via the left subtree, and vice-versa - a
consequence of the mirror-symmetry property. Thus we have a trivial way of
transforming a state-space graph to its corresponding shortest-path tree.
The most important property of the shortest-path tree is its recursive
structure. Let T(n) be the shortest-path tree representing the movements of n
discs. Define T(0) as a single-node tree because no disc needs to be moved in
order to attain the goal state. A way of constructing T(n + 1) is to append two
T(n)'s to the lowest left-most and the lowest right-most leaves of a T(n)
respectively. This defines the recursive structure of the shortest-path tree. An
instance may be seen in figure 2.

4. Analysis.

The minimum number of moves in transferring n discs given in a


configuration to a specified peg is equivalent to the distance between the root
and a node representing the starting state in the shortest-path tree. Starting
from the root of the shortest-path tree, we assign a zero to it. By moving down
the tree from its root, each node at the next level is assigned a value one greater
than the value of the node descending from. In this way, all nodes in the
shortest-path tree are assigned values appropriate to their levels. Such a value is
indeed the distance between the root and the node concerned. Let M~(n) be the
value assigned to node s of T(n). An example is shown in figure 2.
To analyze the complexity of the generalized problem, we consider three cases
- best case, worst case, and average case; each of which is discussed in the
following.

(i) Best-case complexity


When all n discs happen to be on a specified peg, we move no discs at all as
AN ANALYSIS OF THE GENERALIZED TOWERS OF HANOI PROBLEM 433
AAA

BAA CAA

ABC

7 7 7 7 7 7
/\
7
ACC

7
BBB eBB CAB AAB AAC BAC BCC CCC

Figure 2. A shortest-path tree for the generalized Towers of Hanoi problem with three discs.

the goal is already attained. Hence,

Mbest(n) = 0. (1)

The number of such instances is only 1 ; and thus we have a probability of 1/3".

(ii) Worst-case complexity.


The worst cases are precisely given by those nodes with maximum value in
the shortest-path tree. Let H(n) be the height of T(n). By the recursive structure
of the shortest-path tree, we have

n(0) = 1

and H(n) = 2 n ( n - 1).

Hence the closed-form solution is


H(n) = 2".
434 M . C . ER

But, M . . . . t(n) = H(n)-1; thus we have

(2) Mwo,st(n) = 2 " - 1.

Let L(n) be the number of leaves of T(n) at the lowest level. From the way the
shortest-path tree is recursively constructed, we obtain

L(0) = 1 and L(n) = 2L(n-1).

The closed-form solution is L(n)= 2". Thus, the number of the worst-
cases is 2"; and thus the probability of occurrence is 2"/3". Eq. (2) is equal to the
complexity of the standard problem ; and indeed the standard problem contains
only two instances of the worst-case configurations of discs. There are other
configurations of discs which can give rise to the worst-case complexity; and the
worst-case complexity of the generalized problem is not worse than the
complexity of the standard problem. This is a somewhat surprising result.

(iii) Average-case complexity.


A most interesting but also more complicated result is the average-case
complexity; namely, the expected number of disc moves averaged over all
possible initial configurations of discs.
Let IV, be the sum of all Ms(n ) of T(n). As noted in the foregoing section, T(n)
can be constructed by appending two T(n-1)'s to the lowest left-most and the
lowest right-most leaves of a T(n) respectively. Further, we notice that each
node of the newly appended subtrees has a value 2"-t in excess of the value
associated with the corresponding node in the old tree. Putting all the
arguments together, we derive,

V. = 2(V._1 + 3 " - 1 " 2 " - 1 ) + V . _ 1 .

After simplification, we have

(3) IV, = 3V,_1 + 2 - 6"-1

with boundary condition,

(4) Vo = O.

Solving the recurrence Eq. (3) with Eq. (4),we obtain the foUowing closed-form
solution

(5) v.= 2-3"-1(2"-1).


AN ANALYSISOF THE GENERALIZEDTOWERSOF HANOI PROBLEM 435

Hence the average-case complexity is given by

M . . . . . ge(n) = V./3"

or

(6) M ..... ge(n) = 2 ( 2 " - 1)/3.

In other words, the average-case complexity of the generalized problem is 2/3 of


the complexity of the standard problem.

5. Concluding remarks.

In this paper, the state-space graph is used to represent the states and the
transitions of moving n discs to other configurations. Many interesting
properties of the state-space graph are also discussed. The state-space graph is
then transformed to the shortest-path tree which represents the shortest paths of
transferring n discs in any configuration to a specified peg. The shortest-path
tree simplifies the analysis of the generalized Towers of Hanoi problem
tremendously. In particular, the best-case, the worst-case and the average-case
complexities of the generalized problem are analyzed. Of particular interest is
the average-case complexity of the generalized problem, which is shown to be
2/3 of the complexity of the standard problem. Also, the worst-case complexity
of the generalized problem is shown to be not worse than the complexity of the
standard problem.

Acknowledgements.

The author is indebted to the referee for his many valuable comments and
suggestions. The work was supported by the RGC under grant 05-143-105.

REFERENCES
1. P. Buneman and L. Levy, The Towers of Hanoi problem, Information Processing Letters 10
(1980) 243-244.
2. E. W. Dijkstra, A short introduction to the art of programming, EWD316 (1971).
3. M. C. Er, A representation approach to the Tower of Hanoi problem, Computer Journal 25 (1982)
442-447.
4. M. C. Er, The generalized Towers of Hanoi problem, Information Processing Letters (in press).
5. M. C. Er, An iterative solution to the generalized Towers of Hanoi problem, Technical Report,
Department of Computing Science, University of Wollongong (1982).
6. P. J. Hayes, A note on the Towers of Hanoi problem, Computer Journal 20 (1977) 282-285.

You might also like