UNIT V
Backtracking
Backtracking
The General Method
1.Explicit constraints
2.Implicit constraints
Example: xi ≥ 0 or S={set of all non negative real numbers}
xi =0 or xi =1 or S={0,1}
(x1, x2, x3, x4)=(2,4,1,3)
Enumeration problems: find all possible solutions
Decision problems: is there any feasible solution or not
Optimization problems: whether there exits any best solution.
1.Live node: A node which has been generated but its children have not yet
been generated is called a “live node”
2.E-node:A live node whose children are currently being generated is
called a “E-node”.
3.Dead node: A node which is already expanded and there is no use for
future.
In other words, if we expand the node, if it gets the complete solution,
then it is called a promising node.
The n-Queens Problem
To solve the n×n board problems we have to follow the following constraints.
1.No two Queens are in the same row
2.No two Queens are in the same column
3.No two Queens are in the same diagonal
Example:
Let us take n=1 n=2
This is called a trivial solution
Let take n=3
Q1
1
1
Q1 | |
| |
1
2
1
2
no solution when n=2
Q1 | | |
| | Q3 |
| | |
1 2 3
1
2
3
No solution when n=3
Let us take when n=4
The solution vector is
(x1, x2, x3, x4)=(2,4,1,3)
Qi can be placed on ith row
When two coordinates (i,j) and (k,l) are diagonal elements then these two pairs satisfies the following condition.
|i-k|=|j-l|
Here the explicit constraints are Si ={1,2,3,4}
| | | |
| | | |
| | | |
| | | |
1 2 3 4
1
2
3
4
4 Queen problem: State space tree
8 Queen problem
| | | Q | | | | |
| | | | | Q | | |
| | | | | | | Q |
| Q | | | | | | |
| | | | | | Q | |
Q | | | | | | | |
| | Q | | | | | |
| | | | Q | | | |
1 2 3 4 5 6 7 8
Sum of subsets
xk =1
xk =0
Draw the state space tree for m=31 and w[1:4]={7,11,13,24}
0,1,55
7,2,48
0,2,48
x1 =1
x1 =0
18,3,37
7,3,37
x2 =1
x2 =0
31,4,24
18,4,24
x3 =1
x3 =0
42,5,0
18,5,0
x4 =1
x4 =0
Kill the node because 42>31
We are using bounding function
20,4,24
7,4,24
x3 =1
x3 =0
44,5,0
20,5,0
x4 =1
x4 =0
44>31
31,5,0
7,5,0
x4 =1
x4 =0
Solution 2
Solution 1
Right sub tree
0,2,48
11,3,37
0,3,37
x2 =1
x2 =0
24,4,24
11,4,24
x3 =1
x3 =0
48,5,0
24,5,0
x4 =1
x4 =0
35,5,0
11,5,0
x4 =1
x4 =0
13,4,24
0,4,24
x3 =1
x3 =0
Kill the node because 48>31
We are using bounding function
35>31
37,5,0
13,5,0
x4 =1
x4 =0
37>31
24,5,0
0,5,0
x4 =1
x4 =0
Graph Colouring
1.m-colourability decision problem
2.m-colourability optimization problem
a
b
c
d
e
Example:
a
b
c
Example: draw the state space tree for n=4 and m=3
Branch and Bound
Branch and Bound
The General Method
1.BFS is an FIFO search in terms of live nodes. List of live nodes is a
queue.
2.DFS is an LIFO search in terms live node. List of live nodes is a stack
3.Least cost search based on the minimum cost.
Example:
Here c^ is a ranking function
1
2
3
4
5
6
7
8
C^=2
C^=3
C^=4
C^=3
C^=2
C^=1
C^=5
1.Tree organization of solution space
2.Use of bounding functions to limit the search that is to avoid the
generation of subtrees that do not contain answer node
1.0/1 knapsack problem
2.Travelling Sales person problem
3.Job sequencing with deadlines
1.Lower bound(c^)
2.Upper bound(u^)
1.Lower bound(c^(x))
2.Upper bound(u^(x)) where x is a any node
3.Global Upper Bound(U^(x))
if a lower bound of a node c^(x) is greater than global upper bound then we
kill the node otherwise we have to expand the node
The Traveling salesman problem
where V is set of vertices and E is set of edges
The edges are given along with their cost Cij where Cij >0 for all i,j
cost(i,j) Cij if (i,j) ϵ E(G)
ꝏ if (i,j) ϵ E(G)
Let |V|=n
iff < ij, ij+1 > ϵ E , 0≤j≤n-1 and i0, in ϵ1
1.Substract from row: subtract t from each out going edge of i
2.Substract from column: Subtract t from each in coming edge j
Example: Solve the following TSP problem using LCBB.
If (R,S) is an edge
Cost(R)=sum of minimum value of each row + sum of minimum value from each column
Cost(R)=21+4=25
Finding the cost for node 2
Node 2 🡪include the edge <1,2>
1.Change all the entries in row ‘i’ and column ‘j’ to ꝏ (prevents use of any more vertex leaving ‘i’ or entering ‘j’ )
2.Set C1(j,1)=ꝏ (which prevents use of edge <j,1>
3.Reduce all rows and columns in resulting matrix except for rows and columns containing ꝏ
r=total subtracted in reduction (3)
The resultant matrix is C2 is
Finding the cost for node 3
Node 3-> include the edge <1,3>
Finding the cost for node 4
Finding the cost for node 5
Similarly find the cost for node 6,7,8 and expand the least cost node
Find the cost for node 6
3+25+0=28
Similarly find cost for node 7 and node 8
Next node to be expanded is node 6 since it has least cost
Next node to be expanded is node 6 since it has least cost
Similarly find cost for node 10,the cost(10)=28
Next node to be expanded is node 10 since it has least cost
Node 10 includes <2,5> edge
Step1:Make row 2 and column 5 as ꝏ
Step 2: C6 [5,1] as ꝏ
Step 3:Reduce rows and column
ꝏ ꝏ ꝏ ꝏ ꝏ
ꝏ ꝏ ꝏ ꝏ ꝏ
0 ꝏ ꝏ ꝏ ꝏ
11 ꝏ 0 ꝏ ꝏ
Cost(10)= C6 [2,5] + cost(parent) + r
=0+28+0=28
find cost for node 10,the cost(10)=28
Next node to be expanded is node 10 since it has least cost
Find cost for node 11,the cost(11)=28
The tour is 1-4-2-5-3-1
0/1 knapsack Problem
Notion of Branch-and-Bound
In 0/1 knapsack c’(x) means the maximum profit at that node(with fractions ).
In 0/1 knapsack u(x) means the maximum profit at that node (without fractions).
0/1 knapsack Problem-using Least Cost(LC) BB
1
c’(1)= -38
u (1)= -32
Consider the knapsack instance with n =4, (p1,p2,p3,p4)=(10,10,12,18) ,
(w1,w2,w3,w4) = (2,4,6,9) and m=15 .
Cost function - c’(x) - The profit obtained using the given instance (with fractions allowed)i.e; c’(1) =
items | Profit earned | Xi | weight | Remaining weight |
1 | 10 | 1 | 2 | 13(=15-2) |
2 | 20(=10+10*1) | 1 | 4 | 9 (=13-4) |
3 | 32(=20+12*1 | 1 | 6 | 3 (=9-6) |
4 | 38(=32+18*(3/9)) | 3/9 | 9 | 0 |
items | Profit earned | Xi | weight | Remaining weight |
1 | 10 | 1 | 2 | 13(=15-2) |
2 | 20(=10+10*1) | 1 | 4 | 9 (=13-4) |
3 | 32(=20+12*1 | 1 | 6 | 3 (=9-6) |
4 | 32(=32+18* 0) | 0 | 9 | 0 |
Improved upper bound - u(x) – The profit obtained using the given instance(without allowing fractions) i.e., u(1) =
Note: Since, object 4 cannot be placed in the bag ,so fraction
of it i.e., 3/9 can be placed .
upper = -32
1
c’(1)= -38
u (1)= -32
Consider the knapsack instance with n =4, (p1,p2,p3,p4)=(10,10,12,18) ,
(w1,w2,w3,w4) = (2,4,6,9) and m=15 .
c’(3) (with fractions allowed) =
items | Profit earned | Xi | weight | Remaining weight |
1 | 0 | 0 | 2 | 15 |
2 | 10(=0+10*1) | 1 | 4 | 11 (=15-4) |
3 | 22(=10+12*1) | 1 | 6 | 5 (=11-6) |
4 | 32(=22+18*(5/9)) | 5/9 | 9 | 0 |
items | Profit earned | Xi | weight | Remaining weight |
1 | 0 | 0 | 2 | 15 |
2 | 10(=0+10*1) | 1 | 4 | 11 (=15-4) |
3 | 22(=10+12*1) | 1 | 6 | 5 (=11-6) |
4 | 22(=22+18* 0) | 0 | 9 | 5 |
u(3) (without allowing fractions) =
Note: Since, object 4 cannot be placed in the bag ,so fraction
of it i.e., 5/9 can be placed .
2
3
x1=1
x1=0
c’(3)= -32
u(3)= -22
c’(2)= -38
u (2)= -32
which node to expand?
Node 2 is expanded. Since , c’(2)<c’(3)
upper = -32
1
c’(1)= -38
u (1)= -32
Consider the knapsack instance with n =4, (p1,p2,p3,p4)=(10,10,12,18) ,
(w1,w2,w3,w4) = (2,4,6,9) and m=15 .
c’(5) =
items | Profit earned | Xi | Weight | Remaining weight |
1 | 10 | 1 | 2 | 13(=15-2) |
2 | 10(=10+10*0) | 0 | 4 | 13 |
3 | 22(=10+12*1) | 1 | 6 | 7 (=13-6) |
4 | 36(=22+18*(7/9)) | 7/9 | 9 | 0 |
items | Profit earned | Xi | weight | Remaining weight |
1 | 10 | 1 | 2 | 13(=15-2) |
2 | 10(=10+10*0) | 0 | 4 | 13 |
3 | 22(=10+12*1) | 1 | 6 | 7 (=13-6) |
4 | 22(=22+18*0) | 0 | 9 | 7 |
u(5)=
Note: Since, object 4 cannot be placed in the bag ,so fraction
of it i.e., 7/9 can be placed .
2
3
x1=1
x1=0
c’(3)= -32
u (3)= -22
c’(2)= -38
u (2)= -32
which node to expand?
Node 4 is expanded. Since , c’(4)<{c’(3), c’(5)}
4
5
x2=1
x2=0
c’(4)= -38
u(4)= -32
c’(5)= -36
u(5)= -22
upper = -32
1
c’(1)= -38
u (1)= -32
Consider the knapsack instance with n =4, (p1,p2,p3,p4)=(10,10,12,18) ,
(w1,w2,w3,w4) = (2,4,6,9) and m=15 .
c’(7) =
items | Profit earned | Xi | Weight | Remaining weight |
1 | 10 | 1 | 2 | 13(=15-2) |
2 | 20(=10+10*1) | 1 | 4 | 9(=13-4) |
3 | 20(=20+12*0) | 0 | 6 | 9 |
4 | 38(=20+18*1) | 1 | 9 | 0(=9-9) |
items | Profit earned | Xi | weight | Remaining weight |
1 | 10 | 1 | 2 | 13(=15-2) |
2 | 20(=10+10*1) | 1 | 4 | 9(=13-4) |
3 | 20(=20+12*0) | 0 | 6 | 9 |
4 | 38(=22+18*1) | 1 | 9 | 0(=9-9) |
u(7)=
2
3
x1=1
x1=0
c’(3)= -32
u (3)= -22
c’(2)= -38
u(2)= -32
which node to be expand?
4
5
x2=1
x2=0
c’(4)= -38
u(4)= -32
c’(5)= -36
u(5)= -22
6
7
x3=1
x3=0
c’(7)= -38
u(7)= -38
c’(4)= -38
u(4)= -32
upper = -32
-38
1
c’(1)= -38
u (1)= -32
Consider the knapsack instance with n =4, (p1,p2,p3,p4)=(10,10,12,18) ,
(w1,w2,w3,w4) = (2,4,6,9) and m=15 .
c’(9) =
items | Profit earned | Xi | Weight | Remaining weight |
1 | 10 | 1 | 2 | 13(=15-2) |
2 | 20(=10+10*1) | 1 | 4 | 9(=13-4) |
3 | 20(=20+12*0) | 0 | 6 | 9 |
4 | 20(=20+18*0) | 0 | 9 | 9 |
items | Profit earned | Xi | weight | Remaining weight |
1 | 10 | 1 | 2 | 13(=15-2) |
2 | 20(=10+10*1) | 1 | 4 | 9(=13-4) |
3 | 20(=20+12*0) | 0 | 6 | 9 |
4 | 20(=20+18*0) | 0 | 9 | 9 |
u(9)=
2
3
x1=1
x1=0
c’(3)= -32
u (3)= -22
c’(2)= -38
u(2)= -32
4
5
x2=1
x2=0
c’(4)= -38
u(4)= -32
c’(5)= -36
u(5)= -22
6
7
x3=1
x3=0
c’(7)= -38
u(7)= -38
c’(4)= -38
u(4)= -32
8
9
c’(8)= -38
u(8)= -38
x4=1
x4=0
c’(9)= -20
u(9)= -20
upper = -38
1
c’(1)= -38
u (1)= -32
2
3
x1=1
x1=0
c’(3)= -32
u (3)= -22
c’(2)= -38
u(2)= -32
8 --- > 7 ---> 4 ---> 2 ----> 1
4
5
x2=1
x2=0
c’(4)= -38
u(4)= -32
c’(5)= -36
u(5)= -22
6
7
x3=1
x3=0
c’(7)= -38
u(7)= -38
c’(4)= -38
u(4)= -32
8
9
c’(8)= -38
u(8)= -38
x4=1
x4=0
c’(9)= -20
u(9)= -20
Solution to 0/1 knapsack problem using LCBB
upper = -38
1
c’(1)= -38
u (1)= -32
Consider the knapsack instance with n =4, (p1,p2,p3,p4)=(10,10,12,18) ,
(w1,w2,w3,w4) = (2,4,6,9) and m=15 .
2
3
x1=1
x1=0
c’(3)= -32
u(3)= -22
c’(2)= -38
u (2)= -32
4
5
6
7
x2=1
x2=0
x2=0
x2=1
c’(6) = -32
u(6) = -22
c’(7) = -30
u(7) = -30
c’(5) = -36
u(5) = -22
c’(4) = -38
u(4) = -32
8
9
x3=1
x3=0
c’(8) = -38
u(8) = -32
c’(9) = -38
u(9) = -38
10
11
12
13
x4=1
x4=0
x4=0
x4=1
c’(13) = -20
u(13) = -20
c’(12) = -38
u(12) = -38
upper = -32
-38
c’(11) = -32
u(11) = -32
Infeasible node . So ,it is killed
1 | | | | | | | | | | | |
Solution to 0/1 knapsack problem using FIFO BB
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 |
4 5
2 3
6 7
8 9
After node 9 is generated, upper is updated to -38. The nodes 5,6, and 7 are killed because the cost of 5, 6 and 7 are greater than upper.
11
12 13
Node 11 and 13 are killed because c’(11) and c’(13) are > upper
The only live node available is Node 12 , which is the answer node.
List of live nodes
Control abstraction for Least cost search
Introduction to Complexity classes
Introduction to Complexity classes
P (Polynomial Time):
NP (Nondeterministic Polynomial Time):
Is P = NP?
NP-Complete Problems
Within NP, there is a subset called NP-Complete problems, which are the "hardest" problems in NP:
The End