Skip to content
Kudos AI

Constraint Satisfaction Problem

A problem stated as a set of variables, a domain of permitted values for each, and constraints restricting which combinations of values may be taken simultaneously, so that a general solver can reason about its structure without any domain knowledge.

Also known as: CSP, Constraint programming, Constraint network

Understanding Constraint Satisfaction Problem

A constraint satisfaction problem replaces the black-box view of search - an initial state, an action function and a goal test - with an explicit description of what makes a state good or bad. Variables carry the choices, domains carry the options, and constraints record which combinations are permitted. An assignment is consistent when it violates no constraint, complete when every variable has a value, and a solution when it is both. Because the description is standardised, a solver needs nothing problem-specific: it can inspect the constraints themselves to decide what to try next and what to rule out.

The binary constraints induce a constraint graph, one node per variable and one edge per constraint, and the shape of that graph is the solver’s map of the problem. The degree of a variable, the number of constraints it participates in, predicts how much assigning it will restrict everything else. Russell and Norvig’s standard example colours the seven regions of Australia with three colours under nine adjacency constraints; South Australia has degree five and Tasmania, an island, has degree zero, so Tasmania’s colour is free and the eighteen solutions fall into six mainland colourings times three.

Two ideas make the form tractable in practice. The first is commutativity: reaching a partial assignment by one order of assignments is the same as reaching it by any other, so the order carries no information and a solver may fix one variable per level of its tree. For seven variables and three values that turns 7! × 3⁷ leaves into 3⁷, a factor of 5,040 for free. The second is propagation: before and during search, values with no possible partner can be deleted outright. Arc consistency, enforced by the AC-3 algorithm, makes every ordered pair of variables consistent in time polynomial in the problem size.

Propagation is sound but incomplete. It never deletes a value that could appear in a solution, yet a problem can pass arc consistency and still have none, because arc consistency inspects only two variables at a time and cannot see a contradiction that requires three. What closes the gap is backtracking search, and what makes that search fast is the pairing of inference with ordering heuristics: minimum remaining values chooses the variable closest to failing, degree breaks its ties, and least-constraining-value picks the value that leaves neighbours the most room. The pairing matters more than either half, since a variable-ordering heuristic that reads domain sizes does nothing at all unless something is shrinking domains.

How to Calculate

CSP = (X, D, C), solution: complete assignment violating no c ∈ C

where

X
the variables X₁ … Xₙ, one per choice the problem requires
D
a domain Dᵢ of permitted values for each variable
C
constraints, each restricting the values some subset of variables may take together
consistent
an assignment, possibly partial, that violates no constraint

Example of Constraint Satisfaction Problem

Colouring Australia uses seven variables with domain {red, green, blue} and nine inequality constraints, one per shared border. Running AC-3 on the untouched problem makes zero revisions: every region still has three colours, so no value can be deleted and propagation has nothing to work with until an assignment creates an asymmetry.

Set Western Australia to red and Queensland to green and the branch is already dead, though only one method notices. Forward checking leaves Northern Territory and South Australia each holding the single value blue and continues; full arc consistency propagates that singleton one step further, finds the two regions are adjacent, empties South Australia’s domain and reports failure. Brute force over the remaining five regions confirms there are zero completions.

Measuring N-queens shows why heuristics are usually mispresented. At n = 24, plain backtracking tries 411,608 assignments; adding minimum remaining values alone tries exactly the same number, because nothing is deleting values and every domain ties. Forward checking alone brings it to 286,963, and the two together to 43.

Advantages and Disadvantages

Pros

  • One representation and one solver serve problems with nothing else in common.
  • Propagation can prove a branch hopeless without searching it, often before any assignment is made.
  • Structure is visible: degree, domain size and graph shape all guide the search directly.

Cons

  • Constraint satisfaction is NP-complete, so the standard form organises the difficulty rather than removing it.
  • Arc consistency is incomplete, and stronger k-consistency costs time and space exponential in k.
  • Some problems, especially with complicated numeric or temporal conditions, are awkward to express as constraints at all.

Frequently Asked Questions

How is this different from ordinary state-space search?

A search problem is opaque: the solver can test whether a state is a goal but cannot see why a state is bad. A CSP exposes the structure, so the solver can delete impossible values, detect dead ends early, and choose what to try next from the constraints themselves rather than from a hand-written heuristic.

If arc consistency is incomplete, why run it?

Because it is cheap and it is sound. It runs in polynomial time, never removes a value that could be part of a solution, and frequently collapses domains enough to make the remaining search trivial. It also supplies the domain-size differences that variable-ordering heuristics depend on.

Should inference run once at the start or throughout the search?

Throughout. Preprocessing with AC-3 helps only if the initial domains are already uneven. Every assignment made during search creates a new opportunity to deduce, and those deductions are what let minimum remaining values steer. Interleaving the two is what produces the large speedups.

The Bottom Line

A CSP is a problem described in enough detail that a general solver can reason about it: variables, domains and constraints, plus the graph they induce. Commutativity shrinks the tree for free, propagation deletes what cannot work, and search with inference-fed ordering heuristics does the rest.