Unit 4
Unit 4
Unit: 4
1
11/20/2024
Dynamic Programming(CO4)
11/20/2024 2
Dynamic Programming
11/20/2024 3
Dynamic Programming(CO4)
Knapsack problem(0/1)
• To add fuel to the fire, the thief has an old knapsack which has
limited capacity.
• Obviously, he can’t split the table into half or jewelry into 3/4ths. He
either takes it or leaves it.
11/20/2024 4
Dynamic Programming(CO4)
Dynamic-Programming Approach
• Let i be the highest-numbered item in an optimal
solution S for W dollars. Then S' = S - {i} is an optimal solution
for W - wi dollars and the value to the solution S is Vi plus the value
of the sub-problem.
• We can express this fact in the following formula: define c[i, w] to
be the solution for items 1,2, … , i and the maximum weight w.
• The algorithm takes the following inputs
• The maximum weight W
• The number of items n
• The two sequences v = <v1, v2, …, vn> and w = <w1, w2, …, wn>
11/20/2024
5
Dynamic Programming(CO4)
Dynamic-0-1-knapsack (v, w, n, W)
• for w = 0 to W do
• c[0, w] = 0
• for i = 1 to n do
• c[i, 0] = 0
• for w = 1 to W do
• if wi ≤ w then
• if vi + c[i-1, w-wi] then
• c[i, w] = vi + c[i-1, w-wi]
• else c[i, w] = c[i-1, w]
• else
• c[i, w] = c[i-1, w]
11/20/2024 6
Dynamic Programming(CO4)
• Knapsack problem(0/1)
• The set of items to take can be deduced from the table, starting
at c[n, w] and tracing backwards where the optimal values came
from.
• If c[i, w] = c[i-1, w], then item i is not part of the solution, and we
continue tracing with c[i-1, w]. Otherwise, item i is part of the
solution, and we continue tracing with c[i-1, w-W].
Analysis
11/20/2024 7
Dynamic Programming(CO4)
Knapsack problem(0/1)
Steps
• To calculate the maximum value that we can obtain with item i, we
first need to compare the weight of item i with the knapsack’s weight
capacity.
• Obviously, if item i weighs more than what the knapsack can hold, we
can’t include it, so it does not make sense to perform the calculation.
• In that case, the solution to this problem is simply the maximum value
that we can obtain without item i (i.e. the value in the row above, at
the same column).
11/20/2024 8
Dynamic Programming(CO4)
Knapsack problem(0/1)
• However, suppose that item i weighs less than the knapsack’s
capacity.
• We thus have the option to include it, if it potentially increases the
maximum obtainable value.
• The maximum obtainable value by including item i is thus = the
value of item i itself + the maximum value that can be obtained with
the remaining capacity of the knapsack.
• We obviously want to make full use of the capacity of our knapsack,
and not let any remaining capacity go to waste.
• Therefore, at row i and column j (which represents the maximum
value we can obtain there), we would pick either the maximum
value that we can obtain without item i, or the maximum value that
we can obtain with item i, whichever is larger.
11/20/2024 9
Dynamic Programming(CO4)
• For Example
11/20/2024 10
Dynamic Programming(CO4)
Knapsack problem(0/1)
• If we choose not to, the maximum value we can obtain is the same
as that in the row above at the same column, i.e. 10 (by including
only item 1, which has a value of 10).
11/20/2024 11
Dynamic Programming(CO4)
• Knapsack problem(0/1)
• We can find out the maximum value that can be obtained with a
capacity of 5 by looking at the row above, at column 5.
11/20/2024 12
Dynamic Programming(CO4)
Knapsack problem(0/1)
• Once the table has been populated, the final solution can be found at
the last row in the last column, which represents the maximum value
obtainable with all the items and the full capacity of the knapsack.
11/20/2024 13
Dynamic Programming(CO4)
• At first the output matrix is same as given cost matrix of the graph. After
that the output matrix will be updated with all vertices k as the intermediate
vertex.
11/20/2024 15
Dynamic Programming(CO4)
11/20/2024 16
Dynamic Programming(CO4)
All Pair Shortest Path
Example: Input-The cost matrix of the graph.
0 3 6
3 0 2 1
6 2 0 1 4 2
1 1 0 2 4
4 0 2 1
2 2 0 1
4 1 1 0
11/20/2024 17
Dynamic Programming(CO4)
1 1 0 2 3
3 0 2 1
2 2 0 1
3 1 1 0
11/20/2024 18
Dynamic Programming(CO4)
11/20/2024 20
Dynamic Programming(CO4)
• Consider the following example to understand the concept of
multistage graph.
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/d
esign_and_analysis_of_algorithms_multistage_graph.htm
11/20/2024 21
Dynamic Programming(CO4)
11/20/2024 22
Dynamic Programming(CO4)
11/20/2024 23
Dynamic Programming(CO4)
11/20/2024 24
Backtracking(CO4)- Objective
11/20/2024 25
Prerequisite and Recap
• Prerequisite
• Algorithms Concepts
• C programming
• Graph
• Recap
• Dynamic Programming
11/20/2024 26
Backtracking (CO4)
• It removes the solutions that doesn't give rise to the solution of the
problem based on the constraints given to solve the problem.
11/20/2024 27
Backtracking
11/20/2024 28
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.
11/20/2024 29
Backtracking
ALGORITHM
Step 1 − if current_position is goal, return success
Step 2 − else,
11/20/2024 30
Backtracking
11/20/2024 31
Backtracking
Output:
An array color[V] that should have numbers from 1 to m.
color[i] should represent the color assigned to the ith vertex.
11/20/2024 32
Backtracking
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.
11/20/2024 33
Backtracking
N- Queens Problem
11/20/2024 34
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.
• 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.
11/20/2024 35
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).
11/20/2024 36
Backtracking
N- Queens Problem
11/20/2024 37
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.
11/20/2024 38
Backtracking
4- Queens Problem
11/20/2024 39
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.
11/20/2024 40
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);
}
}
11/20/2024 41
Backtracking
8- Queens Problem
Hamiltonian Cycles
• Hamiltonian Path in an undirected graph is a path that visits each
vertex exactly once.
• 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.
11/20/2024 43
Backtracking
11/20/2024 44
Backtracking
11/20/2024 45
Backtracking
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.
11/20/2024 46
Backtracking
11/20/2024 47
Backtracking
• 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.
11/20/2024 48
Backtracking
11/20/2024 49
Backtracking
11/20/2024 50
Branch and Bound(CO4)- Objective
11/20/2024 51
Prerequisite and Recap
• Prerequisite
• Algorithms Concepts
• C programming
• Graph
• Recap
• Backtracking
11/20/2024 52
Branch and Bound (CO4)
11/20/2024 53
Branch and Bound
11/20/2024 54
Branch and Bound
1) Cost of reaching the node from the root (When we reach a node, we
have this cost computed)
11/20/2024 55
Branch and Bound
• 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.
11/20/2024 56
Branch and Bound
11/20/2024 57
Branch and Bound
11/20/2024 58
Branch and Bound
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.
11/20/2024 59
Branch and Bound
• . For doing this, we need to reduce the minimum value from each
element in each row and column.
11/20/2024 60
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.
11/20/2024 61
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.
11/20/2024 62
Branch and Bound
• The parent node (C0) has below reduced matrix-
.
11/20/2024 63
Branch and Bound
11/20/2024 64
Branch and Bound
.
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
11/20/2024 65
Branch and Bound
11/20/2024 66
Branch and Bound
• 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
11/20/2024 67
Branch and Bound
11/20/2024 68
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.
Cost = Cost of node 0 + cost of the edge (0,3) + Lower bound of the
path starting at node 3
= 25 + 0 + 0= 25
11/20/2024 69
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.
11/20/2024 70
Unit 4
Thank You
11/20/2024 71
11/20/2024 72