Backtracking
Backtracking is a general algorithmic technique for solving combinatorial problems by building a solution incrementally and abandoning a partial candidate once it cannot be extended to a valid solution. This depth-first, generate-and-test approach finds every feasible solution while avoiding exhaustive enumeration.
What Is Backtracking?
Backtracking is a general algorithmic technique for solving combinatorial problems by building a solution incrementally, one component at a time, and abandoning a partial candidate the moment it becomes clear that the candidate cannot be extended to a complete, valid solution. At each decision point, the algorithm selects a value for the next variable, checks whether the partial assignment violates any constraint, and either continues forward if no constraint is violated or backtracks to the previous decision point to try an alternative value. This depth-first, generate-and-test structure ensures that every feasible solution is found while avoiding exhaustive enumeration of the exponentially large search space.
Backtracking applies most naturally to constraint satisfaction problems (CSPs), where the goal is to assign values to variables such that all constraints among them are satisfied simultaneously. Classic examples include the eight-queens problem, Sudoku solving, graph coloring, Boolean satisfiability, and scheduling under resource constraints. The technique is complete: if a solution exists, backtracking will find it, and if the constraint system is unsatisfiable, backtracking will confirm this. Its worst-case time complexity remains exponential in the number of variables, but the actual cost depends heavily on the problem structure and the pruning strategy applied.
Search Tree Structure
Backtracking explores a search tree in which each node represents a partial assignment of values to variables. The root node corresponds to an empty assignment, and each edge represents the assignment of a value to one additional variable. A node is a dead end if the current partial assignment violates a constraint, in which case the algorithm prunes the subtree rooted at that node. A leaf node is either a complete solution (all variables assigned without constraint violation) or a confirmed dead end. The order in which variables are chosen for assignment, called the variable ordering heuristic, strongly influences how quickly the search reaches a solution. The fail-first principle, which assigns the most constrained variable next, is a widely used heuristic that concentrates pruning effort near the top of the tree where it has the largest impact. A detailed survey of these heuristics appears in the backtracking algorithms for constraint satisfaction tutorial.
Constraint Propagation and Pruning
Raw backtracking performs no look-ahead and only detects constraint violations after committing to a value. Forward checking improves on this by examining the remaining domains of unassigned variables immediately after each assignment, removing any values that would violate a constraint with the newly assigned variable. Arc consistency algorithms, particularly AC-3, extend this idea globally: whenever a domain value is removed, the algorithm propagates the change through all constraint arcs connected to that variable, potentially triggering further domain reductions. Combining full arc consistency with backtracking, as in the MAC algorithm (Maintaining Arc Consistency), can reduce search effort by orders of magnitude on structured problems. The constraint satisfaction problem resources from UC Berkeley's introductory AI course illustrate how constraint propagation interacts with backtracking to reduce search depth.
Algorithm Variants
Several problem-specific variants extend basic backtracking. Chronological backtracking always returns to the most recent decision point, but conflict-directed backjumping identifies which earlier decision actually caused the current failure and returns directly to it, skipping intervening decisions that are irrelevant to the conflict. This technique, combined with the recording of no-good sets, forms the basis of modern SAT solvers such as CDCL (Conflict-Driven Clause Learning), which underlie automated reasoning tools used in electronic design automation and formal verification. For optimization problems where the goal is not just any feasible solution but the best one, branch-and-bound extends backtracking with an upper-bound computation at each node, pruning subtrees whose best possible objective value is worse than the current incumbent. These advanced variants are surveyed in the backtracking search overview on ScienceDirect.
Applications
Backtracking algorithms are used across many areas of computer science and engineering, including:
- SAT solvers for formal verification of hardware and software designs
- Compiler register allocation and instruction scheduling under hardware resource constraints
- Automated test pattern generation for digital circuit fault detection
- Scheduling and timetabling in manufacturing and academic institutions
- Puzzle solving and game tree search in artificial intelligence research