37-39 Backtracking Algorithms
37-39 Backtracking Algorithms
37-39 Backtracking Algorithms
• Some sequence of choices (possibly more than one) may be a solution to your problem
?
dead end
dead end
?
start ? ?
dead end
dead end
success!
4
Terminology
A tree is composed of nodes
5
Backtrack Algorithm
1 2 3 4
1 queen 1
2 queen 2
3 queen 3
4 queen 4
We start with the empty board and then place queen 1 in the first possible
position of its row, which is in column 1 of row 1.
Then queen 3 is placed at (3, 2), which proves to be another dead end. The
algorithm then backtracks all the way to queen1 and moves it to (1, 2).
Queen2 then goes to (2, 4), queen3 to (3, 1), and queen4 to (4, 3) which is a
solution to the problem.
Portion of the Permutation
Tree (State Space Tree)
1 2
1
2 18
4
2 4 1 3
3
3 8 13 19 24 29
2 4 3
2 1
9 11 14 16 30
3 3
15 X = {2,4,1,3} 31
Solutions
Leaf 31 Leaf 39
Q Q
Q Q
Q Q
Q Q
{2,4,1,3} {3,1,4,2}
18
(b)The 8-Queens Problem
• Place 8 Queens on an 8 x 8 chessboard such that no two
can attack each other. There are about 4.4 billion trials.
19
The 8-Queens Problem (continued)
• Possible Solution: Q
X = (4 , 6 , 8 , 2 , 7 , 1 , 3 , 5) Q
Q
Q
Q
Q
Q
Q
20
The 8-Queens Problem Animation
21
(c)The n-Queens Problem
22
Graph Coloring Problem
Assignment of colors to the vertices or edges such that no two adjacent vertices are to be similarly
colored.
The smallest c such that a c-coloring exists is called the graph’s chromatic number and any such c-
coloring is an optimal coloring
M - colorability decision problem: Whether the nodes of G can be colored in such a way that no two
adjacent nodes have the same color yet only m colors are used.
M-colorability optimization problem: It asks for the smallest integer m for which the graph G can be
colored.
Coloring of Graph
3-colourable
Optimal Colouring
The state-space tree can be constructed as a binary tree like that shown
in next slide for the instance S={3, 5, 6, 7} and d=15.
The root of the tree represents the starting point, with no decisions
about the given elements made as yet.
with 5 3 w/o 5
0
with 5
w/o 5
8 3 5 0
with 6
w/o 6 with 6 with 6
w/o 6 w/o 6 x
14 8 9 3 11 5
0+13<15
x with 7 x x x x
14+7>15 w/o 7 9+7>15 3+7<15 11+7>15 5+7<15
15 8
solution x
8<15
Figure: Complete state-space tree of the backtracking algorithm applied to the instance S={3, 5, 6, 7} and
d=15 of the subset-sum problem. The number inside a node is the sum of the elements already included in
subsets represented by the node. The inequality below a leaf indicates the reason for its termination.
31
Subset-Sum Problem
Similarly, going to the left from a node of the first level corresponds to
inclusion of s2, while going to the right corresponds to its exclusion, and
so on.
Thus, a path from the root to a node on the ith level of the tree indicates
which of the first i numbers have been included in the subsets
represented by that node. We record the value of s‘, the sum of these
numbers, in the node.
If s‘ is equal to d, we have a solution to the problem. We can either report
this result and stop or, if all the solutions need to be found, continue by
backtracking to the node’s parent.
If s‘ is not equal to d, we can terminate the node as nonpromising if either
of the following two inequalities holds:
s‘ + si+1 > d (the sum s’ is too large)
n
s’ + ∑ sj < d (the sum s’ is too small).
j=i+1
32
Subset-Sum Problem
33