Sub topic 6
Recovering the Optimal Parenthesization
This chapter assumes you have read the matrix chain chapter. You should know what dp[i][j] and split[i][j] hold, and be comfortable with recursion. The DP table tells you the minimum cost. To actually perform the multiplication, you also need the grouping that achieves that cost.
Why the cost table is not enough
The value dp[0][n-1] is a single number. Many different parenthesizations exist, and the number does not say which one produced it. You could rerun the inner loop over k for each interval to find the winning split again, but that redoes work you have already done. The cheaper approach is to record the winning k at the moment you find it. That is what the split table does.
split[i][j] = k means that the best way to multiply matrices i..j is to compute (i..k) and (k+1..j) separately and then multiply the two results. Each half is itself an interval with its own stored split, so you can apply the same rule again until each interval holds a single matrix.
Walking the split table
The reconstruction mirrors the recurrence. An interval of one matrix is its own name. Any longer interval wraps its two halves in parentheses.
def matrix_chain_order(dims):
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
def parenthesize(split, i, j):
if i == j:
return f"A{i}"
k = split[i][j]
return "(" + parenthesize(split, i, k) + " " + parenthesize(split, k + 1, j) + ")"
cost, split = matrix_chain_order([10, 30, 5, 60])
print(cost) # 4500
print(parenthesize(split, 0, 2)) # ((A0 A1) A2)For [10, 30, 5, 60] the table gives split[0][2] = 1, so the top-level cut falls after A1. The left half 0..1 has split[0][1] = 0, which yields (A0 A1). The right half 2..2 is just A2.
Reconstruction visits each node of this binary tree once. A chain of n matrices has n leaves and n - 1 internal nodes, so reconstruction takes O(n) time. That is negligible next to the O(n^3) fill.
Common errors
| Mistake | Symptom | Fix |
|---|---|---|
| Recursing on `(k, j)` instead of `(k + 1, j)` | Matrix `k` appears twice, or the recursion never ends | The right half always starts at `k + 1` |
| Reading `split[i][i]` as a real split | Spurious parentheses around single matrices | Return the name when `i == j` before you read the table |
| Saving `k` outside the `if cost < dp[i][j]` block | The grouping does not match the reported cost | Update `split` and `dp` together |
| Rebuilding from `dp` alone | Extra O(n^3) work, or the wrong tree when costs tie | Store the split during the fill |
The same pattern works for any split-point DP. Store the choice that produced each optimal cell, then walk those choices from the full interval down.
Exercise
For dims [40, 20, 30, 10, 30], what does parenthesize(split, 0, 3) print, and what is the minimum cost?
Show answer
The minimum cost is 26000. Here split[0][3] = 2 and split[0][2] = 0, so the output is ((A0 (A1 A2)) A3). In that order, A1·A2 costs 6000, multiplying A0 by that result costs 8000, and the final product with A3 costs 12000.
Exercise
Why does reconstruction cost only O(n) even though filling the table costs O(n^3)?
Show answer
Reconstruction visits only the intervals on the optimal split tree, which has n leaves and n - 1 internal nodes. Each visit does one O(1) lookup in the split table, so it never examines any of the other candidate splits.