Problem
Given an undirected graph with n nodes, given 3 colors, assign a color to each node so that no two adjacent nodes (with an edge between them) are the same color
Solution 1——Backtracking(more in charpter 2)
function Backtrack(γ,a) returns solution, or “inconsistent”
if a is inconsistent with γ then return “inconsistent”
if a is total then return a
select some variable v for which a is not defined
for each d in Dv in some order do
a′←a ∪ {(v,d)}
a′′ ← Backtrack(γ, a′)
if a′′ ̸= “inconsistent” then return a′
end
return “inconsistent”
Comments: The size of the search space depends on the order in which we choose variables and values.
Improvement
1.Variable ordering
· Common strategy: Choose most constrained variable (aka “first-fail”) ——Choose a variable with the smallest (consistent) domain.
· Most constraining variable——Involved in as many constraints as possible
· Seek biggest effect on domains of unassigned variables——Detect inconsistencies earlier, shortening search tree branches
· Random selection can also help, especially for tie-breaking
2.Value ordering
· Common strategy: least constraining value——Choose a value that won’t conflict much with others
·Minimise useless backtracking below current node
3.Inference
Inference in CSP solving: deducing additional constraints that follow from the already known constraints.
· Just once before search starts
· At every recursive call of backtracking
Solution2——Forward Checking
Definition: remove from domains any value not consistent with those that have been assigned.
Obviously sound: it does not rule out any solutions.
Can be implemented incrementally for efficiency: only necessary to consider v to be the variable which has just been assigned.
Forward checking is rather weak on its own, but it combines well with the first-fail heuristic( Choose most constrained variable) for variable ordering, to make a powerful technique.
n= number of variables
d = size of initial domains
s = maximum number of constraints involving a given variable (s ≤ n-1)
Forward checking takes O(nsd) time
Solution 3——Arc Consistency(AC-3)
A stronger inference rule: make all variables arc consistent.
Variable v is arc consistent with respect to another variable u iff for every d∈Dv there is at least one d′ ∈Du such that (d,d′)∈Cv.
Unlike forward checking, makes inferences from unassigned variables
In sum, arc consistency extends this to all variables, whether assigned or not. It is stronger than forward checking and unit propagation, but costs more to compute.
The difference between forward checking and arc consistency is that the former only checks a single unassigned variable at time for consistency, while the second also checks pairs of unassigned variables for mutual consistency.