putQueen(row) { For every position col on the same row If the position col is available Place the next queen in position col If(row<8) putQueen(row+1) else success remove the queen from positioncol } |
Step 1: start with an empty set Step 2: Add element from given list to the set Step 3: If the set is having sum M then stop with that subset as solution Step 4: if the set is not feasible then backtracks until we find the solution. Step 5: If the set is feasible and sum of subset < M then go to step 2 Step 6: if we have visited all numbers given in the set without finding a suitable solution and if no backtrack is possible then stop. |
Given set of numbers= {1,3,2,4} Find subsets with sum=3 Step 1: consider an empty set{ } Add 1 into empty set { 1 } (sum=1,1<3) Add 3 into set {1,3} (sum=4,4>3,backtrack) Remove 3 from subset { 1 } Add 2 into set { 1,2 } (sum=3,3=3,found solution) We remove 2 and check all elements have been considered. Solution: { } {1} {1, 3}(backtrack) {1} {1,2 }(found) {1}(end) ● Problem: Considering the n positive integer w1,... Wn, and S with a positive integer. Select all of the w1 subsets, ... Wn to S for that number. ● For example : N=3, w1=2, w2=4, w3=6, and S=6 ● Solutions: {2,4} and {6}, respectively, ● We will assume a space tree with a binary state. ● Nodes at depth 1 are intended to include (yes, no) item 1, and nodes at depth 2 are intended for item 2, etc. ● Wi contains the left branch, and wi is absent from the right branch. ● The nodes hold the sum of the weights so far included. Sum of subset Problem: State SpaceTree for 3 items W1 = 2, W2 = 4, W3 = 6 & S = 6 |
job | Machine | |||||
| 1 | 2 | . . . . . . | n | Source | |
1 | C11 | C12 | . . . . . . | C1n | 1 | |
2 | C21 | C22 | . . . . . . | C2n | 1 | |
– | . | . |
| . | – | |
– | . | . |
| . | – | |
– | . | . |
| . | – | |
n | Cn1 | Cn2 | . . . . . . | Cnn | 1 | |
Destination | 1 | 1 | 1 | . . . . . . | 1 |
|
Profit | 10 | 10 | 12 | 18 |
weight | 2 | 4 | 6 | 9 |
Place first item in knapsack calculate remaining weight ie. 15-2=13 Place next item in knapsack and remaining weight is 13-4=9 Place next item in knapsack and remaining weight is 9-6=3 Fraction is not allowed in calculation of upper bound in knapsack hence next item cannot be placed in the knapsack. Profit=p1+p2+p3=10+10+12=32 ie. Upper bound |
Fraction is allowed in calculation of lower bound in knapsack hence the next item can be placed in the knapsack. Lower bound=10+10+12+(3/9*18)=32+6=38 Knapsack is a maximization problem but branch and bound can be applied on minimization problems. Hence to convert maximization problem into minimization we have to take negative sign for upper bound and lower bound. Therefore upper bound (U)=-32 Lower bound(L)=-38 We choose the path which has minimized difference of upper bound and lower bound. If the difference is equal then choose the path by comparing upper bound and discard node with maximize upper node. Now calculate upper and lower bound for remaining nodes. For node 2, U=10+10+12=32 ≈ -32 L=10+10+12+(3/9*18)=32+6=38 ≈ -38 For node 3, U=10+12=22 L=10+12+(5/9*18)=32 ≈ -32 |
Similarly continues the process, Calculate difference of lower and upper bound of nodes 8 ,9. For node 8,U-L=-38+38=0 For node 9,U-L=-20+20=0 Here the difference is the same hence compare the upper bound of node 8 and 9. Discard the node with maximum upper bound. Consider the path from 1→ 2→4→7→8 The solution for 0/1 knapsack problem is (1,1,0,1) Maximum profit= ∑PiXi = 10*1+10*1+12*0+18*1=10+10+18=38 FIFO branch and bound solution Let's assume the above example in this problem. Initially node 1 is E node and the queue of live nodes are empty. Here U=-32 hence node 2 and 3 are generated as children of node 1 and added to the queue. The value of the upper bound remains unchanged. Now node 2 becomes E node and its children are added to the queue. Next node 3 is expanded and its children are generated. Node 6 is also added to the queue. Node 7 is killed as L(7)>U. |
● If path (i,j) is considered then change all entries in row i and column j of A to α. ● Set A(j,1) to α. ● Apply row reduction and column reduction except for rows and columns containing only α. Let r is the total amount subtracted to reduce the matrix. ● Find ĉ(S)= ĉ(R)+A(i,j)+r. ● Repeat step 2 until all nodes are visited |
∞ | 2 | 4 | 0 |
4 | ∞ | 2 | 0 |
1 | 0 | ∞ | 8 |
6 | 5 | 0 | ∞ |
1 | 0 | 0 | 0 |
∞ | 4 | 6 | 2 | 2 |
7 | ∞ | 5 | 3 | 3 |
2 | 1 | ∞ | 9 | 1 |
8 | 7 | 2 | ∞ | 2 |
Minimum value of each row is 2,3,1,2 respt. Total reduction=(2+3+1+2)+1=9 CA = 9 |
∞ | 2 | 4 | 0 |
3 | ∞ | 2 | 0 |
0 | 0 | ∞ | 8 |
5 | 5 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 2 | 0 |
0 | ∞ | ∞ | 8 |
5 | ∞ | 0 | ∞ |
We get same column after row & column reduction. Therefore reduction=0 Hence, CB=C[A,B]+CA+REDUCTION =2+9+0 =11 Let choose AB C[A,C]=4 AND CA=9 PUT A ROW∞ C COLUMN∞ C[C,A] ∞ |
∞ | ∞ | ∞ | ∞ |
3 | ∞ | ∞ | 0 |
∞ | 0 | ∞ | 8 |
5 | 5 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ |
3 | ∞ | ∞ | 0 |
∞ | 0 | ∞ | 8 |
0 | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ |
3 | ∞ | 2 | 0 |
0 | 0 | ∞ | 8 |
∞ | 5 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ |
1 | ∞ | 0 | ∞ |
0 | 0 | ∞ | ∞ |
∞ | 5 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ |
1 | ∞ | 0 | ∞ |
0 | 0 | ∞ | ∞ |
∞ | 5 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 0 | ∞ |
0 | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ |
1 | ∞ | ∞ | ∞ |
∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ |
0 | ∞ | ∞ | ∞ |
∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ |
0 | ∞ | ∞ | ∞ |
∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ |
Round up the result to the nearest integer if all the distances are integers: lb = [s/2] For instance, formula produces : lb = [ [(1+3) + (3+6) + (1+2) + (3+4) + (2+3)] / 2 ] = 14 By summing the lengths of the two shortest edges with each of the vertices, with the necessary inclusion of edges (a, d) and (d, a), we get the following lower bound:
[ [ (1+5) + (3+6) + (1+2) + (3+5) + (2+3) ] /2 ] = 16.
|
ub = v + (W - w) (vi+1 / wi+1)
|