Adsa - Unit-4-Mic 23
Adsa - Unit-4-Mic 23
5. State the concept of branch and bound method and also list its applications.
6. Explain 0/1 Knapsack problem using branch and bound.
7.Solve the Travelling Salesman problem using branch and bound algorithms
1. Define the Solution Space: Identify the structure of a potential solution and the choices
to be made at each step. This space may involve paths, sequences, or combinations of
elements.
2. Recursive Approach: Design a recursive function that tries to solve the problem by:
o Exploring a potential step or choice in the solution space.
o Checking if this choice is valid (i.e., doesn’t violate any constraints).
o If valid, recursively calling the function to continue building the solution.
o If a choice leads to a complete solution, it may be recorded.
o If it leads to a dead end, backtrack to the previous step and try an alternative.
3. Backtracking: If a particular choice fails (it either violates constraints or doesn’t lead
to a solution), undo the last decision and backtrack to the previous step. Then, try the
next possible option.
4. Terminate or Return: The recursion terminates when either:
o A complete solution is found (return the solution).
o All possible choices at each step have been tried without success (return failure).
Example Problem: N-Queens Problem, Sum of Subsets problem, 0/1 Knapsack, Graph
Coloring, etc..,
ADSAA UNIT-4 1
In backtracking technique,we will get optimal solution with less number of steps.
We can solve problems in an efficient way when compared to othermethods
like greedy method and dynamic programming. In backtracking we will use
bounding functions (criterion functions), implicit and explicit conditions.
The major advantageof backtracking method is, if a partial solution (x1,x2,x3…..,xi)
can’t lead to optimal solution then (xi+1…xn) solution may be ignored entirely.
Explicit constraints: These are rules which restrict each xi to take on values
only from a given set.
Example :
1) Knapsack problem, the explicit constraints are,
i) xi =0 or 1
ii) 0<=xi<=1
2) 4-queens problem : in 4 queens problem, the 4 queens canbe placed in
4x4 chess board in 44 ways.
Implicit constraints: These are rules which determine which of the tuples in the
solution space satisfy criterion function.
Algorithm place(k,i)
{
for j:=1 to k-1 do
if ((x[j]=i) or (abs(x[j]-i) = abs(j-k)) then
return false;
else
return true;
}
ADSAA UNIT-4 3
This algorithm is invoked by nqueens (1,n).
The algorithm for obtaining solution to n-queens problemis given below.
Algorithm nqueens(k,n)
{
for i:=1 to n do
{
if (place(k,i) then
{
X[k]:=i;
if (k=n) then
write(x[i:n);
else
nqueens(k+1,n);
}
}
}
For an 8x8 chess board there are 64C8 possible ways to place 8 Queens using
brute force approach. However by allowing only placements of queens on
distinct rows and columns, we require the examination of at most 8! Tuples.
Backtracking is a depth-first search (DFS) algorithm that tries to build a solution incrementally.
Pruning: If at any step a partial solution violates the constraints, the algorithm backtracks and
tries the next possible move.
Efficiency: Although backtracking may seem exhaustive, it prunes many impossible
configurations early, which helps reduce the number of total checks.
Time Complexity: O(N!) where N is the number of queens (8 in this case).
Space Complexity: O(N), due to the space needed to store the configuration.\
Note : This method ensures that all possible solutions are explored systematically.
3. State and explain the subset sum problem ? Consider the following Sum of Subsets
problem instance: n = 6, m = 30, and w[1:6] ={5, 10, 12, 13, 15, 18}. Find all possible subsets
of w that sum to m. Draw the portion of the state space tree that is generated.
Suppose we are given n distinct positive numbers (usually called weights) desire to find all
combinations of these numbers whose sum are m.This is called the sum of subsets problem.
The sum of sub set is based on fixed size tuple. Let us draw a tree structure for fixed
tuple size formulation.
All paths from root to a leaf node define a solution space. The left subtree of the root
defines all subsets containing Xi = 1 and the right subtree defines all subsetsnot
containing Xi=0 and so on.
Algorithm sumofsubsets(s,k,r)
{
X[k]:=1;
if (s+w[k]=m) then write (x[1:k]); // subset foundelse
if (s+w[k]+w[k+1]<=m) then
sumofsubsets(s+w[k],k+1,r-w[k]);
ADSAA UNIT-4 5
The rectangular nodes list the values of s,k and r. Circular nodes represent
points at which subsets withsums m are printed out. {5, 10, 12, 13, 15, 18}.
Solution A = (1,1,0,0,1,0)
Solution B = (1,0,1,1,0,0)
Solution C = (0,0,1,0,0,1)
Note that the tree contains only 23 rectangular nodes.The full space tree for n=6 contains 26-1=63
nodes from which calls could be made
- - - - - - - - - - -
For practice
Example2 : n=4, (w1,w2,w3,w4)=(7,11,13,24) and m=31
Solution Vector=(x[1],x[2],x[3],x[4])
Solution A = {1,1,1,0}
Solution B = {1,0,0,1}
4. What is graph coloring? Present an algorithm which finds all m colorings of a graph ?
Let G be a graph and m be a given positive integer. We want to discover whether the node of
G can be colored in such a way that no two adjacent nodes have the same color yet only m
colors are used.
ADSAA UNIT-4 6
This is termed the m-colorability decision problem. Note that if d is the degree of
the given graph, then it canbe colored with d+1 colors.
The m-colorability optimization problem asks for the smallest integer m for which the
graph G can be colored. The integer is referredto as the chromatic number of the graph.
The algorithm m-coloring was formed using the recursive backtracking schema.
The graph is represented by its Boolean adjacency matrix G[1:n,1:n].
All assignments of 1,2,…m to the vertices of the graph such that adjacent vertices are
assigned distinct integers are printed. K isthe index of the next vertex to color.
Algorithm mcoloring(k)
{
repeat
{
nextvalue(k);
if (x[k] = 0) then return;if
(k=n) then
write(x[1
:n]);else
mcoloring(k+1);
}until(false);
}
No of vertices= n
No of colors= m
Solution vector = X[1], X[2], X[3] ................................. X[n]
ADSAA UNIT-4 7
Example:
No of vertices= n
No of colors= m
5.State the concept of branch and bound method and also list its applications
Branch and Bound is another method to systematically search a solution space. Just like
backtracking, we will use bounding functions to avoid generating subtrees that do not contain
an answer node. It uses BFS method.
Branch-and-bound procedure requires two tools.
1. Branching:
A way of covering the feasible region by several smaller feasible sub-regions
(ideally, splitting into sub-regions).
Since the procedure may be repeated recursively to each of the sub regions
and all produced sub-regions naturally form a tree structure, called search
tree or branch-and-bound-tree.
Its nodes are the constructed sub-regions.
2. Bounding :
which is a fast way of finding upper and lower bounds for the optimal
solution within a feasible sub-region.
The Branch and bound technique like Backtracking explores the implicit graph and deals
with the optimal solution to a given problem. In this technique at each stage we calculate the
bound for a particular node and check whether this bound will be able to give the solution or
not. If we find that at any node the solution so obtained is appropriate but the remaining
solution is not leading to a best case then we leave this node without exploring.
ADSAA UNIT-4 8
Live node is a node that has been generated but whose children have not yet been generated.
Advantages :
1. In the algorithm of branch and bound it doesn’t explore the nodes in that tree.
Therefore the complexity of time of the algorithm of branch and bound is less as time-
consuming compared to other algorithms.
2. If in such a case the given problem is not very large and if it is possible to do the
branching of the nodes in a reasonable amount of time, then it finds an approximate
optimal solution for that particular problem.
3. The algorithm of branch and bound get a minimal path in order to reach the optimal
solution for that particular problem. Therefore it does not keep on repeating nodes
while it explores the tree.
Disadvantages :
1. This branch and bound algorithm are mostly time consuming. It depends on the size
of that particular problem and the number of particular nodes in that given tree can be
too large in its worst case.
2. Secondly, the process of parallelization is also too difficult in that bound and branch
algorithm.
Terminology:
E-node is a live node whose children are currently being explored. In other words, an E-node
is a node currently being expanded.
Dead node is a generated node that is not to be expanded or explored any further. All
children of a dead node have already been expanded.
Branch-an-bound refers to all state space search methods in which all children of an E-node
are generated before any other live node can become the E-node.
The adjective "heuristic", means" related to improving problem solving performance". As a
noun it is also used in regard to "any method or trick used to improve the efficiency of a
problem solving problem". But imperfect methods are not necessarily heuristic or vice versa.
"A heuristic (heuristic rule, heuristic method) is a rule of thumb, strategy, trick simplification
or any other kind of device which drastically limits search for solutions in large problem
spaces. Heuristics do not guarantee optimal solutions, they do not guarantee any solution at
all. A useful heuristic offers solutions which are good enough most of the time.
The backtracking based solution works better than brute force by ignoring infeasible
solutions. We can do better (than backtracking) if we know a bound on best possible solution
subtree rooted with every node. If the best in subtree is worse than current best, we can
simply ignore this node and its subtrees. So we compute bound (best solution) for every node
and compare the bound with current best solution before exploring the node.
ADSAA UNIT-4 9
Consider the instance:
M = 15, n= 4,
(P1, P2,P3,P4)=(10, 10, 12, 18) and
(w1, w2, w3, w4) = ( 2, 4, 6, 9).
0/1 knapsack problem can be solved by using branch and bound technique.
In this problem we will calculate lower bound and upper bound for each node.
Place first item W1 in knapsack. Remaining weight of knapsack is 15 – 2 = 13.
Place next item w2 in knapsack and the remaining weight of knapsack is 13 – 4 = 9.
Place next item w3 in knapsack then the remaining weight of knapsack is 9 – 6 = 3.
No fractions are allowed in calculation of upper bound so w4 cannot be placed in knapsack.
Profit = P1 + P2 + P3 = 10 + 10 +12
So, Upper bound = 32
To calculate lower bound we can place w4 in knapsack since fractions are allowed in
calculation of lower bound.
Lower bound = 10 + 10 + 12 + (X93 18) = 32 + 6 = 38
ADSAA UNIT-4 10
Knapsack problem is maximization problem but branch and bound technique is applicable for
only minimization problems. In order to convert maximization problem into minimization
problem 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 minimum difference of upper bound and lower bound. If the
difference is equal then we choose the path by comparing upper bounds and we discard node
with maximum upper bound.
Now we will calculate upper bound and lower bound for nodes 2, 3.
For node 2 , xi= 1, means we should place first item in the knapsack.
U = 10 + 10 + 12 = 32, make it as -32
L = 10 + 10 + 12 + x93 18 = 32 + 6 = 38, make it as -38
For node 3, xi = 0, means we should not place first item in the knapsack.
U = 10 + 12 = 22, make it as -22
Now we will calculate lower bound and upper bound of node 4 and 5. Calculate difference of
lower and upper bound of nodes 4 and 5.
ADSAA UNIT-4 11
For node 4, U – L = -32 + 38 = 6
For node 5, U – L = -22 + 36 = 14
Choose node 4, since it has minimum difference value of 6.
Now we will calculate lower bound and upper bound of node 8 and 9. Calculate difference of
lower and upper bound of nodes 8 and 9.
For node 6, U – L = -32 + 38 =6
For node 7, U – L = -38 + 38 =0
Choose node 7, since it is minimum difference value of 0.
Now we will calculate lower bound and upper bound of node 4 and 5. Calculate difference of
lower and upper bound of nodes 4 and 5.
For node 8, U – L = -38 + 38 =0
For node 9, U – L = -20 + 20 =0
Here the difference is same, so compare upper bounds of nodes 8 and 9. Discard the node,
which has maximum upper bound. Choose node 8, discard node 9 since, it has maximum
upper bound.
Consider the path from 1 ->2 ->4 ->7 ->8
The solution for 0/1 Knapsack problem is (x1, x2, x3, x4) = (1, 1, 0, 1)
Maximum profit is:
ADSAA UNIT-4 12
∑Pixi=10x1+10x1+12x0+18x1
= 10 + 10 + 18 = 38.
7. Solve the Travelling Salesman problem using branch and bound algorithms.
The Travelling Salesman Problem (TSP) is an optimization problem where a salesman
needs to visit a set of cities exactly once and return to the starting city, with the goal of
minimizing the total travel distance or cost.
The branch and bound algorithm can be used to solve this problem effectively by
systematically exploring and pruning the solution space.
The time complexity of traveling sale person problem using LC branch and bound is O(n22n))
which shows that there is no change or reduction of complexity than dynamic programming
method.
We start at a particular node and visit all nodes exactly once and come back to initial node
with minimum cost.
LetG=(V,E)is a connected graph. Let C(i,J) be the cost of edge <i,j>. cij= ∞ ,
If <i, j> Not€ E and let |V| = n, the number of vertices.
Every tour starts at vertex 1 and ends at the same vertex.
So, the solution space is given by S = {1, π, 1 | π is a permutation of (2, 3, . . . , n)}
and |S| = (n – 1)!.
The size of S can be reduced by restricting S so that (1, i1, i2, . . . . in-1, 1) €S
iff <ij, ij+1> €E, 0 <j <n - 1 and i0 = in=1.
ADSAA UNIT-4 13
Associate the cumulative reduced sum to the starting state as lower bound and ∞ as upper
bound.
2. Calculate the reduced cost matrix for every node R. Let A is the reduced cost matrix for
node R. Let S be a child of R such that the tree edge (R, S) corresponds to including edge <i,
j> in the tour. If S is not a leaf node, then the reduced cost matrix for S may be obtained as
follows:
a) Change all entries in row i and column j of A to∞.
b) Set A (j, 1) to ∞.
c) Reduce all rows and columns in the resulting matrix except for rows and column
containing only . Let r is the total amount subtracted to reduce the matrix.
d) Find c(S)=c(R) +A(i, j)+r, where “r‟ is the total amount subtracted to reduce the matrix,
(R)indicates the lower bound of the ith node in (i, j) path and c (S) is called the cost function.
ADSAA UNIT-4 14
Initial root node cost = Cost of Row reduction + Cost of Column reduction
= (10+2+2+3+4) + (1+3)
= 21 +4
=25
So the minimum cost of the tour may be 25 or more.
Initial upper bound= ∞. The upper bound will be updated after reaching the leaf node only.
Starting with the root node as E-node , nodes 2,3,4,5 are generated.
Consider the reduced matrices for further calculations.
From root node 1 the possible ways are noe1,3,4 & 5.
Cost of node2:
As we are checking form node1 to node 2, make 1st row and 2nd column as infinity in the 1st
reduced matric. We came from 1 to 2, so we don’t move back from 2 to 1, so make 2,1 as
infinity.
After making 1st row and 2nd column as infinity, check for matric reduction.
Cost(2)=C(1,2) + r + r^
10 + 25 + 0= 35
In the same way calculate C(3), C(4),C5)
C(3) = 53
C(4) = 25
C9 5) = 31
Explore the Minimum of them i.e C4) with cost 25.
From Node 4 we can move to node 2,3 & 5
ADSAA UNIT-4 15
Repeat the above process and find
C(2) = 28
C(3) = 50
C9 5) = 36
ADSAA UNIT-4 16