All Topics  
Backtracking

 

   Email Print
   Bookmark   Link






 

Backtracking



 
 
Backtracking is a general algorithm
Algorithm

In mathematics, computing, linguistics and related subjects, an algorithm is a sequence of finite instructions, often used for calculation and data processing....
 for finding all (or some) solutions to some computational problem, that incrementally builds candidates to the solutions, and abandons each partial candidate c ("backtracks") as soon as it determines that c cannot possibly be completed to a valid solution .

The classic textbook example of the use of backtracking is the eight queens puzzle
Eight queens puzzle

The eight queens puzzle is the problem of putting eight chess Queen s on an 8?8 chessboard such that none of them is able to capture any other using the standard chess queen's moves....
, that asks for all arrangements of eight queens on a standard chessboard
Chess

Chess is a recreational and competitive game played between two Player . Sometimes called Western chess or international chess to distinguish it from History of chess and other chess variants, the current form of the game emerged in Southern Europe during the second half of the 15th century after evolving from similar, much older...
 so that no queen attacks any other.






Discussion
Ask a question about 'Backtracking'
Start a new discussion about 'Backtracking'
Answer questions from other users
Full Discussion Forum



Encyclopedia


Backtracking is a general algorithm
Algorithm

In mathematics, computing, linguistics and related subjects, an algorithm is a sequence of finite instructions, often used for calculation and data processing....
 for finding all (or some) solutions to some computational problem, that incrementally builds candidates to the solutions, and abandons each partial candidate c ("backtracks") as soon as it determines that c cannot possibly be completed to a valid solution .

The classic textbook example of the use of backtracking is the eight queens puzzle
Eight queens puzzle

The eight queens puzzle is the problem of putting eight chess Queen s on an 8?8 chessboard such that none of them is able to capture any other using the standard chess queen's moves....
, that asks for all arrangements of eight queens on a standard chessboard
Chess

Chess is a recreational and competitive game played between two Player . Sometimes called Western chess or international chess to distinguish it from History of chess and other chess variants, the current form of the game emerged in Southern Europe during the second half of the 15th century after evolving from similar, much older...
 so that no queen attacks any other. In the common backtracking approach, the partial candidates are arrangements of k queens in the first k rows of the board, all in different rows and columns. Any partial solution that contains two mutually attacking queens can be abandoned, since it cannot possibly be completed to a valid solution.

Backtracking can be applied only for problems which admit the concept of a "partial candidate solution" and a relatively quick test of whether it can possibly be completed to a valid solution. It is useless, for example, for locating a given value in an unordered table. When it is applicable, however, backtracking is often much faster than brute force enumeration of all complete candidates, since it can eliminate a large number of candidates with a single test.

Backtracking is an important tool for solving constraint satisfaction problem
Constraint satisfaction problem

Constraint satisfaction problems or CSPs are mathematics problems defined as a set of objects whose state must satisfy a number of constraint s or limitations....
s, such as crosswords, verbal arithmetic
Verbal arithmetic

Verbal arithmetic, also known as alphametics, cryptarithmetic, crypt-arithmetic, or cryptarithm, is a type of mathematical game consisting of a mathematical equation among unknown numbers, whose numerical digits are represented by Letter s....
, and many other puzzles. It is often the most convenient (if not the most efficient) technique for parsing
Parsing

In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a sequence of lexical analysis#Token to determine their grammatical structure with respect to a given formal grammar....
, for the knapsack problem
Knapsack problem

The knapsack problem or rucksack problem is a problem in combinatorial optimization. It derives its name from the following maximization problem of the best choice of essentials that can fit into one bag to be carried on a trip....
 and other combinatorial optimization
Combinatorial optimization

Combinatorial optimization is a branch of Optimization . Its domain is optimization problems where the set of feasible solutions is Discrete set or can be reduced to a discrete one, and the goal is to find the best possible solution....
 problems. It is also the basis of the so-called logic programming
Logic programming

Logic programming is, in its broadest sense, the use of mathematical logic for computer programming. In this view of logic programming, which can be traced at least as far back as John McCarthy 's [1958] Advice taker proposal, logic is used as a purely Declarative programming language representation language, and a automated theorem proving o...
 languages such as Icon
Icon programming language

Icon is a very high-level programming language featuring goal directed execution and many facilities for managing string and textual patterns. It is related to SNOBOL, a string processing language....
, Planner
Planner programming language

Planner is a programming language designed by Carl Hewitt at MIT, and first published in 1969. First subsets such as Micro-Planner and Pico-Planner were implemented, and then essentially the whole language was implemented in Popler....
 and Prolog
Prolog

Prolog is a logic programming language. It is a general purpose language often associated with artificial intelligence and computational linguistics....
. Backtracking is also utilized in the (diff) difference engine for the MediaWiki
MediaWiki

MediaWiki is a World Wide Web wiki software application used by all projects of the Wikimedia Foundation, all wikis hosted by Wikia, and many other wikis, including some of the largest and most popular ones....
 software.

Backtracking depends on user-given "black box procedure
Procedural parameter

In computing, a procedural parameter is a parameter of a subroutine that is itself a procedure.This concept is an extremely powerful and versatile programming tool, because it allows programmers to modify certain steps of a library in arbitrarily complicated ways, without having to understand or modify the code of that procedure....
s" that define the problem to be solved, the nature of the partial candidates, and how they are extended into complete candidates. It is therefore a metaheuristic
Metaheuristic

A metaheuristic is a heuristic method for solving a very general class of computing problems by combining user-given procedural parameters ? usually heuristics themselves ? in the hope of obtaining a more efficient or more robust procedure....
 rather than a specific algorithm --- although, unlike many other meta-heuristics, it is guaranteed to find all solutions to a finite problem in a bounded amount of time.

The term "backtrack" was coined by American mathematician D. H. Lehmer
Derrick Henry Lehmer

Derrick Henry "Dick" Lehmer was an American mathematician who refined Edouard Lucas' work in the 1930s and devised the Lucas?Lehmer test for Mersenne primes....
 in the 1950s. The pioneer string-processing language SNOBOL
SNOBOL

SNOBOL is a computer programming language developed between 1962 and 1967 at AT&T Bell Laboratories by David J. Farber, Ralph E. Griswold and Ivan P....
 (1962) may have been the first to provide a built-in general backtracking facility.

Description of the method


The backtracking algorithm enumerates a set of partial candidates that, in principle, could be completed in various ways to give all the possible solutions to the given problem. The completion is done incrementally, by a sequence of candidate extension steps.

Conceptually, the partial candidates are the nodes of a tree structure
Tree structure

A tree structure is a way of representing the hierarchy nature of a structure in a graphical form.It is named a "tree structure" because the graph looks a bit like a tree, even though the tree is generally shown upside down compared with a real tree; that is to say with the root at the top and the leaves at the bottom....
, the potential search tree. Each partial candidate is the parent of the candidates that differ from it by a single extension step; the leaves of the tree are the partial candidates that cannot be extended any further.

The backtracking algorithm traverses this search tree recursively
Recursion (computer science)

Recursion is a way of thinking about and solving problems. In fact, Recursion_ is one of the central ideas of computer science. Solving a problem using recursion means the solution depends on solutions to smaller instances of the same problem....
, from the root down, in depth-first order
Depth-first search

Depth-first search is an algorithm for traversing or searching a tree data structure, tree structure, or graph . One starts at the root and explores as far as possible along each branch before backtracking....
. At each node c, the algorithm checks whether c can be completed to a valid solution. If it cannot, the whole sub-tree rooted at c is skipped (pruned). Otherwise, the algorithm (1) checks whether c itself is a valid solution, and if so reports it to the user; and (2) recursively enumerates all sub-trees of c. The two tests and the children of each node are defined by user-given procedures.

Therefore, the actual search tree that is traversed by the algorithm is only a part of the potential tree. The total cost of the algorithm is basically the number of nodes of the actual tree times the cost of obtaining and processing each node. This fact should be considered when choosing the potential search tree and implementing the pruning test.

Pseudocode

In order to apply backtracking to a specific class of problems, one must provide the data P for the particular instance of the problem that is to be solved, and six procedural parameter
Procedural parameter

In computing, a procedural parameter is a parameter of a subroutine that is itself a procedure.This concept is an extremely powerful and versatile programming tool, because it allows programmers to modify certain steps of a library in arbitrarily complicated ways, without having to understand or modify the code of that procedure....
s, root, reject, accept, first, next, and output. These procedures should take the instance data P as a parameter and should do the following:
  1. root(P): return the partial candidate at the root of the search tree.
  2. reject(P,c): return true only if the partial candidate c is not worth completing.
  3. accept(P,c): return true if c is a solution of P, and false otherwise.
  4. first(P,c,s): generate the first extension of candidate c.
  5. next(P,s): generate the next alternative extension of a candidate, after the extension s.
  6. output(P,c): use the solution c of P, as appropriate to the application.


The backtracking algorithm reduces then to the call bt(root(P)), where bt is the following recursive procedure:

procedure bt(c) if reject(P,c) then return if accept(P,c) then output(P,c) s ? first(P,c) while s ? ? do bt(s) s ? next(P,s)

Usage considerations

The reject procedure should be a boolean-valued function
Boolean-valued function

A boolean-valued function, in some usages a Predicate_ or a Proposition, is a function of the type f : X ? B, where X is an arbitrary Set and where B is a boolean domain....
 that returns true only if it is certain that no possible extension of c is a valid solution for P. If the procedure cannot reach a definite conclusion, it should return false. An incorrect true result may cause the bt procedure to miss some valid solutions. The procedure may assume that reject(P,t) returned false for every ancestor t of c in the search tree.

On the other hand, the efficiency of the backtracking algorithm depends on reject returning true for candidates that are as close to the root as possible. If reject always returns false, the algorithm will still find all solutions, but it will be equivalent to a brute-force search.

The accept procedure should return true if c is a complete and valid solution for the problem instance P, and false otherwise. It may assume that the partial candidate c and all its ancestors in the tree have passed the reject test.

Note that the general pseudo-code above does not assume that the valid solutions are always leaves of the potential search tree. In other words, it admits the possibility that a valid solution for P can be further extended to yield other valid solutions.

The first and next procedures are used by the backtracking algorithm to enumerate the children of a node c of the tree, that is, the candidates that differ from c by a single extension step. The call first(P,c) should yield the first child of c, in some order; and the call next(P,s) should return the next sibling of node s, in that order. Both functions should return a distinctive "null" candidate, denoted here by '?', if the requested child does not exist.

Together, the root, first, and next functions define the set of partial candidates and the potential search tree. They should be chosen so that every solution of P occurs somewhere in the tree, and no partial candidate occurs more than once. Moreover, they should admit an efficient and effective reject predicate.

Early stopping variants

The pseudo-code above will call output for every candidate that is a solution to the given instance P. The algorithm is easily modified to stop after finding the first solution, or a specified number of solutions; or after testing a specified number of partial candidates, or after spending a given amount of CPU
Central processing unit

A central processing unit is an electronic circuit that can execute computer programs. This broad definition can easily be applied to many early computers that existed long before the term "CPU" ever came into widespread usage....
 time.

Examples


Constraint satisfaction

The general constraint satisfaction problem
Constraint satisfaction problem

Constraint satisfaction problems or CSPs are mathematics problems defined as a set of objects whose state must satisfy a number of constraint s or limitations....
 consists in finding a list of integers x = (x[1],x[2], …, x[n]), each in some range , that satisfies some arbitrary constraint (boolean function) F.

For this class of problems, the instance data P would be the integers m and n, and the predicate F. In a typical backtracking solution to this problem, one could define a partial candidate as a list of integers c = (c[1],c[2], … c[k]), for any k between 0 and n, that are to be assigned to the first k variables x[1],x[2], …, x[k]). The root candidate would then be the empty list . The first and next procedures would then be function first(P,c) k ? length(c) if k = n then return ? else return (c[1], c[2], …, c[k], 1)

function next(P,s) k ? length(s) if s[k] = m then return ? else return (s[1], s[2], …, s[k-1], 1 + s[k]) Here "length(c)" is the number of elements in the list c.

The call reject(P,c) should return true if the constraint F cannot be satisfied by any list of n integers that begins with the k elements of c. For backtracking to be effective, there must be a way to detect this situation, at least for some candidates c, without enumerating all those mn-k n-tuples.

For example, if F is the conjunction
Conjunction

Conjunction can refer to:*Conjunction , an astronomical phenomenon*Astrological aspect, an aspect in horoscopic astrology*Grammatical conjunction, a part of speech...
 of several boolean predicates, F = F[1] F[2] F[p], and each F[i] depends only on a small subset of the variables x[1], …, x[n], then the reject procedure could simply check the terms F[i] that depend only on variables x[1], …, x[k], and return trueif any of those terms returns false. In fact, reject needs only check those terms that do depend on x[k], since the terms that depend only on x[1], …, x[k-1] will have been tested further up in the search tree.

Assuming that reject is implemented as above, then accept(P,c) needs only check whether c is complete, that is, whether it has n elements.

It is generally better to order the list of variables so that it begins with the most critical ones (i.e. the ones with fewest value options, or which have a greater impact on subsequent choices).

One could also allow the next function to choose which variable should be assigned when extending a partial candidate, based on the values of the variables already assigned by it. Further improvements can be obtained by the technique of constraint propagation.

In addition to retaining minimal recovery values used in backing up, backtracking implementations commonly keep a variable trail, to record value change history. An efficient implementation will avoid creating a variable trail entry between two successive changes when there is no choice point, as the backtracking will erase all of the changes as a single operation.

An alternative to the variable trail is to keep a time stamp of when the last change was made to the variable. The time stamp is compared to the time stamp of a choice point. If the choice point has an associated time later than that of the variable, it is unnecessary to revert the variable when the choice point is backtracked, as it was changed before the choice point occurred.

External links



See also

  • Ariadne's thread (logic)
    Ariadne's thread (logic)

    Ariadne's thread, named for the legend of Ariadne, is the term used to describe the solving of a problem with multiple apparent means of proceeding - such as a physical maze, a logic puzzle, or an ethical dilemma - through an exhaustive application of logic to all available routes....
  • Backjumping
    Backjumping

    In backtracking algorithms, backjumping is a technique that reduces search space, therefore increasing efficiency. While backtracking always goes up one level in the search tree when all values for a variable have been tested, backjumping may go up more levels....
  • Recursion (computer science)
    Recursion (computer science)

    Recursion is a way of thinking about and solving problems. In fact, Recursion_ is one of the central ideas of computer science. Solving a problem using recursion means the solution depends on solutions to smaller instances of the same problem....