Constraint Satisfaction
Backtracking search with forward checking and the MRV, degree, and least-constraining-value heuristics, applied to map coloring and circuit layout.
╌╌╌╌
A constraint satisfaction problem (CSP) is a triple: variables, a domain of values for each, and constraints that forbid certain combinations. Map coloring and circuit-board layout both take this form — assign a color to each region, or a position to each component, so that no constraint is violated. This project solves them with backtracking search, sharpened by forward checking and variable- and value-ordering heuristics.
Backtracking assigns variables one at a time, checks the constraints touching each new assignment, and on a dead end undoes the last assignment to try the next value. It walks the same tree as naive generate-and-test but prunes a branch the moment it turns inconsistent, rather than only at a complete assignment.
- 1input: a partial assignment , a CSP
- 2if is complete then return
- 3unassigned variable chosen by MRV, then degree
- 4for each value of , ordered by LCV, do
- 5if is consistent with then
- 6add to ; propagate by forward checking
- 7if no neighbor's domain is empty then
- 8
- 9if then return
- 10remove from ; restore pruned domains
- 11return failure
Three ordering heuristics decide which branch to try first:
- Minimum remaining values (MRV) picks the variable with the fewest legal values left, failing fast on the tightest variable.
- Degree breaks MRV ties by choosing the variable tied to the most constraints with still-unassigned neighbors.
- Least-constraining value (LCV) orders the chosen variable's values by how few options they remove from neighbors, keeping the rest of the search open.
Forward checking propagates each assignment into neighbors' domains, deleting values it has just made illegal; when a domain empties, the current path is abandoned before it is extended further. This catches conflicts one step earlier than testing constraints only at assignment time.
References
- Project repository
- Reference notes: Constraint Satisfaction Problems
- Reference notes: CSP Search and Structure
- Reference notes: Constraint Search: N-Queens & Sudoku
╌╌ END ╌╌