DSA-II Unit-4
DSA-II Unit-4
Noida
Unit: 4
4
Syllabus
1) Cost of reaching the node from the root (When we reach a node, we
have this cost computed)
• In this method we expand the node which is most promising means the
node which promises that expanding or choosing it will give us the
optimal solution.
Travelling
. Salesman Problem- Steps
• As we can see from above diagram , every node has a cost associated
to it.
• To get the lower bound of the path starting from the node , we reduce
each row and column in such a wat that there must be atleast one zero
in each row and column.
05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 11
Branch and Bound
• . For doing this, we need to reduce the minimum value from each
element in each row and column.
• . After reducing the row, we get the below matrix. We then reduce
the minimum value in each column from each element in that
column.
• The total expected cost at the root node is sum of all reductions.
• Cost=[10 2 2 3 4] + [1 0 3 0 0]= 25
• Since we have discovered the root node C0, the next node to be
expanded can be any node from C1,C2, C3, C4. Whichever node
has minimum cost, that node will be expanded further. So we have
to find out the expanding cost of each node.
.
3. We try to calculate lower bound of the path starting at node 1 using
above resulting cost matrix.
Cost= cost of node 0 + cost of the edge (0,1) + lower bound of the
path starting at node 1
= 25 + 10 +0= 35
• Now calculate lower bound of the path starting at node 2 using the
Approach discussed earlier. Resulting matrix will be-
Cost = Cost of node 0 + cost of the edge (0,2) + Lower bound of the
path starting at node 2
= 25 + 17 + 11= 53
• Now calculate lower bound of the path starting at node 3 using the
Approach discussed earlier.
• Lower bound of the path starting at node 3 is 0 as it is already in
reduced form, i.e. all rows and all columns have zero value.
Cost = Cost of node 0 + cost of the edge (0,3) + Lower bound of the
path starting at node 3
= 25 + 0 + 0= 25
• Now we find a live node with estimated cost. Live nodes 1,2,3 and 4
has costs 35, 53, 25 and 31 respectively.
• The minimum among them is node 3 having cost 25. So node 3 will
be expanded further as shown in state space tree diagram.
• After adding its children to list of live nodes, we again find a live
node with least cost and expand it.
• It removes the solutions that doesn't give rise to the solution of the
problem based on the constraints given to solve the problem.
For Example,
• Here, Green is the start point, blue is the intermediate point, red are
points with no feasible solution, dark green is end solution.
ALGORITHM
Step 1 − if current_position is goal, return success
Step 2 − else,
Output:
An array color[V] that should have numbers from 1 to m.
color[i] should represent the color assigned to the ith vertex.
N- Queens Problem
• It can be seen that for n =1, the problem has a trivial solution, and
no solution exists for n =2 and n =3.
N- Queens Problem
N- Queens Problem
• Now, we place queen q1 in the very first acceptable position (1, 1).
• Next, we put queen q2 so that both these queens do not attack each
other.
• Thus the first acceptable position for q 2 in column 3, i.e. (2, 3) but
then no position is left for placing queen 'q 3' safely.
N- Queens Problem
• So we backtrack one step and place the queen 'q 2' in (2, 4), the
next best possible solution. Then we obtain the position for
placing 'q3' which is (3, 2).
• But later this position also leads to a dead end, and no place is
found where 'q4' can be placed safely.
• Then we have to backtrack till 'q1' and place it to (1, 2) and then
all other queens are placed safely by moving q2 to (2, 4), q3 to (3,
1) and q4 to (4, 3).
N- Queens Problem
N- Queens Problem
• Fig shows the complete state space for 4 - queens problem. But
we can use backtracking method to generate the necessary node
and stop if the next node violates the rule, i.e., if two queens are
attacking.
• It can be seen that all the solutions to the 4 queens problem can
be represented as 4 - tuples (x1, x2, x3, x4) where xi represents the
column on which queen "qi" is placed.
4- Queens Problem
N- Queens Problem
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;
return true;
}
• Place (k, i) return true if a queen can be placed in the kth row
and ith column otherwise return is false.
• x [] is a global array whose final k - 1 values have been set. Abs
(r) returns the absolute value of r.
N- Queens Problem
• x [] is a global array whose final k - 1 values have been set.
Abs (r) returns the absolute value of r.
N - Queens (k, n)
{
For i ← 1 to n
do if Place (k, i) then
{
x [k] ← i;
if (k ==n) then
write (x [1....n));
else
N - Queens (k + 1, n);
}
}
05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 37
Backtracking
8- Queens Problem
Hamiltonian Cycles
• Hamiltonian Path in an undirected graph is a path that visits each
vertex exactly once return to initial vertex.
• A Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian
Path such that there is an edge (in the graph) from the last vertex
to the first vertex of the Hamiltonian Path.
Algorithm
subsetSum(set, subset, n, subSize, total, node, sum)
Input: The given set and subset, size of set and subset, a total of the
subset, number of elements in the subset and the given sum.
Output: All possible subsets whose sum is the same as the given sum.
• The implicit binary tree for the subset sum problem is shown in Fig.
• The number inside a node is the sum of the partial solution elements
at a particular level.