Skip to content
StackDecode
AI & ML Atlas
Artificial IntelligencealgorithmInteractive simulation

Constraint Satisfaction Algorithm

Assign values to variables so that every constraint holds.

The idea

A CSP has variables, a domain of possible values for each, and constraints between them (for example "neighbouring regions have different colours"). Solvers combine search with inference: choose a variable, try a value, and use the constraints to rule out other values early.

Watch it run

This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.

WANTQNSWVSAT
  • red
  • green
  • blue
  • being assigned

Plain backtracking: assign regions in order and only notice a clash when a region has no colour left.

1/13

Compare the number of tries between plain backtracking and forward checking.

Step by step

  1. 1List variables, domains and constraints.
  2. 2Pick an unassigned variable.
  3. 3Try a value that does not break any constraint.
  4. 4Recurse; if you get stuck, undo and try another value.
  5. 5Stop when every variable has a value.

Where it is used

Scheduling, timetables, Sudoku, map colouring, configuration.

Watch out for

Variable and value ordering heuristics can change the running time from hours to milliseconds.

Related