Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Unit 4.

1
Branch and Bound
• Branch and Bound (B&B) is a systematic method for solving
optimization problem (Basically for minimization).
• B&B is a rather general optimization technique that applies
where the greedy method and dynamic programming fail.
• However, it is much slower. Indeed, it often leads to exponential
time complexities in the worst case.
• B&B is based on the principle that the total set of feasible
solution can be partitioned into smaller subsets can then be
evaluated systematically until the best solution is found.
• 1

2 3 4

3 4 2 4 2 3

4 3 4 2 3 2
Travelling Salesman Problem
• Travelling Salesman Problem (TSP): Given a set of
cities and distance between every pair of cities, the
problem is to find the shortest possible route that visits
every city exactly once and returns to the starting
point.
1->2->4->3->1
= 10+25+30+15
= 80

• The problem is a famous NP hard problem. There is no


polynomial time know solution for this problem.
• Travelling salesman problem can be solved using
following steps:-
(i) Perform Row minimization
(ii) Perform Column minimization
(iii) Obtain reduced cost
(iv) Using dynamic reduction technique, select a node
from state space tree with optimum cost. Thus by
expanding E-node at each level of state space tree,
reach to the leaf node with optimum cost of tour.
E-node is that node which is being expanded
Example:- Find the optimum cost of the given tour

1 2 3 4 5
1 ∞ 20 30 10 11
2 15 ∞ 16 4 2
3 3 5 ∞ 2 4
4 19 6 18 ∞ 3
5 16 4 7 16 ∞

Step:1- Row Minimization:- Find minimum of each row and


subtract the row minimum value from corresponding row
1 2 3 4 5 1 2 3 4 5
1 ∞ 20 30 10 11 - 10 1 ∞ 10 20 0 1
2 15 ∞ 16 4 2 -2 2 13 ∞ 14 2 0
3 3 5 ∞ 2 4 -2 3 1 3 ∞ 0 2
4 19 6 18 ∞ 3 -3 4 16 3 15 ∞ 0
5 16 4 7 16 ∞ -4 5 12 0 3 12 ∞
Step:1- Column Minimization:- Find minimum of each column
and subtract the column minimum value from corresponding
column. Consider the updated matrix
1 2 3 4 5 1 2 3 4 5
1 ∞ 10 20 0 1 1 ∞ 10 17 0 1
2 13 ∞ 14 2 0 2 12 ∞ 11 2 0
3 1 3 ∞ 0 2 3 0 3 ∞ 0 2
4 16 3 15 ∞ 0 4 15 3 12 ∞ 0
5 12 0 3 12 ∞ 5 11 0 0 12 ∞

-1 X -3 X X
X means ignore Total Cost == Cos t of Red_Row + Cos t of Red_Column
(10+2+2+3+4) + (1+3)
= 25 // It means all the tours in the original graph have a
length atleast 25

Note: If there is 0 in any row or column while calculating row or column


minimization then don’t perform subtraction or just ignore it.
25 1 2 3 4 5
1
1 ∞ 10 17 0 1
2 3 4 5 2 12 ∞ 11 2 0
3 0 3 ∞ 0 2
4 15 3 12 ∞ 0
Now find the total cost of each path from 1-node 5 11 0 0 12 ∞
Consider path 1,2. Make 1st row and 2nd column ∞ and set M[2][1]= ∞

1 2 3 4 5 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞ X 1 ∞ ∞ ∞ ∞ ∞
2 ∞ ∞ 11 2 0 X 2 ∞ ∞ 11 2 0
3 0 ∞ ∞ 0 2 X 3 0 ∞ ∞ 0 2
4 15 ∞ 12 ∞ 0 X 4 15 ∞ 12 ∞ 0
5 11 ∞ 0 12 ∞ X 5 11 ∞ 0 12 ∞
X X X X X
Total Cost = Optimum cost + Cost of Red_Row + Cos t of Red_Column + value of M[1][2] in last
level optimized cost matrix
= 25 + 0 + 0 + 10
= 35
1 2 3 4 5
1
25
1 ∞ 10 17 0 1
35 2 12 ∞ 11 2 0
2 3 4 5 3 0 3 ∞ 0 2
4 15 3 12 ∞ 0
5 11 0 0 12 ∞

Consider path 1,3. Make 1st row and 3rd column ∞ and set M[3][1]= ∞
1 2 3 4 5 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞ X 1 ∞ ∞ ∞ ∞ ∞
2 12 ∞ ∞ 2 0 X 2 12 ∞ ∞ 2 0
3 ∞ 3 ∞ 0 2
X 3 ∞ 3 ∞ 0 2
X 4 15 3 ∞ ∞ 0
4 15 3 ∞ ∞ 0
5 11 0 ∞ 12 ∞ X 5 11 0 ∞ 12 ∞
11 X X X X
Total Cost = Optimum cost + Cost of Red_Row + Cost of Red_Column + value of M[1][3] in last
level optimized cost matrix
= 25 + 0 + 11 + 17
= 53
1 2 3 4 5
1 25
1 ∞ 10 17 0 1
35 53 2 12 ∞ 11 2 0
2 3 4 5 3 0 3 ∞ 0 2
4 15 3 12 ∞ 0
5 11 0 0 12 ∞

Consider path 1,4. Make 1st row and 4th column ∞ and set M[4][1]= ∞
1 2 3 4 5 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞ X 1 ∞ ∞ ∞ ∞ ∞
2 12 ∞ 11 ∞ 0 X 2 12 ∞ 11 ∞ 0
3 0 3 ∞ ∞ 2
X 3 0 3 ∞ ∞ 2
X
4 ∞ 3 12 ∞ 0 4 ∞ 3 12 ∞ 0
5 11 0 0 ∞ ∞ X 5 11 0 0 ∞ ∞
X X X X X
Total Cost = Optimum cost + Cost of Red_Row + Cost of Red_Column + value of M[1][4] in last
level optimized cost matrix
= 25 + 0 + 0 + 0
= 25
1 2 3 4 5
1 25
1 ∞ 10 17 0 1
35 53 25 2 12 ∞ 11 2 0
2 3 4 5 3 0 3 ∞ 0 2
4 15 3 12 ∞ 0
5 11 0 0 12 ∞

Consider path 1-5. Make 1st row and 5th column ∞ and set M[5][1]= ∞
1 2 3 4 5 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞ X 1 ∞ ∞ ∞ ∞ ∞
2 12 ∞ 11 2 ∞ 2 2 10 ∞ 9 0 ∞
3 0 3 ∞ 0 ∞ X 3 0 3 ∞ 0 ∞
4 15 3 12 ∞ ∞ 3 4 12 0 9 ∞ ∞
5 ∞ 0 0 12 ∞ X 5 ∞ 0 0 12 ∞
X X X X X
Total Cost = Optimum cost + Cost of Red_Row + Cost of Red_Column + value of M[1][5] in last
level optimized cost matrix
= 25 + 5 + 0 + 1
= 31
1 25 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞
35 53 25 31 2 12 ∞ 11 ∞ 0
2 3 4 5
E-node 3 0 3 ∞ ∞ 2
4 ∞ 3 12 ∞ 0
2 3 5
5 11 0 0 ∞ ∞

Consider path 1- 4 - 2 Make 1st , 4th row and 4th , 2nd column ∞ and
set M[4][1]= ∞, M[2][1]= ∞
1 2 3 4 5 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞ X 1 ∞ ∞ ∞ ∞ ∞
2 ∞ ∞ 11 ∞ 0 X 2 ∞ ∞ 11 ∞ 0
3 0 ∞ ∞ ∞ 2 X 3 0 ∞ ∞ ∞ 2
4 ∞ ∞ ∞ ∞ ∞ X 4 ∞ ∞ ∞ ∞ ∞
5 11 ∞ 0 ∞ ∞ X 5 11 ∞ 0 ∞ ∞
X X X X X
Total Cost = Optimum cost + Cost of Red_Row + Cost of Red_Column + value of M[4][2] in last
= 25 + 0+ 0 + 3 level optimized cost matrix
= 28
1 25 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞
35 53 25 31 2 12 ∞ 11 ∞ 0
2 3 4 5
E-node 3 0 3 ∞ ∞ 2
2 28 3 5
4 ∞ 3 12 ∞ 0
5 11 0 0 ∞ ∞

Consider path 1-4-3 Make 1st , 4th row and 4th , 3rd column ∞ and
set M[4][1]= ∞, M[3][1]= ∞
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞ X 1 ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞ ∞ ∞ ∞
2 12 ∞ ∞ ∞ 0 X 2 12 ∞ ∞ ∞ 0 2 1 ∞ ∞ ∞ 0
3 ∞ 3 ∞ ∞ 2 2 3 ∞ 1 ∞ ∞ 0 3 ∞ 1 ∞ ∞ 0
4 ∞ ∞ ∞ ∞ ∞ X 4 ∞ ∞ ∞ ∞ ∞ 4 ∞ ∞ ∞ ∞ ∞
5 11 0 ∞ ∞ ∞ X 5 11 0 ∞ ∞ ∞ 5 0 0 ∞ ∞ ∞
11 X X X X
Total Cost = Optimum cost + Cost of Red_Row + Cost of Red_Column + value of M[4][3] in last
= 25 + 2+ 11 + 12 level optimized cost matrix
= 50
1 25 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞
35 53 25 31 2 12 ∞ 11 ∞ 0
2 3 4 5
E-node 3 0 3 ∞ ∞ 2
28 50 5 4 ∞ 3 12 ∞ 0
2 3
5 11 0 0 ∞ ∞

Consider path 1-4-5 Make 1st , 4th row and 4th , 5th column ∞ and
set M[4][1]= ∞, M[5][1]= ∞
1 2 3 4 5 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞ X 1 ∞ ∞ ∞ ∞ ∞

2 12 ∞ 11 ∞ ∞ 11 2 1 ∞ 0 ∞ ∞

3 0 3 ∞ ∞ ∞ X 3 0 3 ∞ ∞ ∞

4 ∞ 3 12 ∞ ∞
X 4 ∞ ∞ ∞ ∞ ∞

5 ∞ 0 0 ∞ ∞ X 5 ∞ 0 0 ∞ ∞
X X X X X
Total Cost = Optimum cost + Cost of Red_Row + Cost of Red_Column + value of M[4][3] in last
= 25 + 11 + 0 + 12 level optimized cost matrix
= 36
1 25 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞
35 53 25 31
2 3 4 5 2 ∞ ∞ 11 ∞ 0
3 0 ∞ ∞ ∞ 2
28 50 36
2 4 ∞ ∞ ∞ ∞ ∞
3 5
5 11 ∞ 0 ∞ ∞
3 5
Consider path 1-4-2-3. Make 1st , 4th, 2nd row and 2nd , 4th , 3rd column ∞ and
set M[4][1]= ∞, M[2][1]= ∞, M[3][1]= ∞
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞ X 1 ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞ ∞ ∞ ∞
2 ∞ ∞ ∞ ∞ ∞ X 2 ∞ ∞ ∞ ∞ ∞ 2 ∞ ∞ ∞ ∞ ∞
3 ∞ ∞ ∞ ∞ 2 2 3 ∞ ∞ ∞ ∞ 0 3 ∞ ∞ ∞ ∞ 0
4 ∞ ∞ ∞ ∞ ∞ X 4 ∞ ∞ ∞ ∞ ∞ 4 ∞ ∞ ∞ ∞ ∞
5 11 ∞ ∞ ∞ ∞ 5 0 ∞ ∞ ∞ ∞
5 11 ∞ ∞ ∞ ∞ X
11 X X X X
Total Cost = Optimum cost + Cost of Red_Row + Cost of Red_Column + value of M[2][3] in last
= 25 + 2 + 11 + 11 level optimized cost matrix
= 52
1 25 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞
35 53 25 31
2 3 4 5 2 ∞ ∞ 11 ∞ 0
3 0 ∞ ∞ ∞ 2
2
28 50 36
5 4 ∞ ∞ ∞ ∞ ∞
52 3
5 11 ∞ 0 ∞ ∞
3 5
Consider path 1-4-2-5. Make 1st , 4th, 2nd row and 2nd , 4th , 5th column ∞ and
set M[4][1]= ∞, M[2][1]= ∞, M[5][1]= ∞
1 2 3 4 5 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞ X 1 ∞ ∞ ∞ ∞ ∞
2 ∞ ∞ ∞ ∞ ∞ X 2 ∞ ∞ ∞ ∞ ∞
3 0 ∞ ∞ ∞ ∞ X 3 ∞ ∞ ∞ ∞ 0
4 ∞ ∞ ∞ ∞ ∞ X 4 ∞ ∞ ∞ ∞ ∞
5 11 ∞ ∞ ∞ ∞
5 ∞ ∞ 0 ∞ ∞ X
X X X X X
Total Cost = Optimum cost + Cost of Red_Row + Cost of Red_Column + value of M[2][5] in last
= 28 + 0 + 0 + 0 level optimized cost matrix
= 28
1 25 1 2 3 4 5
35 53 25 31 1 ∞ ∞ ∞ ∞ ∞
2 3 4 5 2 ∞ ∞ ∞ ∞ ∞
28 50 36 3 ∞ ∞ ∞ ∞ 0
2 3 5
52 28 4 ∞ ∞ ∞ ∞ ∞
3 5 5 11 ∞ ∞ ∞ ∞

3
Consider path 1-4-2-5-3. Make 1st , 4th, 2nd , 3rd row and 2nd , 4th , 5th , 3rd column ∞ and
set M[4][1]= ∞, M[2][1]= ∞, M[5][1]= ∞, M[3][1]= ∞
1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞
Hence the optimum cost of the
2 ∞ ∞ ∞ ∞ ∞ tour is 28
3 ∞ ∞ ∞ ∞ ∞
4 ∞ ∞ ∞ ∞ ∞
5 ∞ ∞ ∞ ∞ ∞

Total Cost = Optimum cost + Cost of Red_Row + Cost of Red_Column + value of M[5][3] in last
= 28 + 0 + 0 + 0 level optimized cost matrix
= 28
Backtracking Approach
• Backtracking is a technique based on algorithm to solve
problem. It uses recursive calling to find the solution by
building a solution step by step increasing values with time.
• It removes the solutions that doesn't give rise to the solution
of the problem based on the constraints given to solve the
problem.
• Backtracking algorithm is applied to some specific types of
problems:-
(i) Decision problem used to find a feasible solution of
the problem.
(ii) Optimization problem used to find the best solution that
can be applied.
(iii) Enumeration problem used to find the set of all feasible
solutions of the problem.
• In backtracking approach while solving a given
problem, a tree is constructed. Such a tree with all
possible solution is called State Space Tree
• In backtracking problem, the algorithm tries to find a
sequence path to the solution which has some small
checkpoints from where the problem can backtrack if
no feasible solution is found for the problem.

Green is the start point


blue is the intermediate point
red are points with no feasible
solution
dark green is end solution
Basic Approach behind Backtracking algorithm:-
Step 1 − if current_position is goal, return success
Step 2 − else
Step 3 − if current_position is an end point, return failed
Step 4 − else, if current_position is not end point, explore
and repeat above steps.
Queen’s Problem
• In N-Queen problem, we are given an NxN chessboard
and we have to place n queens on the board in such a
way that no two queens attack each other. A queen will
attack another queen if it is placed in horizontal, vertical
or diagonal points in its way.
• It can be seen that for n =1, the problem has a trivial
solution, and no solution exists for n =2 and n =3. So first
we will consider the 4 queens problem and then
generate it to n - queens problem.
Queen(n)
{
if (n==0)
return 1;
for(int i=0; i<n; i++)
{ // Checking if we can place a queen here or not, Queen will
for (int j=0; j<n; j++) not be placed if the place is being attacked or already
{ occupied
if ( (!attack(i, j) && (Board[i, j]!=1) )
{
Board[i, j] = 1;
if ( Queen(n-1)==1 ) // Using Recursion check whether we can put
{ return 1;} next queen with this arrangement or not
Board[i, j] =0;
}
}
}
}
int attack(int i, int j) // attack() checks whether Board[i, j] is under attack
{ by other queen or not
for(int k=0; k<n; k++)
{ if(Board[i, k]==1) || (Board[k, j]==1)
return 1;
}
for(k=0; k<n; k++) // Checking for diagonal
{
for(int m=0;m<n; m++)
{
if( ((k+m)==(i+j)) || ((k-m)==(i-j)) )
{
if (Board[k, m]==1)
return 1;
}
}
}
return 0;
}
Example:- 6 Queens Problem

You might also like