Chapter-5 Backtracking Algorithm
Chapter-5 Backtracking Algorithm
Backtracking Algorithms
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
Go left
Go right
Portion B
systematically trying and
eliminating possibilities.
A standard example of
backtracking would be going
through a maze.
◦ At some point in a maze,
you might have two options
of which direction to go:
Backtracking
One strategy would be to try
i on
going through Portion A of nc t
Ju
the maze.
If you get stuck before Portion B
you find your way out,
then you "backtrack" to
Portion A
the junction.
At
this point in time you
know that Portion A will
NOT lead you out of the
maze,
so you then start searching
in Portion B
Backtracking
Clearly,at a single junction
you could have even more
than 2 choices.
15
Solving a puzzle
? dead end
dead end
dead end
?
start ? ? dead end
dead end
?
success!
17
N-Queens Problem
18
The 8-Queens Example
19
0 1 2 3 4 5 6 7
0
1
2
3
4
5
6
7
20
A t (4,4) attack from (0,0) A t (5,4) attack from (2,1) A t (6,4) attack from (4,2) A t (7,4) success
0 1 2 3 4 5 6 7
0
1
2
3
4
5
6
7 21
Backtracking – Eight Queens Problem
Findan arrangement of 8
queens on a single chess
board such that no two
queens are attacking one
another.
In
dealing with a maze, to make sure you
don't try too many possibilities,
◦ one should mark which locations in the maze
have been visited already so that no location in
the maze gets visited twice.
◦ (If a place has already been visited, there is no
point in trying to reach the end of the maze from
Backtracking – homework
problem
Determine how many solutions
there are to the 5 queens
problem.
◦ Demonstrate backtracking for at
least 2 solutions to the 5 queens
problem, by tracing through the
decision tree as shown in class.
Let’s look at it run
31
Terminology I
36
Sum-of-Subsets problem …
37
Example
38
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)
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
39
nonpromising if weight + wi+1 > W
Sum-of-Subsets problem …
40
Full example: Map coloring
The Four Color Theorem states that any map on a plane can be
colored with no more than four colors, so that no two countries
with a common border are the same color
44
State Space Tree
45
Example
18
C F
3 15 4 10
12 5 19
A B D H
6 5 4
22
E G
46
Backtracking Algorithms
47
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...
48
Backtracking Algorithms
50
Backtracking Search
(3 variables)
Assignment = {}
51
Backtracking Search
(3 variables)
X1
v11
Assignment = {(X1,v11)}
52
Backtracking Search
(3 variables)
X1
v11
X3
v31
X1
v11
Then, the search algorithm
X3 backtracks to the previous
variable and tries another
v31 value
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
v11 previous variable (X3)
and tries another value.
X3 But assume that X3 has
only two possible values.
v31 v32
The 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)}
57
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({})
63
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 64
Chapter Summary