1
1
Chapter 7: Backtracking
Backtracking
• The desired solution must be expressible as n-tuple
(x1,x2,...,xn) where xi∈Si; Si is a finite set.
• Often the problem to be solved calls for finding
(x1, x2,...,xn) which satisfies (maximizes or minimizes)
criterion function P(x1,x2,...,xn).
2
Chapter 7: Backtracking
• Suppose mi is the size of set Si. Then there are m = m1,
m2,...,mn, n-tuples, which are possible candidates. The
backtrack algorithm, on an average, requires far less
than m trials.
• Buildup the vector, one component (x1, x2,...,xn) at a
time and use modified criterion functions Pi(x1,
x2,...,xn) (also called bounding functions) to test
whether the vector formed has any chance of success?
2
3
Chapter 7: Backtracking
• Realization that the partial vector (x1, x2,...,xi) can in no
way lead to optimal solution, then mi+1, mi+2, ..., mn
possible test vector can be ignored entirely.
• Many problems require solution to satisfy a complex set
of constraints. These constraints can be divided in to two
categories:
�Explicit
�Implicit
4
Chapter 7: Backtracking
• Explicit constraints are the rules which restrict each xi to
take value from a given set.
Ex. xi = 0 or 1 or Si = {0, 1}
li ≤ xi ≤ ui or Si = {α : li ≤ α ≤ ui}
• All tuples that satisfy explicit constraints define a
possible solution space.
3
5
Chapter 7: Backtracking
• Implicit constraints determine which of the tuples in the
solution space actually satisfy the criterion function.
“they describe the way in which the xi must be
related to each other.”
6
Chapter 7: Backtracking
• Ex. (8-Queens problem): Place 8 queens on the 8××××8
chess board so that no two queens attack each other, i.e.
no two of them are on the same row, column, or diagonal.
• Number rows and columns of chessboard 1,2,...,8. Since
each of them must be on different row, we assume that
queen i is places on row i.
• All solutions to 8-queens problem can now be represented
as 8-tuples (x1, x2,...,x8) where xi is the column on which
queen i is placed.
4
7
Chapter 7: Backtracking
• The explicit constraints using this formulation are:
Si = {1,2,...,8}, 1≤ i ≤ n
The solution space consist of tuples.
The implicit constraints for this problem are:
1. No two xi’s can be same (i.e., all queens must be placed
on different columns).
2. No two queens can be on the same diagonal.
88
8
Chapter 7: Backtracking
• The first of these constraints implies that all solutions are
permutations of 8-tuple (1, 2, 3, 4, 5, 6, 7, 8).
This realization reduces the size of the problem space
to 8! tuples.
5
9
Chapter 7: Backtracking
1 2 3 4 5 6 7 8
1
2
3
4
5
6
7
8
10
Chapter 7: Backtracking
1 2 3 4 5 6 7 8
1
2
3
4
5
6
7
8
6
11
Two solutions to the Eight Queens Problem
Chapter 7: Backtracking
12
Chapter 7: Backtracking
• Backtracking algorithms determines problem solutions by
systematically searching for the solution space for the
given problem instance.
• This search is often facilitated by a tree organization
(permutation tree) for the solution space.
7
13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
35 40 4524 29
34 50
51 56 61
25 27
23 26 28
30 32
31 33
36 38 41 43 46 48
37 39
52 54 57 59 62 64
42 44 47 49 53 55 58 60 63 65
x2=2 x2=3 x2=4 x2=1 x2=3 x2=4 x2=1 x2=2 x2=4 x 2=1 x2=2 x2=3
3 4 2 4 2 3 3 4 1 4 1 3 2 4 1 4 1 2 2 3 1 3 1 2
4 3 4 3 3 2 4 3 4 1 3 1 4 2 4 1 2 1 3 2 3 1 2 1
x1 = 1 x1 = 2 x1 = 3 x1 = 4
Tree organization of 4-queens solution space
Chapter 7: Backtracking
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
35 40 4524 29
34 50
51 56 61
25 27
23 26 28
30 32
31 33
36 38 41 43 46 48
37 39
52 54 57 59 62 64
42 44 47 49 53 55 58 60 63 65
x2=2 x2=3 x2=4 x2=1 x2=3 x2=4 x2=1 x2=2 x2=4 x 2=1 x2=2 x2=3
3 4 2 4 2 3 3 4 1 4 1 3 2 4 1 4 1 2 2 3 1 3 1 2
4 3 4 3 3 2 4 3 4 1 3 1 4 2 4 1 2 1 3 2 3 1 2 1
x1 = 1 x1 = 2 x1 = 3 x1 = 4
Tree organization of 4-queens solution space
Chapter 7: Backtracking
8
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
35 40 4524 29
34 50
51 56 61
25 27
23 26 28
30 32
31 33
36 38 41 43 46 48
37 39
52 54 57 59 62 64
42 44 47 49 53 55 58 60 63 65
x2=2 x2=3 x2=4 x2=1 x2=3 x2=4 x2=1 x2=2 x2=4 x 2=1 x2=2 x2=3
3 4 2 4 2 3 3 4 1 4 1 3 2 4 1 4 1 2 2 3 1 3 1 2
4 3 4 3 3 2 4 3 4 1 3 1 4 2 4 1 2 1 3 2 3 1 2 1
x1 = 1 x1 = 2 x1 = 3 x1 = 4
Tree organization of 4-queens solution space
Chapter 7: Backtracking
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
35 40 4524 29
34 50
51 56 61
25 27
23 26 28
30 32
31 33
36 38 41 43 46 48
37 39
52 54 57 59 62 64
42 44 47 49 53 55 58 60 63 65
x2=2 x2=3 x2=4 x2=1 x2=3 x2=4 x2=1 x2=2 x2=4 x 2=1 x2=2 x2=3
3 4 2 4 2 3 3 4 1 4 1 3 2 4 1 4 1 2 2 3 1 3 1 2
4 3 4 3 3 2 4 3 4 1 3 1 4 2 4 1 2 1 3 2 3 1 2 1
x1 = 1 x1 = 2 x1 = 3 x1 = 4
Tree organization of 4-queens solution space
Chapter 7: Backtracking
9
17
Chapter 7: Backtracking
• Terminology regarding tree organization of solution
space-
• Problem State: Each node in a tree defines a problem
state.
• State space: All paths from the root to other nodes define
the state space of the problem.
• Solution states: Solution states are those problem states
S for which the path from the root to S defines a tuple in
the solution space.
18
Chapter 7: Backtracking
• Answer states: Answer states are those problem states S
for which the path from the root to S defines a tuple
which is the member of the set of solutions (i.e. satisfies
implicit constraints).
• State space tree: Tree organization of the solution
space is state space tree.
• Once the state space tree has been conceived for any
problem, this problem may be solved systematically
generating the problem states, determining which of
these are solution states and finally determining which
solution states are answer states.
10
19
Chapter 7: Backtracking
• There are two different ways of generating the problem
states, both begin with the root node and generate other
nodes.
• Live node: A node which has been generated and all of
whose children have not been generated is called a live
node.
• Dead node: A dead node is a generated node that is not
to be expanded further, or one, for which all is children
have been generated.
20
Chapter 7: Backtracking
• E-node: The live node whose children are currently being
expanded is called the E-node.
• In both the method of generating the problem states, we
will have a list of live nodes.
11
21
Chapter 7: Backtracking
• First Method: As soon as a new child “C”, of a current
E-node “R”, is generated, the child will become a new E-
node. R will become E-node again when the subtree has
fully been explored. This corresponds to the depth-first
generation of the problem states.
• Second Method: In the second method of state
generation, E-node remains E-node until it is dead.
• In both methods bounding function is used to kill live
nodes without generating all their children.
22
Chapter 7: Backtracking
• Backtracking: Depth first generation with bounding
function is known as Backtracking.
• State generation method in which E-node remains E-
node until it is dead leads to Branch and Bound.
12
23
Chapter 7: Backtracking
• Ex. (4-queens problem): As a bounding function, we will
be using the criteria that if (x1, x2,..., xi) is a path to the
current E-node then all children nodes with parent-child
labelling xi+1 are such that (x1, x2,..., xi+1) represents a
chess-board configuration in which no two queens are
attacking each other.
• We start with the root node as only live node, this
becomes the E-node and the path is (). We generated one
child. Assume that the children are generated in the
ascending order, thus node number 2 in the solution space
is generated and path is now (1). This corresponds to
placing queen 1 on column 1. ........
24
Chapter 7: Backtracking
1 1
* * 2
Backtrack solution to the 4-queens problem
13
25
Chapter 7: Backtracking
1
* *
2
* *
1
* 3
2
Backtrack solution to the 4-queens problem
26
Chapter 7: Backtracking
1
3
2
1
Backtrack solution to the 4-queens problem
14
27
Chapter 7: Backtracking
1
* * * 2
1
2
3
4
Backtrack solution to the 4-queens problem
28
Chapter 7: Backtracking
Portion of the tree for 4-queens problems while solving it using backtracking
1
2
3 8 13
18
9 11 14 16
19 24 29
30
31
x1 = 1 x1 = 2
x2 = 2 x2 = 3 x2 = 4 x2 = 1 x2 = 3 x2 = 4
x3 = 2 x3 = 4 x3 = 2 x3 = 3
x4 = 3
x3 = 1
x4 = 3
B B B
B
B B B
15
15
29
Chapter 7: Backtracking
Formulation of the backtracking process
• We shall assume that all answer nodes are to be found
and not just one.
• Let (x1, x2,..., xi) is a path from the root to a node in the
state space tree.
• Let T(x1, x2,..., xi) be the set of all possible values for
xi+1 such that (x1, x2,..., xi+1) is also a path to a problem
state.
30
Chapter 7: Backtracking
• We shall assume the existence of the bounding functions
Bi+1(expressed as predicates) such that Bi+1(x1, x2,..., xi)
is false for a path (x1, x2,..., xi+1) from the root node to a
problem state only if path cannot be extended to reach
answer node.
• Thus, the candidates for position i+1 of the solution
vector x[1..n] are those values which are generated by T
and satisfy Bi+1.
• General backtracking schema can now be given which
makes use of T and Bi+1.
16
31
Chapter 7: Backtracking
procedure BackTrack (n:integer);
var k, n:integer;
x :array[1..n] of items;
begin
k := 1;
while (k > 0) do
begin
if there remains untried x[k] such that
x[k] ∈∈∈∈T(x[1],...,x[k-1]) and (Bk(x[1],...,x[k]) = true) then
begin
if (x[1],...,x[k]) is a path to an answer node then
print (x[1],...,x[k]);
32
Chapter 7: Backtracking
k := k+1; {consider the next set}
end
else
k := k-1 {backtrack to previous set}
end;
end.
17
33
Chapter 7: Backtracking
Global n :integer;
x :array[1..n] of items;
procedure RBackTrack (k:integer);
begin
for each x[k] such that
x[k]∈∈∈∈T(x[1],...,x[k-1]) and (Bk(x[1],...,x[k]) = true) then
begin
if (x[1],...,x[k]) is a path to an answer node then
print (x[1],...,x[k]);
RBackTrack(k+1);
end;
end.
34
Chapter 7: Backtracking
Note that when k exceeds n, T(x[1],...,x[k-1]) returns empty set
and hence for-loop is never entered; no recursive call.
The above recursive version is initially invoked by:
RBackTrack(1);
18
35
Chapter 7: Backtracking
• The main factors affecting efficiency of the
BackTrack program:
1. Time to generate the next x[k].
• The bounding function are regarded as good if they
substantially reduce the number of nodes that are
generated.
2. The Number of x[k] satisfying the explicit constraints.
3. Time for the bounding function Bi.
4. The Number of x[k] satisfying the Bi, for all i.
36
Chapter 7: Backtracking
• Once the state space tree organization is selected,
first three factors are relatively independent of
the problem instance being solved. Only fourth,
the number of nodes generated, varies from one
problem instance to another.
• A backtracking algorithm on one problem
instance might generate only O(n) nodes while
on a different instance might generate almost all
nodes in the state space tree.
19
37
Chapter 7: Backtracking
• Estimation of the number of nodes generated by the
backtracking algorithm:
• To estimate m from mi’s, we need to make an assumption
on the bounding function:
We assume that these functions are static, i.e. the
backtracking algorithm does not change its bounding
function as it gathers information during its execution.
Exactly the same function is used for all nodes on the
same level of the state space tree.
38
Chapter 7: Backtracking
• The number of unbounded nodes at level 2 is m1.
• we would expect each level 2 node to have m2 children
unbounded.
• This yields total of m1m2 nodes on level 3.
• In general expected number of unbounded nodes on level i
are m1m2...mi-1.
• Hence m, the number of unbounded nodes generated in
solving a given problem instance I is m = 1+m1+m1m2+....
20
39
Chapter 7: Backtracking
• Procedure Estimate is an algorithm to determine m. it
selects a path from the root of the state space tree.
• The function SIZE returns the size of the set Tk.
• The function CHOOSE makes a random choice of an
element in Tk.
• The desired sum is built using vaiables m and r.
40
Chapter 7: Backtracking
procedure Estimate(var m:integer);
var r, k :integer;
begin
m :=1; r:=1; k :=1;
Tk = {x[k]: x[k] ∈∈∈∈ T(x[1],...,x[k-1]) and (Bk(x[1],...,x[k])}
while (SIZE(Tk) ≠≠≠≠0) do
begin
r := r * SIZE(Tk); m := m+r;
X[k] := CHOOSE(Tk); k := k+1;
Tk = {x[k]: x[k] ∈∈∈∈T(x[1],...,x[k-1]) and (Bk(x[1],...,x[k])}
end;
end.
21
41
Chapter 7: Backtracking
=
8
64
• With the knowledge that no two queens are on the same
row, solution space is reduced to
88
• With the knowledge that no two queens are on the same
column, solution space is reduced to
!8
4.4 billion 8-tuples =
4400000000 tuples
= 1,67,77,216 8-tuples
= 40320 8-tuples
• On Chess board total possible ways in which 8
queens can be placed
88×
42
Chapter 7: Backtracking
n-Queens problem
• If we imagine the squares of the chess board being
numbered as indices of two dimensional array A[1..n,
1..n], then for every element on same diagonal running
from upper left to lower right, each element has same
“row-column” value.
• Similarly every element on the same diagonal running
from upper right to lower left has the same
“row+column” value. Suppose that two queens are placed
at (i, j) and (k, l) positions.
22
43
Chapter 7: Backtracking
• They are on the same diagonal iff
i - j = k - l
or i + j = k + l
or j - l = i - k
or j - l = k - i.
• function PLACE(k) returns true if kth queen can be placed
at x[k]. Its computing time is O(k).
44
Chapter 7: Backtracking
global x :array[1..n] of integer; n:integer;
function PLACE(k:integer):boolean;
var i :integer;
begin
PLACE := true;
for i := 1 to k-1 do
if (x[i] = x[k]) {two queens in the same column}
or (ABS(x[i] - x[k]) = ABS(i - k)) {in same diagonal}
then PLACE := false;
end.
23
45
Chapter 7: Backtracking
procedure NQUEENS(n:integer);
var k : integer;
x : array[1..n] of integer;
begin
x[1] := 0; k := 1; {k is the current row; x[k] is current column}
while(k>0) do {for all rows do}
begin
x[k]:= x[k]+1; {move to the next column}
while((x[k] ≤≤≤≤ n) and (not PLACE(k))) do
x[k] := x[k] +1; {can this queens be placed}
46
Chapter 7: Backtracking
if (x[k] ≤≤≤≤ n) then {a position is found}
if (k=n) then PRINT(x) {a solution is complete}
else begin {go to the next row}
k := k+1; x[k] := 0;
end
else k := k-1 {backtrack}
end
end.
24
47
Chapter 7: Backtracking
Sum of Subsets Problem:
• Given n+1 positive numbers: wi, 1≤ i ≤ n and M, find all
subsets of wi whose sum is M. For ex. If n = 4, (w1, w2,
w3, w4) = (11,13,24,7) and M = 31, then the desirable
subset are (11,13,7) and (24,7).
• If represented in terms of indices then the solution is
(1,2,4) and (3,4).
• In general the desired solution is k-tuples (x1, x2,..., xk)
1≤ i ≤ k and different solutions may have different size
tuples.
48
Chapter 7: Backtracking
Sum of Subsets Problem:
• The explicit constrainst require xi to be an integer,
explicit constraints are xi ={j | j is an integer and 1≤j≤n }
• The implicit constrints require that no two xi be the
same (avoid (1,2,4) and (1,4,2)) and the sum of the
corresponding wi be M.
• Since we wish to avoid generating multiple instances of
the same set, another implicit constrain imposed is xi <
xi+1 , 1≤ i < n.
25
49
Chapter 7: Backtracking
Sum of Subsets Problem:
Variable size tuple representation
50
Chapter 7: Backtracking
Sum of Subsets Problem:
• Another formulation of subset sum problem is
represented by an n-tuple (x1, x2, .., xn) such that xi ∈
{0,1}, 1≤i≤n. When xi = 0, wi is not chosen and when
xi = 1, wi is chosen.
• Hence in the example, solutions are (1,1,0,1) and
(0,0,1,1). This method expresses solution using fixed-
sized tuples. In both the cases, solution space consist of
2n distinct tuples.
26
51
Chapter 7: Backtracking
Sum of Subsets Problem: (BFS numbering)
Fixed size tuple representation
52
Example 1Suppose that n = 5, M = 21, and
w1 = 5, w2 = 6, w3 = 10, w4 = 11, and w5 = 16.
Find the solutions.
Chapter 7: Backtracking
W20
W3
W4W4
W3 0
0
0
0
11
21 11
22 11
5
155
16 5
W1
5
0
5
W5
21
W3
W2
W30
0
0
0
6
16 6
0
10 0
0
0
W40
10
W4
21
0
6
W4
17 11
0
6
W5
22
0
27
53
Example 2Suppose that n = 3, M = 6, and
w1 = 2, w2 = 4, w3 = 5.
Find the solutions.
W1
W3
0
W2
W3W3W3
W2
0
0
0 0
0
0
2
6
11 6
2
7 2
0
4
9 4
0
5 0
Chapter 7: Backtracking
54
Example 3Suppose that n = 4, M = 13, and
w1 = 3, w2 = 4, w3 = 5 , w4 = 6.
Find the solutions.
Sol:For the weights sorting in nondecreasing order,
W20
W3
W4
W3 0
0
07
12 7
13 7
3
83
W1
3
Chapter 7: Backtracking
a node is nonpromising if
weight+wi+1>M
where weight is the total
weight up to a node at
level i.
weight+totalr<M
is also nonpromising.
28
55
Problem: Given n positive weights and a positive integer M,
find all combinations of the weights that sum to M.
Inputs: positive integer n, sorted array w index from 1 to n,
and a positive integer M.
Output: all combinations of the weights that sum to M.
Chapter 7: Backtracking
56
Global M, n: Integer; W(1..n): real; X(1..n): boolean;
Procedure SUMOFSUBSET(s, k, r)
//find all subsets of W(1:n) that sums to M, //
// and , W(j) are in the ascending order. It is //
// assumed that W(1) ≤ M and //
Begin
//Generate left child. Note s+W(k)≤ M since Bk-1 = true//X[k] ←1;if (s+W[k]=M) //subset found//
then write(X[1..k]); // there is no recursive call here as// //W[j]>0,1≤j ≤ n//
Chapter 7: Backtracking
∑−
=
=
1
1
)()(k
j
jXjWs
∑=
=
n
kj
jWr )(
MiWn
i
≥∑=1
)(
29
57
else if (s+W[k]+W[k+1]≤ M) then //Bk = true//
SUMOFSUBSET (s+W[k], k+1,r- W[k]);
endif
endif
//generate right child and evaluate Bk //
if ((s+ r- W[k]>=M)and(s+ W[k+1]<=M)) then //Bk = true// begin
X[k]=0;SUMOFSUBSET (s, k+1, r- W[k]);
end
End
Call it with SUMOFSUBSET(0, 1, )
Chapter 7: Backtracking
∑=
n
ni
iW )(
58
Chapter 7: Backtracking
EXAMPLE:
Given n=6, M=30 and W(1…6)=(5,10,12,13,15,18).
• We have to generate all possible combinations of
subsets whose sum is equal to the given value M=30.
• In state space tree of the solution the rectangular node
lists the values of s, k, r,
• where ‘s’ is the sum of subsets,
• ‘k’ is the iteration and
• ‘r’ is the sum of elements after ‘k’ in the original set.
30
59
Chapter 7: Backtracking
STEP 1
• Initially the sum of subsets is 0 and 1 denotes the first
iteration and the sum of all elements in the subset (i.e.,
5+10+12+13+15+18=73).
s, r, k
0, 1, 73
M=30 and W(1…6)=(5,10,12,13,15,18).
60
Chapter 7: Backtracking
STEP 2
• In the next step left node is obtained by including the
first element i.e. 5. Assign X[1] = 1. After including
the first element, the sum of subset becomes 5
(0+5=5). ‘2’ refers to second iteration and 68 is the
sum of elements in the set excluding the first element
i.e. 73-5=68. The right child is obtained by not
including the first element i.e. X[1] = 0. s, r, k
0, 1, 73
X(1)=1
5, 2, 68
X(1)=0
0, 2, 68
W(1…6)=(5,10,12,13,15,18),
M=30
31
61
Chapter 7: Backtracking
STEP 3
• Similarly, the above procedure is continued at every
level by including or excluding next numbers. Every
(parent)node is branched into two indicating whether
the next element is to be included or not.
s, r, k
0, 1, 73
X(1)=1
5, 2, 68
X(1)=0
0, 2, 68
X(2)=1
15, 3, 58
X(2)=0
5, 3, 58
X(2)=1
10, 3, 58
X(2)=0
0, 3, 58
W(1…6)=(5,10,12,13,15,18).
W(1…6)=(5,10,12,13,15,18),
M=30
62
Chapter 7: Backtracking
• STEP 4
The same procedure is continued, and a node becomes a leaf
node under two conditions:
• when the sum of subset(s) after including the next element
becomes equal to M i.e. 30 for this example. This becomes
the answer state.
• when the sum of subset after including the next element is
greater than 30. In this case, it is not necessary to generate
the space tree after that node. (In the given example shown
below, the node 27,4,46 after including the fourth element
13 exceeds 30 hence this becomes the leaf node)
32
63
Chapter 7: Backtracking
15,3,58
s, r, k
0, 1, 73
X(1)=1
5, 2, 68
X(1)=0
0, 2, 68
X(2)=1
15, 3, 58
X(2)=0
5, 3, 58
X(2)=1
10, 3, 58
X(2)=0
0, 3, 58
X(3)=1
27, 4, 46
X(3)=0
15, 4, 46
X(3)=1
17, 4, 46
X(3)=0
5, 4, 46 12, 4, 46 0, 4, 4610, 4, 46
W(1…6)=(5,10,12,13,15,18)
M=30 and
64
Chapter 7: Backtracking
15,3,58
STEP 5: Thus the final state space tree is:
A
B
C
5, 4, 46
W(1…6)=(5,10,12,13,15,18)
M=30 and
33
65
Chapter 7: Backtracking
There are three answer states in the state space diagram. The
answer states are denoted by A,B,C. There are three solutions:
• The first solution is the path from the root to A. The
solution set X[ ] = [1 1 0 0 1 0] i.e. We include first,
second and fifth numbers to get the given sum M = 30. i.e.
5 + 10 + 15 = 30.
• The second solution is the path from the root to B. The
solution set X[ ]=[1 0 1 1 0 0]. We include first, third and
fourth numbers to get the given sum M = 30. i.e. 5 + 12 +
13 = 30.
66
Chapter 7: Backtracking
There are three answer states in the state space diagram. The
answer states are denoted by A,B,C. There are three solutions:
• The third solution is the path from the root to C. The
solution set X[ ]=[0 0 1 0 0 1]. We include third and sixth
numbers to get the given sum M = 30. i.e. 12 + 18 = 30
• Given M=30 and W[1…6]=[5,10,12,13,15,18], the
solutions are {1,1,0,0,1,0} , {1,0,1,1,0,0} , {0,0,1,0,0,1}.
34
67
Chapter 7: Backtracking
One can note that the last figure contains only 23 rectangular
nodes. The full state space tree for n=6 contains 26-1 = 63
nodes.
68
Global M, n: Integer; W(1..n): real; X(1..n): boolean;
Procedure SUMOFSUBSET(s, k, r)
//find all subsets of W(1:n) that sums to M, //
// and , W(j) are in the ascending order. It is //
// assumed that W(1) ≤ M and //
Begin
//Generate left child. Note s+W(k)≤ M since Bk-1 = true//X[k] ←1;if (s+W[k]=M) //subset found//
then write(X[1..k]); // there is no recursive call here as// //W[j]>0,1≤j ≤ n//
Chapter 7: Backtracking
∑−
=
=
1
1
)()(k
j
jXjWs
∑=
=
n
kj
jWr )(
MiWn
i
≥∑=1
)(
35
69
else if (s+W[k]+W[k+1]≤ M) then //Bk = true//
SUMOFSUBSET (s+W[k], k+1,r- W[k]);
endif
endif
//generate right child and evaluate Bk //
if ((s+ r- W[k]>=M)and(s+ W[k+1]<=M)) then //Bk = true// begin
X[k]=0;SUMOFSUBSET (s, k+1, r- W[k]);
end
End
Call it with SUMOFSUBSET(0, 1, )
Chapter 7: Backtracking
∑=
n
ni
iW )(
70
Chapter 7: Backtracking
Graph Coloring Problem:
• Let G be a graph and m be any given positive integer,
we want to discover if 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
decision problem).
• The m-colorability optimization problem asks for the
smallest integer m (chromatic number) for which graph
can be colored.
36
71
Chapter 7: Backtracking
• A graph is planner if it can be drawn in a plane in such a
way that no two edges crosses each other.
• 4 color problem asks if a map can be colored in such a
way that no two regions have the same color yet only four
colors are needed.
72
Chapter 7: Backtracking
A Map and corresponding Graph
4
1
3
2
5
24
37
73
Chapter 7: Backtracking
Global G[1..n, 1..n] of boolean;
m, n:integer; x[1..n] array of integer;
procedure MCOLORING(k:integer);
begin
loop {while (k >0) do}
NEXTVALUE(k); //assigns to x[k] a legal color//
if (x[k] = 0) then exit; //no new color possible//
if (k = n) then print(x) //at most m colors are assignedto n vertices//
else MCOLORING(k+1)
repeat
end.
74
Chapter 7: Backtracking
procedure NEXTVALUE(k:integer);
var j, m : integer ;
begin
j := 1;
while (j ≠ m) do
begin
x[k] := (x[k]+1)mod(m+1); //next highest color//
if (x[k] = 0) then exit; //all colors have been
exhausted//
38
75
Chapter 7: Backtracking
j :=1;
while(((G[k,j] = true) and (x[k] = x[j]))) and
(j<=n)) do
//check if this color is distinct from adjacent color//
j := j+1;
j := j-1;
end;
end;
The process is started by the call MCOLORING(1).
76
Chapter 7: Backtracking
1 2
4 3x1= 1 2 3
x2 = 2 3 1 3 1 2
x3= 1 3 1 2 2 3 1 2 2 3 1 3
x4 = 2 3 2 2 3 3 1 3 1 3 1 3 1 1 2 2 1 2
1 2
4 3
39
77
Chapter 7: Backtracking
Time Complexity
No. of internal nodes in the state space tree are where n
is the number of nodes and m is the colors. At each internal
node O(mn) time is needed by NEXTVALUE to determine the
children corresponding to legal colorings. Hence the total time
is bounded by = (n(mn+1-1)/(m-1)) = O(nmn)
∑−
=
1
1
n
i
im
∑=
n
i
ni
m
1
Exponential
78
End of Chapter 7
Chapter 7: Backtracking