Backtracking search with propagation
Also known as MAC, Maintaining arc consistency, Fail-first (MRV)
constraint constraint-search
Backtracking search finds, counts or enumerates the solutions of a model. At each node it picks an unfixed variable (by default the one with the fewest values left), orders its values (ascending, descending, least constraining first, or random) and tries them in turn: it assigns the value, propagates to the fixpoint, and undoes the choice through the trail on a conflict. Propagating every constraint after each choice prunes more than the forward checking of Haralick and Elliott, and it keeps their fail-first principle as the default variable order. The walk keeps its choice points on an explicit stack, so it can pause at a solution or when the budget runs out and continue later; count with a limit of 2 decides whether a puzzle has a unique solution. Use it for logic puzzles, colouring and small scheduling problems; a hint value per variable makes a known solution come first.
Pseudocode
algorithm Backtracking-Search(model, D)input: model with propagators, current domains D, variable order, value orderoutput: each solution in turn (the walk pauses there and resumes after it)if propagate-all(D) = failure thenreturnsearch(D)procedure search(D)x ← pick-variable(D) ▷ MRV: fewest values left, ties by index; or lowest indexif x = nil thenyield D ▷ every variable is fixedreturnVALUES ← order(D(x)) ▷ ascending, descending, least constraining or randommove the hint value of x, if any, to the front of VALUESfor each v in VALUES dopush-level(D)if assign(D, x, v) ≠ failure and propagate-changes(D) ≠ failure thensearch(D)pop-level(D) ▷ the trail restores every changed domainfunction count(model, D, limit)n ← 0for each solution s of Backtracking-Search(model, D) don ← n + 1if n = limit thenreturn at least n ▷ a limit of 2 decides uniquenessreturn exactly nIn the crates
constraint-searchDepth-first search with propagation after each choice, MRV variable order and LCV or random value order, resumable under a budget; counts solutions with early stop.
Lineage
Papers
- Increasing tree search efficiency for constraint satisfaction problems Robert M. Haralick, Gordon L. Elliott · 1980· Artificial Intelligence
- Contradicting conventional wisdom in constraint satisfaction Daniel Sabin, Eugene C. Freuder · 1994· Principles and Practice of Constraint Programming (PPCP 1994), Lecture Notes in Computer Science