25.12.2012 Views

11. Tower of Hanoi - Computer Science - University of Vermont

11. Tower of Hanoi - Computer Science - University of Vermont

11. Tower of Hanoi - Computer Science - University of Vermont

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong><br />

Robert Snapp<br />

snapp@cs.uvm.edu<br />

Department <strong>of</strong> <strong>Computer</strong> <strong>Science</strong><br />

<strong>University</strong> <strong>of</strong> <strong>Vermont</strong><br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 1 / 20


The <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong>: Origin<br />

In the great temple at Benares, beneath the dome which marks the center<br />

<strong>of</strong> the world, rests a brass plate in which are fixed three diamond needles,<br />

each a cubit high and as thick as the body <strong>of</strong> a bee. On one <strong>of</strong> these<br />

needles, at the creation, God placed sixty-four discs <strong>of</strong> pure gold, the<br />

largest disk resting on the brass plate, and the others getting smaller and<br />

smaller up to the top one. This is the <strong>Tower</strong> <strong>of</strong> Bramah. Day and night<br />

unceasingly the priests transfer the discs from one diamond needle to<br />

another according to the fixed and immutable laws <strong>of</strong> Bramah, which<br />

require that the priest on duty must not move more than one disc at a time<br />

and that he must place this disc on a needle so that there is no smaller disc<br />

below it. When the sixty-four discs shall have been transferred from the<br />

needle on which at the creation God placed them to one <strong>of</strong> the other<br />

needles, tower, temple, and Brahmins alike will crumble into dust, and with<br />

a thuderclap the world will vanish.<br />

(From, De Parville, La Nature, 1884, part I, pp. 285–286; as translated in W. W. Rouse Ball and<br />

H. S. M. Coxeter, Mathematical Recreations and Essays, 13th edition, Dover, NY, 1987,<br />

p. 317.)<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 2 / 20


The <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong>: The Puzzle<br />

Initial State:<br />

Goal State:<br />

Only one disk can be moved at a time, from one peg to another, such that it never is<br />

placed on a disk <strong>of</strong> smaller diameter.<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 3 / 20


<strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong>: One Disk Solution<br />

1 Move.<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 4 / 20


Two Disk Solution<br />

Note that we perform the solution to the one-disk <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> twice (once in the<br />

top row, and once in the bottom row). Between rows, we move the bottom disk. Thus,<br />

we require<br />

2 1 C 1 D 3 Moves<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 5 / 20


<strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong>: Three Disk Solution<br />

Note that we perform the solution to the two-disk <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> twice (once in the<br />

top row, and once in the bottom row). Between rows we move the bottom disk. Thus,<br />

we require<br />

2.2 1 C 1/ C 1 D 7 Moves<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 6 / 20


<strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong>: What we know so far<br />

Let M.n/ denote the minimum number <strong>of</strong> legal moves required to complete a tower<br />

<strong>of</strong> <strong>Hanoi</strong> puzzle that has n disks.<br />

n M.n/<br />

1 1<br />

2 3<br />

3 7<br />

Following the pattern, for n D 4 we need to solve the three-disk puzzle twice, plus<br />

one more operation to move the largest disk. Thus,<br />

M.4/ D 2 M.3/ C 1 D 2 7 C 1 D 15:<br />

Similarly, for n D 5 disks, we expect that we will need to perform<br />

M.5/ D 2 M.4/ C 1 D 2 15 C 1 D 31:<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 7 / 20


<strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong>: n Disk Analysis<br />

Let M.n/ denote the minimum number <strong>of</strong> legal moves required to complete a tower<br />

<strong>of</strong> <strong>Hanoi</strong> puzzle that has n disks.<br />

Before the largest disk (i.e., the n-th disk) can be moved to the rightmost peg, all<br />

<strong>of</strong> the remaining (n 1) disks must moved to the center peg. (These n 1 disks<br />

must be somewhere, and they can’t obstruct the transfer <strong>of</strong> the largest disk.)<br />

This requires M.n 1/ legal moves.<br />

It takes 1 more operation to move the n-th disk to the rightmost peg.<br />

Finally, another legal sequence <strong>of</strong> M.n 1/ steps is required to move the n 1<br />

disks from the center peg, to the rightmost peg.<br />

We thus obtain the recursion relation,<br />

M.n/ D 2M.n 1/ C 1:<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 8 / 20


<strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong>: Solution<br />

With the solution for a single disk<br />

the recursion relation<br />

defines the solution<br />

M.1/ D 1<br />

M.n/ D 2M.n 1/ C 1:<br />

M.n/ D 2 n<br />

In are hierarchy <strong>of</strong> algorithms, this would be called exponential or O.2 n /.<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 9 / 20<br />

1:


<strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong>: Practical Consequences<br />

The practical difficulty with exponential algorithms is that they can quickly grow out <strong>of</strong><br />

hand. (With each additional disk, the minimum number <strong>of</strong> operations essentially<br />

doubles.) N.B., 1 century 4:5 10 9 seconds. (The age <strong>of</strong> the universe is 4 10 17<br />

seconds.)<br />

n 2 n 1 n 2 n 1 n 2 n 1<br />

8 255 19 524287 30 1073741823<br />

9 511 20 1048575 31 2147483647<br />

10 1027 21 2097151 32 4294967295<br />

11 2047 22 4194303 33 8589934591<br />

12 4095 23 8388607 34 17179869183<br />

13 8191 24 16777215 35 34359738367<br />

14 16383 25 33554431 36 68719476735<br />

15 32767 26 67108863 37 137438953471<br />

16 65635 27 134217727 38 274877906943<br />

17 131071 28 268435455 39 549755813887<br />

18 262143 29 536870911 64 18446744073709551615<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 10 / 20


<strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong>: Strategy<br />

We have seen that the solution to this puzzle is recursive:<br />

In order to move all n disks to the right peg, we must first move the top n 1<br />

disks to the center peg.<br />

Before this, we must move the top n 2 disks to the right peg.<br />

And before this, we must move the top n 3 disks to the center peg, and so on.<br />

So what should the first move be? Should the top disk be moved to the center peg, or<br />

to the right peg?<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 11 / 20


<strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong>: Solution Path<br />

If the number <strong>of</strong> disks is odd, then the first move should be to transfer the top<br />

disk to the right peg.<br />

If the number <strong>of</strong> disks is even, then the first move should be to transfer the top<br />

disk to the center peg.<br />

The sequence <strong>of</strong> states that is visited in the course <strong>of</strong> solving the puzzle is called the<br />

solution path.<br />

The length <strong>of</strong> the shortest solution path to the n-disk puzzle is 2 n .<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 12 / 20


<strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong>: Legal States<br />

A configuration <strong>of</strong> disks in the <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> puzzle is said to be a legal state if no<br />

disk rests on a disk <strong>of</strong> smaller diameter. That is, the largest disk on each peg must be<br />

placed on the bottom, and the remaining disks must be placed in the order <strong>of</strong><br />

decreasing diameter.<br />

Question: How many legal states are there for n disks placed on three pegs?<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 13 / 20


<strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong>: Accessible States<br />

A configuration <strong>of</strong> disks is said to be anaccessible state, if it can be realized from the<br />

initial state after a legal sequence <strong>of</strong> moves.<br />

Question: How many accessible states are there for n disks placed on three pegs?<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 14 / 20


<strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong>: Examples<br />

Number Length <strong>of</strong> Number Fraction <strong>of</strong><br />

<strong>of</strong> Disks Solution Path <strong>of</strong> Legal States Visited States<br />

n 2 n 3 n .2=3/ n<br />

1 2 3 0.6666667<br />

2 4 9 0.4444446<br />

3 8 27 0.2962963<br />

4 16 81 0.1975309<br />

5 32 243 0.1316872<br />

6 64 729 0.0877915<br />

7 128 2187 0.0585277<br />

8 256 6561 0.0390184<br />

32 4294967296 1853020188851841 2:32 10 6<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 15 / 20


State Representation<br />

It’s cumbersome to represent a state by an illustration. Instead we will adopt a list<br />

notation.<br />

For example, for a five disk puzzle, the notation<br />

indicates<br />

(2 1 3 1 1)<br />

The smallest disk is on peg 2 (the center peg).<br />

The next larger disk is on peg 1 (the left peg).<br />

The next larger disk is on peg 3 (the right peg).<br />

The next larger disk is on peg 1.<br />

The next larger (i.e., largest) disk is on peg 1.<br />

The only legal configuration that describes this is<br />

1 2 3<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 16 / 20


State Graph: 1 disk<br />

(2)<br />

(1)<br />

Each state is represented by a labeled vertex; legal moves are represented by edges.<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 17 / 20<br />

(3)


State Graph: 2 disks<br />

(23)<br />

(21)<br />

(11)<br />

(31)<br />

(32)<br />

(33) (13) (12) (22)<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 18 / 20


State Graph: 3 disks<br />

(223)<br />

(221)<br />

(111)<br />

(311) (211)<br />

(321)<br />

(121) (131)<br />

(231)<br />

(331)<br />

(332)<br />

(123) (323) (232) (132)<br />

(313) (212)<br />

(133) (122)<br />

(333) (222)<br />

(233) (213) (113) (112) (312) (322)<br />

Robert R. Snapp © 2012 <strong>11.</strong> <strong>Tower</strong> <strong>of</strong> <strong>Hanoi</strong> CS 32, Fall 2012 19 / 20

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!