Artificial Intelligence A Modern Approach (3rd Ed.): Figure 6.5, Page 215.
function BACKTRACKING-SEARCH(csp) returns a solution, or failure return BACKTRACK({ }, csp) function BACKTRACK(assignment, csp) returns a solution, or failure if assignment is complete then return assignment var = SELECT-UNASSIGNED-VARIABLE(csp) for each value in ORDER-DOMAIN-VALUES(var, assignment, csp) do if value is consistent with assignment then add {var = value} to assignment inferences = INFERENCE(csp, var, value) if inferences != failure then add inferences to assignment result = BACKTRACK(assignment, csp) if result != failure then return result remove {var = value} and inferences from assignment return failure
Figure 6.5 A simple backtracking algorithm for constraint satisfaction problems. The algorithm is modeled on the recursive depth-first search of Chapter 3. By varying the functions SELECT-UNASSIGNED-VARIABLE and ORDER-DOMAIN-VALUES, we can implement the general-purpose heuristic discussed in the text. The function INFERENCE can optionally be used to impose arc-, path-, or k-consistency, as desired. If a value choice leads to failure (noticed wither by INFERENCE or by BACKTRACK), then value assignments (including those made by INFERENCE) are removed from the current assignment and a new value is tried.
@author Ruediger Lunde