The idea: the order of a product changes its cost

To compute a product of several matrices A1 × A2 × … × An you multiply two at a time. Matrix multiplication is associative: (A1A2)A3 and A1(A2A3) give the same matrix. But they do not cost the same. The matrix chain problem asks for the order, the parenthesization, that needs the fewest scalar multiplications. The page solves it with dynamic programming: a cost table filled from short chains to long ones, and a split table that remembers where each best order splits.

Three matrices A1 10×100, A2 100×5, A3 5×50. Left tree (A1A2)A3: A1A2 costs 10·100·5 = 5,000 and gives a 10×5 matrix, times A3 costs 10·5·50 = 2,500, total 7,500, marked right. Right tree A1(A2A3): A2A3 costs 100·5·50 = 25,000 and gives a 100×50 matrix, times A1 costs 10·100·50 = 50,000, total 75,000, marked wrong.
The same product, two orders: (A1A2)A3 needs 7,500 multiplications, A1(A2A3) needs 75,000, ten times as many.

The cost of one product

Write the sizes as a list of dimensions p0, p1, …, pn: matrix Ai is pi−1 × pi, so neighbours fit together. Multiplying a p × q matrix by a q × r matrix with the school method computes p·r entries, each a sum of q products: p·q·r multiplications, and the result is p × r. With 10×100, 100×5 and 5×50, doing A1A2 first leaves a small 10×5 matrix; doing A2A3 first builds a big 100×50 one. (Demo: 3 matrices, (AB)C vs A(BC))

Why not try every order?

Every order has a last multiplication, which splits the chain into a left part and a right part, each ordered in its own way. Counting the orders this way gives the Catalan numbers: n matrices have Cn−1 orders.

matrices n3461020
orders Cn−125424,8621,767,263,190

The count grows about like 4n, so trying them all is only possible for tiny chains.

The recurrence: try every last split

Let m[i][j] be the fewest multiplications for Ai..Aj. If the best order's last multiplication splits at k, it multiplies the result of Ai..Ak (a pi−1 × pk matrix) by the result of Ak+1..Aj (pk × pj). Both parts must themselves be done in their cheapest way, or the whole would not be cheapest. This is optimal substructure. We don't know k, so we try them all:

m[i][i] = 0,   m[i][j] = min over i ≤ k < j of m[i][k] + m[k+1][j] + pi−1·pk·pj

The split table s[i][j] stores the k that gave the minimum. On the canvas, the candidate list under the split table shows every k for the cell being filled; the blue and green cells are the two halves of the winning split, and the matrices in the chain are coloured the same way.

Cost table for dimensions 40, 20, 30, 10, 30. Length 1 diagonal: zeros. Length 2: m[1][2] = 24,000, m[2][3] = 6,000, m[3][4] = 9,000. Length 3: m[1][3] = 14,000, m[2][4] = 12,000. Length 4: m[1][4] = 26,000. m[1][4] tries k = 1: 0 + 12,000 + 40·20·30 = 36,000; k = 2: 24,000 + 9,000 + 40·30·30 = 69,000; k = 3: 14,000 + 0 + 40·10·30 = 26,000, the smallest. The split table gives the order ((A1(A2A3))A4).
The table fills one diagonal (one chain length) at a time; m[1][4] reads cells from shorter diagonals and keeps k = 3, which leads to ((A1(A2A3))A4).

Filling by chain length

A cell m[i][j] reads only cells for shorter chains. So the page fills the table by length l = j − i + 1: first the diagonal (length 1, all zeros), then all chains of two matrices, then three, up to m[1][n], the answer. Each Step fills one cell. Filling row by row from the top would be wrong: m[1][3] needs m[2][3], which sits in a later row. (Demo: 4 matrices, fill the table by length)

Trace back: the split table gives the order

The cost table only says how cheap the best order is. To get the order itself, start at s[1][n] = k: the last multiplication is (A1..Ak) × (Ak+1..An). Then look up s for each half, and so on, until every part is a single matrix. The page shows unfinished parts in brackets, like ([A1..A3][A4..A6]). For the CLRS chain 30, 35, 15, 5, 10, 20, 25 the result is ((A1(A2A3))((A4A5)A6)) with 15,125 multiplications. (Demo: 6 matrices from CLRS)

The demos in numbers

dimensionsbestbest orderleft to rightright to leftworst
10, 100, 5, 507,500((A1A2)A3)7,50075,00075,000
40, 20, 30, 10, 3026,000((A1(A2A3))A4)48,00051,00069,000
30, 35, 15, 5, 10, 20, 2515,125((A1(A2A3))((A4A5)A6))40,50047,50058,000
100, 100, 100, 100, 130,000(A1(A2(A3A4)))2,010,00030,0002,010,000
5, 10, 3, 12, 5, 50, 62,010((A1A2)((A3A4)(A5A6)))3,3802,55613,360

The fourth row is the everyday case of matrices times a vector, A·B·C·x: multiplying from the right keeps every intermediate result a vector, which is 67 times cheaper here. (Demo: matrices times a vector) The last row shows that the best order can be neither end first: it multiplies pairs in the middle and joins them. (Demo: the best order is balanced, neither end first) A greedy rule such as "always do the cheapest product first" can also miss the best order; only the table compares all splits.

Running time and space

There are n(n − 1)/2 cells above the diagonal, and a cell for a chain of length l tries l − 1 splits. In total that is (n³ − n)/6 split checks, Θ(n³) time, and Θ(n²) space for the two tables: 35 checks for 6 matrices instead of 42 whole orders, and 1,330 checks for 20 matrices instead of 1.7 billion orders. The same "interval DP" shape, a table over ranges filled by length with a split in the middle, also solves optimal binary search trees, polygon triangulation and parsing with CYK.

See also Matrix Multiplication for what one product computes, Longest Common Subsequence and 0/1 Knapsack for other dynamic-programming tables with a trace back.

What the page leaves out

Chains are capped at 6 matrices and dimensions at 200 so both tables fit on the canvas. The page counts only scalar multiplications of the school method; it leaves out additions, memory traffic and cache effects, faster multiplication algorithms such as Strassen's, the memoized top-down version of the same recurrence, and Hu and Shing's O(n log n) algorithm for this problem.