0% found this document useful (0 votes)
21 views16 pages

Adsa - Unit-4-Mic 23

Uploaded by

Manohar J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views16 pages

Adsa - Unit-4-Mic 23

Uploaded by

Manohar J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

UNIT – IV:

Backtracking: General Method, 8-Queens Problem, Sum of Subsets problem, Graph


Coloring.
Branch and Bound: The General Method, 0/1 Knapsack Problem, Travelling Salesperson
problem.
Questions
1.What is Backtracking. Explain general method of backtracking?
2. Explain the 8-Queens problem
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
4. What is graph coloring? Present an algorithm which finds all m colorings of a graph ?

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.What is Backtracking. Explain general method of backtracking?

Backtracking is a refinement of the brute force approach. It systematically searches


for all possible solution to a problem.
It does so by assuming that the solutions are represented by vectors (v1, ..., vm) of values
and by traversing, in a depth first search(DFS) manner, the domains of the vectors until the
solutions are found.
When invoked, the algorithm starts with an empty vector. At each stage it extends the
partial vector with a new value.Upon reaching a partial vector (v1, ..., vi) which can’t
represent a partial solution, the algorithm backtracks by removing the trailing value from the
vector, and then proceeds by trying to extend the vector with alternative values.

General Method of Backtracking


The general method of backtracking follows these steps:
 Define the Solution Space
 Recursive Approach
 Backtracking
 Terminate or Return

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.

Example: In 4 queens problem, the implicit constraints are no 2 queens can be on


the same row, same column and same diagonal.

2. Explain the 8-Queens problem ?

The 8-Queens problem is a classic combinatorial problem in computer science and


mathematics. It involves placing 8 queens on an 8×8 chessboard such that no two queens can
attack each other. A queen can attack another if they are in the same row, column, or diagonal.
The task is to find all possible configurations or solutions that satisfy these conditions.
Objective: Place 8 queens on the board so that no two queens threaten each other.
Constraints:
 No two queens should share the same row.
 No two queens should share the same column.
 No two queens should share the same diagonal.
Using the Backtracking Method:
Backtracking is a systematic way to iterate through all possible configurations to find a solution.
The idea is to place queens one by one in different columns and backtrack when a conflict
occurs.
Steps for the Backtracking Algorithm:
Place the first queen in the first row and try placing it in each column one by one.
Check for conflicts:
 If placing the queen does not violate the constraints (i.e., no queen is attacked), move to
the next row and try placing the next queen.
 If a conflict occurs, backtrack by removing the queen and placing it in the next available
column.
Repeat the above steps for each row until either:
 A solution is found (all 8 queens are placed without conflicts).
 No valid placement is possible for a queen, prompting backtracking to the previous row
ADSAA UNIT-4 2
to change the placement.
Continue this process until all solutions are found or the search space is exhausted.

Fig) Tree organization of the 4-queen solution space


Edges from level 1 to level 2 nodes specify the values for x1. Thus, the left most sub-tree
contains all solutions with x1=1.
Edges from level i to level i+1 are labelled with the values of xi. The solution space is defined
by all paths from the root node to a leaf node. There are 4!=24 leaf nodes in the permutation
tree.
If we imagine the chess board squares being numbered as the indices of the two dimensional
array a[1..n,1..n] then we observe that every element on the same diagonal that runs from upper
left to lower right has the same row-col value.
R/C 1 2 3 4 5 6 7 8
1 Q
2 Q
3 Q
4 Q
5 Q
6 Q
7 Q
8 Q

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.

Step 1)Start with an empty set


Step 2)Add next element in the list to the sub set
Step 3)If the subset is having sum = m then stop with that sub set as
solution.
Step 4)If the sub set is not feasible or if we have reached the end of the set then
ADSAA UNIT-4 4
backtrack through the subset until we find the most suitable value.
Step 5)if the subset is feasible then repeat step 2
Step 6)if we have visited all elements without finding a suitable subset and if no
backtracking is possible, then stop with no solution.
s – sum of all selected elements
k – denotes the index of chosen element
r – initially sum of all elements. After selection ofsome element from the set
subtract the chosen value from r each time.
W(1:n) – represents set containing n elements.X[i]-solution vector
1<=i<=k

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]);

//generate right child and evaluate Bk. if ((s+r-

w[k]>=m) and (s+w[k+1]<=m)) then


{
X[k]:=0;
sumofsubsets(s,k+1,r-w[k]);
}
}
Time Complexity: O(2n) in the worst case due to the combinatorial nature.
Space Complexity: O(n) for the recursion stack and subset storage.
For every element there are two options only i.e include or not include ( 0/1).
Xi =1 means xi is included.
Xi = 0 means xi in not included.

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]

The values of solution vector may belongs to {0,1,2,..,m}.


The following Algorithm is used to generate next color.
Assume that X[1],..X[k-1] have been assigned integer values in the range [1,m]
such that adjacent vertices have distinct integers.
A value for x[k] is determined in the range [0, m].
X[k] is assigned the next highest numbered color while maintaining distinctness from the
adjacent vertices of vertex k. if no such color exists, then x[k]=0.
Algorithm nextvalue(k)
{
Repeat
{
X[k]=(x[k]+1)mod(m+1); // next highest color
if (x[k]=0) then
return; //all colors have been used
for j:=1 to n do
{
if ((G[k,j]!=0) and (x[k]=x[j])) then break;//g[k,j] an edge and
//vertices k and j have same color
}
if (j=n+1) then
return;
}
until (false);

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.

6.Explain 0/1 Knapsack problem using branch and bound.

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

L = 10 + 12 +x59 18 = 10 + 12 + 10 = 32, make it as -32


Next, we will calculate difference of upper bound and lower bound for nodes 2, 3
For node 2, U – L = -32 + 38 = 6
For node 3, U – L = -22 + 32 = 10
Choose node 2, since it has minimum difference value of 6.

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.

Procedure for solving traveling sale person problem:


1. Reduce the given cost matrix. A matrix is reduced if every row and column is reduced. A
row (column) is said to be reduced if it contain at least one zero and all-remaining entries are
non-negative. This can be done as follows:
a) Row reduction: Take the minimum element from first row, subtract it from all elements of
first row, next take minimum element from the second row and subtract it from second row.
Similarly apply the same procedure for all rows.
b) Find the sum of elements, which were subtracted from rows.
c) Apply column reductions for the matrix obtained after row reduction.
Column reduction: Take the minimum element from first column, subtract it from all
elements of first column, next take minimum element from the second column and subtract it
from second column. Similarly apply the same procedure for all columns.
d) Find the sum of elements, which were subtracted from columns.
e) Obtain the cumulative sum of row wise reduction and column wise reduction.
Cumulative reduced sum = Row wise reduction sum + column wise reduction sum.

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.

3. Repeat step 2 until all nodes are visited.


Example:
Find the LC branch and bound solution for the traveling sale person problem whose cost
matrix is as follows:

The above figure (a) is the initial cost matrix .


Figure (b) is the initial reduced matrix.

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

Among them minimum cost node is Node 2 with cost 28.


Explore node 2.
Possible ways from node 2 are node 3 & node 5.
C(2) = 52
C(5) = 28
Among the unexplored nodes minimum is 28 for node 5.
Next possible way from node5 is node 3.
C(3) = 28.
After reaching the leaf node update the upper limit with the minimum cost i.e 28.
So nodes whose cost is more than upper limit i.e 28 , can be cancelled. They need not to be
explored.
So the tour is 1 -> 4 -> 2 ->5 -> 3->1
The reduced matrices are represented below.

ADSAA UNIT-4 16

You might also like