INFORMATION PROCESSING LETIERS
Volume 15, Number 2
6 September 1982
THE TOWERS OF HANOI REVISITED: MOVING THE RINGS BY COUNTING THE MOVES T.R. WALSH Dept. of Computer Science, University of Western Ontario, Lundon, Ontario, N6A 3K7 Canadh
Received 2 December 1981; revised version received 3 June 1982 A non-recursive algorithm, distinct from the one already in the literature, is presented for solving the n-ring Towers of Hanoi problem in the minimum 2” - 1 moves. In addition, a simple relation is found between the binary representation of k and the position of the rings after k moves, allowing either to be obtained from the other in O(n) operations. Thus the position of the rings can be effectively stored in a work of n bits.
Keywords: Towers of Hanoi, iterative algorithms
The well-known Towers of Hanoi problem is as follows. Given three pegs and n rings of different sizes stacked in order of size on one of the pegs with the Iargest ring on the bottom, the object is to move all the rings onto one of the other pegs, and the restrictions are the following: 0. Move one sing at
a tirr e (the
topmost ring on one of the
Pegs)* 1. Never place a ring onto a smaller one.
An algorithm for iolving this problem dates at least as far back as 1892 [2] and has since appeared in many computer science texts as an example of recursion: PROCEDURE RECURSlVE (n, a, b) [move n rings from peg a to peg b) If n = 1, then move the ring from peg a to peg b Else begin RECURSIVE (n - 1, a, c) [move the n - 1 smallest rings from peg a to peg c - the spare Peg - leaving the largest ring sitting on peg a] Move the largest ring from peg a to peg b; RECURWE (n- 1, c, b) [move the n - 1 smallest rings from peg c to peg b, stacking them on top of the kgest ring] End. 64
It was shown in [ZJ (and elsewhere) that this algorithm halts after 2” - 1 moves, and it was essentially shown in [S] that any algorithm which moves the n rings from peg a to peg b in the minimum number of moves is identical to RECURSIVE (n, a, b). Thus not only is 2” - 1 the minimum number of moves in which n rings can be moved from one peg to another, as claimed in f5], but in addition there is a unique sequence of 2” - 1 moves which stacks the rings onto a given peg. To actually execute a recursive procedure like RECURSIVE involves storing in a stack the necessary information at each level of recursion, in this case the names of the pegs from and to which the smallest m rings are being moved for each m = 1, 2 , . ..,n. This is difficult to do when the stacks are being stored mentally (try solving the puzzle with 10 rings). Of course, once stacks are used, the algorithm can be g&n a non-recursive description, but to formulate, understand and correctly prove such a cnon-recursive’ algorithm would appear to merit two stars [l, p. 72, Example 2.161. A simpler non-recursive algorithm is suggested in 121: “It will be nc:&ed that every alternative move consists of a transfer of the smallest disc from one peg to another, the pegs being taken in cyclical order.. . “. This suggests the following
0020-0190/82/0000-0000/$4.75
0 1982 North-Holland
Volume 15,Number 2
INFORMATION
PROCESSING LETTERS
The COrreCtneSS Of LABEL will fOhW
non-recursive algorithm: PROCEDURE CYCLIC (n, a, b) [move n rings from peg a to peg bl If n is odd, then assign the cyclic order ad b+c+
a to the
Pegs Else assign to them the cyclic order a+e+ b-+a [c is the spare peg in either case] 1: Move the smalllest ring to the next peg in the cyclic order; 2: If all the rings are on the same peg, then EXIT Else move the second-smallest top ring onto the peg not containing the smallest ring and GO TO 1.
This algorithm has the advantage that the amount of information needed to determine the next move is independent of n (now try the lo-ring puzzle again!). It can be described in non-algorithmic terms by imposing, in addition to restrictions 0 and I, the following two restrictions: II. Ill.
6 September1982
Never move the ring you just moved (an obvious time-waster!). On the first move: if n is odd, move the smallest ring onto peg b; otherwise onto peg c. Thenceforth never move the smallest ring onto the peg from whence it came.
This algorithm is well known and appears in the literature in several places - see [4], for example and a Mine prLoofof its correctness appears in 131. The following non-recursive algorithm is less well known, and may very well be new:
frmn parts
A and B of the theorem stated and proved below; part C gives a simple relation (similar but not identical to the one in [3]) between the binary representation of k and the position of the rings after k moves of the tinique minimal solution, enabling either to be obtained from the other in Q(n) operations. Theorem. Suppose the rings and pegs are Iubelledas required by LABEL(n, a, b), and then RECURSIVE (n, a, b) is executed. After k G 2” - 1 moves, A: each ring is on a ring or empty peg of opposite parity; 6: exactly one of the top labels (riny or emp;y peg) is even; C: lfj n+l In ’ - - ej, is the binary representation of k, then for m=n, n-l , . . . , 1 ring m is on ring (or empty peg) m t- 1 if and only if j,,,=j,,,+, (since jn+?=O for OGkG2”-1, the case where m=n means that ring n is on peg a iff j, =O).
Proof (by induction on n). Basis of induction: With n = 1, ring 1 is moved from peg a with label 2 to peg b with label 4, and peg c has label 3. A: Ring 1 is always on an empty peg with even label: 2 for k=O and 4 for k=l. B: Two of the three top labels are odd - 1 (ring 1) and 3 (peg c) - and the other one is even - 4 (peg b) when
k=O and 2 (peg a) when k=l. PROCEDURE LABEL (n, a, b) [move n rings from peg a to peg bl Label the rings 1, 2,. . . , n in increasing order of size; Label peg a, b arid c with n+ 1, n +3 and n +2, respectively; [these labels become visible when the pegs are
empty1 1: Move the smal:lest ring onto another ring or onto an empty peg with an even label [we will prove that there will be Iexactly one even label showing] 2: If all the rings are on the same peg, then EXIT Else move the second-smallest top ring onto the peg not containing the smallest ring and GO TO 1.
To describe ILABEL in non-algorithmic terms we impose restrictions 0, I, II and Ill’. Paint the ringls alternately white and black. Paint the ‘spare’ peg in the same colour as the largest ring and the other two pegs in the opposite colour. When movlng the smallest ring, never more it onto a ring or empty peg of the same colour.
c: When k-0, j,=O=j2 and ring 1 is on peg a with laber 2. When k=l, j, =1 #ja and ring 1 is on peg b with label 422.
Induction step: Suppose that there are n > 1 rings so that A, B and C are true for n - 1 rings. For 0 4 k G 2”-’ - 1, RECURSIVE@ - 1, a, c) is being executed. Then ring n may be treated as part of peg a. The labels on the pegs a, c and b (the spare peg for RECURSIVE@ - 1, a, c)) are now n, n + 2 and n + 3, respectively, which differ from the labels required by LABEL(n - 1, a, c) only in that peg c is labelled n + 3 instead of n + 1 - an insignificant difference since n + 1 and n + 3 are of the same parity and are both greaterthan n - 1. Sy the induction hypothesis A is true for rings 1, n-l, I3 is true and C is true for m=l, 29***9 n - 1. The case where m = n - 1 follows from 2 9**‘9 65
INFORMATION PROCESSING LETTERS
Volume15, Number2
6 September 1982
MOVE 45 0101100
-
0101101
Fig.1. 1. But A the fact that j, =0 when O
LABEL(n, a, b) determines a unique sequence of moves, the same sequence us RECURSIVE in, a, b)? and is therefore correct. Proof. We first show that independently lof whether LABELdetermines uniquely each of its moves, each move of RECURSIVE is identical to one of the possible moves of LABEL.Since RECURSIVE and 66
CYCLICtepresent the same sequence of moves, each even-numbered move of RECURSIVEis as described by line 2 of both CYCLIC and LABELand each odd-numbered move of RECURSIVE moves ring 1. By part A of the theorem, ring 1 must be moved onto a ring or empty peg with an even label, as required by LABEL. The first move of LABEL is uniquely determined, since there is exactly one even label onto which ring 1 may be moved - peg b or peg c according to whether n is odd or even. Suppose that the first k moves of LABELare uniquely determined. Then these are identical to the first k moves of RECURSIVE. If k is odd, line 2 of LABEL uniquely determines the next move. If k is even, line 1 of LABELuniquely determines the next move, since by part B of the theorem there is exactly one even label onto which ring 1 may be moved. The proof now follow:s by induction on k. q We show that the cyclic order assigned to the pegs by CYCLICinduces the cyclic order 1 --+even --) odd -* 1 or I--) odd + even + 1 on the top labels for these pegs according to whether the number of moves made is even or odd. By the uniqueness of the minimal algorithm cycLIc(n, a, b) and LABEI..(~, a, b) represent the same sequence of moves. After an even number of moves has been made9 the next rnove is given by line 1 of both CYCLICand LABEL,which are compatible only if the cychc order induced on the top labels is 1 + even --podd --*1. ?%is move uncovers an even label under ring 1, changing the cyclic order to 13 odd
Volume 15, Number 2
INFORMATION
PROCESSING LETTERS
+ even + 1. This proves the assertion. It follows that the cyclic order and the labels together determine the parity of the number of moves made, and thus determine the next move even if the previous one has been forgotten. Also, line 2 of CYCLIC must either move an odd ring according to the cyclic order or an even ring in the opposite order; thus all the odd-numbered rings move in one direction and all the even-numbered ones in the other, as was noticed in [2]. Given the binary representation of k, we can determine the position of the rings after k moves in O(1) opelrations per ring by placing rings n, a -1 , . . ., 1 on the pegs in the way uniquely determined by parts A and C of the theorem. Suppose that rings n, n - 1,...,m+ 1 havebeenplaced. If j, = jm+,, ring m is placed upon ring m + 1 (peg m + 1 if m = n); otherwise ring m is placed upon one of the other two pegs, and by A, ring m must be placed on a top label of opposite parity to m. We show that exactly one top label will be of the same parity as m, so that in either case there is exactiy one place to put ring m. This assertion is true for m ==n, since the top labels are n + 1, n + 2 and n + 3. ,4ssume it to be true for a given m; then there are two labels of the same parity as m - 1, but placing ring m covers one of them. This completes the induction, and incidentally the case m = 0 is an ind.ependent proof of part B of the theorem. Conversely, given the position of the rings, we can detem.tie in O(1) operations per ring how many moves have been made, assuming the algorithm to have been executed correctly, PP~ to catch an error which has not been corrected. Set j ,,+ , = 0, and assume j,, j n--l,...,jm+l to have been determined. If ring m is on a ring or empty peg of the same parity as m, then an error was made andi not corrected; otherwise j,,,+ 1 = j m according Ito when ring m is on ring (or empty peg) m + 1. Then, unless an error was made and cor-
6 September 1982
rected, the binary representation of the number of moves made is jn+, j, ‘9j,. This last algorithm - determining the number of moves from the position of the rings - and the uniqueness of the minimal solution lead to a parlour trick with a Towers of Hanoi puzzle with appropriately coloured rings and pegs. Ask one guest to make a sequence of moves in your absence, while the other guests count his moves and enforce restrictions 0 and I. On your return, ‘guess’ the number of moves made, and either complete the minimal solution or challenge him to do it according to whether your guess was right or wrong. A wrong guess implies a corrected error, and a violation of part A implies an uncorrected error: in either case, a minimal solution is now impossible. The parity of a correct guess determines which line of LABEL was last exectued, tid completing the minimal sequence of moves is now as easy as counting them. l
Acknowledgment The author wishes to thank AK. Dewdney, E.W. Elcock and the referees for suggestions to improve the presentation of this note.
[l] A. Aho, J. Hopcroft and J. Ullman, The Design and Analysis of Computer Algorithms (Addison-Wesley, Reading, MA, 1974). 121R.R.W. Ball, Mathematical Recreations and Essays (McMillan, London, 1982). [31 P. Buneman and L. Levy, The towers of Hanoi problem, Inform. Process. Lett. 10 (1980) 243-244. [41 P.J. Hayes, A note on the towers of Hanoi problem, Comput. J. 20 (1977) 282-285. [51 D. Wood, The towers of Brahma and Hanoi revisited, J. of Recreational Math. 14 (1981) 17-24.
67