The Backtracking Scheme
Some problems resist every technique seen so far. There is no greedy criterion that guarantees the optimum, the problem does not split into independent halves, and there is no small table of subproblems to fill. Very often the only way to be sure of the answer is to examine the candidate solutions one by one. Backtracking is the paradigm that organizes that examination so that it is systematic (no candidate is forgotten) and as cheap as possible (whole families of candidates are discarded at once).
The central idea fits in one sentence: build the solution step by step, and as soon as a partial solution cannot be completed into a valid one, undo the last step and try something else. The name comes from that "undo": when the algorithm reaches a dead end, it goes back along the track it has followed and takes a different turn.
Backtracking is the second-to-last design technique of the course. The last one, branch and bound, explores the same kind of search space with a different strategy.
- How combinatorial problems are modeled as implicit state-space trees and solutions as decision vectors.
- The choose, explore, unchoose pattern and why undoing decisions is essential.
- The difference between the first-solution scheme and the all-solutions / optimization scheme.
- How pruning (feasibility, look-ahead, bounds, symmetry) turns brute force into a practical search.
- Time and memory complexity of backtracking ( stack space) and when to prefer dynamic programming.
Why brute force is not enough
Brute force (also called exhaustive search) generates every possible candidate and checks each one at the end. Sometimes it is the only option, but its cost explodes very quickly. This phenomenon is called combinatorial explosion.
The magic square
A magic square of side is an table filled with the numbers , each used once, so that every row, every column and both diagonals add up to the same value (the magic constant). For the constant is 15, and this is a valid square:
| 2 | 7 | 6 |
| 9 | 5 | 1 |
| 4 | 3 | 8 |
How many ways are there to fill the table? Number 1 can go in any of the 9 cells, number 2 in any of the remaining 8, and so on until number 9, which goes in the only free cell:
- For : arrangements.
- For : arrangements.
- For : arrangements.
Even at a billion arrangements per second, checking every arrangement would take hundreds of millions of years.
The key observation is that most arrangements are doomed long before they are complete. If the first row already sums to 16, nothing placed in the other six cells can fix it. A brute-force algorithm does not notice this and keeps generating the completions of that row. Backtracking checks each row, column and diagonal as soon as it is full and abandons the branch immediately.
We measured both strategies on the square:
| Strategy | Nodes visited (partial and complete tables) | Magic squares found |
|---|---|---|
| Brute force: fill all 9 cells, then check | 986,410 | 8 |
| Backtracking: check every line as soon as it is full | 2,378 | 8 |
Both find the same 8 magic squares (they are rotations and reflections of the one above), but backtracking does about 400 times less work. The gap grows with .
Brute force in the real world
The same arithmetic explains why long passwords are safe. A password of 8 lowercase letters has possibilities, which a modern attacker can try exhaustively. With 12 characters drawn from letters, digits and symbols there are more than , which is out of reach. Attackers therefore combine brute force with knowledge (dictionaries of common words and patterns) to discard unlikely candidates, which is the same idea that turns brute force into backtracking.
Problems as graphs of states
Many problems can be modeled as an abstract graph:
- Nodes represent the states of the problem (a partially filled table, a partial assignment, a position in a game).
- Edges represent valid changes from one state to another (placing a number, making a move).
Solving the problem means finding a node, or a path, with some property in that graph: a full magic square, a winning sequence of moves, a route between two cities.
It is the nodes that represent states and the edges that represent changes, not the other way round.
Explicit and implicit graphs
If the graph were small, we could build it in memory and apply a standard graph search. In combinatorial problems this is impossible: the magic square graph has more than nodes. Backtracking therefore works on an implicit graph:
- The graph is never built. We only have a description of its nodes and edges (the rules of the problem).
- Nodes are generated only when the search reaches them, and they are discarded once they have been examined.
- If a solution is found early, the rest of the graph is never generated, which saves time.
- Only the nodes on the current path are kept, which saves space.
The tree of states
The implicit graph explored by backtracking is usually drawn as a tree, the tree of states (or search tree):
- The root is the initial state (nothing decided yet).
- Each level corresponds to one decision.
- Each child of a node is one way of making the next decision.
- Leaves are either complete solutions or dead ends.
A familiar example is tic-tac-toe ("three in a row"). The root is the empty board, its 9 children are the 9 possible first moves of X, each of those has 8 children for the replies of O, and so on. Game-playing programs for chess or Go explore trees like this one; techniques such as minimax, alpha-beta pruning and Monte Carlo tree search are all refinements of the same idea.
The backtracking method
Solutions as vectors
In backtracking, a solution is usually expressed as a vector
where each component is the -th decision, chosen from a finite set of candidates. Each level of the tree fixes one component. At any moment, the algorithm is at some level with a partial solution :
- If a value for is compatible with the decisions already made, the algorithm adds it and moves down to level .
- If the partial solution becomes a complete solution, the path has succeeded. The algorithm stops if one solution is enough (first solution) or keeps searching if it needs every solution, or the best one (all solutions).
- If no value for is compatible, the path has failed. The algorithm goes back to level , removes the last decision, and tries the next candidate there. When it returns to a node that still has unexplored children, the search continues through them.
This is a depth-first search of the tree of states, with two additions: the tree is generated on the fly, and branches are cut as soon as they are known to be useless. The pattern is often summarized as choose, explore, unchoose.
A small trace
Let us generate all binary strings of length 3 with no two consecutive 1s. The solution is a vector with . Before setting , we check that .
The algorithm goes down the leftmost branch to , records it, goes back one level, tries , goes back two levels, tries , rejects without generating it, and so on. It finds the 5 valid strings. The pruned node would have had two children; in longer strings, cutting a node near the root removes a whole subtree of size .
General schemes
The two schemes below are the skeleton of every backtracking algorithm in this course. Only the meaning of "state", "children" and "solution" changes from problem to problem.
- First solution
- All / best / worst solutions
Use this scheme when any valid solution is enough (or when we only need to know whether one exists). A found flag stops the exploration as soon as the first solution appears.
boolean found = false;
void solve(State initial) {
backtracking(initial);
if (!found)
System.out.println("THERE IS NO SOLUTION");
}
void backtracking(State state) {
if (isSolution(state)) {
process(state); // print it, copy it...
found = true;
} else {
for (State child : childrenOf(state))
if (!found && isValid(child))
backtracking(child);
}
}
Use this scheme when we need every solution, their number, or the best (or worst) one according to some objective. The search never stops early.
int count = 0;
void solve(State initial) {
backtracking(initial);
if (count == 0)
System.out.println("THERE IS NO SOLUTION");
}
void backtracking(State state) {
if (isSolution(state)) {
process(state); // print it, or compare it with the best so far
count++;
}
else { // see the note below about this 'else'
for (State child : childrenOf(state))
if (isValid(child))
backtracking(child);
}
}
The two versions differ in very little: the found flag. Two details deserve attention:
- The
elseafter a solution. It is correct only if no further solution can appear below a solution node. That is the case when solutions are always leaves (a permutation of elements cannot be extended). If a solution can be extended into another solution (for example, "all subsets whose sum is at least "), theelsemust be removed so that the search continues below it. - Saving the solution. The "unchoose" step erases decisions as the recursion returns. If the solution is needed after the search finishes, it must be copied when it is found (or the undo step must be skipped once
foundis true). Otherwise the caller receives an empty vector.
For optimization, the "all solutions" scheme keeps a variable with the best solution found so far and its value. Every time a complete solution is reached, it is compared with the best one and replaces it if it is better. This variable is also the basis of the most powerful kind of pruning, described below.
ProofBacktracking finds every solutionDesigning a backtracking algorithm
To apply the schemes to a concrete problem, answer these questions in order:
- What is a solution? Define the vector and the meaning of each component. A good representation often makes the constraints hold by construction. In the N-queens problem, storing one row per column guarantees that no two queens share a column.
- What are the candidates at each level? These are the children of a node. Their number is the branching factor (or degree) of the tree.
- When is a partial solution still valid? This is the feasibility test. It should be cheap: auxiliary arrays that answer "is this row used?" in are better than scanning the whole partial solution.
- When is a node a solution? Usually when the last level is reached, sometimes earlier.
- What must be undone when going back? Every change made when choosing a candidate (marks, sums, counters) must be reverted after the recursive call, so that the state is exactly as it was before.
- Is there an objective? If we look for the best solution, decide how to compare solutions and what bound can be used to prune.
Pruning: the key to efficiency
Without pruning, backtracking is just brute force written recursively. Pruning means not generating a child (or not exploring it) when we can prove that its subtree contains nothing useful. There are several kinds:
- Feasibility pruning. The partial solution already violates a constraint: two queens attack each other, a line of the magic square does not add up, two adjacent regions of a map have the same color.
- Look-ahead pruning. The partial solution is still valid, but it can no longer be completed. In the subset sum problem with positive numbers, if the current sum plus all the remaining numbers is still below the target, no completion can reach it.
- Bound pruning (optimization). The partial solution is valid, but its cost already equals or exceeds the cost of the best complete solution found so far, so it cannot lead to a better one. This is the bridge between backtracking and branch and bound.
- Symmetry breaking. If several branches are mirror images of each other, explore only one. For example, in the N-queens problem we can place the first queen only in the top half of the first column and mirror the solutions found.
The choice of pruning tests involves a trade-off. Each test costs time at every node where it is evaluated. A test that rarely rejects anything can make the algorithm slower. As a rule of thumb, simple tests are almost always worth it; expensive tests pay off only when the tree is very large.
The order in which children are tried also matters. Trying the most promising candidate first finds good solutions earlier, which makes bound pruning more effective. Choosing the most constrained variable first (the one with fewest candidates left) detects dead ends earlier. In the knight's tour, a simple ordering rule reduces 27 million recursive calls to 64.
ProofBound pruning never loses the optimumComplexity analysis
Time
The execution time of a backtracking algorithm is
The time per node is usually constant or close to it, so the analysis focuses on the number of nodes. In the worst case (no pruning at all) every combination of values is generated. If each component can take values, the tree has:
- nodes at level 1,
- nodes at level 2,
- ...
- nodes at level .
Two typical cases:
Constant degree. If every node has children, the number of nodes is
For (include or exclude an element, set a bit to 0 or 1): .
Degree decreasing by one. If each level can only use the elements not used above (permutations), the degree is at the root, below, and so on:
In fact the sum is less than , so the leaves dominate.
Backtracking algorithms therefore have, in general, exponential or factorial worst-case complexity. Among the usual functions, the ordering from best to worst is
since and, for large , eventually exceeds any .
Pruning does not change the worst case, but it can change the practical behavior dramatically. For the N-queens problem we counted the nodes generated when looking for all solutions, and compared them with the size of the full tree of degree (one row per column, leaves) and with (one row per column, all different):
| Solutions | Nodes generated | Full tree of degree | Time (all solutions) | ||
|---|---|---|---|---|---|
| 8 | 92 | 2,057 | 40,320 | 0.5 ms | |
| 10 | 724 | 35,539 | 3.5 ms | ||
| 12 | 14,200 | 856,189 | 71 ms | ||
| 14 | 365,596 | 27,358,553 | 2.5 s |
The algorithm generates thousands of times fewer nodes than , yet the count still grows by a factor of about 5 per extra queen. Pruning buys a few more sizes; it does not make the problem polynomial.
Memory
Backtracking keeps only the current path of the tree, not the whole tree. The memory used by the recursion stack is
where is the height of the tree of calls and the stack space of each call. With space per call, . The height is usually:
- when each call reduces the problem by subtraction (one decision per level, as in all the examples of this chapter),
- when each call reduces it by division.
To this we must add the data structures of the problem (the solution vector, the marks), which are usually or .
Backtracking and the other paradigms
| Paradigm | How it explores | Guarantees the optimum? | Typical cost |
|---|---|---|---|
| Brute force | Generates every complete candidate, checks at the end | Yes | Exponential or factorial |
| Greedy | One choice per step, never reconsidered | Only for some problems | Polynomial |
| Divide and conquer | Splits into independent subproblems | Yes (when applicable) | Often polynomial |
| Dynamic programming | Solves each distinct subproblem once and stores it | Yes (when the principle of optimality holds) | Polynomial or pseudo-polynomial |
| Backtracking | Depth-first over the tree of states, with pruning | Yes | Exponential or factorial in the worst case |
| Branch and bound | Best-first over the tree of states, with bounds | Yes | Exponential in the worst case |
If a problem can be solved both with dynamic programming and with backtracking, dynamic programming is usually preferable: it reuses subproblems instead of exploring them again. Backtracking is the right choice when:
- we need to enumerate all solutions (DP gives a value, not the list);
- the constraints are complicated and easy to check incrementally, but hard to express as a recurrence;
- the problem is NP-hard and no efficient method is known, so an exact answer requires exploring the search space.
Backtracking is a poor choice when partial solutions cannot be checked until they are complete (nothing can be pruned), or when the instance is so large that even the pruned tree is out of reach. In that case, heuristics (greedy, local search, metaheuristics) give good but not guaranteed answers.
Backtracking in practice
Backtracking is everywhere, often hidden inside tools we use daily:
- Puzzles and constraint satisfaction: Sudoku, crosswords, timetabling, scheduling. Constraint programming solvers are backtracking engines with very sophisticated pruning.
- SAT solvers, which decide whether a Boolean formula can be satisfied, are based on the DPLL algorithm, a backtracking search with clever propagation. They are used to verify hardware and software.
- Logic programming: the Prolog language executes programs by backtracking over the possible ways to prove a goal.
- Regular expressions: the engines of Java, Python, JavaScript and many other languages match patterns by backtracking. Badly written patterns can trigger catastrophic backtracking. In Python, matching
(a+)+bagainst a string of lettersafollowed byctakes about twice as long for each extra letter: 0.7 s for and 2.8 s for . This has caused real outages of web services. - Compilers use graph coloring (a classic backtracking problem) to assign variables to processor registers.
- Games: chess and Go engines explore game trees with depth-first search plus pruning.
Self-check
1. Brute force or backtracking? Why does backtracking explore far fewer nodes than brute force on the magic square, if both find the same 8 solutions?
Brute force builds all 362,880 complete tables (and every partial table along the way) before checking anything. Backtracking checks each row, column and diagonal as soon as it is full, so a wrong first row is rejected after placing 3 numbers instead of 9, and its completions are never generated.
2. Nodes and edges. True or false: "In the graph of a problem, nodes represent valid changes and edges represent states."
False. It is the other way round: nodes are states, edges are valid changes between states.
3. Which scheme? You need the cheapest way to assign 10 tasks to 10 agents. Do you use the "first solution" or the "all solutions" scheme?
The "all solutions" scheme, keeping the best solution found so far. The first complete assignment found is not necessarily the cheapest, so the search cannot stop there (although bound pruning will discard most of the tree).
4. Complexities. Which of these are typical complexities of backtracking algorithms: , , , , , ? And which is the worst of , and ?
The typical ones are , and (exponential and factorial). The polynomial ones are not. The worst of the three is , since for large .
5. Memory. What does the memory used by the recursion stack depend on, and how high is the tree when the problem shrinks by subtraction or by division?
It depends on the height of the tree of calls and on the stack space used by each call: . The height is usually when the problem shrinks by subtraction and when it shrinks by division.
6. Undo. A student's "first solution" algorithm prints a correct solution while searching, but the vector returned to the caller is full of -1. Why?
The "unchoose" step runs after the recursive call returns, even after the solution has been found, and erases every decision. The solution must be copied when it is found, or the undo must be skipped once found is true.
7. DP or backtracking? You know how to solve a problem with dynamic programming and also with backtracking. Which one should you usually use?
Dynamic programming, because it solves each distinct subproblem once (usually in polynomial or pseudo-polynomial time), while backtracking may explore an exponential number of nodes. Backtracking remains necessary when all solutions must be listed or no DP formulation exists.
- Forgetting to undo a decision when going back. Choose, explore, unchoose: every change must be reverted.
- Checking feasibility only at the leaves. Prune as soon as a partial solution cannot lead to a valid one.
- Not stopping in the first-solution scheme. Use a flag so that the search ends as soon as a solution is found.
- Keeping the best solution by reference. Copy the vector; otherwise later changes overwrite it.
Key ideas
- Backtracking performs a depth-first search of an implicit tree of states, generating nodes only when they are reached.
- A solution is a vector of decisions ; each level of the tree fixes one decision.
- The pattern is choose, explore, unchoose: every change must be undone when going back.
- There are two schemes: first solution (stop as soon as one is found) and all solutions (enumerate, count, or keep the best).
- Pruning (feasibility, look-ahead, bounds, symmetry) and a good ordering of candidates make the difference between a usable algorithm and brute force.
- The worst-case time is usually exponential (, ) or factorial (); the memory is only for the stack.
- When dynamic programming applies, it is usually better. Backtracking is the tool for enumeration and for hard combinatorial problems.
The next lesson applies the schemes to classic problems: permutations, subset sum, N-queens, the knight's tour, graph coloring, task assignment, magic squares and Sudoku.
Errors, suggestions and public questions open a form on GitHub, visible to everyone.