Matrix Chain Multiplication
Suppose you're organizing a road trip with multiple legs. The order you combine those legs doesn't change your final destination β but it can make a huge difference in how much fuel you burn. Matrix chain multiplication is exactly like this: no matter how you parenthesize A Γ B Γ C Γ D, the result is the same matrix. But the cost β the number of arithmetic operations β can vary by orders of magnitude.
Multiplying a (aΓb) matrix by a (bΓc) matrix costs a Γ b Γ c scalar multiplications and produces an (aΓc) result.
A Concrete Example
Three matrices: A(10Γ30), B(30Γ5), C(5Γ60).
(AΓB)ΓC: AΓB costs 10Γ30Γ5 = 1,500 β (10Γ5); then ΓC costs 10Γ5Γ60 = 3,000. Total: 4,500.
AΓ(BΓC): BΓC costs 30Γ5Γ60 = 9,000 β (30Γ60); then AΓ costs 10Γ30Γ60 = 18,000. Total: 27,000.
The first order is 6Γ cheaper! For longer chains, the gap grows much larger.
The DP State
Define dp[i][j] = minimum scalar multiplications to compute the product of matrices i through j. Dimensions are given as an array dim[] where matrix i has shape dim[i-1] Γ dim[i]. We want dp[1][n].
The Recurrence
For any split point k (i β€ k < j), we first compute matrices i..k, then k+1..j, then multiply the two results:
dp[i][j] = min over k of: dp[i][k] + dp[k+1][j] + dim[i-1] Γ dim[k] Γ dim[j]
Base case: dp[i][i] = 0 (a single matrix needs no multiplication).
Fill Order: Short Chains First
dp[i][j] needs dp[i][k] and dp[k+1][j] β both shorter chains. This is a classic case of Interval DP. So fill by increasing chain length: l = 1 (base cases), then l = 2 (pairs), up to l = n. For each length, iterate over all valid starting positions i (with j = i + l - 1).
Recovering the Optimal Parenthesization
Store the best split point s[i][j] alongside dp[i][j]. Then recursively read the parenthesization: s[1][n] gives the top-level split; recurse into [1, s[1][n]] and [s[1][n]+1, n].