Uiversity of Wollogog Research Olie Departmet of Computig Sciece Workig Paper Series Faculty of Egieerig ad Iformatio Scieces 98 A represetatio approach to the tower of Haoi problem M. C. Er Uiversity of Wollogog Recommeded Citatio Er, M. C., A represetatio approach to the tower of Haoi problem, Departmet of Computig Sciece, Uiversity of Wollogog, Workig Paper 8-8, 98, 3p. http://ro.uow.edu.au/compsciwp/8 Research Olie is the ope access istitutioal repository for the Uiversity of Wollogog. For further iformatio cotact the UOW Library: research-pubs@uow.edu.au
A REPRESENTATION APPROACH TO THE TOWER OF HANOI PROBLEM M. C. ER Departmet of Computig Sciece, The Uiversity of Wollogog, Post Office Box 44, Wollogog, N.S.W. 2500 Australia ABSTRACT By makig the movig directio of each disc explicit i the represetatio, a bit-strig so costructed ca be used to drive the Tower of Haoi algorithm. The behaviour of disc moves is further aalyzed based o the bit-strig represetatio. It has bee show that the bit-strig for movig discs ca be used to geerate successively the Gray codes of bits. Keywords ad phases: Tower of Haoi, represetatio approach, Gray codes, combiatorial algorithms. CR Categories: 5.30, 5.39
A REPRESENTATION APPROACH TO THE TOWER OF HANOI PROBLEM M. C. ER Departmet of Computig Sciece, The Uiversity of Wollogog, Post Office Box 44, Wollogog, N.S.W. 2500 Australia.. INTRODUCTION Algorithms for the Tower of Haoi problem are ofte used i the itroductory texts o computer programmig for demostratig the power of recursio (Hayes, 977j Dijkstra, 97; Dromey, 98). Iterestig though these recursive algorithms are, begiers are ot always coviced that these algorithms will work util they are ru. Give such recursive algorithms, it is ot obvious how to move discs aroud util oe actually steps through the programs. Hayes (977), Buema ad Levy (980) preset two iterative algorithms for solvig the Tower of Haoi problem, tha the recursive solutios. is always moved i the cyclic order. hopig that they are less mysterious It is however ot clear why the smallest disc At ay rate, they did ot argue that their solutios are the optimal oes; amely, to move the tower of discs from ~ oe peg to the other peg i the miimum umber of steps. A aalysis is clearly ecessary. It is a commo experiece i the Articifical Itelligece research (Korf, 980) that a suitable represetatio may lead to a efficiet ad trasparet algorithm. It is the theme of this paper to examie the Tower of Haoi problem i this light. By ecodig the disc moves ito a bit-strig, we show that a straight forward iteractive algorithm ca be costructed. More importat, the bit-strig leds a had to a aalysis of the behaviour of the algorithm.
- 2-2. THE PROBLEM The Tower of Haoi problem ivolves three pegs (PI, P2 ad P3) ad discs (0, O 2, 0... O) such that 0 < O < 0 < < O' where 0 is 3 2 3 the smallest disc. Iitially, all the discs are placed o a peg pi as a pyramid with 0 o the top. The task is to move these discs from Pi to Pj such that i ~ j, subject to the followig costraits: Cl : Oly the top disc of a tower may be moved from oe.2eg to the other; C2 No disc may rest upo a smaller disc at ay time; C3 Oly oe disc may be moved at a time. 3. OTHER ITERATIVE SOLUTIONS Hayes (977) gives reasos to believe that the smallest-disc moves must alterate with other disc moves. However, he did ot explai why the smallest disc must move cyclically. Nor did Buema ad Levy (980) give reasos to support their algorithm that the smallest disc must always move i the clockwise directio. Clearly, to move discs from peg (PI) to peg 3 (P3), oe eeds ot have to move discs from PI to peg 2 (P2), the move them to P3. Buema ad Levy's algorithm therefore does ot provide a optimal solutio uder certai circumstaces. Cosider the geeral case. Suppose the smallest disc (0 ) is o top of PI, ad the other two discs O. ad O. are o top of P2 ad P3 respectively.. J Clearly 0 < 0i ad 0 < OJ. Suppose we have just moved 0, either 0i or o. will be moved i the ext move. As O. ~ D., the ext move is a uique J. J solutio depedig o which of O. ad O. is smaller. Suppose 0. < D., the. J. J ext move is to move 0i o top of OJ' After that, we have to move, Now we have two choices; either to move 0 o top of 0i or to move 0 to P2. It is uder these circumstaces that both Hayes (977), ad Buema ad Levy (980) fail to satisfy us that oe choice is better tha the other. A deeper aalysis is clearly called for.
- 3-4. TREE AND BIT-STRING REPRESENTATION Suppose the three pegs are arraged i a circle as show i figure. pl :'\ atic0ckwise( ( \\clockwise P3 \"~ P2 Figure Arragemet of Pegs Defie the clockwise directio as Pl + P2 + P3 + PI, ad the aticlockwise directio as Pl + P3 + P2 + Pl. Ay top disc ca be moved to its eighbourig peg i either clockwise or aticlockwise directio at ay movemet. We ca view the clockwise ad aticlockwise moves as traversal of left ad right braches of a biary tree respectively. It is the possible to represet the directios of disc moves as a biary tree. I other words, we make explicit the directios of disc moves i the represetatio. Suppose we are asked to move discs from a peg to its eighbourig peg i the clockwise directio, we ca assume that the source is Pl, ad the destiatio is P2 without loss of geerality. This ca be expressed formally as follows: PI i P2. Whe =, oly disc D I ca be moved from PI to P2 i the clockwise directio i oe step: Whe N = 2, the smallest disc D should be moved i the aticlockwise l directio from PI to P3. The the larger disc D will be moved from pi 2 to P2 i the clockwise directio. After that, D will be moved from P3 l to PI i the aticlockwise directio thus completig the task. Namely, PI -' P3 PI.J. P2 P3 ~ P2
- 4 - The tree represetatios of disc movig directios are show i figure 2. disc tree represetatio bit-strig represetatio / o = 2 0 3 00000 > = 4 00000 Figure 2 Movig towers clockwise
- 5 - Whe = 3, the steps are as follows:, { D l, D PI P3 2 D, D, D PI ~ P2 D PI l 2 3 3.l. P2 D, D P3 l 2., P2 To move D ad D i the aticlockwise directio, the steps ca be detailed l 2 as follows: PI ~ P2 PI L P3 P2 ~ P3 From the tree represetatio, it is apparet that the tree for movig two discs aticlockwise is the mirror image of the tree for movig them clockwise as show i figure 3. clockwise mirror aticlockwise Figure 3 clockwise ad aticlockwise trees Ideed, the clockwise ad aticlockwise trees are mirror images of each other is~geerally true for ~. PI ~ Hece, it appears that to costruct the tree for D l, D 2,..., D -l' D P2, we simply preped ad apped the mirror image of the tree for D l, D 2..., D -l : PI ~ P2 to the root ad leaf of the followig tree: / If we ow ecode the biary trees by usig Huffma's (952) method (amely left ad right braches are represeted by 0 ad respectively), the biary trees are collapsed ito bit-strigs as show i figure 2. By virtue of the way the biary trees are costructed, we ca geerate the bit-strigs without referrig to the trees. Let BS( ~ ) deotes the bit-strig for movig discs clockwise.
- 6 - It is obvious that B8( ~ ) = () Let C(bs) be the oe's complemet of the bit-strig bs. Oe ca easily show that the mirror image of a biary tree is precisely the oe's complemet of its bit-strig. Therefore, B8 ( ~ ) = C (B8(-,).. )) C (BS (-l ~ ) ) (2) For movig discs aticlockwise, D, D 2,..., D : PI ~ P3, oe ca derive that the tree is precisely the mirror image of the clockwise case. results are show i figure 4. The Let BS( ~) be the bit-strig for movig disks aticlockwise. By the ature of biary digits, oe ca easily verify that BS (.(,) C (BS ( Il- )) or BS( ~ ) = C(BS( ~ )) (3) Fially, we establish a property of the bit-strigs so geerated. Property 0 : The bit-strig for movig discs is symmetric with respect to the cetre bit. Proof This property readily follows from () ad (2) by iductio.
- 7 - disc tree represetatio bit-strig represetatio = \ = 2 00 = 3 00 = 4 I 0000000000 Figure 4 Movig towers aticlockwise
- a 5. HOW TO MAKE MOVES Give a bit-strig for discs«w;~~eed to be able to iterpret it i. -'..-... order to guide the disc moves. First '-C;fa,ll, let us establish some properties. Property Proof D is the first ad last disc to move. Whe =, this is trivially true. I Whe 2, BS (2 ~ ) = C (BS ( ~» C(BS ( ll. ) ) = BS ( ~) BS ( "' ) where BS ( "') is D'S move. Likewise for BS (2... ) Suppose BS ( ~) ad Bs(") preserve this property. The, by (2) BS(+l ~} = C(BS( 'U C(BS("» We have proved by iductio. Likewise for BS (+l '" ). Property 2 Proof D 's moves always alterate with the moves of other discs. Whe =, it is trivial. Whe = 2, BS (2 ~) = BS (,,) BS ( '" ) where BS(l,(.) is D 'S move. This property is obviously true. Similarly for BS (2 J, ) Suppose this property holds for discs. Combie with property, the Dl'S moves must occur at the odd positios. Now we ca prove tqat this property also holds for (+l) discs. By (2) BS (+l ~) = C(BS ( ~» C(BS ( " ) ) Note that oe's complemet does ot chage the positio of its bits idicatig the D'S moves. By virtue of the fact that BS ( "') has D ' s moves as first ad last moves, as well as at odd positios, C(BS ( "» C(BS (,ft» therefore preserves property 2. Likewise for BS(+l "').
- 9 - Now, we are i a positio to iterpret the bit-strig. Let {b l, b 2,..., b m } be a bit-strig. For a bit b i, i is odd, 0 is moved accordig to this parity: 0 ad are clockwise ad aticlockwise respectively. For b., i is eve, other disc other tha 0 is moved. Suppose 0 rests o PI ad b. = 0, we caot move the top disc from P3 to PI due to costrait C2. Therefore, we have a uique solutio, amely to move the top disc from p2 to P3. Similar argumet applies to top disc from p. to p.. J summarized i figure 5. b. =. Suppose Moveoisc (P., P.) is to move the J The algorithm for movig D., such that i f I is Switcho 0 ito "{ Case P~If b. = 0 the Moveoisc (P2, P3) edcase else Moveoisc (P3, P2) ; } ~ ~ P2 If b. = 0 the MoveOisc (P3, PI) edcase else Moveoisc (PI, P3) P3 If b. = a the Moveoisc (PI, P2) edcase else MoveOisc (P2, PI) Figure 5 Movig other disc
- 0-6. FURTHER ANALYSIS We ow further aalyze the bit-strig for movig discs to reveal the iheret properties of the Tower of Haoi problem. Property 3 The smallest disc always moves i a cyclic order. Proof Whe =, this is trivial. Suppose this property holds for (-l) discs. Namely, all the odd positio bits are havig the same parity. By properties ad 2 ad (2), the odd positio bits of the bit-strig for movig discs agai have the same parity. Property 4 The solutio offered by the bit-strig for movig discs is optimal. Proof Whe =, BS ( ~) for 0 : PI ~ P2 or BS ( ~) for 0 : pi, P2 is obviously optimal. Suppose as (-lll..) ad BS (-l "') are optimal. We ow show that as ( ~) is optimal too. To move discs from PI to P2, we eed to move the top (-l) discs from PI to P3, the move D from pi move the (-l) discs agai from P3 to p2. to P2, ad fially That is, PI ~ P2 D PI " P3 pi ~ P2 P3.4 P2 As as (-l J,) is optimal for D, D,.., D : l 2 -l Pi "' Pj, where i t- j. It follows that as ( Il.) for the composite solutio of movig discs, 0' 2., D : PI ~ P2, is optimal. Similar argumet holds for as ( '" )
- - Property 5 Proof The optimal solutio takes 2 - steps. Let S be the umber of bits i BS (.L ) From (2), S 2 * S + - - As Sl = by () Therefore S -l = I i=o 2 i = 2 - As a bit correspods to a takes 2 - steps. step, thus the optimal solutio Property 6 All D., i = odd, move i the same directio. Whereas, all ~ D., J j = eve, move i the opposite directio. Proof From (2), BS(~) = C(BS(-I!l-» C(BS(-l~») where C {BS (-l!l-») = C (C (BS (-2 Il-» I C {BS (-2 ~ )) ) C(C (BS (-2 I).. )» 0 C (C (BS (-2 i. ) ) ) As the cetre bit of BS(!l-) is a idicatio of the movig directio of 0, so the cetre bit of C (BS (-Ill..» idicates the movig directio of D. - As the cetre bits of BS ( ~) ad C{BS (-l.l.» are of differet parity, we have proved that move i opposite directios. D. ~ ad D., such that li-jl =, J Furthermore, C {C {BS (-2 ~ ) ) ) BS (-2 ~ ) C (BS (-3 v.- ) C {BS (-3 U. ) ) As the cetre bits of BS ( ~) ad C {C (BS (-2 ~ )» are havig the same parity, we thus prove that o movig i the same directio. ad D -2 are By iductio, the property holds.
- 2 - Property 7 Let M. be the umber of steps take by D. to get to the.. -i destiatio i the optimal solutio. The M. = 2. Proof: As we kow, the cetre bit of BS ( ~) idicates the movig directio of D, ad that is the oly bit to do so. Hece, M = -i Suppose, M. = 2 for 0 < i S.. By (2), BS (i ~ ) Thus, M..- = =C(BS(i-l~» 2 * 2 - i -(i-i) 2 C(BS(i-lJ.». 7. IMPLEMENTATIONS Now we are i a positio to implemet the algorithm. From property 5, we kow that it takes 2 - steps to move discs from a peg to a target peg. So the cotrol loop ca be implemeted as a for-loop. Further, we kow from property 2 that the moves of D alterate'with other disc moves. l Therefore, the body of the for-loop comprises two move-disc istructios; oe for movig D ' aother for movig other discs. A program based o these l ideas has bee writte usig. C, ad is icluded i Appedix A for referece. Notice that the geeratig fuctio, Geerate, successively geerates the bitstrig from disc up to discs based o (2). A momet's reflectio would covice us that to geerate the bit-strig for discs, it is ot ecessary t? geerate all the bit-strigs for disc up to discs. We ca ideed geerate the bit-strig for discs straight away, by takig the advatage of property 6. Before we spell out the details of the directio geeratig fuctio, we prove a property first. Property 8. " 2 i - l + 2 i * J' S 2 _l All b.t pos.t.os where same parity. j=o,,2... ad i=eve, are occupied by bits of Whereas, other bit positios, i-i i... " p 'I 2 + 2 * J, are occup.ed by b.ts of 0ppos.te pa.ty.