The Tower of Hanoi Ben Epstein Special Topics 2003 Lucas and The Tower of Brahma Origins of The Tower of Hanoi The Tower of Hanoi puzzle was invented in 1883 by the French mathematician Edouard Lucas. A year later another French mathematician, De Parville, composed the following tale commonly associated with The Tower of Hanoi. "In the great temple at Benares, says he, beneath the dome which marks the centre of the world, rests a brass plate in which are fixed three diamond needles, each a cubit high and as thick as the body of a bee. On one of these needles, at the creation, God placed sixty-four discs of pure gold, the largest disc resting on the brass plate, and the others getting smaller and smaller up to the top one. This is the Tower of Bramah. Day and night unceasingly the priests transfer the discs from one diamond needle to another according to the fixed and immutable laws of Bramah, which require that the priest on duty must not move more than one disc at a time and that he must place this disc on a needle so that there is no smaller disc below it. When the sixty-four discs shall have been thus transferred from the needle on which at the creation God placed them to one of the other needles, tower, temple, and Bramahns alike will crumble into dust, and with a thunderclap the world will vanish." http://www.article19.com/shockwave/toh.htm How To Play • The puzzle consists of p pegs and d disks, with each disk being a different size. • The object is to relocate the entire tower to a different peg. • The stipulations are that only one disk can be moved at a time and no disk can be placed atop a smaller disk. We will examine the puzzle when p 3 and 3 d 8. http://www.cut-the-knot.org/recurrence/hanoi.shtml Peg Labeling In order to explain the solutions we must distinguish between the pegs. So let the peg on which the tower sits be the source peg. Call the peg you wish to move to the destination peg. Call the peg necessary for auxiliary moves the auxiliary peg. Source Peg Auxiliary Peg Destination Peg NOTE: A new tower will be created with every move. Consequently, the pegs which we initially called the source, auxiliary and destination pegs will constantly change. Preliminary Observations Let q equal to the minimum number of moves necessary to complete the relocation. Examining the case when d = 3 we find q = 7. d=4 d=5 d=6 d=7 d=8 Can you see the pattern? q = 15 q = 31 q = 63 q = 127 q = 255 http://www.cut-the-knot.org/recurrence/hanoi.html Observations Continued The following equation gives q for any d: q 2d 1 This will be true for all d provided that p = 3. In the case of De Parville’s story the priests had to relocate a tower of 64 disks. Therefore: q 264 1 18, 446, 744, 073, 709,551, 615 Supposing that the priests were proficient enough to make a move a second, the world would end in 584,942,417,355.1 years. http://www.cut-the-knot.org/recurrence/hanoi.html 0 1 Move Sequence for d = 3 2 3 4 5 6 7 More Observations • When looking at the case of d = 3 the first move must be made to the destination peg to ensure a solution with minimal q. • Similarly, for d = 4 the first move must be made to the auxiliary peg. • It will be the case that for odd d the first move must always be made to the destination peg. For even d the first move must always be made to the auxiliary peg. http://www.cut-the-knot.org/recurrence/hanoi.shtml Recurrence Relations Another way of approaching this problem is to look at the first d – 1 disks. The goal would then be to first relocate those disks to the auxiliary peg. Next one would simply move the last disk to the destination peg. Finally, the solution would be achieved by relocating the d – 1 disk tower to the destination peg. This method gives rise to the notion of recurrence. 0 1 Move Sequence for d = 3 2 3 4 5 6 7 Recurrence Relations Continued Consider T(d), where T(d) = q and d = 3 and q = 7. We know T(3) = 7. Using the recurrence relation we can see that T (3) 2 T (3 1) 1 In this example T(3-1) is the number of moves to relocate the top two disks. Clearly this must be done twice: once to uncover the largest disk and again to re-cover it, therefore it is multiplied by two. The “+1” comes from the single move of the largest disk from the source to destination peg. Likewise, for all d except d = 0 the following holds: T (d ) 2 T (d 1) 1 NOTE: T(0) = 0 Sierpinski’s Gasket http://www.cut-the-knot.com/triangle/Hanoi.shtml http://www.rci.rutgers.edu/~cfs/305_html/Computation/ProbRed_305.html Sierpinski’s Gasket Peg # 3 1 1 d1 d2 d3 Class Exercises What is T(15)? 2 ^15 1 32768 1 32767 Draw the move sequence for d = 4.