1
Designing a Dynamic Designing a Dynamic Programming Algorithm for an Programming Algorithm for an Optimization Problem Optimization Problem
- Step 1.
Step 1. Characterize the structure of optimal Characterize the structure of optimal solution solution
- Step 2.
Step 2. Recursively define the value of an Recursively define the value of an
- ptimal solution
- ptimal solution
- Step 3.
Step 3. Compute the value of an optimal Compute the value of an optimal solution in a bottom solution in a bottom-
- up fashion
up fashion
- Step 4.
Step 4. Construct an optimal solution from Construct an optimal solution from computed information computed information Dynamic Programming: Matrix Chain Multiplication
Matrix Chain Multiplication Problem Given a chain of matrices, where (for ) is a matrix Fully parenthesize the product so that the number of scalar multiplications is minimum minimum
1 2, , ,
nA A A … n 1,2, , i n = …
iA
i ip p ×
1 2 nA A A
- Time Complexity of Matrix Multiplication
Time Complexity of Matrix Multiplication Matrix-Multiply(A, B) if columns[A] rows[B] then error “incompatible dimensions” else for i1 to rows[A] = p do for j 1 to columns[B] = r do C[I, j] 0 for k 1 to columns[A] = q do C[I, j] C[I, j] + A[I, k]*B[k, j] return C
≠
p x q q x r
{{{
pqr rq q Example Two ways to parenthesize A1 A2 A3 10 100 5 50 ( A1 A2 ) A3 10 100 5 50
= (10)(100)(5) + (10)(5)(50) = 5000 + 2500 = 7500
A1 ( A2 A3 ) 10 100 5 50
= (100)(5)(50) + (10)(100)(50) = 25000 + 50000 = 75000