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.
- red
- green
- blue
- being assigned
Plain backtracking: assign regions in order and only notice a clash when a region has no colour left.
Compare the number of tries between plain backtracking and forward checking.
Step by step
- 1List variables, domains and constraints.
- 2Pick an unassigned variable.
- 3Try a value that does not break any constraint.
- 4Recurse; if you get stuck, undo and try another value.
- 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.