AI Course · Week 8

Constraint Satisfaction

When the goal is not a path but an assignment. Variables, domains, and rules that must all hold at once, solved by backtracking that is smarter than brute force.

By Arj Search Est. reading time: 16 minutes

Sudoku, exam timetables, seating plans, and colouring a map so no two neighbours match all share a shape. You are not looking for a route from A to B. You are trying to assign values to a set of variables so that a list of rules is satisfied all at once. These are constraint satisfaction problems, and because they have this common structure, one general method solves all of them. Better still, a few simple tricks make that method dramatically faster.

1

A different shape of problem

In the search problems from earlier weeks, a state was a black box and the answer was a sequence of actions. A constraint satisfaction problem, or CSP, opens the box. It says a state is made of variables, each of which must be given a value, and there are constraints saying which combinations of values are allowed.

The goal is not a path but a complete, legal assignment: a value for every variable such that no constraint is broken. Because so many real problems fit this mould, treating them as CSPs lets us reuse one solver and one set of speed-up tricks everywhere.

The shift in thinking
Ordinary search asks "what sequence of moves reaches the goal?" A CSP asks "what values make every rule true at once?" Same idea of searching, but the structure of variables and constraints lets us be far cleverer about it.
2

Defining a CSP

Every CSP is described by three things. Learn these labels, because exam questions ask you to identify them for a given problem.

  • Variables: the things that need a value. For map colouring, each region is a variable.
  • Domains: the set of allowed values for each variable. For map colouring, the domain might be {red, green, blue}.
  • Constraints: the rules restricting which combinations are legal. For map colouring, adjacent regions must have different colours.
ProblemVariablesDomainConstraint
Map colouringEach regionThe available coloursAdjacent regions differ
SudokuEach empty cellDigits 1 to 9No repeat in row, column, or box
N-queensEach queen (one per column)The rowsNo two queens attack each other
Exam timetableEach examThe time slotsClashing exams get different slots
Exam technique
If a question says "formulate this as a CSP," give all three parts explicitly: what the variables are, what values each can take (the domain), and the exact constraints. Naming them clearly is where the marks are.
3

The constraint graph

A CSP where each constraint links two variables can be drawn as a constraint graph: one node per variable, and an edge between any two variables that share a constraint. For map colouring, the graph has a node per region and an edge between every pair of neighbouring regions.

This picture is not just decoration. The shape of the graph tells the solver a lot: densely connected variables are the hardest to assign and should be tackled first, and separate components can be solved independently. Many of the speed-up tricks in section 5 are really about reading the constraint graph.

Why the graph helps
Tasmania, an island with no land neighbours, is a lone node with no edges. It can be coloured anything, any time. That is obvious from the graph and captures the general point: structure in the graph is structure you can exploit.
4

Backtracking search

The standard way to solve a CSP is backtracking search. It is depth-first search with one crucial addition: it checks constraints as it goes and abandons a path the moment it becomes illegal.

  1. Pick an unassigned variable.
  2. Try a value from its domain that does not break any constraint with already-assigned variables.
  3. If a legal value exists, assign it and move on to the next variable.
  4. If no legal value exists, backtrack: undo the previous assignment and try a different value there.

The word backtrack is the whole trick. Instead of building a full assignment and then checking it (hopeless, there are far too many), backtracking fails fast. The instant a variable has no legal value, it knows the current partial assignment is doomed and retreats, pruning away everything beneath it.

Backtracking is just careful DFS
It is the depth-first search you already know, walking down one assignment at a time, but it refuses to continue down any branch that already violates a constraint. That early checking is what makes it practical.
5

Making backtracking smart

Plain backtracking works, but a few well-known heuristics make it hugely faster by choosing what to do next more wisely and spotting dead ends earlier.

Choosing which variable to assign next

  • Minimum remaining values (MRV): assign the variable with the fewest legal values left. Tackle the most constrained variable first, because if it is going to fail, fail now rather than after lots of wasted work.
  • Degree heuristic: break ties by choosing the variable involved in the most constraints, the most connected node in the graph.

Choosing which value to try

  • Least constraining value (LCV): prefer the value that rules out the fewest options for neighbouring variables, keeping future choices open.

Spotting dead ends early

  • Forward checking: when you assign a value, immediately remove it from the domains of neighbouring variables. If any neighbour is left with no legal values, backtrack straight away rather than later.
  • Constraint propagation (arc consistency, AC-3): go further and repeatedly prune domains across the whole graph, so that every variable is consistent with its neighbours. This often solves easy CSPs with almost no search at all.
One idea underneath all of them
Do the hardest, most constrained work first, and detect failure as early as possible. MRV picks the hardest variable, forward checking and arc consistency detect doomed branches before wasting effort on them.
6

Watch a map get coloured

Here is the classic example: colour the states of Australia with three colours so no two neighbouring states share a colour. The nodes are the states and the edges are the "must differ" constraints. Step through backtracking as it tries colours, hits a dead end, and backtracks to fix an earlier choice.

Interactive Map colouring by backtracking

Each node is a state; an edge means the two states must have different colours. Three colours are available.

red blue green unassigned
Press step or play. Order: WA, NT, SA, NSW, Q, V, T.
Watch Q: after NSW is coloured, all three colours clash with its neighbours, so the search backtracks and recolours NSW before Q can be filled. That is backtracking in action.
7

Key points and practice

  • A CSP is defined by variables, domains, and constraints; the goal is a legal complete assignment.
  • A constraint graph links variables that share a constraint and reveals structure to exploit.
  • Backtracking search is depth-first search that checks constraints as it goes and retreats the moment a variable has no legal value.
  • MRV and the degree heuristic choose which variable to assign; LCV chooses which value.
  • Forward checking and arc consistency prune domains to spot dead ends early.
4 marks
Q1. Formulate map colouring as a CSP, stating variables, domains, and constraints.
Model answer

The variables are the regions to be coloured. The domain of each variable is the set of available colours, for example {red, green, blue}. The constraints state that any two regions sharing a border must be given different colours. A solution is an assignment of a colour to every region such that no bordering pair shares a colour.

3 marks
Q2. What is backtracking search, and how does it differ from generating and testing full assignments?
Model answer

Backtracking search assigns variables one at a time, checking constraints against already-assigned variables at each step, and undoes the most recent assignment as soon as a variable has no legal value. It differs from generate-and-test, which builds a complete assignment before checking it, by failing fast: it abandons a doomed partial assignment immediately, pruning away all the complete assignments that would have extended it and saving enormous effort.

4 marks
Q3. Explain the MRV heuristic and forward checking, and why they speed up backtracking.
Model answer

MRV (minimum remaining values) chooses the next variable to assign as the one with the fewest legal values remaining, tackling the most constrained variable first so that failures are found early rather than deep in the search. Forward checking, after each assignment, removes that value from the domains of neighbouring variables, and if any neighbour is left with an empty domain the search backtracks immediately. Both speed up backtracking by detecting dead ends as early as possible instead of exploring branches that are already doomed.

From rules to patterns in data
CSPs solve problems defined by strict rules. Next we turn to learning from data with no rules given at all, starting with cluster analysis: finding hidden groups.
Cluster analysis
Scroll to Top