CH-7
CH-7
1 2 3 4 5 6 7 8
1 Q
2 Q
3 Q
4 Q
5
6
7
8
The Eight Queens problem
• Diagonal conflict can be checked using following formula,
• Let, P1=(i, j), P2=(k, l)
• Here P1 and P2 positions are on same diagonal, if
• i+j = k+l OR i-j =k-l
1 2 3 4 5 6 7 8
1 Q
2 Q
3 Q
4 Q
5 Q
6 Q
7 Q
8 Q
The knapsack problem using backtracking
• Problem Statement:
• If we are given n objects a knapsack or a bag in which the object i with
weight wi is to be placed.
• The knapsack has capacity M.
• The profit value that can be earned is Vi.
• Then objective is to obtain filling up of knapsack with maximum profit
earned, but it should not exceed the capacity m of knapsack.
• Here also we can not divide objects in different parts.
Steps to solve knapsack problem using backtracking
• Arrange objects m non-decreasing order (i.e. Vi/Wi)
• Find upper bound value.
• Draw state space tree.
• Follow DFS (Depth First Search)
• Explore node based on bounding function (upper bound)
x4=1 x4=0
1 0 1 0
cv=100 cw=17 cv=90 cw=12
Branch and Bound method
• In branch and bound method a state space tree is built and all the children of E
nodes (a live node whose children are currently being generated) are generated
before any other node can become a live node.
• For exploring new nodes either a BFS or D-search technique can be used.
• In Branch and bound technique, BFS-like state space search will be called FIFO
(First In First Out) search. This is because the list of live node is First In First
Out list (queue). On the other hand the D-search like state space search will be
called LIFO search because the list of live node is Last in First out list (stack).
• In this method a space tree of possible solutions is generated. Then
partitioning(called as branching) is done at each node of the tree. We compute
lower bound and upper bound at each node. This computation leads to selection
of answer node.
• Bounding functions are used to avoid the generation of subtrees that do not
contain an answer node.
Travelling salesman problem
• Problem Statement:
• If there are n cities and cost of travelling from any city to any other city is
given.
• Then we have to obtain the cheapest round trip such that each city is visited
exactly once and then returning to starting city, complete the tour.
Condition:
1. No city is to be visited twice before the
tour of all cities is completed
2. Going from city i to i is not permitted.
1 2 3
1 ∞ 20 30
2 15 ∞ 16
3 3 5 ∞
Example
• Find the solution of the following travelling salesman problem using
dynamic approach and branch and bound approach.
Consider an instance for TSP is given by G as.
∞ 20 30 10 11
15 ∞ 16 4 2
3 5 ∞ 2 4
19 6 18 ∞ 3
16 4 7 16 ∞
Example
• State space tree
Dynamic Reduction
• Steps in dynamic reduction
1. Draw a state space tree with optimum cost at root node.
a) Full reduction (row minimization and column minimization)
1 0 3 0 0 ∞ 10 17 0 1
12 ∞ 11 2 0
0 3 ∞ 0 2
15 3 12 ∞ 0
11 0 0 12 ∞
Step-1:Draw a state space tree with optimum cost at root node.
• To find optimum cost we must know row minimization, column
minimization and full reduction.
• Full reduction
∞ 20 30 10 11 ∞ 10 17 0 1
15 ∞ 16 4 2 12 ∞ 11 2 0
∞ ∞
Fully
3 5 2 4 0 3 0 2
∞ ∞
Reduction
19 6 18 3 Matrix 15 3 12 0
16 4 7 16 ∞ 11 0 0 12 ∞
• Step-3: cost of 2 is
=optimum cost + reduced cost + old value of M[1][2]
column entries as ∞, also set M[j][i]= ∞.
• Step-2: obtain the cost of matrix for path ij by making ith row and jth
2 1
2
35
column entries as ∞, also set M[j][i]= ∞.
• Step-2: obtain the cost of matrix for path ij by making ith row and jth
25
2 1
4
3
2
35 4
3
53
25
column entries as ∞, also set M[j][i]= ∞.
• Step-2: obtain the cost of matrix for path ij by making ith row and jth
2 1
5
3 4
2
35 5
3 4
53
25 31
E-node
• Consider path 1,4,2 for node 6. make 1 st row and 4th
row to ∞. Set 2nd column to ∞.
And set M[4][1]=∞, M[2][1]= ∞.
Step-3: cost of 6 is
25 = optimum cost + reduced cost +
1 value of M[4][2] from original matrix
2
5 =25+0+3
3 4 =28
2
35 5
3 4
53
25 31
2
28 6
• Consider path 1,4,3 for node 7. make 1 st row and 4th
row to ∞. Set 3rd column to ∞.
And set M[4][1]=∞, M[3][1]= ∞.
Step-3: cost of 7 is
25 = optimum cost + reduced cost +
1 value of M[4][3] from original matrix
2
5 =25+13+12
3 4 =50
2
35 5
3 4
53
25 31
2 3
6 7
28 50
• Consider path 1,4,5 for node 8. make 1 st row and 4th
row to ∞. Set 5th column to ∞.
And set M[4][1]=∞, M[5][1]= ∞.
Step-3: cost of 8 is
25 = optimum cost + reduced cost +
1 value of M[4][5] from original matrix
2
5 =25+11+0
3 4 =36
2
35 5
3 4
53
25 31
2 3 4
6 7 7
28 50 36
E-node
• Consider path 1,4,2,3 for node 9. make 1 st row and 4th
row and 2nd row to ∞. Set 3rd column to ∞.
And set M[4][1]=∞, M[2][1]= ∞, M[3][1]= ∞.
Step-3: cost of 9 is
25 = optimum cost + reduced cost +
1 value of M[2][3] from original matrix
2
5 =28+13+11
3 4 =52
2
35 5
3 4
53
25 31
2 3 4
6 7 7
28 50 36
52 9
• Consider path 1,4,2,5 for node 9. make 1 st row and 4th
row and 2nd row to ∞. Set 5th column to ∞.
And set M[4][1]=∞, M[2][1]= ∞, M[5][1]= ∞.
Step-3: cost of 10 is
25 = optimum cost + reduced cost +
1 value of M[2][5] from original matrix
2
5 =28+0+0
3 4 =28
2
35 5
3 4
53
25 31
2 3 4
2 1
5
3 4
2
35 5
3 4
53
25 31
2 3 4
6 7 7
28 5 50 36
3