Lecture 07.1: Constraint Satisfaction Problems and Consistency
An introduction to CSPs, variables, domains, constraints, and enforcing arc consistency using the AC-3 algorithm.
While traditional search algorithms focus on planning (finding a sequence of actions to reach a goal), Constraint Processing focuses on assignment (identification). In these problems, the path to the goal is irrelevant; the goal itself—a complete, consistent assignment of values—is the only thing that matters.
A Constraint Satisfaction Problem (CSP) is a computational framework defined by three components:
- : A set of variables .
- : A set of domains , one for each variable.
- : A set of constraints that specify allowable combinations of values.
A state in a CSP is defined by an assignment of values to some or all variables.
- Consistent Assignment: An assignment that does not violate any constraints.
- Complete Assignment: Every variable is assigned a value.
- Solution: A complete and consistent assignment.
Varieties of Constraints
Constraints limit the values that variables can take:
- Unary constraints: Involve a single variable (e.g., ).
- Binary constraints: Involve pairs of variables (e.g., ).
- Higher-order constraints: Involve 3 or more variables (e.g., Crypt-arithmetic puzzles).
- Global constraints: Involve an arbitrary number of variables (e.g.,
Alldiffin Sudoku). - Preference (soft constraints): Represented by a cost for each assignment, turning the problem into a Constrained Optimization Problem (e.g., Jan prefers teaching in the morning).
Constraint Graphs
A binary CSP can be visualized as a Constraint Graph, where nodes are variables and arcs are constraints.

(In the Map Coloring problem for Australia, adjacent territories cannot share the same color. The graph above shows a valid assignment where connected nodes have different colors. Notice that T (Tasmania) is an independent subproblem!)
Inference and Enforcing Consistency
Before or during the search, we can use Constraint Propagation to eliminate inconsistent values, shrinking the domains of variables and radically reducing the search space.
1. Node Consistency
A variable is node-consistent if every value in its domain satisfies all unary constraints.
- Example: If domain of and constraint is , we reduce the domain to .
2. Arc Consistency
A variable is arc-consistent with another variable (denoted as arc ) if, for every value in the domain , there exists at least one value in the domain that satisfies the binary constraint between and . If a value lacks such "support" in , it is pruned from .
[!IMPORTANT] Arc Consistency is Directed: An arc being consistent does not guarantee that the reverse arc is consistent. We must check and enforce consistency in both directions.
Concrete Step-by-Step Example
Consider two variables and with the following domains and a binary inequality constraint:
- Domains: ,
- Constraint:
Let's evaluate the arc consistency for :
- For : We look for a supporting such that . Both and work. (Kept)
- For : We look for a supporting such that . The value works. (Kept)
- For : We look for a supporting such that . No value in satisfies this. (Removed!)
The revised domain of becomes .
The AC-3 Algorithm
The AC-3 algorithm (Mackworth, 1977) is the standard method for enforcing arc consistency across an entire constraint graph. It maintains a queue of arcs that need to be checked. Whenever a variable's domain is reduced, any constraints pointing into that variable must be re-evaluated, as their supporting values may have disappeared.
Pseudocode
function AC-3(csp) returns false if inconsistency is found, true otherwise
inputs: csp, a binary CSP with variables X, domains D, constraints C
local variables: queue, a queue of arcs, initially containing all arcs in csp
while queue is not empty do
(Xi, Xj) = REMOVE-FIRST(queue)
if REVISE(csp, Xi, Xj) then
if size of Di == 0 then
return false // Domain Wipe Out (DWO) - no solution exists
for each Xk in Xi.NEIGHBORS - {Xj} do
add (Xk, Xi) to queue
return true
function REVISE(csp, Xi, Xj) returns true iff we revise the domain of Xi
revised = false
for each x in Di do
if no value y in Dj satisfies the constraint between Xi and Xj then
delete x from Di
revised = true
return revisedWalkthrough: The Divisibility Constraint Problem (Rina Dechter's Example)
To see AC-3 in action, let's trace a network of three variables with two divisibility constraints:
- Constraints: divides and divides (i.e., and are integers).
- Initial Domains:
Let's initialize the queue of directed arcs:
- Process : We check if every has a multiplier in (such that divides ).
- : Supported by and ( and ). (Kept)
- : does not divide or . No support! We delete from .
- Domain update: .
- Propagation: Since changed, we add neighbors of (except ) pointing to back to the queue. The arc is already in the queue, so no new additions are needed.
- Process : We check if every has a divisor in .
- : Supported by (). (Kept)
- : does not divide . No support! We delete from .
- Domain update: .
- Propagation: Since changed, we must add to the queue (already there).
- Process : Check if every divides some . Yes, divides . (No change).
- Process : Check if every has a divisor in .
- : Supported by .
- : Supported by . (No change).
The queue is now empty. The algorithm terminates successfully, returning: This network is now completely arc-consistent!
The Arc Consistency Family: From AC-1 to AC-4
The evolution of arc consistency algorithms reflects a classic computer science trade-off between simplicity, time complexity, and memory overhead.
| Algorithm | Mechanism | Time Complexity | Space Complexity | Pros & Cons |
|---|---|---|---|---|
| AC-1 | Brute Force: If any variable's domain is revised during a pass over all arcs, it re-checks every single arc from scratch. | Simple but highly redundant; performs many useless constraint checks. | ||
| AC-2 | Variable-Based Queueing: Organizes checks by variable. When is revised, only arcs pointing into are queued. | More selective than AC-1, but superseded by the simpler arc-oriented AC-3. | ||
| AC-3 | Arc-Based Queueing: Maintains a queue of individual directed arcs. Only propagates updates along affected constraints. | Most popular: Excellent average-case performance and very low space requirements. | ||
| AC-4 | Support Lists & Counters: Pre-computes and stores how many supports each value has, updating counters dynamically. | Optimal worst-case time complexity, but high memory overhead and complex to implement. |
(Where is the number of variables, is the maximum domain size, and is the number of binary constraint arcs.)
Lecture 06.3: Monte Carlo Tree Search (MCTS)
Scaling adversarial search to massive state spaces using randomness and the UCB1 algorithm in Monte Carlo Tree Search.
Lecture 07.2: Backtracking and Local Search for CSPs
Exploring backtracking algorithms, intelligent heuristics (MRV, Degree, LCV), Forward Checking, and the Min-Conflicts Local Search.