The towers of Hanoi problem

The towers of Hanoi problem

Volume 10, number 4,s INFORMATION PROCESSING LETTERS 5 July 1980 Peter BUNEMAN and Leon LEVY * Department of Computer and Information Science, Moor...

197KB Sizes 83 Downloads 199 Views

Volume 10, number 4,s

INFORMATION PROCESSING LETTERS

5 July 1980

Peter BUNEMAN and Leon LEVY * Department of Computer and Information Science, Moore School, Universityof Pennsylvania,Phikdelphia, PA 13104, t?.S.A, Received 4 May 1979; revised version received 1 April 1980 Towers of Hanoi, graycode

In the Towers of Hanoi Problem there are three pegs A, B and C, and n (n 3 1) disks of different diameters. The disks are placed on peg A in order of decreasing diameter with the smallest disk on top. The object is to move all the disks from peg A to one of the other two pegs by moving one disk at a time. Only the top disk may be moved from any peg, and this must never be placed upon a disk of smaller diameter. There is a simple recursive solution to this problem, which is used by many texts as an example of recursive programming. However, there is an equally simple iterative program that is tr otivated by the following observations. Consider the pegs to be in a ring so that, moving clockwise, the pegs are A, B, C, A, B, ... . Given any configuration of the disks, there are at most three possible moves: the smallest disk ran be moved to either of two pegs, and the second smallest of the topmost disks may be moved onto the other peg that does not contain the smallest disk. Now, two consecutive moves of the smallest disk may be combined into one move, and two consecutive moves of the other movable disk will cancel each other. Therefore one should consider an algorithm that alternately moves the smallest disk and makes the other possible’ move. If, for simplicity, we assume that the smallest disk should move in only one direction, say clockwise, then we are led to the algorithm Move smallest disk one peg clockwise; do a disk (other than the smallest) can be moved -+ Move that disk; * On leave from University of D&ware; current address: BeUi Telephone Laboratories, Whippany, NJ 0798 1, U.S.A.

Yove the smallest disk one peg clockwise od Note that the complement of the guard of the loop (together with the obvious invariant that no larger disk lies on a smaller one) implies that if the algorithm terminates, then all disks are on one peg. Correct termination of the algoritm follows from the following theorem. Theorem. For any k, 1 0) smallest disks are on some peg A, say. Then by induction, after 2k - 1 moves, disks 1 through k are on peg B or C (depending on whether k is odd or even). The next move places ‘diskk + 1 on C (or B), Now note that the next 2k - 1 moves are equivalent to executing the algorithm from the beginning so that, by induction, after these moves, disks 1 through k will be on top of disk k + 1, which is on peg C (or B). IDiscussion.The Towers of Hanoi algorithm just shown can be related to the Chinese &?s; puzzle, in which at most two moves are possible at each step. can also be related to the Gray code seqyem: a sequence of 2” bit strings, each of length n, which contains every possible such bit string and in which successivt U._strings differ in at most one position. To generate the binary reflected Gray code from the algorithm given here, take the number of moves made by ifh disk (mod 2) after j moves of the algo and use this as the ith bit in the jth st Y

INFORMATION PROCESSING LETTERS

connections have been noted by Gardner ndently developed the algorithm other references for ;&ted ssions of the Towers of Hanoi, guzzle and Gray codes.

II ] J.R. Bitna, 6;. Erlich and L..d. Reingold, Efficient genern of the binary reflected Gray code and its applica-

244

5 July 1980

tions, Comm. ACM 19 (1976) 517-521. [2] O.P. Buneman and L.S. Levy, Gray code gleanings, Proc. 1978 Conference on Information Sciences and Systems, Johns Hopkins University. [ 3 ] M. Gardner, Mathematical games: the curious properties of the Gray code and how it can be used to solve puzzles, Sci. Amer. (August 1972) 106-109. [4] P.J. Hayes, A note on the towers of Hanoi problem, Comput. J. (August 1977) 282-285. [5] D.E. Knuth, Structured programming with GOTOs, Comput. Surveys (December 1974). [6] J. Misra, Remark on algorithm 246, ACM Trans. Math. Software 1 (1975) 285. [ 71 J. Misra, personai communication.