Professional Documents
Culture Documents
Tower of Hanoi
Robert Snapp
snapp@cs.uvm.edu
Department of Computer Science
University of Vermont
1 / 20
(From, De Parville, La Nature, 1884, part I, pp. 285286; as translated in W. W. Rouse Ball and
H. S. M. Coxeter, Mathematical Recreations and Essays, 13th edition, Dover, NY, 1987,
p. 317.)
Robert R. Snapp 2012
2 / 20
Initial State:
Goal State:
Only one disk can be moved at a time, from one peg to another, such that it never is
placed on a disk of smaller diameter.
3 / 20
1 Move.
4 / 20
Note that we perform the solution to the one-disk Tower of Hanoi twice (once in the
top row, and once in the bottom row). Between rows, we move the bottom disk. Thus,
we require
2 1 C 1 D 3 Moves
5 / 20
Note that we perform the solution to the two-disk Tower of Hanoi twice (once in the
top row, and once in the bottom row). Between rows we move the bottom disk. Thus,
we require
2.2 1 C 1/ C 1 D 7 Moves
6 / 20
M.n/
1
2
3
1
3
7
Following the pattern, for n D 4 we need to solve the three-disk puzzle twice, plus
one more operation to move the largest disk. Thus,
7 / 20
M.n/ D 2M.n
1/ C 1:
8 / 20
M.1/ D 1
the recursion relation
M.n/ D 2M.n
1/ C 1:
M.n/ D 2n
1:
9 / 20
n
8
9
10
11
12
13
14
15
16
17
18
2n
2n
255
511
1027
2047
4095
8191
16383
32767
65635
131071
262143
19
20
21
22
23
24
25
26
27
28
29
524287
1048575
2097151
4194303
8388607
16777215
33554431
67108863
134217727
268435455
536870911
30
31
32
33
34
35
36
37
38
39
64
2n
1073741823
2147483647
4294967295
8589934591
17179869183
34359738367
68719476735
137438953471
274877906943
549755813887
18446744073709551615
10 / 20
So what should the first move be? Should the top disk be moved to the center peg, or
to the right peg?
11 / 20
If the number of disks is odd, then the first move should be to transfer the top
disk to the right peg.
If the number of disks is even, then the first move should be to transfer the top
disk to the center peg.
The sequence of states that is visited in the course of solving the puzzle is called the
solution path.
The length of the shortest solution path to the n-disk puzzle is 2n .
12 / 20
13 / 20
14 / 20
Number
of Disks
Length of
Solution Path
Number
of Legal States
Fraction of
Visited States
2n
3n
.2=3/n
2
4
8
16
32
64
128
256
4294967296
3
9
27
81
243
729
2187
6561
1853020188851841
1
2
3
4
5
6
7
8
32
0.6666667
0.4444446
0.2962963
0.1975309
0.1316872
0.0877915
0.0585277
0.0390184
2:32 10
15 / 20
State Representation
Its cumbersome to represent a state by an illustration. Instead we will adopt a list
notation.
For example, for a five disk puzzle, the notation
(2 1 3 1 1)
indicates
The smallest disk is on peg 2 (the center peg).
The next larger disk is on peg 1 (the left peg).
The next larger disk is on peg 3 (the right peg).
The next larger disk is on peg 1.
The next larger (i.e., largest) disk is on peg 1.
The only legal configuration that describes this is
1
Robert R. Snapp 2012
2
11. Tower of Hanoi
3
CS 32, Fall 2012
16 / 20
(1)
(2)
(3)
Each state is represented by a labeled vertex; legal moves are represented by edges.
17 / 20
(11)
(21)
(31)
(32)
(23)
(33)
(13)
(12)
(22)
18 / 20
(111)
(311)
(321)
(221)
(211)
(231)
(121) (131)
(331)
(332)
(223)
(123)
(133)
(333)
(323)
(313)
(232)
(212)
(132)
(122)
(222)
19 / 20