Definition · The matrix-chain problemA chain of n matrices is given by n + 1 dimensions: p(0), p(1), …, p(n), where matrix i is p(i−1) by p(i). Multiplying a p by q matrix with a q by r matrix costs p·q·r scalar multiplications. The problem is to choose the bracketing — not the order of the matrices, which is fixed — that minimises the total.
Seven numbers describe six matrices. The lab loads 30 35 15 5 10 20 25, so the first matrix is 30 by 35, the second is 35 by 15, and so on. The product is the same matrix whatever the bracketing, because matrix multiplication is associative; only the cost of computing it changes, and on this chain it changes by a factor of nearly four between the best and worst bracketings.
Theorem · The chain recurrenceLet cost(i, j) be the fewest scalar multiplications needed to multiply matrices i through j, with cost(i, i) = 0. Then for i < j, cost(i, j) is the minimum over split points k with i ≤ k < j of cost(i, k) + cost(k+1, j) + p(i)·p(k+1)·p(j+1).
Any bracketing of matrices i through j has a last multiplication, which joins a bracketed product of i through k with a bracketed product of k+1 through j for some k. Those two products have dimensions determined by i, k and j alone, so the cost of the join is p(i)·p(k+1)·p(j+1) whatever the two halves did internally.
If either half were bracketed more cheaply, substituting it in would bracket the whole more cheaply, so each half of an optimal bracketing is itself optimal — optimal substructure, discharged. Taking the minimum over the j − i possible values of k therefore gives the optimum, and the recurrence is exact.
□
The order the recurrence forces, and the order a reader writes
cost(i, j) reads cost(i, k) and cost(k+1, j), and both of those cover shorter intervals than i to j. So the cells have to be filled in increasing order of j − i: all the zero-length intervals, then all the length-one intervals, and so on. That is a diagonal-by-diagonal sweep, and it fills 21 cells of the 36 in a six-by-six table, because only the upper triangle means anything.
The order a reader writes, because it is the order every other table in this course is filled in, is row by row. Row-major visits (0,0), (0,1), (0,2) and so on across the whole first row before it has filled a single cell of the second. But (0,2) needs (1,2), which is in the second row. It reads it anyway.
Example · The cell where row-major goes wrong, in fullCorrect: cost(0,2) = min(cost(0,0) + cost(1,2) + 30·35·5, cost(0,1) + cost(2,2) + 30·15·5) = min(0 + 2625 + 5250, 15750 + 0 + 2250) = 7875. Row-major: when it reaches (0,2), the cell (1,2) has not been written, so the first option evaluates to 0 + 0 + 5250 = 5250, which is smaller than 18 000, and 5250 is stored. The table now contains a number that no bracketing of those three matrices achieves.
The damage propagates. Every later cell that reads (0,2) reads 5250, and the final answer in the corner comes out 9 000. Thirty-five reads over the whole fill land on cells that have not been written, and the lab counts them and paints them red. Fill by interval length and that count is zero.
Why the wrong table does more work and still finishes
Row-major writes all 36 cells; by-length writes 21. So the wrong order is not a shortcut — it does more work, produces a fuller-looking table, and reports a number two-fifths below the truth. Both orders make the same 70 reads, because the recurrence is the same recurrence; what differs is whether the read finds anything.
The measured counts here are 15 125 and 9 000 on one chain of six matrices, and 42 bracketings agreeing with the first of them. The proved bound is a different statement: the table has Θ(n²) meaningful cells and each takes O(n) work, so filling it is Θ(n³), against Catalan(n−1) bracketings for the enumeration — 42 at six matrices, 132 at seven, 1 430 at nine. The enumeration is the only reason we know 15 125 is right, and it is also the thing that stops being runnable first.