On this page
Matrix multiplication is associative: (AB)C and A(BC) give the same answer, but not for the same work. The matrix chain multiplication article on Wikipedia states the rule everything rests on: multiplying an X×Y matrix by a Y×Z matrix the straightforward way needs XYZ ordinary multiplications. So the bracketing decides the cost, and dynamic programming finds the cheapest one without trying them all.
Why does the order of matrix multiplication matter?
If an intermediate product is thin, later steps are cheap; if it is a large square block, every later step pays for it. A p×q times q×r product costs pqr and gives a p×r matrix. Write the chain's sizes as one list p₀, p₁, …, pₙ, where Aᵢ is pᵢ₋₁×pᵢ.
How much can a bad bracketing cost? A worked 4-matrix example
Take four matrices with the size list 100, 10, 100, 10, 50:
A₁ is 100×10
A₂ is 10×100
A₃ is 100×10
A₄ is 10×50
Four matrices can be bracketed five ways. Here is each one, step by step, using the pqr rule from the Wikipedia article:
((A₁A₂)A₃)A₄: 100,000 + 100,000 + 50,000 = 250,000
(A₁(A₂A₃))A₄: 10,000 + 10,000 + 50,000 = 70,000
(A₁A₂)(A₃A₄): 100,000 + 50,000 + 500,000 = 650,000
A₁((A₂A₃)A₄): 10,000 + 5,000 + 50,000 = 65,000
A₁(A₂(A₃A₄)): 50,000 + 50,000 + 50,000 = 150,000
The best order, A₁((A₂A₃)A₄), needs 65,000 multiplications; the worst, (A₁A₂)(A₃A₄), needs 650,000, exactly 10 times as many. The gap starts with the first step: A₁A₂ builds a 100×100 matrix that every later step pays for, while A₂A₃ builds a 10×10 one. Even plain left-to-right costs almost four times the best.
How does the dynamic programming table m[i][j] work?
Let m[i][j] be the fewest multiplications needed to compute Aᵢ…Aⱼ. A single matrix costs nothing, so m[i][i] = 0. For a longer stretch, the last multiplication splits it at some k into (Aᵢ…Aₖ)(Aₖ₊₁…Aⱼ), so:
m[i][j] = min over i ≤ k < j of m[i][k] + m[k+1][j] + pᵢ₋₁·pₖ·pⱼ
Store the winning k in a second table, s[i][j]. Fill the tables by chain length: all pairs, then all triples, up to the whole chain. There are about n²/2 cells and each tries up to n splits, which is why the dynamic programming solution runs in O(n³) time instead of exponential time.
Filling the table for our example
Length 2:
m[1][2] = 100·10·100 = 100,000
m[2][3] = 10·100·10 = 10,000
m[3][4] = 100·10·50 = 50,000
Length 3:
m[1][3]: k = 1 gives 0 + 10,000 + 100·10·10 = 20,000; k = 2 gives 100,000 + 0 + 100·100·10 = 200,000. So m[1][3] = 20,000 and s[1][3] = 1.
m[2][4]: k = 2 gives 0 + 50,000 + 10·100·50 = 100,000; k = 3 gives 10,000 + 0 + 10·10·50 = 15,000. So m[2][4] = 15,000 and s[2][4] = 3.
Length 4, the whole chain:
k = 1: 0 + 15,000 + 100·10·50 = 65,000
k = 2: 100,000 + 50,000 + 100·100·50 = 650,000
k = 3: 20,000 + 0 + 100·10·50 = 70,000
So m[1][4] = 65,000 with s[1][4] = 1, the same answer as the brute-force list.
Reading back the brackets
Start at s[1][4] = 1: the last step is A₁ times (A₂A₃A₄). Then s[2][4] = 3: that part is (A₂A₃)A₄. The order is A₁((A₂A₃)A₄). The hidden Markov model forward algorithm uses the same pattern: a table filled in an order where every cell needs only cells already done.
How many ways can you bracket n matrices?
The Catalan number Cₙ counts the ways n + 1 factors can be completely parenthesized. So a chain of n matrices has Cₙ₋₁ bracketings. The OEIS entry for the Catalan numbers begins 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862. Four matrices give C₃ = 5, our five orders. Ten matrices already give C₉ = 4,862, and Wikipedia gives the growth as Θ(4ⁿ/n^(3/2)), so checking every order soon stops being practical. The same numbers count triangulations of a convex polygon.
O(n³) is not the limit: T. C. Hu and M. T. Shing gave a more complex O(n log n) algorithm in Computation of Matrix Chain Products, Part II (SIAM Journal on Computing, 1984). The O(n³) table is the one to learn first.
How should you practise matrix chain multiplication?
Write the size list p₀…pₙ and check neighbouring sizes match.
Fill the diagonal with zeros, then lengths 2, 3, up to n.
For each cell, write every k with its three terms before taking the minimum.
Record s[i][j] beside m[i][j].
Read the brackets back from s[1][n] and recompute that order's cost as a check.
Try it on the size list 10, 20, 50, 1, 100. If matrix basics feel shaky, revise the Class 12 matrices and determinants key terms first. The Fermi Concept Library covers every concept in mathematics and computing, each with its own lesson and its place in the map. To test yourself, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



