📝 CSP — Quick Notes
GATE Cheatsheet
1. CSP Definition
| Component | Description |
|---|---|
| Variables (X) | X = {X1, X2, …, Xn} — the unknowns to be solved |
| Domains (D) | Di = set of allowable values for Xi. Can be finite, infinite, or continuous. |
| Constraints (C) | Each Cj has a scope (variables) and a relation (allowed value tuples). |
| Solution | Complete assignment satisfying all constraints simultaneously. |
| Consistent | Partial assignment that violates no constraint. |
| Complete | Every variable is assigned a value. |
2. Constraint Types
| Type | Arity | Example | Notes |
|---|---|---|---|
| Unary | 1 | X ≠ Red | Handled by domain reduction (preprocessing) |
| Binary | 2 | X ≠ Y | Most algorithms designed for binary CSPs |
| Global | ≥ 2 | AllDiff(X1…Xn) | Specialized propagators exist (e.g., matching) |
| Soft | any | Prefer X = Blue | Optimisation variant (not standard CSP) |
Key fact: Any CSP can be reduced to a binary CSP using hidden-variable or dual encoding.
3. Algorithm Comparison
| Algorithm | Type | Propagation | Completeness | Key Idea |
|---|---|---|---|---|
| Backtracking | Search | None | Complete | DFS, one variable at a time, undo on failure |
| BT + Forward Checking | Search + Inference | Current → neighbours | Complete | After assignment, prune neighbours' domains |
| BT + MAC | Search + Inference | Full AC-3 | Complete | Run full arc consistency after each assignment |
| AC-3 alone | Preprocessing | Full AC | Incomplete | Reduce domains; may not solve the CSP |
| Tree CSP Solver | Direct | Backward arc pass | Complete | O(nd²) for tree-structured constraint graphs |
4. Heuristics Summary
| Heuristic | Applies To | Strategy | Rule |
|---|---|---|---|
| MRV | Variable selection | Fail-first | Choose variable with fewest remaining values |
| Degree | Variable selection (tie-breaker) | Most constraining | Choose variable involved in most constraints on unassigned variables |
| LCV | Value ordering | Fail-last | Choose value that eliminates fewest values from neighbours |
⚠ Remember
Variables: fail-first (MRV). Values: fail-last (LCV). These are complementary strategies!
5. Consistency Hierarchy
| Level | k-Consistent | Checks | Cost |
|---|---|---|---|
| Node consistency | 1-consistent | Unary constraints | O(nd) |
| Arc consistency | 2-consistent | Binary arcs | O(cd³) for AC-3 |
| Path consistency | 3-consistent | Triples | O(n³d³) |
| Strong n-consistency | n-consistent | All subsets | Exponential |
Strong n-consistency → backtrack-free solving, but enforcing it is as hard as solving the CSP.
6. Complexity Quick Reference
| Item | Complexity | Notes |
|---|---|---|
| Brute-force (generate & test) | O(dn) | Check all complete assignments |
| Plain backtracking (worst) | O(dn) | But prunes early in practice |
| AC-3 | O(c · d³) | c = #constraints, d = max domain |
| Tree CSP solver | O(n · d²) | Only for tree-structured graphs |
| Cutset conditioning | O(dc · n · d²) | c = cutset size |
| Tree decomposition | O(n · dw+1) | w = tree-width |
7. Classic CSP Formulations
Map Coloring (Australia)
7 variables (WA, NT, SA, Q, NSW, V, T), domains {R,G,B}, 9 binary ≠ constraints. SA is most constrained (5 neighbours). Tasmania is independent.
N-Queens
n variables (Qi = row in column i), domains {1…n}. Constraints: AllDiff + no-diagonal (|Qi − Qj| ≠ |i − j|). 4-Queens solutions: [2,4,1,3] and [3,1,4,2].
Sudoku
81 variables, domains {1…9}, 27 AllDiff constraints (9 rows + 9 cols + 9 boxes). AC-3 + MRV solves most instances without backtracking.
8. Common GATE Question Patterns
- CSP formulation: “How many variables / constraints does this CSP have?”
- MRV selection: “Which variable is chosen next by MRV?” → Pick the one with smallest remaining domain.
- AC-3 execution: “After running AC-3, what are the domains?” → Trace the REVISE steps.
- Forward checking: “After assigning X=v with forward checking, which domains change?”
- Constraint graph: “Is the constraint graph a tree?” → If yes, O(nd²) solution.
- Complexity: “What is the time complexity of AC-3?” → O(cd³).
- Backtracking count: “How many backtracks for this CSP?” → Trace the search tree.
9. Key Formulae & Facts
- Arc (Xi, Xj) consistent: ∀ v ∈ Di, ∃ w ∈ Dj : (v, w) ∈ Rij
- AC-3 initial queue: all 2c directed arcs (both directions)
- AC-3 terminates when: queue empty OR some domain becomes empty
- Arc consistent ⇏ solvable (necessary but not sufficient)
- Commutativity: order of variable assignment doesn't matter for reachability
- Four Colour Theorem: every planar graph can be coloured with 4 colours
- Binary CSP reduction: any CSP can become binary via dual/hidden-variable encoding