Many problems are solved most naturally by assigning values to variables under constraints. Exploiting that structure beats blind search.

Defining a CSP

A constraint satisfaction problem (CSP) consists of a set of variables X₁..Xₙ, a domain Dᵢ of possible values for each variable, and a set of constraints restricting the allowed combinations of values. A state is an assignment of values to some or all variables; an assignment is consistent (legal) if it violates no constraint, complete if every variable is assigned. A solution is a consistent and complete assignment. Examples: map colouring (variables: regions; domain: colours; constraint: neighbours differ), Sudoku, n-Queens, timetabling, scheduling, crypt-arithmetic puzzles and the Magic Square. Constraints may be unary, binary (two variables, drawn as a constraint graph) or of higher order. CSPs are solved faster than general search because a violation can be detected early on a partial assignment.

Definition: Constraint satisfaction problem. A problem defined by variables, their domains of values and constraints on combinations of values, whose solution is a complete assignment that satisfies all constraints.

Backtracking Search for CSPs

Backtracking search is depth-first search that assigns one variable at a time and backtracks when a variable has no legal value left. Since assignments are commutative (the order of assigning does not change the problem), we need to consider only one variable per level, so the tree has dⁿ leaves rather than n!·dⁿ. Three heuristics improve it: Minimum Remaining Values (MRV), choose the variable with the fewest legal values left (the "fail-first" principle); Degree heuristic, choose the variable involved in most constraints with unassigned variables; Least Constraining Value, try first the value that rules out the fewest choices for neighbours.

Constraint Propagation

Forward checking deletes inconsistent values from the domains of neighbouring unassigned variables after each assignment, and backtracks as soon as a domain becomes empty. Arc consistency (AC-3) goes further: an arc X → Y is consistent if for every value of X some value of Y satisfies the constraint; values failing this are removed, repeatedly, which can solve small puzzles without any search. Smarter backtracking (backjumping) and exploiting a tree-structured constraint graph (solvable in O(n·d²)) improve efficiency further.

Table 2.8: Elements of a CSP: map colouring

Element Meaning Example
Variables Things to assign Regions A, B, C, D
Domains Allowed values {Red, Green, Blue}
Constraints Allowed combinations Neighbours get different colours
Solution Complete consistent assignment A = R, B = G, C = B, D = R

Example 2.8: Colouring four regions

Problem: Regions A, B, C, D; neighbours A-B, A-C, B-C, C-D; colours {R, G, B}. Find a colouring by backtracking and count all solutions.

Solution:

  1. Assign A = R. Then B = R conflicts with A, so B = G.
  2. C must differ from A (R) and B (G): C = B.
  3. D must differ from C (B): D = R (first value tried).
  4. All constraints hold. Count: A, B, C are mutually adjacent, so 3! = 6 colourings; D has 2 choices: 6 × 2 = 12.

Result: First solution A = R, B = G, C = B, D = R; there are 12 solutions in all.

adj = {"A": "BC", "B": "AC", "C": "ABD", "D": "C"}
cols = "RGB"
def solve(order, asg, out):
    if len(asg) == len(order):
        out.append(dict(asg))
        return
    v = order[len(asg)]
    for c in cols:
        if all(asg.get(n) != c for n in adj[v]):
            asg[v] = c
            solve(order, asg, out)
            del asg[v]
out = []
solve("ABCD", {}, out)
print("first:", out[0])
print("number of solutions:", len(out))

Output

first: {'A': 'R', 'B': 'G', 'C': 'B', 'D': 'R'}
number of solutions: 12

Common mistakes

  • Treating CSP search as ordinary state-space search and ignoring the early constraint checks.
  • Considering all orderings of assignment; order does not matter for the final solution.
  • Not backtracking when a variable has an empty domain.
  • Forgetting that constraints can involve more than two variables.

Key takeaways

  • CSP = variables + domains + constraints; solution = complete consistent assignment.
  • Backtracking assigns one variable at a time and undoes failures.
  • Heuristics: MRV, degree, least constraining value.
  • Forward checking and arc consistency prune domains early.

Practice questions

  1. Define a CSP.
  2. What does the MRV heuristic do?
  3. What is forward checking?

Answers to the practice questions

  1. A problem of variables with domains and constraints, solved by a complete assignment satisfying all constraints.
  2. It chooses the variable with the fewest remaining legal values first.
  3. Removing from neighbouring variables' domains the values inconsistent with the latest assignment.

Sources

  • Official BCA syllabus Artificial Intelligence, course code 26BCA404DS04, Unit II