DYNAMIC PROGRAMMING
Anjali Mohapatra.
Introduction
• Optimization problem: there can be many possible solution. Each
solution has a value, and we wish to find a solution with the optimal
(minimum or maximum) value
• Dynamic Programming VS. Divide-and-Conquer
– Solve problems by combining the solutions to sub-problems
– The sub-problems of D-A-C are non-overlap
– The sub-problems of D-P are overlap
• Sub-problems share sub-sub-problems
– D-A-C solves the common sub-sub-problems repeatedly
– D-P solves every sub-sub-problems once and stores its
answer in a table
» “Programming” refers to a tabular method
Development of A Dynamic-
Programming Algorithm
• Characterize the structure of an optimal
solution
• Recursively define the value of an optimal
solution
• Compute the value of an optimal solution
in a bottom-up fashion
• Construct an optimal solution from
computed information
Matrix-Chain Multiplication
Overview
• Given a sequence (chain) <A1, A2,…, An> of n matrices to be
multiplied, compute the product A 1A2…An in a way that minimizes the
number of scalar multiplications
• Matrix multiplication
– Two matrices A and B can be multiplied only if they are compatible
• The number of columns of A must equal the number of rows of
B
• A(p*q) * B(q*r) C(p*r)
– The number of scalar multiplication is p*q*r
• Example: <A1, A2, A3> (10*100, 100*5, 5*50)
– ((A1A2)A3) 10*100*5 + 10*5*50 = 5000 + 2500 = 7500
– (A1(A2A3)) 100*5*50 + 10*100*50 = 25000 + 50000 = 75000
Matrix-chain Multiplication
• Suppose we have a sequence or chain
A1, A2, …, An of n matrices to be
multiplied
– That is, we want to compute the product
A1A2…An
• There are many possible ways
(parenthesizations) to compute the
product
11-6
Matrix-chain Multiplication …contd
• Example: consider the chain A1, A2, A3, A4 of
4 matrices
– Let us compute the product A1A2A3A4
• There are 5 possible ways:
1. (A1(A2(A3A4)))
2. (A1((A2A3)A4))
3. ((A1A2)(A3A4))
4. ((A1(A2A3))A4)
5. (((A1A2)A3)A4)
11-7
Matrix-chain Multiplication …contd
• To compute the number of scalar
multiplications necessary, we must know:
– Algorithm to multiply two matrices
– Matrix dimensions
• Can you write the algorithm to multiply
two matrices?
11-8
Algorithm to Multiply 2 Matrices
Input: Matrices Ap×q and Bq×r (with dimensions p×q and q×r)
Result: Matrix Cp×r resulting from the product A·B
MATRIX-MULTIPLY(Ap×q , Bq×r)
1. for i ← 1 to p
2. for j ← 1 to r
3. C[i, j] ← 0
4. for k ← 1 to q
5. C[i, j] ← C[i, j] + A[i, k] · B[k, j]
6. return C
Scalar multiplication in line 5 dominates time to compute
CNumber of scalar multiplications = pqr
11-9
Matrix-chain Multiplication …contd
• Example: Consider three matrices A10100,
B1005, and C550
• There are 2 ways to parenthesize
– ((AB)C) = D105 · C550
• AB 10·100·5=5,000 scalar multiplications Total:
7,500
• DC 10·5·50 =2,500 scalar multiplications
– (A(BC)) = A10100 · E10050
• BC 100·5·50=25,000 scalar multiplications
• AE 10·100·50 =50,000 scalar multiplications
Total:
75,000 11-10
Matrix-chain Multiplication …contd
• Matrix-chain multiplication problem
– Given a chain A1, A2, …, An of n matrices,
where for i=1, 2, …, n, matrix Ai has
dimension pi-1pi
– Parenthesize the product A1A2…An such that
the total number of scalar multiplications is
minimized
• Brute force method of exhaustive search
takes time exponential in n
11-11
Dynamic Programming Approach
• STEP1:The structure of an optimal solution
– Let us use the notation Ai..j for the matrix that
results from the product Ai Ai+1 … Aj
– An optimal parenthesization of the product
A1A2…An splits the product between Ak and
Ak+1 for some integer k where1 ≤ k < n
– First compute matrices A1..k and Ak+1..n ; then
multiply them to get the final matrix A1..n
11-12
Dynamic Programming Approach
…contd
– Key observation: parenthesizations of the
subchains A1A2…Ak and Ak+1Ak+2…An must
also be optimal if the parenthesization of the
chain A1A2…An is optimal (why?)
– That is, the optimal solution to the problem
contains within it the optimal solution to
subproblems
11-13
Illustration of Optimal
SubStructure Minimal
Cost_A1..6 + Cost_A7..9+p0p6p9
A1A2A3A4A5A6A7A8A9
Suppose ((A1A2)(A3((A4A5)A6))) ((A7A8)A9) is optimal
Then (A1A2) (A3((A4A5)A6)) must be optimal for A1A2A3A4A5A6
Otherwise, if (A1(A2 A3)) ((A4A5)A6) is optimal for A1A2A3A4A5A6
Then ((A1(A2 A3)) ((A4A5)A6)) ((A7A8)A9) will be better than
((A1A2)(A3((A4A5)A6))) ((A7A8)A9)
Dynamic Programming Approach …
contd
• STEP2: Recursive definition of the value
of an optimal solution
– Let m[i, j] be the minimum number of scalar
multiplications necessary to compute Ai..j
– Minimum cost to compute A1..n is m[1, n]
– Suppose the optimal parenthesization of Ai..j
splits the product between Ak and Ak+1 for
some integer k where i ≤ k < j
11-15
Dynamic Programming Approach …
contd
– Ai..j = (Ai Ai+1…Ak)·(Ak+1Ak+2…Aj)= Ai..k · Ak+1..j
– Cost of computing Ai..j = cost of computing Ai..k +
cost of computing Ak+1..j + cost of multiplying Ai..k
and Ak+1..j
– Cost of multiplying Ai..k and Ak+1..j is pi-1pk pj
– m[i, j ] = m[i, k] + m[k+1, j ] + pi-1pk pj for i
≤k<j
– m[i, i ] = 0 for i=1,2,…,n
11-16
Dynamic Programming Approach …
contd
– But… optimal parenthesization occurs at
one value of k among all possible i ≤ k < j
– Check all these and select the best one
0 if i=j
m[i, j ] =
min {m[i, k] + m[k+1, j ] + pi-1pk pj } if i<j
i ≤ k< j
11-17
Step 3
• Computing the optimal costs
– How much sub-problems in total?
• One for each choice of i and j satisfying 1 i j n
(n2)
– MATRIX-CHAIN-ORDER(p)
• Input: a sequence p= <P1, P2,…, Pn> (length[p] = n+1)
• Try to fill in the table m in a manner that corresponds
to solving the parenthesization problem on matrix
chains of increasing length
• Lines 4-12: compute m[i, i+1], m[i, i+2], … each time
Dynamic Programming Approach …
contd
• To keep track of how to construct an
optimal solution, we use a table s
• s[i, j ] = value of k at which Ai Ai+1 … Aj is
split for optimal parenthesization
• Algorithm: next slide
– First computes costs for chains of length l=1
– Then for chains of length l=2,3, … and so on
– Computes the optimal cost bottom-up
11-19
Algorithm to Compute Optimal Cost
Input: Array p[0…n] containing matrix dimensions and n
Result: Minimum-cost table m and split table s
MATRIX-CHAIN-ORDER(p[ ], n)
for i ← 1 to n Takes O(n3) time
m[i, i] ← 0
for l ← 2 to n Requires O(n2) space
for i ← 1 to n-l+1
j ← i+l-1
m[i, j] ←
for k ← i to j-1
q ← m[i, k] + m[k+1, j] + p[i-1] p[k] p[j]
if q < m[i, j]
m[i, j] ← q
s[i, j] ← k
return m and s
11-20
O(n3), (n3) (n3) running time
(n2) space
l =3
l=2
35*15*5=2625 10*20*25=5000
m[3,4]+m[5,5] + 15*10*20
=750 + 0 + 3000 = 3750
m[3,5] = min
m[3,3]+m[4,5] + 15*5*20
=0 + 1000 + 1500 = 2500
Step 4
• Constructing an optimal solution
– Each entry s[i, j] records the value of k such
that the optimal parenthesization of AiAi+1…Aj
splits the product between Ak and Ak+1
– A1..n A1..s[1..n] As[1..n]+1..n
– A1..s[1..n] A1..s[1, s[1..n]] As[1, s[1..n]]+1..s[1..n]
– Recursive…
Constructing Optimal Solution
• Our algorithm computes the minimum-
cost table m and the split table s
• The optimal solution can be constructed
from the split table s
– Each entry s[i, j ]=k shows where to split the
product Ai Ai+1 … Aj for the minimum cost
11-25
Elements of Dynamic
Programming
Optimal substructure
Overlapping subproblems