<<

A PROCEDURE MECHANISM FOR BACKTRACK PROGRAMMING*

David R. HANSON + Department o£ , The University of Arizona Tucson, Arizona 85721

One of the difficulties in using nondeterministic algorithms for the solution of combinatorial problems is that most programming languages do not include features capable of easily repre- senting backtracking processes. This paper describes a procedure mechanism that uses co- routines as a means for the description and realization of nondeterministic algorithms. A solution to the eight queens problem is given to illustrate the application of the procedure mechanism to backtracking problems.

I. INTRODUCTION

Although backtrack programming has been known for To facilitate comparison with previous work, the several years [1-4], the method has yet to become eight queens problem [13-15] is used as the exam- a common programming technique for the realization ple o£ backtracking throughout this paper. This of nondeterministic algorithms. Floyd [1] alluded is a nontrivial problem whose solution is ideally to the reason for this situation: most program- suited to the backtracking strategy, and has ming languages do not include features that facil- frequently been used as an example that can be itate backtrack programming. He suggested that solved by nondeterministic programming. programming languages ought to possess mechanisms capable of representing nondeterministic - 2. THE SL5 rithms. SL5 is an expression-oriented language that is Since the appearance of Floyd's paper, consider- structurally similar to BLISS or Algol 68. SL5 able research has been undertaken to add facili- is a "typeless" language in the same sense that ties of this kind to new or existing languages. SNOBOL4 is -- a variable can have a value of any This work has covered a large part of the spec- datatype at any time during program execution. trum of programming languages, from general de- scriptions with a slant toward Algol-like lan- 2.1 Control Structures and Signaling guages [5,6], to languages for artificial intel- ligence research [7], and even to Fortran [8]. An expression returns a signal, "success" or In all the work cited, features that were added "failure", as well as a value. The combination or proposed for backtracking were cast in a o£ a value and a signal is called the result of framework of recursive functions with additional the expression. SL5 possesses most o£ the "mod- built-in mechanisms or primitives with which to ern" control structures, each of which is an implement backtracking. That is, the basic pro- expression and returns a result. Control cedure mechanism of the proposed languages or structures are driven by signals rather than by language extensions was the traditional recursive boolean values. For an example, in the expression function. i_~f e 1 then e 2 else e 3 This paper presents a general procedure mechanism that includes as a means for the de- e I is evaluated first. If the resulting signal scription and realization of nondeterministic is success, e 2 is evaluated. Otherwise, e 5 is algorithms. The SL5 programming language [9-12] evaluated. The result of the if-then-else ex- in which this procedure mechanism is implemented pression is the result (value and signal) o£ e 2 is the vehicle used to describe this method and or e 3, whichever is evaluated. its application to backtrack programming. Other typical control structures are:

*This work was supported by the National Science while e I do e 2 Foundation under Grant DCR75-01307. until e I do e 2 repeat e +Author's present address: Department of Computer for v from e I to e 2 do e 5 Science, Yale University, New Haven, Connecticut I 06520.

401 The while and for expressions behave in the con- whichreturn V as the value of the procedure and ventlo~-6n~manner. The until expression repeated- signal either success or failure as indicated. ly evaluates e 2 until e I succeeds. The repeat If the procedure is activated by a resume, the expression evaluates e repeatedly until a failure result given in succeed or fail is transmitted signal is returned. Expressions may be grouped and becomes the result of teh-6"~esume expression. together as a single expression using The execution of succeed or fail causes the sus- begin ... end or { ... }. pension of that environment. If the environment is again resumed, execution proceeds from where 2.2 Procedures it left off. The argument v may be omitted, in which case the null string is assumed. In SL5, procedures and their environments (activation records) are separate source-language A label generator illustrates a simple example of data objects. A procedure is "created" by an usage: expression such as genlab :={procedure (n) gcd := procedure (x, y) repeat while x ~= y do succeed "U' l] lp~Cn, 3, "0"); if x > y then x := x-y else y := y-x; n := n+1 succeed x } end; end; which assigns to gcd a procedure that computes An environment for genlab generates the next the greatest common divisor of its arguments. label of the form Lnnn each time it is resumed. The sequence begins at the integer given by the The invocation of a procedure in the standard argument. (lpad is a built-in procedure that recursive fashion is accomplished using the usual pads n on the left with zeros to form a functional notation f(el,e 2 ..... en) , which S-character string, and [] denotes string concat- invokes the procedure that is the current value enation.) For example, an expression such as of the variable f. gen := create genZab with 10 Procedure activation may be decomposed into sev- eral distinct source-language operations that assigns to gen an environment for genlab that permit SL5 procedures to be used as coroutines. generates a sequence of labels beginning at L010. These operations are the creation of an environ- To obtain the next label, the execution of the ment for the execution of the given procedure, environment is resumed; the bindin~ of the actual arguments to that envi- ronment, and the resumption of the execution of x := resume gen the procedure. Notice that the sequence may be restarted by The create expression takes a single argument of retransmitting the argument, e.g., datatype procedure, creates an environment for its execution, and returns this environment as gen := gen with 10 its value. For example, the expression 2.3 Declarations e := create f SL5 has declarations for identifiers that are assigns to e an environment for the execution of used to determine only the interpretation and f. scope of identifiers that appear in a procedure, not their type. The declaration The with. expression is used to bind the actual arguments to an environment. The expression private x

e with (el,e 2, .... e n) declares x to be a private identifier whose value is available only to the procedure in which it is binds the actual arguments, e I through en, to the declared; it cannot be examined or modified by environment e. any other procedure. Private identifiers are used, for example, when a coroutine must "remem- The execution of a procedure is accomplished by ber" information from one resumption to the next. "resuming" it via the resume expression. The Other declarations and the scope of identifiers expression are described in refs. 9 and 12.

resume e 3. BACKTRACKINGAND THE EIGHT QUEENS PROBLEM

suspends execution of the current procedure and There are many problems for which an analytic activates the procedure for which e is an envi- solution is not known, but for which a solution ronment. can be constructed by trial and error. A classic example is the eight queens problem, sometimes A procedure usually "returns" a result to its referred to as the n-by-n nonattacking queens "resumer". This is accomplished by the expressions problem. The object is to place eight queens on a chess board so that no queen can capture any of succeed v the others. One such solution is shown in fig. I.

402 There are 92 solutions to this problem, although For example, it is easy to place the first five only 12 are unique. queens to form the partial solution (1,3,5,2,4). But the sixth queen cannot be placed. It is necessary to backtrack to the partial solution (1,3,5,2) and try again. This partial solution 8 can be extended to (1,3,5,2,8) but no further. It is necessary to backtrack all the way to the 7 partial solution (1,3,5), which can then be ex- 6 tended to (1,3,5,7,2,4,6). This backtracking process continues until the solution 5 (1,5,8,6,3,7,2,4) is found, which is shown in tOW fig. 1. 4 A more formal description of the backtracking 5 strategy is given in ref. 2. A particularly lucid explanation can be found in ref. 16, which 2 describes a method for estimating the efficiency of backtracking programs.

5.2 Realization of the Nondeterministic I 2 5 4 5 6 7 8 Algorithm column The usual method for programming the solution to the eight queens problem is to use a procedure Fig. 1 - A Solution to the Eight Queens Problem that generates all solutions with the first queen on rows 1 to 8 by calling itself recursively to generate all solutions for the second queen in A brute force approach to this problem is to test rows 1 to 8, etc. The foliowing procedure, simi- all the possible configurations of the queens to lar to the Pascal solution given in ref. 15, find the 92 "safe" ones. Although the number of operates in this fashion. possible configurations can be substantially reduced by observing that only one queen may generate := procedure (col) private row; occupy a given column, the brute force approach for row from 1 to 8 do requires an impractical amount of computation. i_~f teet(row, col) t-hen { occupy (row, col) ; 3.1 Backtracking x[col] := row; if col = 8 then print(x) A better approach for solving this type of prob- else generate (col+l) ; lem is to construct a solution one queen at a release (row, col) time rather than testing the validity of every }; possible configuration. This is called the succeed "backtracking" approach. For example, if the end; first queen (the leftmost one in fig. I) is placed on row 1, the second queen can only be The details of the board representation are con- placed on rows 5 through 8. Configurations with tained in procedures test, occupy, and release. the first queen on row 1 and the second queen on test(row, col) succeeds if the queen in column row 1 or 2 cannot lead to a solution regardless col can be placed on the indicated row. The of the positions of queens 5 through 8. Thus procedure occupy(row, col) marks as occupied all only the partial solutions (1,3), (1,4) .... , positions covered by the queen at the position {1,8) need to be considered when searching for a row, col. relea8e(row, col) reverses the effect solution. of occupy; it marks those positions covered by the given queen as free. Possible representa- The idea in backtracking is to form the k th par- tions for the actual board are given in refs. 1 and tial solution (Xl,X2,...,Xk) and extend it to a 15-15. print(x) prints the contents of the solu- k+Ist partial solution (Xl,X2,...,Xk,Xk+l) by tion vector x. selecting a suitable Xk+ 1. When k+l is equal to 8, a complete solution has been found. The term The program is started by generate(1). A portion backtrackin~ is derived from the action taken of the backtracking in this solution is somewhat when the k th partial solution cannot be extended obscured by the recursion; it is accomplished to a k+Ist partial solution. In this case, it is implicitly by repeated recursive invocations of necessary to "backtrack" to the k-I partial solu- generate. It is not necessary to use recursion tion and try to compute a different x k for a k th to accomplish the backtracking but it is some- partial solution. This backtracking step re- times used because the only form of procedure quires that whatever computation was required to available is the recursive function. form the k th partial solution be undone in order to get back to the k-I partial solution. This is The coroutine method, on the other hand, does not often called "reversing effects" or "backwards require the use of recursion to accomplish the execution". For the eight queens problem, this backtracking. The basic approach is to create amounts to freeing the squares on the board eight environments for a single procedure; one covered by the k th queen. for each column. Each environment represents one queen. The procedure, called queen, attempts ¢o place a queen on the given column beginning with

403 row 1. I£ a queen is successfully placed, the after recording it, in order to search for all procedure suspends its execution and signals possible solutions using the backtracking strategy. success to its resumer. If it is subsequently Theprocess is stopped when the first queen resumed, it reverses its previous effects, i.e. signals failure. This loop can be written as removes the queen from the row, and tries the follows. next row. If the queen cannot be placed, the procedure fails indicating that backtracking must i := I; occur. Subsequent resumption after failure indi- until i = 0 do cates that the process should begin again at i~ resume q-~i] row1. ---th-~---~(_£i = 8 then p~ntCx) else i := i+l) else i := i-1; The eight environments for procedure queen are stored in a vector q. The first step is to Notice that i is not incremented after successful create the eight environments for procedure placement of the eighth queen, thus forcing its queen, each with the proper column number: repositioning at the next resumption. This pro- gram can be generalized for n queens by substi- q := vector(I, 8); tuting n wherever 8 appears. for i from 1 to 8 do q~i] := create queen with i; The general form is the same for many similar backtracking problems. For example, if the pro- To begin the search for a solution, the execution cedures test, occupy, and release are modified to of the first queen, q[1], is resumed. The second assume rooks instead of queens, the program com- queen is then resumed, and so on. If the resump- putes all possible permutations of the integers 1 tion of a queen fails, backtracking is indicated. to n. If the i th queen fails, queen "i-1 must be resumed 4. COMPARISON OF THE METHODS in order to be repositioned. This is equivalent to queen i-1 attempting to find a new i-1 partial The major difference between the recursive ap- solution. I£ the ith queen succeeds, ~een i+1 proach and the coroutine approach is in the con- is resumed in hopes of extending the i partial trol regime used to achieve backtracking. This solution. A complete solution has been found is illustrated in fig. 2. The left part of fig. 2 when the eighth queen is successfully placed. shows the control relationship among the eight This entire process can be written as instantiations o£ generate when a recursive solu- tion has been computed. The relationship is i := ~; strictly hierarchical: generate is written to use until i > & do recursion in order to "resume" the next queen. ~resume q-~i] The procedure generate must include not only the ---then i :ffi i+1 semantics of placing a queen, but is must also e-~i := i-I; contain the backtracking mechanism. p~nt(-'(~;

The index i is incremented as long as the ith The right part of fig. 2 shows the control rela- queen is success~ully nlaced, i.e., as long as tionship among the eight environments for the the extension to the i~h partial solution is coroutine solution. In this case, the procedure possible. It is decremented when the~ th queen only needs to know how to place a queen, not signals failure indicating that the ivn partial about the order in which each environment is solution could not be formed. resumed. The main program controls the resump- tion of the coroutines. The procedure queen is as follows. main f queen := procedure (col) private row; progr~n ~,, repeat ( for from to generate ( I )( rob) I 8 do • program i_fftest~, ~l) t-~en { in occupy(row, col); 2( x[col] := row; succeed; ~(row, col) }; '( fail ) end; 6( quee~l queen The expression repeat { ... } is a nonterminating 7( I 2 3 4 5 6 7 8 loop. generate (8)( All 92 solutions can be found by modifying the until loop given above so that after a solution has been found the execution of the eighth queen Fig. 2 - Control Regimes among the Eight Queens is again resumed. If the subsequent placement is successful, a second solution is generated. If it fails, the seventh queen must be repositioned. This is equivalent to making a solution fail,

404 5. CONCLUSIONS REFERENCES

The procedure facility of a high-level language [I] Robert W. Floyd, Nondeterministic algorithms, is one of the most powerful tools for abstraction J. ACM, vol. 14, October 1967, 636-644. available to the programmer. The SL5 mechanism is [2] Solomon W. Golomb and Leonard D. Baumert, designed to provide, at the linguistic level, Backtrack programming, J. ACM, vol. 12, facilities that permit the programmer to implement October 1965, 516-524. solutions to backtracking problems in a way that [3] Derrick H. Lehmer, Combinatorial problems closely parallels the abstract formulation of the with digital computers, Prec. of the Fourth problem. Canadian Math. Congress, 1957, 160-173. [4] Robert J. Walker, An enumerative technique The coroutine approach to backtracking is not lim- for a class of combinatorial problems, ited to SLS. The same idea can be used in other Prec. of the Symposium o__n_nApplied Mathemat- languages that support coroutines, such as ics, vol. 10, October 1960, 91-94. 67 LITJ. Alternatively, SL5 can be used as a [5] Charles J. Prenner, Jay M. Spitzen and Ben specification language in which to formulate the Wegbreit, An implementation of backtracking solutions to backtracking problems. The resulting for programming languages, Proc. of the ACM program can then be used as a guide to an actual Annual Conference, August 1972, 763-771. implementation in a lower-level language. This is [6] John A. Self, Embedding non-determinism, done in the Appendix for the eight queens problem; -- Practice and Experience, vol. the SL5 program given in sec. 5.2 is used as a 5, September 1975, 221-227. guide for constructing a solution in Fortran. [7] Daniel G. Bobrow and Bertram Raphael, New programming languages for artificial intel- There are other problems, such as parsing and ligence, Computing Surveys, vol. 6, Septem- string pattern matching, that can be solved using ber 1974, 155-174. backtracking techniques. Unlike the eight queens [8] Jacques Cohen and Eileen Carton, Non- problem, however, the domain of the search is not deterministic fortran, Computer ~., vol. 17, known beforehand, but is determined as the search February 1974, 44-51. proceeds. Non'etheless, the coroutine approach [9] Dianne E. Britton, et al., Procedure refer- appears to be applicable to these types of prob- encing environments in SLS, Third ACM lems. For example, SL5 contains a pattern- Symposium on Principles of Programming matching facility that is based on a coroutine Languages, January 1976, 185-191. model of pattern matching in SNOBOL4 [18]. The [10] Ralph E. Griswold and David R. Hanson, An SL5 facility is significantly more general and overview of the SL5 programming language, flexible tSan the facility in SNOBOL4, and has SL5 project document SSLDIa, Dept. of proven to be easier to implement and to understand Computer Science, The University of Arizona, than the resursive approach used in SNOBOL4 Tucson, February 1976. [19,20]. [II] David R. Hanson, The syntax and semantics of SL5, SL5 project document SSLD2a, Dept. of ACKNOWLEDGEMENT Computer Science, The University of Arizona, Tucson, April 1976. Significant contributions to SLS have been made [12] David R. Hanson and Ralph E. Griswold, The by Dianne E. Britton, Frederick C. Druseikis, and SL5 procedure mechanism, SL5 project document Ralph E. Griswold. SSLD4, Dept. of Computer Science, The Univer- sity of Arizona, Tucson, February 1976. APPENDIX [13] Ole-Jahn Dahl, Edsger W. Dijkstra and C. A. R. Hoare, , Academic The following Fortran program computes all 92 Press, London, 1972, sec. 1.17. solutions to the eight queens problem, and is [14] Niklaus Wirth, Program development by step- derived from the SL5 program given in sec. 3.2. wise refinement, Comm. ACM, vol. 14, April The board representation, embodied in test, occu- 1971, 221-227. py, and release, can be derived from that given [15] Niklaus Wirth, Algorithms + Data = Prosrams, in refs. 13-15. Prentice-Hall, Englewood Cliffs, New Jersey, c main program logical function queen(col) 1976, sec. 3.5. logical queen integer ro~iJ, col, j. p(8) [16] Donald E. Knuth, Estimating the efficiency of integer row, i logical test common /env/ row(8) common /env/ row(8) backtrack programs, Mathematics of Computa- C data p/B*I/ tion, vol. 29, January 1975, 121-139. I=I c 30 if (i .le. O) stop j = p(col) [17] 01e-Jahn Dahl, Bjorn Myhrhaug and Kristen if (queen(i)) go to 40 go to (10, 20, 50),j Nygaard, The Simula 67 common base language, I=i-1 c go to 30 10 if (row(col) .gt. 8) go to 40 Norwegian Computing Centre, Oslo, Norway, c if (.not. test(row(col), col)) 1968. 40 if (i .eq. 8) go to 50 1 go to 30 I=i+1 call occupy(row(col), col) [18] Frederick C. Druseikis and John N. Doyle, A go to 30 P(COI) = 2 50 write(6, I00) row queen = .true procedural approach to pattern matching in 100 format(B(lx, il)) return SNOBOL4, Pr0c. of the ACM Annual Conference, go to 30 ¢ end 20 call relea~(row(co]), col) November 1974, 311-317. 30 row(col) = row(col) + I [19] Ralph E. Griswold, String scanning in SL5, go to 10 c SL5 project document SSLDSa, Dept. of 40 p(col) = 3 Computer Science, The University of queen = .false. return Arizona, Tucson, June 1976. ¢ [20] Ralph E. Griswold, String analysis and 50 row(col) = l go to 10 synthesis in SL5, Proc. of the ACM Annual end Conference, October 1976.

405