collections/backtracking_utils library
Generic depth-first backtracking solver with pruning and result limits (roadmap #486).
Backtracking explores a tree of partial states: from a state it generates
candidate choices, applys each to descend, prunes any branch whose
partial state fails isValid before recursing, and records a state that
passes isComplete. This is the engine behind constraint puzzles
(N-Queens, Sudoku), subset/permutation search, and exact-cover problems —
anything where "try, check, undo, try the next" beats enumerating the whole
space.
The solver is immutable and stateless between runs: every callback is pure
over the State/Choice types you supply, and a fresh State is produced
by apply rather than mutating in place, so the same solver can be reused.
Classes
-
BacktrackingSolver<
State, Choice> -
A reusable backtracking search over states of type
Statereached by applying choices of typeChoice.