Constraint Satisfaction Problems
A constraint satisfaction problem replaces the black-box state with a factored one: variables, domains, and constraints. That structure supports inference before any search runs.
╌╌╌╌
The search algorithms of the previous lessons treat each state as atomic — a black box the algorithm can test for goalhood and expand, but never look inside. A constraint satisfaction problem opens the box: it uses a factored representation, where a state is a set of variables, each holding a value, and the problem is solved when every variable has a value that satisfies all the constraints.1 Once the algorithm can see a state's internal structure, it can do something a generic searcher cannot — reason about why a partial state is doomed and discard whole regions of the space at once, using general-purpose machinery rather than a hand-built heuristic per problem.
This lesson defines the CSP and shows how many different-looking problems share its shape, then develops constraint propagation: using the constraints to shrink the variables' options before any search runs. The search itself — backtracking with CSP-specific heuristics — and the structural tricks that make whole classes of CSP easy are the subject of the companion lesson, CSP Search and Structure.
Defining a CSP
A constraint satisfaction problem has three components, written , , and .
A relation can be listed explicitly (every tuple that satisfies it) or given abstractly as a test — for two variables with domain that must differ, and say the same thing. To search a CSP we define its state space through assignments. An assignment gives values to some or all variables; it is consistent if it violates no constraint; a complete assignment names every variable; and a solution is a consistent, complete assignment. A partial assignment leaves some variables unset — the states backtracking search moves through.2
Map coloring
The canonical example colors a map of Australia. Each region must be red, green, or blue so that no two neighboring regions share a color. The variables are the seven regions,
each with domain . The constraints require adjacent regions to differ, and there are nine adjacencies:
Here abbreviates . One of the many solutions is . It helps to picture a CSP as a constraint graph: one node per variable, and an edge between any two variables that share a constraint.
Recasting a problem this way makes the solver faster. Choose and every one of its five neighbors instantly loses as an option. A blind state-space search over those five neighbors would face combinations; propagating the one choice leaves , an 87% cut — and it costs nothing beyond reading the graph.3 More generally, the moment a partial assignment breaks a constraint, we know it can never extend to a solution, and we see exactly which variables to blame.
Other problems, same shape
Sudoku is a CSP with 81 variables, one per square, named through . An empty square has domain ; a pre-filled one has a singleton domain. The rules are 27 Alldiff constraints — one for each row, column, and box — asserting that the nine variables in that unit take distinct values.4 Alldiff is a global constraint: it relates an arbitrary number of variables, and can be enforced by a special-purpose algorithm more efficiently than the equivalent pile of pairwise constraints.
Job-shop scheduling models the assembly of a product as tasks with start times. Each task is a variable whose value is the minute it begins; a precedence constraint says task , which takes duration , must finish before starts.5 A disjunctive constraint — two tasks that share a tool may not overlap — reads . Real schedulers run CSPs of this form with thousands of variables.
The variety hints at a taxonomy of constraint types.
| Kind | Arity | Example |
|---|---|---|
| Unary | one variable | |
| Binary | two variables | |
| Higher-order / global | variables | |
| Preference (soft) | any | assigning Prof. R the 2 p.m. slot costs 2 |
Domains vary too: finite (map colors, digits), infinite discrete (integer start times with no deadline, needing a constraint language rather than an enumerated relation), and continuous (the best-known continuous CSPs are linear programs). The preference constraints of the last row soften the formalism into a constraint optimization problem, where violating a constraint costs points rather than ruling a solution out; every constraint we treat below is absolute.6 Any higher-order constraint can be rewritten with auxiliary variables as a set of binary ones, so the algorithms that follow assume binary CSPs without loss of generality.
Constraint propagation
A generic searcher has one move: search. A CSP solver has two — it can search, or it can infer, using the constraints to shrink domains before or during search. If choosing a value somewhere rules out values elsewhere, those values can be deleted before search ever reaches them. Inference works through local consistency: enforce consistency in one corner of the constraint graph, and inconsistent values get eliminated throughout it. Sometimes propagation alone solves the problem; often it makes the remaining search much cheaper.7
Node consistency. A variable is node-consistent when every value in its domain satisfies its unary constraints. If South Australians dislike green, we make node-consistent by deleting , leaving . Enforcing node consistency once removes all unary constraints, which is why solvers can assume they've been discharged.
Arc consistency is the central algorithm for binary CSPs, so we state it carefully.
In plain terms: a value survives in only if it has at least one legal partner across the arc. Take the constraint with both domains the digits . Making arc-consistent with deletes every whose square is not a digit, reducing to ; making arc-consistent with leaves . Arc consistency can go far: on Sudoku, repeatedly enforcing it drives many domains to a single value and can solve easy puzzles outright. But it has limits — on a two-color map of Australia every variable is already arc-consistent (each value has a differing partner across the arc), yet no solution exists. Catching that needs path consistency, which tightens triples of variables by checking whether an assignment to two of them can be extended to a third. Both are special cases of -consistency: 1-consistency is node consistency, 2-consistency is arc consistency, 3-consistency is path consistency.8 A CSP is strongly -consistent if it is -consistent for every ; a strongly -consistent CSP on variables can be solved with no backtracking at all, by assigning variables in order and always finding a legal value. The catch is cost: establishing -consistency is worst-case exponential in in both time and space, so in practice solvers stop at arc consistency (fast, cheap) and reach for path consistency only when it clearly pays.
Sudoku shows both ends of this ladder in action. Enforcing arc consistency across the 27 Alldiff constraints solves the easiest puzzles outright, driving every domain to a singleton. Harder puzzles resist it, and human solvers reach for richer inference with names like naked triples: if three squares in a unit hold domains that are all subsets of the same three digits — say , , — then those three digits are locked into those three squares, so they can be deleted from every other square in the unit. This is nothing but a special-purpose way to enforce consistency of the Alldiff constraint, and it says nothing about Sudoku specifically; the same reasoning applies to any global Alldiff. The lesson generalizes: define a problem in constraint terms and the general machinery — arc consistency, path consistency, global-constraint propagators — takes over, with no problem-specific solver to write.4
The AC-3 algorithm
The standard way to make an entire network arc-consistent is AC-3. It keeps a queue of arcs to examine. Pop an arc and make arc-consistent with ; if that deletes anything from , then every arc into from a neighbor goes back on the queue, because a change in might now permit deletions in . If any domain is emptied, the CSP has no solution and AC-3 reports failure at once. Otherwise it stops when the queue drains, leaving a CSP with the same solutions but smaller domains.9
- 1input: a binary CSP with components
- 2all arcs in the CSP
- 3while is not empty do
- 4remove any arc from
- 5if then
- 6if size of then return falsedomain wiped out
- 7for each in do
- 8add torecheck arcs into
- 9return true
- 1input: variables , sharing a binary constraint
- 2
- 3for each in do
- 4if no value in lets satisfy the constraint then
- 5delete from
- 6
- 7return
The pruning cascades. Consider once more: the figure traces how deleting a value from one domain sends its incoming arcs back to the queue, so the reduction ripples outward until nothing more can be removed.
AC-3's cost is bounded cleanly. With variables, at most values each, and binary arcs, each arc enters the queue at most times (once per possible deletion in ), and checking an arc costs , for a worst case of .9
A worked AC-3 trace
The example prunes but never cascades — each domain is revised once and the queue drains. To see AC-3 propagate, seed the Australia map with two unary constraints and watch one deletion trigger the next. Suppose Western Australia must be (a fixed border color) and South Australia dislikes . Node consistency discharges the unary constraints first: and , every other domain still . Now run AC-3. The table below follows the queue arc by arc; only arcs that revise a domain are shown, and each successful revision re-enqueues the neighbors pointing into the shrunken variable.
| Step | Arc popped | deletes | New domain | Re-enqueued |
|---|---|---|---|---|
| 1 | from | |||
| 2 | from | |||
| 3 | from | |||
| 4 | from | |||
| 5 | from | |||
| 6 | from | |||
| 7 | from | |||
| 8 | from |
Two unary facts have collapsed almost the entire map: after step 8 the domains are , , , , , with and still open. Arc consistency did not fully solve the CSP — and retain choices — but it fixed five of seven variables without a single search step, and the remaining tree (, then ) has no way to fail. That is the point of propagating before searching: the deletion of from in step 2 forced the deletion in in step 3, which forced in step 5, which forced in step 7, a four-hop chain from one unary constraint.
Optimal arc-consistency algorithms
AC-3's bound hides wasted work: when an arc is re-examined, rescans all of even though only the recent deletion in could have changed anything. AC-4 (Mohr and Henderson, 1986) removes that waste by precomputing, for every value , a support count: the number of values in compatible with . Deleting a value from decrements the counts of exactly the values it supported; a count hitting zero deletes and cascades. This reaches the optimal worst case, but its up-front bookkeeping makes it slower than AC-3 on easy instances. AC-2001 (Bessière and Régin, 2001) attains the same bound while keeping only a pointer to the first surviving support per value, and beats AC-4 in practice by not maintaining the full count structure. AIMA presents AC-3 because it is simple and fast enough for the problems in the chapter; production solvers use the AC-2001-style variants.10
Propagation prunes, but it rarely finishes the job on its own — and above still have choices to make. Search handles the rest, and the CSP's structure pays off a second time there. This continues in CSP Search and Structure, which develops backtracking search with the MRV, degree, and least-constraining-value heuristics, and then shows how the shape of the constraint graph controls how hard the whole problem is.
Footnotes
- Russell & Norvig, AIMA, Ch. 6 — introduction: the factored representation of a state as variables with values, versus the atomic black-box states of Chapters 3–4, and the payoff of eliminating variable/value combinations that violate constraints. ↩
- Russell & Norvig, AIMA, §6.1 — Defining Constraint Satisfaction Problems: the triple, and the assignment / consistent / complete / partial / solution vocabulary. ↩
- Russell & Norvig, AIMA, §6.1.1 — Example problem: Map coloring: the Australia CSP, its constraint graph, and the reduction from propagating a single choice. ↩
- Russell & Norvig, AIMA, §6.2.6 — Sudoku example: 81 variables, singleton domains for givens, and 27 Alldiff constraints, one per row, column, and box. ↩ ↩2
- Russell & Norvig, AIMA, §6.1.2 — Example problem: Job-shop scheduling: tasks as start-time variables, precedence constraints , and disjunctive constraints for shared resources. ↩
- Russell & Norvig, AIMA, §6.1.3 — Variations on the CSP formalism: unary/binary/global constraints; discrete finite, infinite, and continuous domains; constraint languages; preference constraints and constraint optimization. ↩
- Russell & Norvig, AIMA, §6.2 — Constraint Propagation: search versus inference, local consistency, and node consistency. ↩
- Russell & Norvig, AIMA, §6.2.3–6.2.4 — Path consistency and -consistency: two-color Australia defeats arc consistency; path consistency reasons over triples; -consistency generalizes the ladder. ↩
- Russell & Norvig, AIMA, §6.2.2 — Arc consistency and the AC-3 algorithm (Figure 6.3): the queue of arcs, re-adding incoming arcs after a revision, and the worst-case bound. ↩ ↩2
- R. Mohr & T. Henderson,
Arc and Path Consistency Revisited,
Artificial Intelligence 28(2), 1986 — the optimal AC-4 algorithm. C. Bessière & J.-C. Régin,Refining the Basic Constraint Propagation Algorithm,
IJCAI 2001 — AC-2001/AC-3.1 achieving with lighter bookkeeping. ↩
╌╌ END ╌╌