Backtracking Search for CSPs
🎯 Learning Objectives
- Understand why generate-and-test is impractical
- Describe the backtracking search algorithm for CSPs
- Apply variable ordering heuristics: MRV and degree heuristic
- Apply value ordering heuristic: Least Constraining Value (LCV)
- Explain forward checking and maintaining arc consistency (MAC)
- Differentiate chronological vs intelligent backtracking
- Describe backjumping and conflict-directed backjumping
1. Naive Generate-and-Test
The simplest approach to solving a CSP is generate-and-test: enumerate all possible complete assignments and check each one against the constraints.
⚠ Why Generate-and-Test Fails
For a CSP with n variables each having domain size d, there are dn complete assignments. For Australia map coloring: 37 = 2,187 assignments. For 25-Queens: 2525 ≈ 8.88 × 1034. Clearly, this brute-force approach does not scale.
The key insight is that we can detect failures early — as soon as a partial assignment violates a constraint, there is no need to extend it further. This is the principle behind backtracking.
2. Backtracking Search Algorithm
📚 Backtracking Search
Backtracking search is a depth-first search that assigns values to one variable at a time and backtracks when a variable has no legal values remaining. It exploits the commutativity property by choosing a single unassigned variable at each node.
Algorithm: Backtracking-Search(csp)
function BACKTRACKING-SEARCH(csp) returns solution or failure
return BACKTRACK({}, csp)
function BACKTRACK(assignment, csp) returns solution or failure
if assignment is complete then return assignment
var ← SELECT-UNASSIGNED-VARIABLE(csp, assignment)
for each value in ORDER-DOMAIN-VALUES(var, assignment, csp) do
if value is consistent with assignment then
add {var = value} to assignment
inferences ← INFERENCE(csp, var, assignment)
if inferences ≠ failure then
add inferences to csp
result ← BACKTRACK(assignment, csp)
if result ≠ failure then return result
remove inferences from csp
remove {var = value} from assignment
return failure
The algorithm has three key customization points:
- SELECT-UNASSIGNED-VARIABLE: which variable to assign next?
- ORDER-DOMAIN-VALUES: in what order to try values?
- INFERENCE: what deductions to make after each assignment?
3. Variable Ordering Heuristics
3.1 Minimum Remaining Values (MRV)
📚 MRV Heuristic (Most Constrained Variable)
Choose the variable with the fewest legal values remaining in its domain. Also called the fail-first heuristic because it picks the variable most likely to cause failure soon, pruning the search tree early.
✍ MRV Example
Suppose after some assignments, the remaining domains are:
- X3: {R, B} — 2 values
- X5: {G} — 1 value
- X7: {R, G, B} — 3 values
MRV selects X5 (only 1 value). If X5's single value leads to failure, we detect it immediately without wasting time on X7's 3 options.
3.2 Degree Heuristic
📚 Degree Heuristic
As a tie-breaker for MRV, choose the variable involved in the most constraints on remaining unassigned variables. This maximizes the pruning effect of the next assignment.
In the Australia map example, if MRV yields a tie between NT and Q (both with 2 remaining values), the degree heuristic would select whichever has more unassigned neighbours.
💡 GATE Tip
MRV and degree heuristic are among the most frequently tested CSP concepts in GATE. Remember: MRV = fewest remaining values (fail-first), Degree = most constraints on unassigned neighbours (tie-breaker).
4. Value Ordering: Least Constraining Value
📚 Least Constraining Value (LCV)
Once a variable is selected, try the value that rules out the fewest values in the domains of neighbouring unassigned variables. The intuition: prefer values that leave maximum flexibility for future assignments.
✍ LCV Example
Variable Q (Queensland) has domain {R, G, B}. Its neighbours are NT, SA, and NSW.
- Q = R → eliminates R from 2 neighbours → rules out 2 values
- Q = G → eliminates G from 3 neighbours → rules out 3 values
- Q = B → eliminates B from 1 neighbour → rules out 1 value
LCV chooses Q = B first (rules out only 1 value), then Q = R, then Q = G.
⚠ Variable vs Value Ordering Strategy
Variable ordering uses a fail-first strategy (pick the most constrained variable). Value ordering uses a fail-last strategy (pick the least constraining value). These are complementary: choose the hardest variable first, but try the easiest value for it first.
5. Forward Checking
📚 Forward Checking
After assigning a value to variable X, forward checking examines each unassigned variable Y that shares a constraint with X, and removes from Y's domain any value that is inconsistent with X's assignment. If any domain becomes empty, the assignment is rejected immediately.
✍ Forward Checking Trace (Australia Map)
Initial domains: all variables have {R, G, B}.
Step 1: Assign WA = R
- NT: remove R → {G, B}
- SA: remove R → {G, B}
Step 2: Assign NT = G (MRV: NT and SA both have 2 values)
- SA: remove G → {B}
- Q: remove G → {R, B}
Step 3: Assign SA = B (forced — only 1 value)
- Q: remove B → {R}
- NSW: remove B → {R, G}
- V: remove B → {R, G}
Forward checking has reduced the remaining problem significantly without any backtracking!
💡 Limitation of Forward Checking
Forward checking only checks constraints between the current variable and future variables. It does not detect inconsistencies between two future variables. For example, if NSW and V both need separate colours but both only have {R}, forward checking won't detect this conflict. Arc consistency (next section) addresses this limitation.
6. Maintaining Arc Consistency (MAC)
📚 MAC
Maintaining Arc Consistency extends forward checking by running AC-3 (arc consistency algorithm) after each assignment. When Xi is assigned, MAC initializes the queue with all arcs (Xj, Xi) where Xj is an unassigned neighbour of Xi, then propagates until the queue is empty or a domain wipe-out occurs.
| Feature | Forward Checking | MAC |
|---|---|---|
| Checks performed | Current → future variables only | Propagates between all future variables |
| Detection power | Detects single-variable domain wipe-out | Detects all arc-inconsistencies |
| Cost per step | Lower | Higher (runs AC-3) |
| Overall efficiency | Good for loosely constrained | Superior for tightly constrained problems |
| Typical use | Simple CSPs | Default in modern solvers |
7. Intelligent Backtracking
7.1 Chronological Backtracking
Standard backtracking is chronological: when a dead-end is reached, it backtracks to the most recently assigned variable. This can be wasteful if the conflict was caused by a much earlier assignment.
7.2 Backjumping
📚 Backjumping
Backjumping backtracks to the most recent variable in the conflict set — the set of previously assigned variables that caused the current variable's domain to become empty. This skips over irrelevant variables.
✍ Backjumping Example
Suppose we assigned WA=R, NT=G, T=R, Q=?, and Q's domain is empty because of constraints with NT and SA (not T). The conflict set of Q is {NT, SA}. Instead of backtracking to T (chronological), backjumping goes directly to NT or SA.
7.3 Conflict-Directed Backjumping
📚 Conflict-Directed Backjumping
An enhancement where the conflict set is accumulated during backjumping. When backjumping from Xj to Xi, the conflict set of Xi is updated to include all variables from Xj's conflict set (minus Xi itself) merged with Xi's own conflict set. This propagates conflict information further up the search tree.
📜 Key Insight
When forward checking or MAC is used, simple backjumping has no advantage over chronological backtracking — because forward checking already ensures that the most recent variable is always relevant to any domain wipe-out. Conflict-directed backjumping, however, can still help in some cases.
Summary
- Generate-and-test checks dn assignments — impractical for large CSPs.
- Backtracking search assigns one variable at a time and prunes on constraint violations.
- MRV (fail-first) picks the variable with fewest remaining values; Degree heuristic breaks ties.
- LCV (fail-last) orders values to maximise remaining flexibility.
- Forward checking removes inconsistent values from future variable domains after each assignment.
- MAC runs full arc consistency after each assignment — stronger than forward checking.
- Backjumping and conflict-directed backjumping skip irrelevant backtrack points using conflict sets.