0% found this document useful (0 votes)
12 views53 pages

DSA-II Unit-4

The document outlines the syllabus for a course on Data Structures and Algorithms II, focusing on Dynamic Programming, Backtracking, and Branch and Bound. It details the course objectives, evaluation scheme, and provides an in-depth explanation of the Branch and Bound algorithm, particularly in solving the Travelling Salesman Problem, as well as the Backtracking technique with examples like the N-Queens problem. The document serves as a guide for students to understand algorithmic design paradigms and their applications in solving complex problems.

Uploaded by

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

DSA-II Unit-4

The document outlines the syllabus for a course on Data Structures and Algorithms II, focusing on Dynamic Programming, Backtracking, and Branch and Bound. It details the course objectives, evaluation scheme, and provides an in-depth explanation of the Branch and Bound algorithm, particularly in solving the Travelling Salesman Problem, as well as the Backtracking technique with examples like the N-Queens problem. The document serves as a guide for students to understand algorithmic design paradigms and their applications in solving complex problems.

Uploaded by

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

Noida Institute of Engineering and Technology, Greater

Noida

Dynamic Programming, Backtracking


and Branch and Bound

Unit: 4

Data Structure and Algorithm-II


Pooja Sharma
Assistant Professor
CS

Ms. Pooja Sharma DSA-II Unit IV


1
05/18/2025
Evaluation Scheme

4
Syllabus

Ms. Pooja Sharma DSA-II Unit IV


05/18/2025 3
Course Objective

• Upon completion of this course, students will be able to do the


following:
• Analyze the asymptotic performance of algorithms.
• Write rigorous correctness proofs for algorithms.
• Demonstrate a familiarity with major algorithms and data structures.
• Apply important algorithmic design paradigms and methods of
analysis.
• Synthesize efficient algorithms in common engineering design
situations.

Ms. Pooja Sharma DSA-II Unit IV


05/18/2025 4
Branch and Bound (CO4)

• Branch and bound is an algorithm design paradigm which is


generally used for solving combinatorial optimization problems.

• These problems are typically exponential in terms of time


complexity and may require exploring all possible permutations in
worst case.

• The Branch and Bound Algorithm technique solves these problems


relatively quickly.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 5


Branch and Bound

Travelling Salesman Problem


Given a set of cities and distance between every pair of cities, the
problem is to find the shortest possible tour that visits every city
exactly once and returns to the starting point.

• In Branch and Bound method, for current node in tree, we compute


a bound on best possible solution that we can get if we down this
node.

• If the bound on best possible solution itself is worse than current


best (best computed so far), then we ignore the subtree rooted with
the node.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 6


Branch and Bound

Travelling Salesman Problem

Note that the cost through a node includes two costs.

1) Cost of reaching the node from the root (When we reach a node, we
have this cost computed)

2) Cost of reaching an answer from current node to a leaf (We compute


a bound on this cost to decide whether to ignore subtree with this node
or not).

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 7


Branch and Bound

Travelling Salesman Problem- Example

• 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.

• So we prepare the tree starting from root then we expand it.

• The cost matrix is defined by


C(i,j) = W(i,j) , if there is a direct path from Ci to Cj.
=, If there is no direct path from Ci to Cj.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 8


Branch and Bound

Travelling Salesman Problem- Example


Consider the following cost matrix

Here C(0,2)=30, C(4,0)=16, C(1,1)= and so on. Below is the


state space tree for the TSP , which shows optimal solution
marked in green
(https://www.techiedelight.com/travelling-salesman-problem-using-branch-and-
bound/
)

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 9


Branch and Bound

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 10


Branch and Bound

Travelling
. Salesman Problem- Steps

• As we can see from above diagram , every node has a cost associated
to it.

• When we go from city I to city j, cost of a node j will be sum of cost of


parent node I, cost of the edge (I,j) and lower bound of the path starting
at node j.

• As root node is the first node to be expanded , it doesn’t have any


parent. So cost will be only lower bound of the path starting at root.

• 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.

Let’s start from root node


• We reduce the minimum value in each row from each element in that
row.
• Minimum in each row of cost matrix M is marked by blue[10,2,2,3,4]
below.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 12


Branch and Bound

• . After reducing the row, we get the below matrix. We then reduce
the minimum value in each column from each element in that
column.

• Minimum in each column is marked by blue[1 0 3 0 0]. After


reducing the column, we get below reduced matrix. This matrix
will be further processed by child nodes of root node to calculate
their lower bound.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 13


Branch and Bound

• 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.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 14


Branch and Bound
• The parent node (C0) has below reduced matrix-
.

• Let us consider edge from 0 1

1. As we are adding edge (0,1) to our search space, we get outgoing


edges for city 0 to infinity and all incoming edges to city 1 to
infinity. We also set (1,0) to infinity.

2. So in reduced matrix of parent node we change all the elements in


row 0 and column 1 and at index (1,0) to infinity(marked in red).

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 15


Branch and Bound

The resulting cost matrix is

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 16


Branch and Bound

.
3. We try to calculate lower bound of the path starting at node 1 using
above resulting cost matrix.

4. The lower bound is 0 as matrix is already in reduced form. i.e. all


rows and all columns have zero value.

Therefore for node 1, cost will be

Cost= cost of node 0 + cost of the edge (0,1) + lower bound of the
path starting at node 1
= 25 + 10 +0= 35

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 17


Branch and Bound

.Lets consider edge from 0 2

1. Change all the elements in row 0 and column 2 and at index(2,0) to


infinity(marked in red).

The resulting cost matrix is:

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 18


Branch and Bound

• Now calculate lower bound of the path starting at node 2 using the
Approach discussed earlier. Resulting matrix will be-

Therefore the cost of node 2, cost 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

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 19


Branch and Bound

Let us consider edge from 0 3


1. Change the elements in row 0 and column 3 and at index(3,0) to
Infinity(marked in red).

The Resulting cost matrix is :

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 20


Branch and Bound

• 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.

Therefore for node 3, cost will be

Cost = Cost of node 0 + cost of the edge (0,3) + Lower bound of the
path starting at node 3
= 25 + 0 + 0= 25

Similarly we calculate for 0 4. Its cost will be 31.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 21


Branch and Bound

• 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.

• We continue the search till a leaf is encountered in space search tree.


If a leaf is encountered, then the tour is completed and we will
return back to the root node.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 22


Backtracking (CO4)

• Backtracking is an algorithmic-technique for solving problems


recursively by trying to build a solution incrementally, one piece
at a time, removing those solutions that fail to satisfy the
constraints of the problem at any point of time.

• It 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.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 23


Backtracking

• Backtracking algorithm is applied to some specific types of


problems.
• Decision problem used to find a feasible solution of the
problem.
• Optimisation problem used to find the best solution that can
be applied.
• Enumeration problem used to find the set of all feasible
solutions of the problem

• 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.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 24


Backtracking

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.

• when the algorithm propagates to an end to check if it is a solution


or not, if it is then returns the solution otherwise backtracks to the
point one step behind it to find track to the next point to find
solution.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 25


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.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 26


Backtracking

Graph Colouring Problem


• Given an undirected graph and a number m, determine if the
graph can be coloured with at most m colours such that no two
adjacent vertices of the graph are colored with the same color.

• Here coloring of a graph means the assignment of colors to all


vertices.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 27


Backtracking

Graph Colouring Problem


Input:
• A 2D array graph[V][V] where V is the number of vertices
in graph and graph[V][V] is adjacency matrix
representation of the graph.
• A value graph[i][j] is 1 if there is a direct edge from i to j,
otherwise graph[i][j] is 0.
• An integer m which is the maximum number of colors that
can be used

Output:
An array color[V] that should have numbers from 1 to m.
color[i] should represent the color assigned to the ith vertex.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 28


Backtracking

N- Queens Problem

• N - Queens problem is to place n - queens in such a manner on an


n x n chessboard that no queens attack each other by being in the
same row, column or diagonal.

• 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.

• Given a 4 x 4 chessboard and number the rows and column of the


chessboard 1 through 4.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 29


Backtracking

N- Queens Problem

Since, we have to place 4 queens such as q1 q2 q3 and q4 on the


chessboard, such that no two queens attack each other. In such a
conditional each queen must be placed on a different row, i.e.,
we put queen "i" on row "i."

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 30


Backtracking

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.

• We find that if we place q2 in column 1 and 2, then the dead end is


encountered.

• 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.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 31


Backtracking

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).

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 32


Backtracking

N- Queens Problem

• That is, we get the solution (2, 4, 1, 3).

• This is one possible solution for the 4-queens problem.

• For another possible solution, the whole method is repeated for


all partial solutions. The other solutions for 4 - queens problems
is (3, 1, 4, 2) i.e.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 33


Backtracking

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.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 34


Backtracking

4- Queens Problem

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 35


Backtracking

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.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 36


Backtracking

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

1.Thus, the solution for 8 -


queen problem for (4, 6, 8, 2, 7, 1, 3, 5).
2.If two queens are placed at position (i, j) and (k, l).
3.Then they are on same diagonal only if (i - j) = k - l or i
+ j = k + l.
4.The first equation implies that j - l = i - k.
5.The second equation implies that j - l = k - i.
6.Therefore, two queens lie on the duplicate diagonal if an
d only if |j-l|=|i-k|
05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 38
Backtracking

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.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 39


Backtracking

Sum of Subsets Problem


• Subset sum problem is to find subset of elements that are
selected from a given set whose sum adds up to a given
number K.

• We are considering the set contains non-negative values. It is


assumed that the input set is unique (no duplicates are
presented).

• Here backtracking approach is used for trying to select a valid


subset when an item is not valid, we will backtrack to get the
previous subset and add another element to get the solution.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 40


Backtracking

Sum of Subsets Problem


• Subset sum problem is to find subset of elements that are
selected from a given set whose sum adds up to a given
number K.

• We are considering the set contains non-negative values. It is


assumed that the input set is unique (no duplicates are
presented).

• Here backtracking approach is used for trying to select a valid


subset when an item is not valid, we will backtrack to get the
previous subset and add another element to get the solution.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 41


Backtracking

Sum of Subsets Problem


Backtracking
. Algorithm for Subset Sum
Using exhaustive search we consider all subsets irrespective of
whether they satisfy given constraints or not. Backtracking can be
used to make a systematic consideration of the elements to be selected.

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.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 42


Backtracking

Sum of Subsets Algorithm

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 43


Backtracking

Sum of Subsets Example


Given a set S = (3, 4, 5, 6) and X =9. Obtain the subset sum using
Backtracking approach.
Solution:
Initially S = (3, 4, 5, 6) and X =9.
S'= (∅)

• 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.

• Thus, if our partial solution elements sum is equal to the positive


integer 'X' then at that time search will terminate, or it continues if all
the possible solution needs to be obtained.

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 44


Backtracking

Sum of Subsets Example

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 45


Backtracking

Sum of Subsets Example

Input: The Set: {10, 7, 5, 18, 12, 20, 15}


The sum Value: 35
Output:
All possible subsets of the given set, where sum of each element for
every subsets is same as the given sum value.
{10, 7, 18}
{10, 5, 20}
{5, 18, 12}
{20, 15}

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 46


MCQ

1. Which of the following is/are property/properties of a dynamic


programming problem?
a) Optimal substructure
b) Overlapping subproblems
c) Greedy approach
d) Both optimal substructure and overlapping subproblems

2. If a problem can be broken into subproblems which are reused


several times, the problem possesses ____________ property.
a) Overlapping subproblems
b) Optimal substructure
c) Memoization
d) Greedy

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 47


MCQ

3. When dynamic programming is applied to a problem, it takes far


less time as compared to other methods that don’t take advantage
of overlapping subproblems.
a) True
b) False

4. Which of the following problems is NOT solved using dynamic


programming?
a) 0/1 knapsack problem
b) Matrix chain multiplication problem
c) Edit distance problem
d) Fractional knapsack problem

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 48


MCQ

5. Which of the following problems should be solved using dynamic


programming?
a) Mergesort
b) Binary search
c) Longest common subsequence
d) Quicksort

6. If a problem can be solved by combining optimal solutions to non-


overlapping problems, the strategy is called _____________
a) Dynamic programming
b) Greedy
c) Divide and conquer
d) Recursion

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 49


MCQ

7. Which of the problems cannot be solved by backtracking method?


a) n-queen problem
b) subset sum problem
c) hamiltonian circuit problem
d) travelling salesman problem

8. Backtracking algorithm is implemented by constructing a tree of


choices called as?
a) State-space tree
b) State-chart tree
c) Node tree
d) Backtracking tree

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 50


MCQ

9. In what manner is a state-space tree for a backtracking algorithm


constructed?
a) Depth-first search
b) Breadth-first search
c) Twice around the tree
d) Nearest neighbour first

10. The problem of finding a subset of positive integers whose sum is


equal to a given positive integer is called as?
a) n- queen problem
b) subset sum problem
c) knapsack problem
d) hamiltonian circuit problem

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 51


Expected Questions

1. Solve the problem of travelling salesman problem on


following graph and perform operation for each vertex.
[CO4]

2. Write Short notes on the following. [CO4]


i) Graph Coloring
ii)Sub Set Sum Problem
iii)Hamiltonian Cycle

05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 52


Expected Questions

3. Find out one optimal solution for following using back


tracking.
4*4 chessboard,4 Queen Problem
8*8 chessboard,8 Queen Problem
[CO4]

4. Write the difference between the Greedy method and


Dynamic programming.
[CO4]

5. Explain the term state space search tree with suitable


example.
[CO4]

6. Explain Floyd algorithm and also analyze its complexity


[CO4]
05/18/2025 Ms. Pooja Sharma DSA-II Unit IV 53

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy