Professional Documents
Culture Documents
Topics
What is Backtracking
N-Queens Problem
Sum of Subsets
Graph Coloring
Hamiltonian Circuits
Other Problems
2
Algorithm Design
Human
Problems Result
Computer
Algorithms
3
Algorithm Design …
For a problem? What is an Optimal Solution?
4
Algorithm Design …
For a problem? What is an Optimal Solution?
a2 a4 a3
(5 comparisons)
a2 a3 a1 a3
a4
5
a2 a3 or a1
Backtracking Problems
N-Queens problem.
6
Backtracking
Suppose you have to make a series of
decisions, among various choices, where
– You don’t have enough information to know what
to choose
– Each decision leads to a new set of choices
– Some sequence of choices (possibly more than
one) may be a solution to your problem
Backtracking is a methodical way of trying out
various sequences of decisions, until you find
one that “works”
7
Introduction
? dead end
dead end
dead end
?
start ? ? dead end
dead end
?
success!
13
N-Queens Problem
14
The 8-Queens Example
15
0 1 2 3 4 5 6 7
0
1
2
3
4
5
6
7
16
A t ( 4 , 4 ) a t t a c k f r o m ( 0 ,0 ) A t ( 5 ,4 ) a t t a c k f r o m ( 2 ,1 ) A t ( 6 , 4 ) a t t a c k f r o m ( 4 ,2 ) A t ( 7 ,4 ) s u c c e s s
0 1 2 3 4 5 6 7
0
1
2
3
4
5
6
7 17
Let’s look at it run
18
Terminology I
21
The backtracking algorithm
23
Sum-of-Subsets problem …
24
Example
25
Tree Space
Visualize a tree in which the children of the root
indicate whether or not value has been picked (left is
picked, right is not picked)
Sort the values in non-decreasing order so the
lightest value left is next on list
Weight is the sum of the weights that have been
included at level i
Let weight be the sum of the weights that have been
included up to a node at level i. Then, a node at the
ith level is nonpromising if
weight + wi+1 > W
26
Sum-of-Subsets problem …
27
Full example: Map coloring
int map[][]; 6
5
void createMap() {
map = new int[7][];
map[0] = new int[] { 1, 4, 2, 5 };
map[1] = new int[] { 0, 4, 6, 5 };
map[2] = new int[] { 0, 4, 3, 6, 5 };
map[3] = new int[] { 2, 4, 6 };
map[4] = new int[] { 0, 1, 6, 3, 2 };
map[5] = new int[] { 2, 6, 1, 0 };
map[6] = new int[] { 2, 3, 4, 1, 5 }; 30
}
Setting the initial colors
31
The main program
void printMap() {
for (int i = 0; i < mapColors.length; i++) {
System.out.print("map[" + i + "] is ");
switch (mapColors[i]) {
case NONE: System.out.println("none"); break;
case RED: System.out.println("red"); break;
case YELLOW: System.out.println("yellow"); break;
case GREEN: System.out.println("green"); break;
case BLUE: System.out.println("blue"); break;
}
} 35
}
Recap
We went through all the countries recursively, starting
with country zero
At each country we had to decide a color
– It had to be different from all adjacent countries
– If we could not find a legal color, we reported failure
– If we could find a color, we used it and recurred with
the next country
– If we ran out of countries (colored them all), we
reported success
When we returned from the topmost call, we were done36
Hamiltonian Circuits Problem
37
State Space Tree
3 15 4 10
12 5 19
A B D H
6 5 4
22
E G
39
Backtracking Algorithms
40
Backtracking Algorithms(1,1)C
0 1 2
0
x (0,0)H (0,1)H (0,2)H (1,0)H...
2
x (0,1)H, (1,0)H, …, (2,2)H
: Computer
x : Human
The tic-tac-toe game
(0,1)C, (1,0)C, (1,2)C, (2,0)C...
41
Backtracking Algorithms
43
Backtracking Search
(3 variables)
Assignment = {}
44
Backtracking Search
(3 variables)
X1
v11
Assignment = {(X1,v11)}
45
Backtracking Search
(3 variables)
X1
v11
X3
v31
X1
v11
Then, the search algorithm
X3 backtracks to the previous
variable and tries another value
v31
X2
Assume that no value of X2
leads to a valid assignment
X1
v11
X3
v31 v32
X2
X1
The search algorithm
backtracks to the previous
v11 variable (X3) and tries
another value. But assume
X3 that X3 has only two
possible values. The
v31 v32
algorithm backtracks to X1
X2 X2
Assume again that no value of
X2 leads to a valid assignment
X1
v11 v12
X3
v31 v32
X2 X2
Assignment = {(X1,v12)}
50
Backtracking Search
(3 variables)
X1
v11 v12
X3 X2
X2 X2
X1
v11 v12
X3 X2
X1
v11 v12
X3 X2
X2 X2 X3
v32
X1
v11 v12
X3 X2
X1
v11 v12
X3 X2
Backtracking Algorithm
CSP-BACKTRACKING(A)
1. If assignment A is complete then return A
2. X select a variable not in A
3. D select an ordering on the domain of X
4. For each value v in D do
a. Add (Xv) to A
b. If A is valid then
i. result CSP-BACKTRACKING(A)
ii. If result failure then return result
5. Return failure
Call CSP-BACKTRACKING({})
56
Map Coloring
{}
WA=red WA=red
NT=green NT=blue
NT
Q
WA
WA=red WA=red
NT=green NT=green SA NSW
Q=red Q=blue V
T 57
Chapter Summary