The following pseudo-code illustrates the AC-3 algorithm which enforces arc-consistency on a binary CSP or returns failure if it is discovered that the CSP cannot be satisfied.
//This is the top-level function that takes in a CSP.
//The CSP contains the variables, domains, and constraints
AC-3(CSP)
queue ← {}
for all constraints (X,Y) in CSP
queue.push((X,Y))
end
while queue not empty
(Xi, Xj) ← queue.pop()
if REMOVE-VALUES(Xi, Xj)
if Di is empty
// There is no way to satisfy this CSP
return FAILURE
end
//Modifying the domain of Xi necessitates re-queueing all incoming arcs
for each arc of the form (Xk, Xi)
queue.push((Xk, Xi))
end
end
end
end
//Makes X arc-consistent with respect to Y
REMOVE-VALUES(X, Y)
modified = false
for each p in DX
//Try to find a value in DY that satisfies the constraint
found = false
for each q in DY
if (p,q) satisfies the constraint on (X,Y)
found = true
break
end
end
if !found
remove p from DX
modified = true
end
end
return modified
end