INFORMATION
SCIENCES
42,137-141(1987)
137
A General Algorithm for Finding a Shortest Path between two n-anfigurations M. C. ER Department of Computer Science, The Universiry of Western Australia, Nedlands, WA 6009, Alcstralia
ABSTRACT Let an n-configuration be an arrangement of n discs on 3 pegs. A simple recursive algorithm for the generalized Towers of Hanoi contest with arbitrary starting and ending n-configurations is presented. This algorithm always finds a shortest path between a pair of n-configurations, and is therefore optimal.
1.
INTRODUCTION
In the standard Towers of Hanoi [l-4], it is required to transfer a tower of n discs from one of the three pegs to another. Initially, all n discs are stacked on a peg in decreasing order of sizes from the bottom to the top. And in termination, all n discs must be stacked in the same order but on a different peg. In each step, the following restrictions must be observed: (a) only one disc may be moved at a time; (b) only the topmost disc on each peg may be moved; (c) a disc may not stack on a smaller disc. A surprisingly simple and elegant recursive solution to this problem is well known [2,4] (see Figure 1). In a recent paper [5], Wood advocated a Towers of Hanoi contest. The contest is concerned with transforming an n-configuration to another n-configuration, where an n-configuration is defined to be an arrangement of n discs on 3 pegs such that restriction (c) is not violated. Such starting and ending n-configurations are, of course, more general than that of the standard Towers of Hanoi. Wood [5] gave a linear-time and -space algorithm for adjudicating the winner in a Towers of Hanoi contest, but did not derive an algorithm for actually solving this generalized version. This problem appears to be rather complex, and no algorithmic solution exists yet. Wlsevier Science Publishing Co., Inc. 1987 52 Vanderbilt Ave., New York, NY 10017
0020-0255/87/$03.50
M. C. ER
138 r
Hanoi(n:disc;pl,p2,p3:peg); if n=l then MoveDisc(n,pl,p2) else begin Hanoi(n -l,pl,p3,p2); MoveDisc(n,pl,p2); Hanoi(n - l,p3,p2,pl);
end; end {Hanoi}; Fig. 1. A recursive algorithm for the standard Towers of Hanoi problem.
In what follows, we shall derive a surprisingly simple and elegant recursive algorithm for solving this generalized problem, which is comparable with the recursive solution as shown in Figure 1 for the standard problem. 2.
BASIC STRATEGIES
One of the fundamental laws in the Towers of Hanoi is that before disc i can move all discs smaller than disc i must move aside to a spare peg which is not the target peg of disc i. The same law is also obeyed in the generalized version. However, the complexity in solving this generalized version arises because all n discs start and end on arbitrary pegs as n-configurations, rather than forming single towers as in the standard problem. It appears that there is no regular pattern governing a sequence of disc moves-they are all influenced by the starting and the ending n-configurations. We are confronted with the problem of dealing with n different goals simultaneously, one for each disc; often they are conflicting. If we recognize a tower of n discs as a special case of n-configuration-call it a simple n-configuration-then a simple solution to this generalized problem may be formulated. Thus to transform an n-configuration to another one, first of all, we transform the smallest n - 1 discs, forming a (n - 1)-configuration, to a simple (n - 1)-configuration; then disc n is moved from its current peg to the goal peg; finally the simple (n - 1)-configuration is transformed to the required (n - l)-configuration. Obviously, if disc n already lies on the goal peg to start with, disc n need not be moved at all, and the transformation of the starting n-configuration to the ending n-configuration can be reduced to the transformation of the starting (n - 1)-configuration, excluding disc n, to the ending (n - l)-configuration. Since the simple case with n = 1 can be solved trivially, by induction the solution is readily seen.
SHORTEST PATH BETWEEN TWO n-CONFIGURATIONS 3.
139
GENERAL ALGORITHM
the strategy for solving the generalized version is simple to state, but may not appear to be easy to implement at first sight. The problem is that in transforming the starting (n - l)-configuration to a simple (n - l)-configuration, it is necessary to store away the goal (n - 1)-configuration so that it can be restored at a later stage after disc n is moved. The bookkeeping operation may be difficult to implement efficiently. Surprisingly, there is a simple method that allows us to overcome this costly operation, as we shall describe below. Assuming the following type definitions in PASCAL: = 1..3; disc = l..maxdisc; discposn = array [disc] of peg;
type peg
We may declare the following global variable: var p: array [peg] of discposn; This global variable is initialized as follows: for i:=l to 3 do for j :=l to n do p[i][j] := i;
Then a simple recursive algorithm for solving the generalized version may be formulated as shown in Figure 2. Note that the procedure Transform should be activated as Transform( n, startconf,goalconf), where startconf and goalconf, of type discposn, contain the
r
Transform(d: disc; varpf,pt: discposn); n if d-l
then
MoveDW,p9UptIll) else if pf[d] = ptjd] then
Transform(d - l,pf,pt) elaehegln Transform(d - 1, pf, p[6 - pfId1 -
pWl1);
MoveDWWb-W[dD; Transform(d - l,p[6 - pqd] - pt[d]],pt); end; end {Transform} ; Fig. 2. A recursion algorithm for transforming a starting n-configuration to an ending n-configuration.
140
M. C. ER
starting and the ending ~-~~ig~atio~, respectively. Note also that the procedure call MoveDisc(i, p, 4) simply prints a message stating that disc i moves from peg p to peg 4. 4.
ANALYSIS OF ALGORITHM
From the previous section, we see that the global storage used by the algorithm Transform is 3n locations. From Figure 2, we note that n is decremented by 1 in each self-recursive call. Thus, the depth of recursive calls is n. Summing the above storage usage, the space complexity of the algorithm Transform is 63(n). With regard to the time complexity of the algorithm, it is dependent on the given starting and ending n-configurations. The best-case time complexity of the algorithm is 9(O) when the starting and the ending n-configurations are the same. ‘Ike worst-case time complexity of the algorithm is @(2”) when the last else path is always selected, which is equivalent to the time complexity of the standard problem. So the average time complexity of the algorithm Transform is bounded in between by e(O) and e(2”). Furthermore, because of the restrictions of the game, an (n - 1)-configuration must be transformed to a simple (n - l)-co~ig~ation on a spare peg before disc n can move; thereafter the simple (n - 1)-~n~g~ation can be transformed to the required (n - l)-~nfig~ation. By induc~on, the algorithm always finds a shortest path between a pair of starting and ending n-configurations. Thus the algorithm Transform is optimal. 5.
REMARKS
In this paper, we have succeeded in deriving an optimal algorithm for the generalized Towers of Hanoi contest with arbitrary starting and ending n-configurations. The recursive algorithm is surprisingly simple and is comparable with the recursive algorithm for the standard Towers of Hanoi problem. A question remaining to be answered is: “Is there a simple iterative algorithm for the generalized version in the sense of [ 1I?” We leave this question to the interested reader. REFERENCES 1. P. Buneman and L. Levy, The Towers of Hanoi problem, Inform. Prucess. L&t. 10:243-244 (1980). 2. E. W. Dijkstra, A Short Introduction to the Art of Programming, EWD 316, 1971.
SHORTEST PATH BETWEEN TWO n-CONFIGURATIONS
141
3. M. C. Er, A representation approach to the Tower of Hanoi problem, Comput. J. 25:442-447 (1982). 4. P. J. Hayes, A note on the Towers of Hanoi problem, Comput. J. 20:282-285 (1977). 5. D. Wood, Adjudicating a Towers of Hanoi contest, Internat. J. Comput. Math. 14:199-207 (1983). Received I2 January 1986; revised 12 January 1987