Why it matters

Many combinatorial problems reduce to backtracking. Understanding the pattern (choose, recurse, undo choice) makes writing solutions to a whole class of problems mechanical.

Advertisement

The architecture

The pattern: at each state, try each possible next choice. If it's valid, recurse. If recursion succeeds, propagate success. Otherwise undo the choice and try the next option.

Pruning: constraint checks before recursing. Also more sophisticated: arc consistency, forward checking, ordering heuristics.

Backtracking core loopChoosenext valueRecurseexplore deeperUndo + try nexton dead endPrune aggressively: check constraints before recursion, order choices by likelihood
Backtracking template.
Advertisement

How it works end to end

N-Queens: place queen in column i, if it conflicts with previous queens skip, else recurse on column i+1.

Sudoku: pick empty cell, try each digit, recurse if it doesn't violate row/column/box constraints.

Ordering heuristics: minimum remaining values, degree heuristic. Dramatically speed up search on constraint satisfaction problems.