Module 2A - DP
Module 2A - DP
(DAA)
Dynamic programming & its applications
Dept. of CSE, rcciit
1
Table of Content Module 2
▪ Dynamic Programming
▪ Definition, characteristics & Applications
▪ Dynamic Programming – difference with D & C
▪ Matrix-Chain Multiplication,
▪ All Pair Shortest Paths - Floyd-Warshall algorithm
▪ Single Source Shortest Path - Bellman-Ford algorithm
▪ Travelling Salesman Problem,
▪ 0-1 Knapsack Problem
Dynamic programming (DP) ….?????
• Dynamic Programming is the most powerful design technique for solving
optimization problems.
• Dynamic programming (DP) is closely related to divide and conquer technique,
where the problem breaks down into smaller disjoin sub problems and each
sub problem is solved recursively and then combine their solution to solve the
original problems.
• Dynamic Programming is used when the subproblems are not
independent, e.g. when they share the same subproblems. In this case,
divide and conquer may do more work than necessary, because it solves
the same sub problem multiple times.
• Dynamic Programming solves each subproblems just once and stores
the result in a table so that it can be repeatedly retrieved if needed
again.
• Dynamic programming (DP) is a bottom up technique, which is used to solve
all possible small problems and then combine them to obtain solutions for
bigger problems.
• Dynamic programming (DP) is the most powerful technique for optimization
problems was invented by Richard Bellman in the 1950s.
• Dynamic programming (DP) is to avoid the requirement of calculating the same
quantity twice.
Steps for achieving Dynamic Programming
The steps for achieving Dynamic Programming are :
• Divide (Sub problems) - The main problem is divided into
several smaller subproblems. The solution of the main problem
is expressed in terms of the solution for the smaller
subproblems.
•If a problem has optimal substructure, then we can recursively define an optimal
solution. If a problem has overlapping subproblems, then we can improve on a
recursive implementation by computing each subproblem only once.
•If the space of subproblems is enough (i.e. polynomial in the size of the input),
dynamic programming can be much more efficient than recursion.
Elements of Dynamic Programming
There are basically three elements that characterize a dynamic programming
algorithm:-
[Link]: Decompose the given problem into smaller subproblems. Express
the solution of the original problem in terms of the solution for smaller
problems.
[Link] Structure: After solving the sub-problems, store the results to the sub
problems in a table. This is done because subproblem solutions are reused
many times, and we do not want to repeatedly solve the same problem over
and over again.
[Link]-up Computation: Using table, combine the solution of smaller
subproblems to solve larger subproblems and eventually arrives at a solution to
complete problem.
Bottom-up means:-
•Start with smallest subproblems.
•Combining their solutions obtain the solution to sub-problems of increasing
size.
•Until solving at the solution of the original problem.
Components of Dynamic programming
1. Stages: The problem can be divided into several subproblems, which are called
stages. A stage is a small portion of a given problem. For example, in the
shortest path problem, they were defined by the structure of the graph.
2. States: Each stage has several states associated with it. The states for the
shortest path problem was the node reached.
3. Decision: At each stage, there can be multiple choices out of which one of the
best decisions should be taken. The decision taken at every stage should be
optimal; this is called a stage decision.
4. Optimal policy: It is a rule which determines the decision at each stage; a policy
is called an optimal policy if it is globally optimal. This is known as Bellman
principle of optimality.
5. Given the current state, the optimal choices for each of the remaining states
does not depend on the previous states or decisions. In the shortest path
problem, it was not necessary to know how we got a node only that we did.
6. There exist a recursive relationship that identify the optimal decisions for stage
j, given that stage j+1, has already been solved.
7. The final stage must be solved by itself.
Development of DP Algorithm
• It can be broken into four steps:
4. Construct the optimal solution for the entire problem from the
computed values of smaller sub-problems.
Divide & Conquer Method vs Dynamic Programming.
Divide & Conquer Method Dynamic Programming
1. It deals (involves) three steps at each level 1. It involves the sequence of four steps:
of recursion: o Characterize the structure of optimal
• Divide the problem into a number of solutions.
sub-problems. o Recursively defines the values of
• Conquer the sub-problems by solving optimal solutions.
them recursively. o Compute the value of optimal solutions
• Combine the solution to the in a Bottom-up minimum.
sub-problems into the solution for original o Construct an Optimal Solution from
sub-problems. computed information.
2. It is Recursive. 2. It is non Recursive.
3. It does more work on subproblems and 3. It solves subproblems only once and then
hence has more time consumption. stores in the table.
4. It is a top-down approach. 4. It is a Bottom-up approach.
5. In this subproblems are independent of 5. In this subproblems are interdependent.
each other.
6. For example: Merge Sort & Binary Search 6. For example: Matrix Multiplication.
etc.
Applications of DP
Applications of DP:
Total Multiplications = 2 * 4 * 3 = 12
Then
•It can be observed that the total entries in matrix 'C' is 'pr' as the matrix is of
dimension p x r Also each entry takes O (q) times to compute, thus the total time
to compute all possible entries for the matrix 'C' which is a multiplication of 'A'
and 'B' is proportional to the product of the dimension p q r.
Matrix-Chain Multiplication:Problem Formulation
A product of matrices is fully parenthesized if it is either a single matrix or the
product of two fully parenthesized matrix products.
• For example, for the product ABCD, there are five possible ways to fully
parenthesize the product:
i. (A(B(CD))),
ii. (A((BC)D)),
iii. ((A(BC))D),
iv. ((AB)(CD)),
v. (((AB)C)D).
Example: Consider three matrices A10×100, B100×5, and C5×50
There are 2 ways to parenthesize
– ((AB)C) = D10×5 · C5×50
• AB ⇒ 10·100·5=5,000 scalar multiplications
• DC ⇒ 10·5·50 =2,500 scalar multiplications Total: 7,500
– (A(BC)) = A10×100 · E100×50
• BC ⇒ 100·5·50=25,000 scalar multiplications
• AE ⇒ 10·100·50 =50,000 scalar multiplications Total: 75,000
Matrix-chain Multiplication
Matrix chain multiplication define recursive solution:
0, if i=j
m[i,j]=
mini(i≤k<j)(m[i, k]+m[k+1, j]+p[i-1] *p[k]* p[j]), if i<j
Algorithm: Matrix_chain_multiplication
► The following pseudo code assumes that –
• The matrix Ai has dimensions pi-1 x pi for i =1, 2, 3, 4, …. n.
• The input is a sequence (p0, p1, p2, …..pn) where length
[p]=n+1.
• The procedure uses an auxiliary table m[1..n, 1..n] for storing the
m[i,j] costs.
• An auxiliary table s[1..n, 1..n] that records which index of k
achieved the optimal cost in computing m[i,j].
Matrix-chain Multiplication
Matrix_Chain_Order ( p[], n )
Steps:
1) for i🡨1 to n Do ►i=j
m[i,j]🡨0 ► since no product is required
2) for L🡨2 to n-1 do ►L is the chain length
for i🡨 1 to n - L Do
j🡨i+L -1
m[i,j] 🡨 ∞ ► INT_MAX
for k🡨 i to j-1 Do
q 🡨 m[i, k] + m[k+1, j] + p[i-1] * p[k] * p[j]
if q < m[i, j] Then
m[i, j] 🡨 q
s[I,j]🡨 k
3) Return m [1, n-1] and s
Matrix-chain Multiplication
We will use table s to construct an optimal solution.
Constructing an Optimal Solution:
PRINT-OPTIMAL-PARENS (s, i, j)
1. if i=j
then print "A"
2. else print "("
PRINT-OPTIMAL-PARENS (s, i, s [i, j])
3. PRINT-OPTIMAL-PARENS (s, s [i, j] + 1, j)
4. print ")“
5. return
MATRIX CHAIN MULTIPLICATION
Problem : Given five matrices A3x5, B5x15, C15x5, D5x10, E10x3. Find scalar
multiplication cost of given chain matrices.
Solution:
Matrix_Chain_Multi( p[], n )
► n is number of dimensions and p is the list of dimension
Steps: Here,
1) for i🡨1 to n Do ►i=j n = 6, and p[]={3, 5, 15, 5, 10, 3}
m[i, i]🡨0 ► number of multiplications are 0(zero) when there is only one matrix*/
2) for L🡨2 to n-1 do ►L is the chain length
for i🡨 1 to n - L Do i/j 1 2 3 4 5
j🡨i+L -1 1 0
m[i,j] 🡨 ∞ ► INT_MAX 2 0
for k🡨 i to j-1 Do 3 0
q 🡨 m[i, k] + m[k+1, j] + p[i-1] * p[k] * p[j] 4 0
if q < m[i, j] Then
5 0
m[i, j] 🡨 q
3) Return m [1, n-1]
m[1, 1] =m[2, 2] =m[3, 3]=m[4, 4] =m[5, 5]=0
MATRIX CHAIN MULTIPLICATION
Solution: Matrix_Chain_Multi( p[], n )
Compute m[i][j] using ► n is number of dimensions and p is the list of dimension
m[i][j]=m[i][k] + m[k+1][j] + Pk-1 Pk Pj for k=I to j-1
Steps:
m[1][2]=m[1][1] + m[2][2] + P0 P1 P2=0 +0 + 3 . 5 . 15
=225 for k=1 1) for i🡨1 to n Do ►i=j
m[2][3]=m[2][2] + m[3][3] + P1 P2 P3=0 +0 + 5 . 15. 5 m[i, i]🡨0
=375 for k=2
2) for L🡨2 to n-1 do ►L is the chain length
m[3][4]=m[3][3] + m[4][4] + P2 P3 P4=0 +0 + 15 . 5 . 10
for i🡨 1 to n - L Do
=750 for k=3
j🡨i+L -1
m[4][5]=m[4][4] + m[5][5] + P3 P4 P5=0 +0 + 5 . 10. 3 m[i,j] 🡨 ∞ ► INT_MAX
=150 for k=4 for k🡨 i to j-1 Do
q 🡨 m[i, k] + m[k+1, j] + p[i-1] * p[k] * p[j]
i/j 1 2 3 4 5 if q < m[i, j] Then
m[i, j] 🡨 q
1 0 225 450
2 0 375 3) Return m [1, n-1]
3 0 750
4 0 150
5 0
MATRIX CHAIN MULTIPLICATION
m[1][3]=min m[1][1] + m[2][3] + P0 P1 P3 Matrix_Chain_Multi( p[], n )
m[1][2] + m[3][3] + P0 P2 P3 ► n is number of dimensions and p is the list of dimensions
=450 for k=1 or 2, take k=1
Steps:
m[2][4]=625 for k=3
m[3][5]=375 for k=3 1) for i🡨1 to n Do ►i=j
m[1][4]=495 for k=3 m[i, i]🡨0
4 0 150
5 0
MATRIX CHAIN MULTIPLICATION
Now apply Print-Optimal-Parens(s,i,j) to parenthesize A1 A2 A3 A5.
Initially, i=1 and j=5. i≠j, then k=s[1][5]=4, we have Print-Optimal-Parens(s,1,4)
and Print-Optimal-Parens(s,5,5).
Similarly, for Print-Optimal-Parens(s,1,4), i≠j, k=s[[1][4]=3, so
Print-Optimal-Parens(s,1,3) and Print-Optimal-Parens(s,4,4).
The process is shown below - i/j 1 2 3 4 5
S,1,5 1 0 1 1 3 4
2 0 2 3 3
3 0 3 3
S,1,4
S,5,5 4 0 4
5 0
S,1,3 A5
S,1,5
A2 A3
MCQ
Which of the following methods can be used to solve the matrix chain
multiplication problem?
a) Dynamic programming
b) Brute force
c) Recursion
d) Dynamic Programming, Brute force, Recursion
Which of the following is the recurrence relation for the matrix chain multiplication
problem where mat[i-1] * mat[i] gives the dimension of the ith matrix?
a) dp[i,j] = 1 if i=j
dp[i,j] = min{dp[i,k] + dp[k+1,j]}
b) dp[i,j] = 0 if i=j
dp[i,j] = min{dp[i,k] + dp[k+1,j]}
c) dp[i,j] = 1 if i=j
dp[i,j] = min{dp[i,k] + dp[k+1,j]} + mat[i-1]*mat[k]*mat[j].
d) dp[i,j] = 0 if i=j
dp[i,j] = min{dp[i,k] + dp[k+1,j]} + mat[i-1]*mat[k]*mat[j].
MCQ
Consider the two matrices P and Q which are 10 x 20 and 20 x 30 matrices
respectively. What is the number of multiplications required to multiply the
two matrices?
a) 10*20
b) 20*30
c) 10*30
d) 10*20*30
• Problem_2:
– Given the four matrices P5 x 4, Q4 x 6, R6 x 2, T2 x 7. Find the optimal
sequence for the computation of multiplication operation.
• ANS: Mini. Multi cost=158 and Optimal sequence is ((P(QR))T)
• Problem_3:
– Given the four matrices A4 x 10, B10 x 3, C3 x 12, D12 x 20 ,E20 x 7. Find the
optimal sequence for the computation of multiplication operation.
• ANS: Mini. Multi cost =1344 and Optimal sequence is ((A B)((C D)E))
References
• [Link]
• [Link]
• [Link]
• [Link]
KNAPSACK PROBLEM
• Knapsack is basically means bag. A bag of given capacity.
• We want to pack n items in your luggage.
• The ith item is worth vi dollars and weight wi pounds.
• Take as valuable a load as possible, but cannot exceed capacity W
pounds.
• vi wi W are integers.
Input:
•Knapsack of capacity
•List (Array) of weight and their corresponding
value.
Step-03: To identify the items that must be put into the knapsack to obtain that
maximum profit,
•Consider the last column of the table.
•Start scanning the entries from bottom to top.
•On encountering an entry whose value is not same as the value stored in the
entry immediately above it, mark the row label of that entry.
•After all the entries are scanned, the marked labels represent the items that
must be put into the knapsack.
Time Complexity
• Each entry of the table requires constant time θ(1) for its
computation.
• It takes θ(nw) time to fill (n+1)(w+1) table entries.
• It takes θ(n) time for tracing the solution since tracing
process traces the n rows.
• Thus, overall θ(nw) time is taken to solve 0/1 knapsack
problem using dynamic programming.
Problem Definition
For the given set of items and knapsack capacity = 5 kg, find the
optimal solution for the 0/1 knapsack problem making use of
dynamic programming approach.
Item Weight Value
1 2 3
2 3 4
3 4 5
4 5 6
OR
Find the optimal solution for the 0/1 knapsack problem making use of
dynamic programming approach. Consider-
n=4
w = 5 kg
(w1, w2, w3, w4) = (2, 3, 4, 5)
(v1, v2, v3, v4) = (3, 4, 5, 6)
Problem Definition
OR
A thief enters a house for robbing it. He can carry a maximal weight
of 5 kg into his bag. There are 4 items in the house with the
following weights and values. What items should thief take if he
either takes the item completely or leaves it completely?
Item Weight (kg) Value ($)
Mirror 2 3
Silver
3 4
nugget
Painting 4 5
Vase 5 6
Problem Solution
Given-
Step-01:
Draw a table say ‘T’ with (n+1) = 4 + 1 = 5 number of rows and (w+1) = 5 + 1 =
6 number of columns.
Fill all the boxes of 0th row and 0th column with 0.
Problem Solution
Step-02:
Start filling the table row wise top to bottom from left to right using the formula-
T (i , j) = max { T ( i-1 , j ) , valuei + T( i-1 , j – weighti ) }
We have, We have,
i=1 i=1
j=1 j=2
(value)i = (value)1 = 3 (value)i = (value)1 = 3
(weight)i = (weight)1 = 2 (weight)i = (weight)1 = 2
We have, We have,
i=1 i=1
j=3 j=4
(value)i = (value)1 = 3 (value)i = (value)1 = 3
(weight)i = (weight)1 = 2 (weight)i = (weight)1 = 2
We have, We have,
i=1 i=2
j=5 j=1
(value)i = (value)1 = 3 (value)i = (value)2 = 4
(weight)i = (weight)1 = 2 (weight)i = (weight)2 = 3
We have, We have,
i=2 i=2
j=2 j=3
(value)i = (value)2 = 4 (value)i = (value)2 = 4
(weight)i = (weight)2 = 3 (weight)i = (weight)2 = 3
We have, We have,
i=2 i=2
j=4 j=5
(value)i = (value)2 = 4 (value)i = (value)2 = 4
(weight)i = (weight)2 = 3 (weight)i = (weight)2 = 3
For 0/1 KNAPSACK problem, the algorithm takes ________ amount of time for
memory table, and ______time to determine the optimal load, for N objects
and W as the capacity of KNAPSACK.
a) O(N+W), O(NW)
b) O(NW),O(N+W)
c) O(N),O(NW)
d) O(NW),O(N)
MCQ
Which of the following problems is equivalent to the 0-1 Knapsack problem?
a)You are given a bag that can carry a maximum weight of W. You are given N
items which have a weight of {w1, w2, w3,…., wn} and a value of {v1, v2,
v3,…., vn}. You can break the items into smaller pieces. Choose the items in
such a way that you get the maximum value
b)b) You are studying for an exam and you have to study N questions. The
questions take {t1, t2, t3,…., tn} time(in hours) and carry {m1, m2, m3,….,
mn} marks. You can study for a maximum of T hours. You can either study a
question or leave it. Choose the questions in such a way that your score is
maximized
c)You are given infinite coins of denominations {v1, v2, v3,….., vn} and a sum S.
You have to find the minimum number of coins required to get the sum S
d)You are given a suitcase that can carry a maximum weight of 15kg. You are
given 4 items which have a weight of {10, 20, 15,40} and a value of {1, 2, 3,4}.
You can break the items into smaller pieces. Choose the items in such a way
that you get the maximum value
Assignments
Problem_1: Consider a 0-1 Knapsack having weight capacity W=3 and
number of items are three such that S=3, wi = { 1, 2, 3} and vi = { 2, 3, 4}.
To computes the maximum possible value that can be taken in the
Knapsack i.e. the value in c[n, W] and also find the actual Knapsack
items.
Ans: S = {1, 2}, wi = { 1, 2}= {3} ≤ W and ci = { 2, 3} = {5}. Optimum value.
Problem_2: Given a Knapsack having maximum weight capacity W=4
and number of items available are three, such that S=3, wi={1, 3, 4} and
vi={3, 4, 5}. Fill the Knapsack such that Knapsack should not exceed its
maximum capacity and it should have maximum profit value.
Problem_3: Define how Knapsack problem is solved by using Dynamic
programming approach? Consider n=3, (w1, w2, w3)=(2, 3, 3), (p1, p2,
p3)=(1, 2, 4) and m=6. Find optimal solution for the given data.
TRAVELLING SALESMAN PROBLEM using DP
Travelling Sales Person problem needs to visit ‘n’ cities in such a manner
that all the cities must be visited only once and in the end he returns to the
city from where he started with minimum distance.
Given a group of cities c={c1, c2, c3,…..cn}, where distance (i,j)
denotes the distance of travelling from city ci to city cj.
Given n cities and the distances dij between any two of them, we wish
to find the shortest tour going through all cities and back to the starting
city.
The TSP is a well known and difficult problem, that can be solved in O(n!)
≈O(nn e.n) steps.
Travelling Salesman problem using DP
TRAVELLING SALESMAN PROBLEM using DP
Cost(3,Φ,1)=d(3,1)=6Cost(3,Φ,1)=d(3,1)=6
Cost(4,Φ,1)=d(4,1)=8Cost(4,Φ,1)=d(4,1)=8
TRAVELLING SALESMAN PROBLEM using DP
TRAVELLING SALESMAN PROBLEM using DP
TRAVELLING SALESMAN PROBLEM using DP
SINGLE-SOURCE SHORTEST PATH PROBLEM
• Single-Source Shortest path problem to determine the shortest
paths from a given source vertex V1 to all the remaining vertices
of directed graph G(V, E, W).
• Given a directed weighted graph G(V,E,W), how we can find a
shortest path from given source vertex s to each other vertex v in
G.
• This problem can be solved easily with BFS Traversal algorithm
under the special case when all weights are 1.
• Bellman-Ford algorithm is preferable that follows Dynamic
Programming.
• Bellman-Ford algorithm can detect a cycle of negative weights if
the graph has an edge with negative weight.
• The Algorithm finds the shortest path repeating a vertex of a
graph.
SINGLE-SOURCE SHORTEST PATH PROBLEM
Algorithm: Bellman_Ford(sp, G) 4. For i🡨1 to n-1 Do//Relaxation
►Where sp is a shortest path length in terms of 1. For j🡨1 to n Do
edge cost/weight and G(V, E, W) is a weighted 1. If W[i][j]≠0 or α Then
digraph. 1. X🡨sp[i]+W[i][j]
Steps: 2. If x < sp[j] Then
[Link] i🡨1 to n Do 1. Sp[j]🡨x
1. For j🡨1 to n Do 2. Pre[j]🡨i
1. If i=j then //self loop exists 5. For i🡨1 to n Do //Check negative wt.
1. W[i][j]🡨0 2. For j🡨1 to n Do
2. Else 1. If W[i][j]≠0 or α Then
1. If vi≠vj then //no edge 1. x🡨sp[i]+W[i][j]
1. W[i][j]🡨α 2. If x < sp[j] Then
2. Else 1. Return false
1. W[i][j]🡨W(vi,vj) 6. Return true.
[Link] i🡨1 to n Do
1. Sp[i]🡨α
2. Pre[i]🡨NULL
[Link][1]🡨0
SINGLE-SOURCE SHORTEST PATH PROBLEM
Example: Find the shortest path using Bellman-Ford algorithm on
the digraph in given figure 1.
W[i][j] = V2 α 0 1 2 α
V3 α α 0 α 6
V4 α 3 9 0 2
V5 7 α 4 α 0
Fig 2.
After relaxing the remaining edges
(v4, v2), (v4,v3), (v4,v5), (v5,v1),
(v5,v3) and computing corresponding
costs.
Fig 3.
SINGLE-SOURCE SHORTEST PATH PROBLEM
Similarly, in 2nd pass relaxing
all the edges of the graph.
Fig 5.
ALL PAIRS SHORTEST PATH PROBLEM
• All pairs shortest paths problem is to determine the shortest path
between all pair of vertices on a weighted digraph G (V, E, W).
• Solving all pair shortest path problem consists of computing the shortest
distance or path in terms of cost/weight between every pair of vertices
or nodes (u, v) where u, v € V in the graph G.
• We assume absence of negative or zero weight cycle.
ALL PAIRS SHORTEST PATH PROBLEM
• Floyd-Warshall algorithm is to find the shortest path for all pair of
vertices.
• This algorithm in O(n3) time that uses Dynamic Programming.
• The cost/weight matrix of G is defined as
0, if i=j
W[i][j] = w, if i≠ j and (vi,vj) €E
α, if (vi,vj) €E
• The matrix is of n x n size since |V|=n, this will be the input to the
algorithm and corresponding output will also be an n x n cost matrix.
• The path matrix shows shortest path cost from vertex vi to vj and is
denoted by SPC[i][j].
Wij, if k=0
A Recursive definition is given by SPCij(k)=
min(SPCij(k-1), SPCik(k-1) + SPCkj (k-1)), ifk≥1
ALL PAIRS SHORTEST PATH PROBLEM
Algorithm: Floyd_Warshall(G, SPC, n) Analysis:
►Where SPC is a matrix for shortest path cost , G(V, E, The Floyd-Warshall
W) is a weighted digraph and n is a |v|, no. of vertices. algorithm compares all
Steps: possible paths in the digraph
[Link] i🡨1 to n Do G between each pair of
1. For j🡨1 to n Do vertices.
2. SPC0 [i][j] 🡨W[i][j] It is possible to do this in n3
[Link] k🡨1 to n Do comparisons as obtained
1. For i🡨1 to n Do from step 2.
1. For j🡨1 to n Do Hence the required time
1. SPCij(k)=min(SPCij(k-1), SPCik(k-1) + SPCkj (k-1)) complexity for this algorithm
[Link] SPC(n). is O(n3).
ALL PAIRS SHORTEST PATH PROBLEM
• Example: Find the shortest path between all pairs using
Floyd-Warshall algorithm on the weighted directed graph given
below – 2 3
V2 V3
7 5
V1
V1 V2 V3
Solution: V1 0 ∞ 5
Consider the following graph. What is the minimum cost to travel from node
A to node C?
a) 5
b) 2
c) 1
d) 3
References
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
Thank you !
H. Tunga 72