Example DP Problem:Matrix Chain Multiplication
Given a sequence A1, A2,…, An, where matrix Ai has dimension pi-1 x pi, fully parenthesize the product in a way that minimizes the number of scalar operations.
Matrix-Multiply(A,B)
for i = 1 to rows [A]
for j = 1 to columns [B]
C[i,j] = 0;
for k = 1 to columns [A]
C[i,j] = C[i,j] + A[i,k]*B[k,j]
A pxq B qxr
result: C p xr
running time: pqr
Previous slide
Next slide
Back to first slide
View graphic version