📝 CSP — Quick Notes

GATE Cheatsheet

1. CSP Definition

ComponentDescription
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).
SolutionComplete assignment satisfying all constraints simultaneously.
ConsistentPartial assignment that violates no constraint.
CompleteEvery variable is assigned a value.

2. Constraint Types

TypeArityExampleNotes
Unary1X ≠ RedHandled by domain reduction (preprocessing)
Binary2X ≠ YMost algorithms designed for binary CSPs
Global≥ 2AllDiff(X1…Xn)Specialized propagators exist (e.g., matching)
SoftanyPrefer X = BlueOptimisation variant (not standard CSP)

Key fact: Any CSP can be reduced to a binary CSP using hidden-variable or dual encoding.

3. Algorithm Comparison

AlgorithmTypePropagationCompletenessKey Idea
Backtracking SearchNoneComplete DFS, one variable at a time, undo on failure
BT + Forward Checking Search + InferenceCurrent → neighboursComplete After assignment, prune neighbours' domains
BT + MAC Search + InferenceFull AC-3Complete Run full arc consistency after each assignment
AC-3 alone PreprocessingFull ACIncomplete Reduce domains; may not solve the CSP
Tree CSP Solver DirectBackward arc passComplete O(nd²) for tree-structured constraint graphs

4. Heuristics Summary

HeuristicApplies ToStrategyRule
MRVVariable selectionFail-first Choose variable with fewest remaining values
DegreeVariable selection (tie-breaker)Most constraining Choose variable involved in most constraints on unassigned variables
LCVValue orderingFail-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

Levelk-ConsistentChecksCost
Node consistency1-consistentUnary constraintsO(nd)
Arc consistency2-consistentBinary arcsO(cd³) for AC-3
Path consistency3-consistentTriplesO(n³d³)
Strong n-consistencyn-consistentAll subsetsExponential

Strong n-consistency → backtrack-free solving, but enforcing it is as hard as solving the CSP.

6. Complexity Quick Reference

ItemComplexityNotes
Brute-force (generate & test)O(dn)Check all complete assignments
Plain backtracking (worst)O(dn)But prunes early in practice
AC-3O(c · d³)c = #constraints, d = max domain
Tree CSP solverO(n · d²)Only for tree-structured graphs
Cutset conditioningO(dc · n · d²)c = cutset size
Tree decompositionO(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