Lecture 04 Backtracking Search
Lecture 04 Backtracking Search
Backtracking Search
(CSPs)
■ Chapter 5
5.3 is about local search which is a very
useful idea but we won’t cover it in class.
Q3 Q4
BT(Level)
If all variables assigned
PRINT Value of each Variable
RETURN or EXIT (RETURN for more solutions)
(EXIT for only one solution)
V := PickUnassignedVariable()
PickUnassignedVariable()
Variable[Level] := V
Assigned[V] := TRUE
for d := each member of Domain(V)
Value[V] := d
OK := TRUE
for each constraint C such that
V is a variable of C
and all other variables of C are assigned.
if C is not satisfied by the current set of assignments
OK := FALSE
if(OK)
BT(Level+1)
return
Search stops
Vj=1 Vj=2 descending if the
assignments on
path to the node
Subtree
violate a constraint
Hojjat Ghaderi, University of Toronto 18
Backtracking Search
● Heuristics are used to determine which variable
to assign next “PickUnassignedVariable”.
● The choice can vary from branch to branch,
e.g.,
■ under the assignment V1=a we might choose to
assign V4 next, while under V1=b we might choose to
assign V5 next.
● This “dynamically” chosen variable ordering
has a tremendous impact on performance.
■ Constrains:
Vi ≠ Vj for all i ≠ j (cannot put two Queens in same column)
|Vi-Vj| ≠ |i-j| (Diagonal constraint)
(i.e., the difference in the values assigned to Vi and Vj can’t
be equal to the difference between i and j.
Solution!
1 2 3
4 5 6
7
8
9
Variable has no
possible value,
but we don’t
detect this. Until
we try to assign it
a value
Q2=3 Q2=4
Solution!
● More intelligent: Simple Backiumping backtracks to the last variable among the set
of variables that caused the failure, called the conflict set. Conflict set of variable
V is the set of previously assigned variables that share a constraint with V. Can be
shown that FC is stronger than simple backjumping.