Sub topic 5
Worked Example: Matrix Chain Multiplication
This chapter assumes you know what a split-point recurrence is and why the table is filled by increasing interval length. Both were covered in earlier chapters. You should also know that multiplying a p×q matrix by a q×r matrix takes p·q·r scalar multiplications.
The Problem
You are given a chain of matrices A0, A1, ..., A(n-1) and must compute their product. Matrix multiplication is associative, so every parenthesization gives the same result. The cost of each parenthesization can be very different, though. The task is to find the order that uses the fewest scalar multiplications.
The input is a list dims of length n + 1, and matrix i has shape dims[i] x dims[i + 1]. For dims = [10, 30, 5, 60], the shapes are as follows.
| Matrix | Shape |
|---|---|
| A0 | 10 × 30 |
| A1 | 30 × 5 |
| A2 | 5 × 60 |
The two possible orders differ in cost by a factor of six:
- (A0 A1) A2 costs 10·30·5 + 10·5·60 = 1500 + 3000 = 4500.
- A0 (A1 A2) costs 30·5·60 + 10·30·60 = 9000 + 18000 = 27000.
Mapping It to Interval DP
State. dp[i][j] is the minimum cost to multiply Ai through Aj, with both ends inclusive.
Base case. dp[i][i] = 0, because a single matrix needs no multiplication.
Split point. Every product of Ai..Aj has a final multiplication. That multiplication joins some left block Ai..Ak with a right block A(k+1)..Aj, where i ≤ k < j. The left block's result has shape dims[i] x dims[k+1], and the right block's result has shape dims[k+1] x dims[j+1]. Joining them therefore costs dims[i] * dims[k+1] * dims[j+1]:
dp[i][j] = min over k in [i, j-1] of
dp[i][k] + dp[k+1][j] + dims[i] * dims[k+1] * dims[j+1]The two sides are independent. The best way to multiply the left block does not depend on how the right block is multiplied, which is why the problem decomposes over contiguous ranges.
The Implementation
def matrix_chain_order(dims):
# Matrix i has shape dims[i] x dims[i + 1], for i in 0..n-1.
n = len(dims) - 1
INF = float("inf")
dp = [[0] * n for _ in range(n)]
split = [[0] * n for _ in range(n)]
for length in range(2, n + 1):
for i in range(n - length + 1):
j = i + length - 1
dp[i][j] = INF
for k in range(i, j):
cost = dp[i][k] + dp[k + 1][j] + dims[i] * dims[k + 1] * dims[j + 1]
if cost < dp[i][j]:
dp[i][j] = cost
split[i][j] = k
return dp[0][n - 1], split
cost, split = matrix_chain_order([10, 30, 5, 60])
print(cost) # 4500The outer loop runs over length so that dp[i][k] and dp[k + 1][j] are always shorter intervals that are already filled. The split table records the winning k for each interval. The next chapter uses it to rebuild the parenthesization.
Tracing the Table
| Interval | Candidates | dp | split |
|---|---|---|---|
| [0,1] | k=0: 10·30·5 | 1500 | 0 |
| [1,2] | k=1: 30·5·60 | 9000 | 1 |
| [0,2] | k=0: 27000, k=1: 4500 | 4500 | 1 |
split[0][2] = 1 means the last multiplication joins (A0 A1) with A2.
Common Errors
- Starting
dp[i][j]at 0 instead of infinity makes the minimum always 0. - Looping
kup tojinclusive creates an empty right block,dp[j+1][j]. - Confusing n with
len(dims): a chain of n matrices needs n + 1 dimensions.
Exercise
For dims = [5, 10, 3, 12], compute dp[0][2] by hand and give the winning split k.
Show answer
dp[0][1] = 5·10·3 = 150 and dp[1][2] = 10·3·12 = 360. Split k=0 gives 0 + 360 + 5·10·12 = 960. Split k=1 gives 150 + 0 + 5·3·12 = 330. The minimum is 330, with k = 1, which is the order (A0 A1) A2.
Exercise
Why does the cost of the final multiplication for interval [i, j] with split k equal dims[i] dims[k+1] dims[j+1]?
Show answer
The product Ai..Ak has shape dims[i] x dims[k+1], and the product A(k+1)..Aj has shape dims[k+1] x dims[j+1]. Multiplying a p×q matrix by a q×r matrix costs p·q·r, with p = dims[i], q = dims[k+1], and r = dims[j+1].