CSP Formulation

GATE Relevance: High

🎯 Learning Objectives

  • Define a Constraint Satisfaction Problem (CSP) formally
  • Identify the three components: variables, domains, and constraints
  • Distinguish unary, binary, and global constraints
  • Represent a CSP as a constraint graph
  • Formulate classic problems (map coloring, N-Queens, Sudoku) as CSPs
  • Understand binary CSPs and constraint hypergraphs
  • Explain why CSPs are important for GATE DA examinations

1. What Is a Constraint Satisfaction Problem?

📚 Formal Definition

A Constraint Satisfaction Problem (CSP) consists of three components:

  • X = {X1, X2, …, Xn} — a set of variables
  • D = {D1, D2, …, Dn} — a set of domains, one for each variable, where Di is the set of allowable values for Xi
  • C = {C1, C2, …, Cm} — a set of constraints that specify allowable combinations of values

A solution is an assignment of a value to every variable such that all constraints are satisfied simultaneously.

Unlike standard search problems where states are atomic (black-box), CSPs use a factored representation: the state is defined by the values assigned to the variables. This structure allows solvers to exploit powerful inference and pruning techniques.

💡 CSPs vs. Standard Search

In standard search, the goal test is a black box. In a CSP, the goal test is decomposed into a set of constraints over variables. This decomposition enables techniques like constraint propagation and backtracking with heuristics that are far more efficient than blind search.

2. Variables, Domains, and Constraints

2.1 Variables

Variables represent the unknowns in the problem. For example, in a map coloring problem, each region is a variable. Variables are typically denoted X1, X2, …, Xn.

2.2 Domains

Each variable Xi has a domain Di — the set of values it can take. Domains can be:

  • Finite discrete: e.g., {Red, Green, Blue} for map coloring
  • Infinite discrete: e.g., the set of integers ℤ
  • Continuous: e.g., real-valued intervals [0, 1]

Most GATE problems deal with finite-domain CSPs. The total number of possible complete assignments is |D1| × |D2| × … × |Dn|, which can be exponential.

2.3 Constraints

Each constraint Cj consists of:

  • A scope: the tuple of variables it involves, e.g., (Xi, Xk)
  • A relation: the set of allowed value combinations, which can be given explicitly (as a table) or implicitly (as a formula)

3. Constraint Types

TypeArityDescriptionExample
Unary1Restricts a single variableX1 ≠ Green
Binary2Relates two variablesX1 ≠ X2
Ternary3Relates three variablesX1 + X2 = X3
GlobalnInvolves an arbitrary number of variablesAllDiff(X1, …, Xn)

💡 Reducing to Binary CSPs

Every finite-domain CSP can be converted to a binary CSP by introducing auxiliary variables. Unary constraints can be handled by simply reducing the domain of the variable. This is why many algorithms focus specifically on binary CSPs.

📚 Common Global Constraints

  • AllDiff(X1, …, Xk): all variables must take distinct values
  • AtMost(n, X1, …, Xk, v): at most n variables can take value v
  • Sum(X1, …, Xk) = s: the sum of variables must equal s

4. Constraint Graph

📚 Constraint Graph

For a binary CSP, the constraint graph has:

  • One node for each variable
  • One edge for each binary constraint between two variables

For higher-arity constraints, we use a constraint hypergraph where each constraint connects a set of variables (a hyperedge).

The structure of the constraint graph is crucial for algorithm efficiency. For example:

  • If the constraint graph is a tree, the CSP can be solved in O(n · d²) time
  • Graph connectivity determines if the CSP can be decomposed into independent sub-problems
  • The tree-width of the constraint graph bounds the complexity of solving

✍ Example: Australia Map Coloring Constraint Graph

Variables: WA, NT, SA, Q, NSW, V, T (seven states/territories)

Edges represent shared borders: WA—NT, WA—SA, NT—SA, NT—Q, SA—Q, SA—NSW, SA—V, Q—NSW, NSW—V

Note that SA has the most edges (5 neighbours), making it the most constrained variable. Tasmania (T) is disconnected — it can take any colour independently.

6. Map Coloring Example (Australia)

✍ Worked Example: Australia Map Coloring

Problem: Color the map of Australia using three colours {R, G, B} such that no two adjacent regions share the same colour.

Variables: X = {WA, NT, SA, Q, NSW, V, T}

Domains: Di = {R, G, B} for all i

Constraints: ∀ adjacent regions Xi, Xj: Xi ≠ Xj

One solution: WA = R, NT = G, SA = B, Q = R, NSW = G, V = R, T = R

Verification: Check each edge — WA(R) ≠ NT(G) ✓, WA(R) ≠ SA(B) ✓, NT(G) ≠ SA(B) ✓, NT(G) ≠ Q(R) ✓, SA(B) ≠ Q(R) ✓, SA(B) ≠ NSW(G) ✓, SA(B) ≠ V(R) ✓, Q(R) ≠ NSW(G) ✓, NSW(G) ≠ V(R) ✓

💡 GATE Tip

Map coloring is a classic GATE question. Remember: a planar graph can always be coloured with 4 colours (Four Colour Theorem). With 3 colours, solutions depend on the graph structure. Be prepared to trace backtracking on small instances.

7. N-Queens as a CSP

✍ N-Queens Formulation

Variables: Q1, Q2, …, Qn — one per column (Qi represents the row of the queen in column i)

Domains: Di = {1, 2, …, n} for all i

Constraints:

  • No same row: ∀ i ≠ j: Qi ≠ Qj   (equivalently, AllDiff(Q1, …, Qn))
  • No same diagonal: ∀ i ≠ j: |Qi − Qj| ≠ |i − j|

For 4-Queens: variables Q1…Q4, domains {1,2,3,4}. One solution: Q1=2, Q2=4, Q3=1, Q4=3.

The N-Queens problem is an excellent example of how CSP formulation is more natural and efficient than standard search. Instead of exploring all nn board configurations, the CSP formulation already eliminates same-column conflicts and reduces the search space to n! assignments (before further pruning by diagonal constraints).

8. Sudoku as a CSP

✍ Sudoku CSP Formulation

Variables: Xij for each cell (i, j) where i ∈ {1,…,9}, j ∈ {1,…,9} — total 81 variables

Domains: Dij = {1, 2, …, 9} for empty cells; singleton {v} for pre-filled cells

Constraints:

  • Row constraints: AllDiff(Xi1, Xi2, …, Xi9) for each row i
  • Column constraints: AllDiff(X1j, X2j, …, X9j) for each column j
  • Box constraints: AllDiff over each 3×3 sub-grid

Total: 27 AllDiff constraints. When decomposed into binary constraints, this gives 810 binary constraints (each AllDiff over 9 variables yields C(9,2) = 36 binary pairs, times 27 = 972, minus duplicates).

9. Binary CSP and Constraint Hypergraph

📚 Binary CSP

A CSP is called a binary CSP if every constraint involves at most two variables. The constraint graph of a binary CSP is an ordinary graph (not a hypergraph). Any CSP with higher-arity constraints can be converted to a binary CSP using dual encoding or hidden variable encoding.

In dual encoding, each constraint becomes a variable whose domain is the set of consistent tuples, and binary constraints enforce shared variables to agree. In hidden variable encoding, a new variable is introduced for each higher-arity constraint, linked to the original variables by new binary constraints.

📚 Constraint Hypergraph

When constraints may involve more than two variables, the constraint structure is represented as a hypergraph where each hyperedge connects the set of variables involved in a single constraint. The hypergraph view is useful for identifying tree decompositions that can speed up solving.

Summary

  • A CSP is defined by variables X, domains D, and constraints C.
  • Constraints come in types: unary (1 variable), binary (2 variables), and global (n variables like AllDiff).
  • The constraint graph reveals the problem structure; tree-structured graphs can be solved in polynomial time.
  • CSPs exploit commutativity: variable ordering doesn't affect reachability, enabling efficient backtracking.
  • Classic examples: map coloring (Australia), N-Queens, Sudoku — all frequently tested in GATE.
  • Any CSP can be reduced to a binary CSP; higher-arity constraints are represented via constraint hypergraphs.