Docsity

backtrack programming techniques, Manuais, Projetos, Pesquisas de Algoritmos

Pré-visualização parcial do texto

Baixe backtrack programming techniques e outras Manuais, Projetos, Pesquisas em PDF para Algoritmos, somente na Docsity!

om À Programming G. Manacher, S. Graham Techniques Editors Backtrack Programming Techniques James R. Bitner and Edward M. Reingold University of Illinois at Urbana Champaiga (REs] The purpose 9f this paper is twofold, First, a brief exposition of the general backtrack technique and its history is given, Second, it is shown how the use of macros can considerably shorten the computation time in many cases. In particular, this technique has allowcd the solution of two previously open combinatorial problems, the computation of new terms in a well-known series, and the substantial reduction in computation time for thc solution to another combinatorial problem. Key Words and Phrases: backtrack, depth-first search, exhaustive search, macros, combinatorial computing, non- attacking queen's problem, difference-preserving codes, pentominoes, tiling problems, squaring the square CR Categories: 5.30 Copyright (6 1973, Association for Computing Machinery, Inc. General permission to republish, but not for profit, all or part Of this material is granted provided that ACM's copyright notice is given and that reference is made to the publication, to its date of issue, and to the fact that reprinting privileges werc granted by permission of the Association for Computing Machinery. This work was supported im part by NSH grant GJ-41538, Authors! address; Depariment of Computer Science, University of Illinois at Urbana-Champaign, Urbana, IL 6180! 651 Introduction 2 S6 ê, | 7 0 Using a computer to answer Goestons such as “how many ways are there to...” “list all possi- ble...,” or “is there a way to...” usually requires an exhaustive search of the set of all potential solutions. One general technique for organizing such searches is backtrack, which works by continually trying to extend 4 partial solution. At cach stage of the search, if an extension of the current partial solution is not possible, e “backtrack” to à shorter partial solution and try again. This technique was first called backtrack by D.H. Lehmer in the 1950's, but has been discovered and rediscovered many times. One early description of such a method used to thread mazes is given in [16]. Currently, this method is used in a wide range of com- binatorial problems including, for example, parsing [1], game playing [21], and optimization [14]. Other applications are discussed in [9] and [12]. Walker [24] was the first to state backtrack in its full generality. Assume that the solution to a problem consists of a vector (Mm, q:,...) of undetermined length. This vector satisfies certain constraints on the components, which makes it a solution. Each a; is a member of a finite, linearly ordered sei 4:. Thus the exhaustive search must consider the elements of AX AX... X A,fori— 0,1,2,...as potential solutions. Initialiy we start with the null vector A as our partial solution, and the constraints tell us which of the members of A, are candidates for a; we call this subset $j. We choose the least element of Si as a, and now we have the partial solution (a:). In general, Lhe various constraints which describe solutions tell us which subset Sk Of 4, comprises candidates for the extension of the partial solution (a, 43,..., Qua) to (a, pocos Gra, 6) If the partial solution (a, au, ..., Gu) admits no possibilities for a, then = (5, so we backtrack and make a new choice for ger. ]f there are no new choices for axa we backtrack still further and make a new choice for ax», and so on. The algorithm can be described formally as follows: au + smalhest clement in Sy SM A, Se Se — la] e! . ca Jit(a ar, Mu) isasolu- wie 4 > do «ie Se mt Si do | gor then record it lc k+I .compute Sy lkek 1 stop all solutions have been found It is helpful to picture this process in terms of a tree Vavo (sue [U). The subsetof AX 4: X X 4, i=,1,2,... which is searched is represented as a tree as follows: The root of the tree (the Dth level) is the null vegtor. lts sons are tbe choices for a, and in general the nodes at the kth level are the choices for ar, Biven the choices made for a, d>,..., Gus AS indicated by the ancestors of these nodes. In the tree shown in Figure 1 backtrack traverses the nodes of the Communications November 1975 of Volume 18 the ACM Number 1L