Bouncing Towers move faster than Hanoi Towers, but still require exponential time
BBouncing Towers move faster than Hanoï Towers,but still require exponential time
Jérémy Barbay
Departamento de Ciencias de la Computación (DCC),Universidad de Chile,Santiago, Chile [email protected]
Abstract
The problem of the Hanoï Tower is a classic exercise in recursive programming: the solutionhas a simple recursive definition, and its complexity and the matching lower bound are thesolution of a simple recursive function (the solution is so easy that most students memorize itand regurgitate it at exams without truly understanding it). We describe how some very minorchanges in the rules of the Hanoï Tower yield various increases of complexity in the solution, sothat they require a deeper analysis than the classical Hanoï Tower problem while still yieldingexponential solutions. In particular, we analyze the problem fo the Bouncing Tower, where justchanging the insertion and extraction position from the top to the middle of the tower resultsin a surprising increase of complexity in the solution: such a tower of n disks can be optimallymoved in √ n moves for n even (i.e. less than a Hanoï Tower of same height), via 5 recursivefunctions (or, equivalently, one recursion function with 5 states). Keywords and phrases
Brähma Tower problem, Computer Science Education,
Hanoï TowerProblem , Recursivity.
The
Hanoï Tower Problem is a classical problem often used to teach recursivity, originallyproposed in 1883 by Édouard Lucas [5, 6], where one must move n disks, all of distinct size,one by one, from a peg A to a peg C using only an intermediary peg B , while ensuringthat at no time does a disk stands on a smaller one. As early as 1892, Ball [3] described anoptimal recursive algorithm which moves the n disks of a Hanoï Tower in 2 n − Hanoï Tower Problem , and the analysis of the original problem is still a sourceof inspiration hundreds of year after its definition: for instance, Allouche and Dress [1]proved in 1990 that the movements of the
Hanoï Tower Problem can be generated by afinite automaton, making this problem an element of
SP ACE (1).The solution to the
Hanoï Tower Problem is simple enough that it can be memorized andregurgitated at will by students from all over the world: asking about it in an assignment orexam does not truly test a student’s mastery of the concept of recursivity, pushing instructorsto consider variants with slightly more sophisticated solutions. Some variants do not makethe problem more difficult (e.g. changing the insertion and removal point to the bottom:the solution is exactly the same), some make it only slightly more difficult (e.g. consideringthe case where the disks are not necessarily of distinct sizes, described and analized inAppendix A), but some small changes can make it surprisingly more difficult.We consider the Bouncing Tower Problem, which only difference with the
Hanoï TowerProblem is the insertion and removal point in each tower, taken to be the middle insteadof the top (see Figure 1 for an illustration with Bouncing Towers of sizes n = 3 and n = 4, © Jérémy Barbay;licensed under Creative Commons License CC-BYLeibniz International Proceedings in InformaticsSchloss Dagstuhl – Leibniz-Zentrum für Informatik, Dagstuhl Publishing, Germany a r X i v : . [ c s . G L ] M a r Bouncing Towers and Section 2.1 for the formal definition). If the disks all weight the same, one can imaginesuch a tower as standing on a spring, the elasticity k of the spring being tuned so that themiddle of the tower is always at the same height, where disks are inserted and removed. Figure 1
An illustration of the rules forthe insertion and removal in a BouncingTower, depending on the parity of its size(sizes n = 3 and n = 4 here). In each case,the shaded disk indicates the removal pointand the arrow indicates the insertion point. As for the classical Hanoï Tower, such insertion and removal rules guarantee thatany move is reversible (i.e. any disk d removed from a peg X can always be immediatelyreinserted in the same peg X ), that the insertion and removal positions are uniquelydefined, that each peg can always receive a disk, and that each tower with one disk or morecan always yield one disk. The problem is very similar to the Hanoï Tower Problem : onewould expect answering the following questions to be relatively easy, possibly by extendingthe answers to the corresponding questions on Hanoï Towers : Consider the problem of moving a Bouncing Tower of n disks, all of distinctsize, one by one, from a peg A to a peg C using only an intermediary peg B , while ensuring that at no time does a disk stands on a smaller one: Which sequences of steps permit to move such a tower? What is the minimal length of such a sequence? How many shortest such sequences are there?
We show that there is a unique shortest sequence of steps which moves a Bouncing Towerof n disks of distinct sizes, and that it is of length at most √ n (i.e. exactly √ n = 3 n if n is even, and √ n − − = 3 n − + 2(3 n − − < √ n if n is odd). As √ ≈ . <
2, thissequence is exponentially shorter than the corresponding one for the
Hanoï Tower Problem (of length 2 n − insertion and removal order of a tower inSection 2.3. We describe a recursive solution in Section 3, via its algorithm in Section 3.1, theproof of its correctness in Section 3.2 and the analysis of its complexity in Section 3.3. Theoptimality of the solution is proved in Section 4, via an analysis of the graph of all possiblestates and transition (defined and illustrated in Section 4.1) and a proof of optimality for eachfunction composing the solution (Section 4.2). We conclude with a discussion (Section 5) ofvarious other variants of similar or increased complexity, and share in Appendix A the textand the solution of a simpler variant successfully used in undergraduate assignments andexams. For a Hanoï Tower, the answer to those question is that there is a single such shortest sequence, oflength 2 n −
1, obtained by the recursion h ( n, A, B, C ) = h ( n − , A, C, B ) . ” A → B ; ” .h ( n − , B, A, C ) if n > ∅ otherwise. . Barbay 3 In this section we define more formally the Bouncing Tower (Section 2.1), how small ex-amples already show that moving such towers require less steps than moving a Hanoï Tower(Section 2.2), and some properties of the order in which disks are inserted or removed on apeg to build or destroy a tower (Section 2.3).
The “middle” disk of a tower of even size is not well defined, nor is the “middle” insertion point in a tower of odd size: we define both more formally in such a way that if n is odd, the removal position is the center one, and the insertion point is below it; while if n is even,the insertion point is in the middle of the tower, while the removal position is below themiddle of the tower (see Figure 1 for an illustration with sizes n = 3 and n = 4). Moreformally, on a peg containing n disks ranked by increasing sizes, the removal point is thedisk of rank b n c + 1; and the insertion point is position b n +12 c .The insertion of disk d on peg X is legal if inserting d in the insertion point of X yields a legal configuration, where no disk is above a smaller one. A move from peg X topeg Y is legal if there is a disk d to remove from X , and if the insertion of d on the Y islegal. For size one or two, there is no difference in the moving cost between a Hanoï Tower anda Bouncing Tower. The first difference appears for size three, when only five steps arenecessary to move a Bouncing Tower (see the sequence of five steps to move a BouncingTower of size n = 3 in Figure 2) as opposed to the seven steps required for moving aclassical Hanoï Tower (see the sequence of seven steps to move a Hanoï Tower of size n = 3in Figure 3). A B C A → B
13 2
A B C A → B A B C A → C
23 1
A B C B → C A B C B → C A B C
Figure 2
A Bouncing Tower of three disks can be moved in just five steps.
A B C A → C
23 1
A B C A → B A B C C → B A B C A → C
12 3
A B C B → A A B C B → C A B C A → C A B C
Figure 3
A Hanoï Tower of three disks require seven steps to be moved between two pegs.
When an odd number of disks is present on the peg A , and an even number is presenton pegs B and C , a sub-tower of height 2 can be moved from A in 2 steps, when in aHanoï Tower we need 3 steps to move any subtower of same height. In the Bouncing TowerProblem, having a third disk “fixed” on A yields a reduced number of steps. We formalizethis notion of “fixed” disk in the next section. Bouncing Towers
Before considering the complete problem over three pegs, we describe some concept aboutsingle pegs, and on the order in which the disks are inserted and removed on a specific peg. (cid:73)
Definition 1.
We define the removal order as the order in which disks (identified by theirrank in the final tower) can be removed from a Bouncing Tower. Symmetrically, we definethe insertion order as the order in which the disks are inserted in the tower.The symmetry of the rules concerning the insertion and removal location of Boun-cing Towers yields that the insertion order is the exact reverse of the removal order (the insertion point of a tower is the removal point of a tower with one more disk), and eachdisk removed from a peg can be immediately replaced exactly where it was.In particular, a key argument to both the description of the solution in Section 3 andto the proof of its optimality in Section 4 is the fact that, when some (more extreme) disksare considered as “fixed” (i.e. the call to the current function has to terminate before suchdisks are moved), the order in which a subset of the disks is removed from a peg dependson the number of those “fixed” disks. (cid:73)
Definition 2.
When moving recursively n disks from a peg X with x > n disks, the x − n last disks in the removal order of X are said to be fixed . The parity of peg X is the parityof the number x of disks fixed on this peg.Bouncing Towers cannot be moved much faster than Hanoï Towers: (cid:73) Lemma 3.
It is impossible to move more than one disk between two pegs of same paritywithout a third peg.
Proof.
Between two pegs of same parity, the removal order is the same. So the first diskneeded on the final peg will be the last one removed from the starting peg. With more thanone disk, we need the third peg to dispose temporally other disks. (cid:74)(cid:73)
Lemma 4.
It is impossible to move more than two disk between two pegs of oppositeparities without a third peg.
Proof.
Between two pegs of opposite parities, the removal orders are different: But thedefinition of the middle is constant when the number of disks changes of 2. So after movingtwo disks the third cannot be inserted in the right place. (cid:74)
The removal and insertion orders are changing with the parity of the Bouncing Tower:Consider a peg with n disks on it:if n = 2 m + 1 is odd, then the disks are removed in the following order:( m + 1 , m + 2 , m, m + 3 , m − , m + 4 , . . . , , m, , m + 1 , n = 2 m is even, then the removal order is:( m + 1 , m, m + 2 , m − , m + 3 , m − , . . . , m − , , m, m and m + 2, of m − m + 3, and more generally of any pairof disks i and m − i for i ∈ [1 .. b n/ c ], are distinct. More specifically, disks are alternatelyextracted below and above the insertion point. This implies the two following connexitylemma: . Barbay 5 (cid:73) Lemma 5.
The k first disks removed from the tower are contiguous in the original tower,and they are either all smaller or all larger than the ( k + 1) -th disk removed. (cid:73) Lemma 6. If k disks are all smaller than the disk below the insertion point, and alllarger than the disk above the insertion point, then there exists an order in which to addthose k disks to the tower. Proof.
By induction: for one disk it is true; for k disks, if the insertion point after the insertion of disc d is above d then add the larger and then the k − k − (cid:74) We present in the next section a solution to the Bouncing Tower Problem which takesadvantage of the cases where two disks can be moved between the same two pegs in twoconsecutive steps.
One important difference between Hanoï Towers and Bouncing Towers is that we need notalways to remove n − n disks to place the n -th disk on another peg (e.g.in the sequence of steps shown in Figure 2, disk 3 was removed from A when there was stilla disk sitting on top of it). But we need always to remove at least n − n -th disk, as it is the last or the last-but-one disk removed. This yields a slightlymore complex recursion than in the traditional case. We describe an algorithmic solutionin Section 3.1, prove its correctness in Section 3.2, and analyze the length of its output inSection 3.3. We prove the optimality of the solution produced separately, in Section 4. Note | A | the number of disks on peg A , | B | on B and | C | on C . For each triplet ( x, y, z ) ∈{ , } , we define the function movexyz ( n, A, B, C ) moving n disks from peg A to peg C using peg B when | A | ≥ n , | A | − n ≡ x mod 2, | B | ≡ y mod 2, | C | ≡ z mod 2, and the n first disks extracted from A can be legally inserted on B and C . Less formally, there are x fixed disks on the peg A , y on B and z on C .We need only to study three of those 2 = 8 functions. First, as the functions aresymmetric two by two: for instance, move000 ( n, A, B, C ) behaves as move111 ( n, A, B, C )would if the insertion point in a tower of odd size was above the middle disk, and the removal point in a tower of even size was above the middle of the tower: in particular,they have exactly the same complexity. Second, the reversibility and symmetry of thefunctions yields a similar reduction: move001 ( n, A, B, C ) has the same structure as thefunction move100 ( n, A, B, C ) and the two have the same complexity.We describe the python code implementing those functions in Figures 4to 7, so thatthe initial call is made through the call move000(n,"a","b","c") , while recursive callsrefer only to functions move000 ( n, A, B, C ) (Figure 4), move100 ( n, A, B, C ) (Figure 5), move001 ( n, A, B, C ) (similar to move100 ( n, A, B, C ) and described in Figure 6) and move010 ( n, A, B, C )(Figure 7).The algorithm for move000 ( n, A, B, C ) (in Figure 4) has the same structure as the cor-responding one for moving Hanoï Towers, the only difference being in the parity of the pegsin the recursive calls, which implies calling other functions than move000 ( n, A, B, C ), in thiscase move001 ( n, A, B, C ) and move100 ( n, A, B, C ). The algorithms for move100 ( n, A, B, C ) Bouncing Towers
Figure 4 move n, A, B, C ) def move (a ,b ):print "("+ a +" ," ,print b +")" ,def move000 (n ,a ,b ,c ):if n >0 :move100 (n -1 ,a ,c ,b)move (a ,c)move001 (n -1 ,b ,a ,c) Figure 5 move n, A, B, C ) def move100 (n ,a ,b ,c ):if n == 1 :move (a ,c)elif n >1 :move100 (n -2 ,a ,c ,b)move (a ,c)move (a ,c)move010 (n -2 ,b ,a ,c) Figure 6 move n, A, B, C ) def move001 (n ,a ,b ,c ):if n == 1 :move (a ,c)elif n >1 :move010 (n -2 ,a ,c ,b)move (a ,c)move (a ,c)move001 (n -2 ,b ,a ,c) Figure 7 move n, A, B, C ) def move010 (n ,a ,b ,c ):if n == 1 :move (a ,c)elif n == 2 :move (a ,b)move (a ,c)move (b ,c)elif n >2 :move010 (n -2 ,a ,b ,c)move (a ,b)move (a ,b)move010 (n -2 ,c ,b ,a)move (b ,c)move (b ,c)move010 (n -2 ,a ,b ,c) (in Figure 5) and move001 ( n, A, B, C ) (in Figure 6) and are taking advantage of the differ-ence of parity between the two extreme pegs to move two consecutive disks in two moves, butstill has a similar structure to the algorithm for move000 ( n, A, B, C ) and the correspondingone for moving Hanoï Towers (just moving two disks instead of one).The algorithm for move010 ( n, A, B, C ) is less intuitive. Given that the removal and insertion orders on the origin peg A and on the destination peg C are the same (becausethe parity of those pegs is the same), n − A before the lastdisk of the removal order, which yields a naive algorithm such as described in Figure 8.Such a strategy would yield a correct solution but not an optimal one, as it reduces the sizeonly by one disk at the cost of two recursive calls and one step (i.e. reducing the size by twodisks at the cost of four recursive calls and three steps), when another strategy (describedin the algorithm in Figure 7) reduces the size by two at the cost of three recursive calls andfour steps: moving n − C , the two last disks of the removal order on B , then n − A , the two last disks of the removal order on C , then finally the n − C . The first strategy ( f ( n ) = 2 f ( n −
1) + 2 = 4 f ( n −
2) + 3) yields a complexity withinΘ(2 n ) while the second strategy ( f ( n ) = 3 f ( n −
2) + 4) yields a complexity within Θ(3 n ).We show in Section 3.2 that moving two disks at a time is correct in this context and inSection 4 that the latter yields the optimal solution. We prove the correctness of our solution by induction on the number n of disks. (cid:73) Theorem 7.
For any positive integer value n , and any triplet ( x, y, z ) ∈ { , } of booleans,the function movexyz ( n, A, B, C ) produces a sequence of legal steps which moves a BouncingTower from A to C via B . . Barbay 7 Figure 8
Alternative (non optimal)take on move n, A, B, C ) % |A|-n >0 is even ;% |B| is odd ;% |C| is even .IF n ==1move (A ,C );ELSEmove101 (n -1 ,A ,C ,B );move (A ,C );move101 (n -1 ,B ,A ,C );ENDIF Figure 9
Alternative (non optimal)take on move n, A, B, C ) % |A|-n >0 is even ;% |B| is odd ;% |C| is even .IF n ==1move (A ,C );ELSEmove010 (n -1 ,A ,C ,B );move (A ,C );move010 (n -1 ,B ,A ,C );ENDIF The proof is based on the following invariant, satisfied by all recursive functions onentering and exiting: (cid:73)
Definition 8.
Requirement for insertion ( i ) : The disks above the insertion point of B or C are all smaller than the first n disks removed from A ; and the disks below the insertion point of B or C are all larger than the first n disks removed from A (see an illustration inFigure 10).4... n − n − n A B C
Figure 10
Requirement for insertion ( i ) : disks 4 to n − B as the insertion point of B is between 2and n −
1; and on C as the insertion point of C is under 3. Proof.
Consider the property IH ( n ) = “ ∀ ( x, y, z ) ∈ { , } , ∀ i ≤ n, movexyz ( i, A, B, C ) iscorrect”. IH (0) is trivially true, and IH (1) can be checked for all functions at once. For allvalues x, y, z , the function movexyz (1 , A, B, C ) is merely performing the step move ( A → C ).The hypothesis IH (1) follows. Now, for a fixed n >
1, assume that IH ( n −
1) holds: weprove the hypothesis IH ( n ) separately for each function.Analysis of move000 ( n, A, B, C ): According to IH ( n −
1) the call to move100 ( n − , A, B, C ) is correct if ( i ) and ( p ) are respected. ( i ) is implied by ( i ) on move000 ( n − , A, B, C ); ( p ) is implied by( p ) and the remaining disk on A ( a − n mod 2 ≡ ⇒ a − ( n −
1) mod 2 ≡ The step move ( A → C ) is possible and legal because of the precondition ( i ) for move000 ( n, A, B, C ): the disk moved was in the n first removed from A , and so canbe introduced on C . The call to move001 ( n, A, B, C ) is symmetrical to 1, and so correct. We can check the final state by verifying that the number of disks removed from A and added to C is ( n −
1) + 1 = n .So move000 ( n, A, B, C ) is correct. Bouncing Towers
Analysis of move100 ( n, A, B, C ): move100 ( n − , A, B, C ) is correct according to IH ( n − i ) is given by ( i ) for the initial call, and the parity ( p ) is respectedbecause we move two disks less than in the current call to move100 ( n, A, B, C ). The two disks left (let us call them α and β ) are in position (given fig. 11, ( i )) suchthat the removal order on A is ( α, β ) and the insertion order on C is ( β, α ) (seefig.11, ( ii )). They can be inserted on C because of requirement ( i ). So the two disksare correctly moved in two steps. The requirements for move010 ( n − , A, B, C ) are satisfied:( i ) stand as a consequence of the precondition ( i ) for the current call, as the n − C were on A before the original call, in the middle of α and β .( p ) : The number of disks on C is still even as we added two disks. The numberof disks on A is still odd as we removed two disks.So, because of IH ( n − move010 ( n − , A, B, C ) is correct.So move100 ( n, A, B, C ) is correct. xy ( i ) n odd: a is removed first, y is removed second. xy ( ii ) n even: y is removed first, x is removed second. Figure 11
Removal order of the last two disks.
Analysis of move001 ( n, A, B, C ): This function is the exact symmetric of move100 ( n, A, B, C ),for a task exactly symmetric, so has a symmetric proof of its correctness.Analysis of move010 (2 , A, B, C ): The two disks (let us call them α and β ) are in position(given fig. 11, ( ii )) such that the removal order on A is ( β, α ) and the insertion orderon C is ( α, β, ), as A and C have the same parity. β can be inserted on B and theycan both be inserted on C because of requirement ( i ). So the two disks are correctlymoved in three steps, using peg B to dispose temporally disk β . So move010 (2 , A, B, C )is correct.Analysis of move010 ( n, A, B, C ) if n >
2: All along of this proof of correctness we shalluse the fact that fixing 2 disks on the same peg doesn’t change the parity of this peg. move010 ( n − , A, B, C ) is correct as: from ( i ) for the initial call results ( i ) for thefirst recursive call; ( p ) is a natural consequence of ( p ) for the initial call (becauseparity conserved when icing two disks). So IH ( n −
1) implies that move010 ( n − , A, B, C ) is correct. . Barbay 9 n f f f d n/ e Figure 12
The first values of f , f and f , computed automatically from the recursion.those corrobolate the intuition that f ( n ) < f ( n ) for values of n larger than 1. A and B having different parities, we can move two consecutive disks in two consec-utive calls as for move100 ( n, A, B, C ). The second recursive call to move010 ( n − , A, B, C ) verifies conditions ( i ) and ( p ) as only two extremes disk have been removed from A . The two next steps are feasible because of the difference of parity between B and C (same argument as point 2). The last recursive call is symmetric to the first call, as we move back the n − C .So move010 ( n, A, B, C ) is correct. (cid:74) We analyze the complexity of this solution in the next section.
Let f xyz ( n ) be the complexity of the function movexyz ( n, A, B, C ), when | A | ≥ n , | A |− n ≡ x mod 2, | B | ≡ y mod 2 and | C | ≡ z mod 2. The algorithms from Figures 4 to 7 yield arecursive system of four equations. ∀ x, y, z f xyz (0) = 0 ∀ x, y, z f xyz (1) = 1 f (2) = 3 ∀ n > , f ( n ) = f ( n −
1) + 1 + f ( n − ∀ n > , f ( n ) = f ( n −
2) + 2 + f ( n − ∀ n > , f ( n ) = f ( n −
2) + 2 + f ( n − ∀ n > , f ( n ) = 3 f ( n −
2) + 4As f is defined exactly as f (because of the symmetry between move001 ( n, A, B, C )and move100 ( n, A, B, C )), we can replace each occurence of f by f , hence reducing thefour equations to a system of three equations: ∀ x, y, z f xyz (0) = 0 ∀ x, y, z f xyz (1) = 1 f (2) = 3 ∀ n > , f ( n ) = 2 f ( n −
1) + 1 ∀ n > , f ( n ) = f ( n −
2) + 2 + f ( n − ∀ n > , f ( n ) = 3 f ( n −
2) + 4Lemmas 9 to 11 resolve the system function by function. The function f ( n ) can besolved independently from the others: (cid:73) Lemma 9. f ( n ) = if n = 0 ; if n = 1 ; if n = 2 ; n +12 − if n ≥ is odd; and × n − − if n ≥ is even. Proof.
Consider the recurrence X k +1 = 3 X k + 4 at the core of the definition of f : a mereextension yields the simple expression X k = 3 k ( X + 2) − n ≥ k = n − ≥ U = 1 and U k +1 = 3 U k + 4 so that f (2 k + 1) = U k = 3 k (1 + 2) −
2. Then f ( n ) = 3 × k − k +1 − n ≥ n ≥ k = n ≥ V = 3 and V k +1 = 3 V k + 4 so that f (2 k ) = V k =3 k (3 + 2) −
2, so that f ( n ) = 5 × k − n ≥ (cid:74) The expression for the function f yields the expression for the function f : (cid:73) Lemma 10. f ( n ) = if n = 0 ; if n = 1 ; if n = 2 ; if n = 3 ; × n − + 2 where n ≥ is even; and n +12 − where n ≥ is odd. Proof.
Consider the projection of the system to just f : f ( n ) = n = 01 if n = 1 f ( n −
2) + 2 + f ( n −
2) if n ≥ k ≥
0, we combine some change of variables with the resultsfrom Lemma 9 to yied two linear systems, which we solve separately: V k = f (2 k ) and V = f (0) = 0 so that f ( n ) = V k if n is even and k = n ; and U k = f (2 k + 1) and U = f (1) = 1 so that f ( n ) = U k if n is odd and k = n − .On one hand, U k = U k − + 2 + f (2 k + 1 −
2) for k > U = 1. This yields alinear recurrence which we develop as follow: U k = U k − + 2 + f (2 k −
1) by definition;= U k − + 2 + 3 × (2 k − − − k − U k − + 3 k by mere simplification;= U + 32 (3 k −
1) by resolution of a geometric serie;= 3 k +1 −
12 because U = 1.Since f ( n ) = U n − when n is odd, the solution above yields f ( n ) = n +12 − if n is odd.On the other hand, V k = V k − + 2 + f (2 k −
2) for k > V = 0. The initialconditions of f for n = 0 , V k : V = 0; V = . Barbay 11 V + 2 + f (0) = 0 + 2 + 0 = 2; and V = V + 2 + f (2) = 2 + 2 + 3 = 7. Then we developthe recursion for k ≥ U k : V k = V k − + 2 + f (2 k −
2) by definition;= V k − + 2 + 5 × (2 k − − − k − ≥ k ≥ V k − + 5 × k − by mere simplification (still only for k ≥ V + 5 (cid:0) + · · · + 3 k − (cid:1) by propagation;= V + 5 3 k − −
22 by resolution of a geometric serie;= 7 + 52 (3 k − −
2) because V = 7;= 52 3 k − + 2 by simplification.Since f ( n ) = V n when n is even, the solution above yields f ( n ) = n − + 2 if n iseven.Reporting those results in the definition of f yields the final formula: f ( n ) = n = 0;1 if n = 1;2 if n = 2;4 if n = 3; × n − + 2 where n ≥ n +12 − where n ≥ (cid:74) Finally, the expression for the function f directy yields the expression for the function f : (cid:73) Lemma 11. f ( n ) = if n = 13 if n = 25 if n = 33 n where n ≥ is even; and n − + 1) where n ≥ is odd. Proof. f ( n ) = n = 1;2 if n = 2;4 if n = 3; n − + 2 where n ≥ n +12 − where n ≥ f ( n ) using that f ( n ) = 2 f ( n −
1) + 1. f ( n ) = n = 13 if n = 25 if n = 35 × n − − + 5 where n ≥ n where n ≥ (cid:74) As √ ≈ . <
2, this value is smaller than the number 2 n − Each legal state of the Bouncing Tower Problem with three pegs and n disks can be uniquelydescribed by a word of length n on the three letters alphabet { A, B, C } , where the i -th letterindicates on which peg the i -th largest disk stands. Moreover, each word of { A, B, C } n corresponds to a legal state of the tower, so there are 3 n different legal states (even thoughnot all of them are reachable from the initial state).To prove the optimality of our algorithm, we prove that it moves the disks along theshortest path in the configuration graph (defined in Section 4.1) by a simple induction proof(in Section 4.2). The configuration graph of a Bouncing Tower has 3 n vertices corresponding to the 3 n legalstates, and two states s and t are connected by an edge if there is a legal move from state s tostate t . The reversibility of moves (seen in Section 2.3) implies that the graph is undirected.Consider the initial state A . . . A (= A n ). The smallest disk 1 cannot be moved beforethe other disks are all moved to peg B or all moved to peg C : we can’t remove disk 1 frompeg A if there is a disk under it, and we can’t put it on another peg if a larger disk is alreadythere. This partitions G into three parts, each part being characterized by the position ofdisk 1; these parts are connected by edges representing a move of disk 1 (see the recursivedecomposition of G ( n ) in Figure 13).Each part is an instance of the configuration graph G ( n −
1) defining all legal steps of( n −
1) disks { , . . . , n } given that disk 1 is fixed on its peg. Figure 13
First decomposition of the configuration graph of the Bouncing Tower Problem.
Let us consider this subgraph G ( n − A ). Note each state of this graph aX . . . Z , where a stands for the disk 1 fixed onpeg A , and X . . . Z for positions of other disks on diverse pegs. The removal order changesfrom those observed in G each time | A | is odd.To remove the two extreme disks 2 and n (not moving disk 1, since it is fixed), it isnecessary to move all other disks to a single other peg (same argument as for G ( n )), so wecan divide our configuration graph in subsets of states corresponding to different positionswhere disks 2 and n are fixed. . Barbay 13 n n − A B C n − nA B C Figure 14
States where disk 2 is on A anddisk n is on another peg (i.e. B or C ) cannot beaccessed from the initial state A . . . A for n > A cannotreceive larger disk than 2 (and all are), B cannotreceive smaller disk than n (and all are), and C cannot receive disk 2 nor n if n > n − nA B C n − nA B C Figure 15
States aBA . . . AC and aBB . . . BC are not connected in the subgraph where disks 2and n are fixed on B and C , and disk 1 is fixed on A : As no disk can be inserted under n , if n > n − > A to B (as to move more than one diskbetween two pegs of same parity require a thirdpeg). This defines 9 parts, as each of the two fixed disks can be on one of the three peg. Ofthose 9 parts, we need focusing only on 5:two parts of the graph cannot be accessed from the initial state aA . . . A , (see an illus-tration in Figure 14); andthe part of the graph where disk 2 is fixed on B and disk n is fixed on C contains twoparts, which are not connected for n > aA . . . A , aB . . . B and aC . . . C . Consider firstthe graphs G ( n ) for n ∈ { , , } ( n + 1 disks in total if we count the fixed one): they arerepresented in Figure 16. When one disk is fixed on A , the task of moving disks from A to B is symmetric with moving them from A to C , but quite distinct from the task of movingdisks from B to C .Now, consider the part of the graph G ( n −
1) where the smallest and the largest disks(2 and n ) are fixed on A . This part contains the initial state A . . . A . The only way to freethe smallest disk is to move the n − n are fixed on the same peg (in addition to disk 1), the situation issimilar to the entire graph, with two fewer disks. It is the case each time two extreme disksare fixed on the same peg: when 2 and n are fixed on peg C or B , or when 1 and n are fixedon peg A ; the process can then ignore the two fixed disks to move the n − G ( n ) in Figure 16for n ∈ { , , } and in Figure 17 for n > To prove the optimality of the solution described in Section 3, we prove that the algorithmis taking the shortest path in the configuration graph defined in the last section. A sideresult is that this is the unique shortest solution. (cid:73)
Theorem 12. ∀ ( x, y, z ) ∈ { , } , ∀ n ≥ , movexyz ( n, A, B, C ) moves optimally n disksfrom A to C . G’(2)(3 disks)aBB aCBaAC aABaBA aCAaAAaBCaB aCaAG’(1) aCC(2 disks) aBBB aBABaBCB aBCA aACA aABAaBAC aCBA aCCCaCAB aCACaCBCaAAAaAAC aAABG’(3)(4 disks) aCBBaBCC aBAAaBBA aCCAaCAAaCCBaBBC
Figure 16
Subgraphs G ( n ) with one disk fixed on the peg A for n ∈ { , , } . Proof.
Define the induction hypothesis IH ( n ) as “ ∀ ( x, y, z ) ∈ { , } movexyz ( n, A, B, C )moves optimally n disks from A to C ”. Trivially IH (0) and IH (1) are true. Suppose thatthere exists an integer N > ∀ n < N , the induction hypothesis IH ( n ) is true. Weprove that IH ( N ) is then also true. move000 ( N, A, B, C ) is optimal: move000 ( N, A, B, C ) for
N > move100 ( N, A, C, B ), one unitarystep, and one call to move001 ( N, B, A, C ).So it moves optimally (by IH ( N − aA . . . A to aB . . . B , and then to cB . . . B ,and after that to cC . . . C . (In Figure 13 the right edge of the triangle.)A path not going through states aB . . . B or cB . . . B would take more steps:if we don’t go through the state aB . . . B , then the state aC . . . C is necessary, with acost of f ( N − bC . . . C (with a cost of 1), and at the end of thepath we have to go through the state cA . . . A , which optimal path to go to the final cC . . . C state is of length f ( N − f ( N − f ( N − move000 ( N, A, B, C ).if we go through aB . . . B , but not through cB . . . B , then the path is not optimal asit must go through aC . . . C and the optimal path from aA . . . A to aC . . . C doesn’tgo through aB . . . B .So move000 ( N, A, B, C ) is optimal. move100 ( N, A, B, C ) is optimal: move100 ( N, A, B, C ) for
N > move100 ( N − , A, C, B )), twosteps, and one call to move010 ( N − , B, A, C )).As before, we shall consider these recursive calls of order smaller than N as optimal be-cause of IH ( N − aAA . . . AA to aAB . . . BA , . Barbay 15 aBC...CA aCB...BAaCC...CAaBB...BA aCB...BCaCA...ACaAB...BAaBC...CB aAC...CAaBB...BCaBA...AC aCC...CBaCA...ABaCA...AAaBA...AA aAA...AA aCC...CCaBA...ABaBB...BB G’(n−1)=
G’(n−3)
G’(n−3)G’(n−3)G’(n−3)G’(n−3)
Figure 17
Recursive definition of G ( n ), the graph of all legal steps when one disk is fixed onthe first peg, for n >
3. There is no way to connect the states aBB....BC , aBA...AC , aCC...CB and aCA...AB without moving some of the disks from { , , n } . to aCB . . . BA , then to aCB . . . BC and to aCC . . . CC (in figure 17), this correspondsto the left edge of the triangle).We must now prove that other paths take more steps:We cannot avoid the state aCB . . . BC , neither aCB . . . BA , as there is no other wayout of aCC . . . CC .if we avoid the state aAB . . . BA then the optimal path to aCB . . . BA necessarilypasses by aBA . . . AA and aCA . . . AA , and is of length f ( N −
2) + 1 + f ( N −
2) + 1 + f ( N − f ( N ) = f ( N −
2) + 2 + f ( N − move100 ( N, A, B, C ) is optimal. move010 ( N, A, B, C ) is optimal: move010 (1 , A, B, C ) and move010 (2 , A, B, C ) are special cases, we can see in graphs G (1)and G (2) on figure 16 page 14 that the optimal paths between aB . . . B and aC . . . C areof length 1 and 3, as the solutions produced by the algorithm. So move010 (1 , A, B, C )and move010 (2 , A, B, C ) are proven optimal. move010 ( N, A, B, C ) for
N > b ): (please report to fig. 17 from aCC . . . CC to aBB . . . BB downleft to down right. ) aCC . . . CC f ( N − −→ aCB . . . BC −→ aAB . . . BC f ( N − −→ aAC . . . CC −→ aBC . . . CB f ( N − −→ aBB . . . BB We shall demonstrate that all other paths take more steps:The states bAC . . . CA and bCA . . . AC are mandatory, for connexity, and so are bAC . . . CB and bCA . . . AB .if we go through bBC . . . CB , then it’s bBA . . . AB which is mandatory. if we contourn bBC . . . CB , then we shall go through bAB . . . BB , bCB . . . BB and bCA . . . AB : the total path would be of length 3 + 4 f ( N − f ( N −
2) (We trade one step with one recursive call). As f ( N − ≥ N − ≥ q (i.e. N ≥ > move010 ( N, A, B, C ) is optimal for
N > move010 ( N, A, B, C ) is optimal. (cid:74)
We discuss further extensions of those results in the next section.
All the usual research questions and extensions about the
Hanoï Tower Problem are stillvalid about the Bouncing Tower Problem. We discuss only a selection of them, such as thespace complexity in Section 5.1, and the extension to other proportional insertion and removal points in Section 5.2.
Allouche and Dress [1] showed that the optimal sequence of steps required to move a HanoïTower of n disks can be obtained by a simple function from the prefix of an infinite uniquesequence, which itself can be produced by a finite automaton. This proves that the spacecomplexity of the Hanoï Tower Problem is constant.The same technique does not seem to yield constant space for Bouncing Towers: whereasthe sequences of steps generated by each of the functions move100 ( n, A, B, C ), move010 ( n, A, B, C )and move001 ( n, A, B, C ) are prefixes of infinite sequences, extracting those suffixes and com-bining them in a sequence corresponding to move000 ( n, A, B, C ) would require a counter us-ing logarithmic space in the length of the sequences to be extracted, i.e. log ( √ n ) ∈ Θ( n ),which would still be linear in the number of disks. An extension of the Bouncing Tower Problem is to parametrize the insertion point, sothat the removal point is at position b αn c + 1 and the insertion point is under the diskat position b α ( n + 1) c in a tower of n disks, for α ∈ [0 , ] fixed (the problem is symmetricalfor α ∈ [ , α - LevitatingTower. This parametrization creates a continuous range of variants, of which the HanoïTower Problem and the Bouncing Tower Problem are the two extremes:for α = 0, the removal / insertion point is always at the top, which corresponds to aHanoï Tower, whilefor α = the problem corresponds to a Bouncing Tower.The complexity of moving a α - Levitating Tower cannot be smaller than the one of aBouncing Tower, as the key configuration permitting to move 2 disks in 2 steps between thesame pegs is less often obtainable in a α - Levitating Tower. References J.-P. Allouche and F. Dress. Tours de Hanoï et automates.
RAIRO, Informatique Théoriqueet applications , 24(1):1–15, 1990. M.D. Atkinson. The cyclic towers of Hanoï.
Information Processing Letters (IPL) , 13(118-119), 1981. . Barbay 17 W. R. Ball.
Mathematical Recreations and Essays . McMillan, London, 1892. J. S. Frame and B. M. Stewart. Solution of problem no 3918.
American MathematicsMonthly (AMM) , 48:216–219, 1941. Édouard Lucas. La tour d’Hanoï, véritable casse-tête annamite. In a puzzle game., Amiens,1883. Jeu rapporté du Tonkin par le professeur N.Claus (De Siam). Édouard Lucas.
Récréations Mathématiques , volume II. Gauthers-Villars, Paris, quai desAugustins, 55, 1883. D. Wood. The towers of Brahma and Hanoï revisited.
Journal of Recreational Mathematics(JRM) , 14(1):17–24, 1981.
Acknowledgements:
We would like to thank Claire Mathieu, Jean-Paul Allouche andSrinivasa Rao for corrections and encouragements, and Javiel Rojas-Ledesma and CarlosOchoa-Méndez for their comments on preliminary drafts.
Funding:
Jérémy Barbay ispartially funded by the Millennium Nucleus RC130003 “Information and Coordination inNetworks”.
AppendixA Disk Pile Problem
The
Hanoï Tower Problem is a classic example on recursivity, originally proposed by Éd-ouard Lucas [5] in 1883. A recursive algorithm is known since 1892, moving the n disksof a Hanoï Tower in 2 n − Disk Pile problem, a very simple variant where we allow some disks tobe of the same size. This obviously introduces some much easier instances, including anextreme one where the disks are all the same size and the resulting tower can be moved inlinear time (see Figure 18 for the sequence of steps moving such a tower of size 3 with asingle size of disks). cba
A B C ba c
A B C a bc
A B C abc
A B C
Figure 18
Moving a
Disk Pile of size 3. Give a recursive algorithm to move a
Disk Pile from one peg to the other, using onlyone extra peg, knowing that ∀ i ∈ { , . . . , s } , n i is the number of disks of size i . Youralgorithm must be efficient for the cases where all the disks are the same size, and whereall the disks are of distinct sizes. Solution:
We present an algorithm in Figure 19. It is very similar to the algorithmmoving a Hanoï Tower, the only difference being that it moves the n i disks of size i atthe same time, in n i consecutive moves. (cid:74) Give and prove the worst case performance of your algorithm over all instances of fixed s and vector ( n , . . . , n s ). Solution:
By solving the recursive formula directly given by the recursion of the al-gorithm, one gets that the n s largest disks are moved once, the n s − second largest disks Figure 19
Python code to move a Disk Pile def diskPileMove (n , sizes ,a ,b ,c ):if n >0 :move (n - sizes [ -1] , sizes [0: -1] ,a ,c ,b)for i in range (0 , sizes [ -1]):move (a ,c)move (n - sizes [ -1] , sizes [0: -1] ,b ,a ,c) are moved twice, the n s − third largest disks are moved four times, and so on to the n smallest disks, which are each moved 2 s − times. Summing all those moves give thenumber of moves performed by the algorithm: X i ∈{ ,...,s } n i s − i Note that for s = n and n = · · · = n s = 1, this yields P s − i =1 i = 2 n −
1, the solution tothe traditional
Hanoï Tower Problem . (cid:74) Prove that a performance of P i ∈{ ,...,s } n i s − i is optimal. Solution:
We prove a lower bound of P i ∈{ ,...,s } n i s − i , for n disks of s distinct sizes,with n i disks of size i by induction on the number of types of disks. We prove byinduction on the number of types of disk s that any pile of disks of sizes ( n , . . . , n s )requires P i ∈{ ,...,s } n i s − i disk moves to be moved to another peg. Initial Case : for s = 1 the bound is n and is obviously true, since each disk must beindividually moved from one peg to the other. Inductive Hypothesis : suppose there is some σ ≥ n , . . . , n σ ) requires P i ∈{ ,...,σ } n i σ − i disk moves to be moved to another peg. Inductive Step : consider a pile of disks of sizes ( n , . . . , n σ +1 ): clearly all the disks ofsizes smaller than σ + 1 need to be gathered on a unique peg before the largest diskscan be moved, to allow those last ones to be moved in n σ +1 disk moves, after whichall the disks of sizes smaller than σ + 1 need to be stacked above the largest ones. Bythe inductive hypothesis, moving the smaller disks will require P i ∈{ ,...,σ } n i σ − i disk moves, to be added to the n σ +1 disk moves. Hence, any pile of disk of sizes( n , . . . , n σ +1 ) requires P i ∈{ ,...,σ +1 } n i σ +1 − i disk moves to be moved to anotherpeg. Conclusion : The inductive hypothesis is verified for the initial case where s = 1, andpropagates to any value of s ≥ n , . . . , n s ) for s ≥ P i ∈{ ,...,s } n i s − i disk moves to bemoved to another peg. (cid:74) What is the worst case complexity of the
Disk Pile problem over all instances of fixedvalue s and fixed total number of disks n ? Solution:
The worst case (of both the algorithms and the most precise lower bound withthe number of disks of each size fixed) occurs when n = n − s + 1 and n = . . . = n s = 1.Using the previous results it yields a complexity of 2 s − ( n − s + 1) + P s − i =1 i = 2 s − ( n − s + 2) − s and fixed total numberof disks n . This correctly yields 2 n − s = n , in the worst case over all instancesof fixed total number of disks n ..