Matrix Chain Multiplication
Matrix Chain Multiplication is a GFG problem (Hard). You are given an array arr of n + 1 numbers describing n matrices: matrix i has arr[i-1] rows and arr[i] columns. Return the fewest scalar multiplications needed to multiply the whole chain.
- Multiplying a
p × qmatrix by aq × rmatrix costsp · q · rmultiplications and gives ap × rmatrix. - You may choose any bracketing, but not reorder the matrices.
- A single matrix needs no work, so
arr = [3, 4]gives 0.
- 2 <= arr.size() <= 100
- 1 <= arr[i] <= 200
Intuition
Every bracketing gives the same product, because matrix multiplication is associative, but the costs differ a lot. For 2×1, 1×3, 3×4, multiplying the left pair first costs 30, the right pair first only 20.
Trying every bracketing is exponential. Instead, ask which multiplication happens last. If the last one joins matrices i..k with k+1..j, both sides were already reduced to single matrices, independently of each other, and the final step costs arr[i-1] · arr[k] · arr[j]. So the best cost for a chain is the cheapest choice of k of: best left side + best right side + that final cost.
Approach
Two ways to fill the chain table
Fill all chains of length 2, then 3, up to the whole chain.
- No recursion: nothing to overflow.
- Clear order: every half is ready before it is read.
The usual final answer.
Write the split rule as a recursive function and cache each (i, j).
- Closest to the idea: the code is the recurrence.
- Lazy: only chains that are needed get solved.
A good first version before switching to the table.
Both compute the same O(n²) chains with O(n) splits each. The bottom-up table is traced below; the recursive version follows the demo.
Define one cell per chain
Let dp[i][j] be the fewest multiplications to turn matrices i through j into one matrix (1-indexed). A chain of one matrix costs nothing, so dp[i][i] = 0.
Try every split point
For i < j, the last multiplication can sit after any k from i to j - 1:
dp[i][j] = min over k of dp[i][k] + dp[k+1][j] + arr[i-1] · arr[k] · arr[j].
The left result is arr[i-1] × arr[k] and the right is arr[k] × arr[j], which is where the three factors come from.
Fill by chain length
Both halves are shorter than the chain being solved, so fill all chains of length 2 first, then length 3, and so on up to n. The answer is dp[1][n].
Recover the brackets
Store the winning k for each cell in a second table. To print the order, split the whole chain at its k, wrap each side in brackets, and repeat on each side until a side is a single matrix.
Code in Python, C++ and Java
Recursion with a memo
solve(i, j) returns the cheapest cost for matrices i..j. It tries every split k and caches the result, so each of the O(n²) chains is solved once. Without the cache the same chains are solved again and again and the running time is exponential.
Common pitfalls
Filling row by row instead of by length
for i in range(1, n + 1):
for j in range(i + 1, n + 1):for length in range(2, n + 1):
for i in range(1, n - length + 2):
j = i + length - 1dp[i][j] reads dp[k+1][j], a later row that row-by-row order has not filled, so it reads 0. On [1, 2, 3, 4, 3] that returns 6 instead of 30.
Using arr[i] for the left side's rows
arr[i] * arr[k] * arr[j]
arr[i - 1] * arr[k] * arr[j]
Matrix i is arr[i-1] × arr[i], so the joined left side has arr[i-1] rows. On [2, 1, 3, 4] the wrong index returns 15 instead of 20.
Letting k reach j
for k in range(i, j + 1)
for k in range(i, j)
With k = j the right side k+1..j is empty. It reads dp[j+1][j], which is 0 or out of range, and invents a split that does not exist.
Complexity
Matrix Chain Multiplication FAQ
Why not just multiply the cheapest pair first?
Because the cheap step can create an expensive matrix. With arr = [5, 1, 10, 10] the cheapest pair is the first one (50), but it leaves a 5 × 10 matrix and the next step costs 500, for 550 in total. Multiplying the right pair first costs 100 + 50 = 150. Only comparing every last split is guaranteed to find the minimum.
Is there something faster than O(n³)?
Yes. Hu and Shing (1981) found an O(n log n) matrix chain multiplication algorithm by turning the matrix chain into a polygon and cutting it into triangles. It is far longer to write, and the cubic DP is what interviews expect.
What other problems use the same split pattern?
Any problem where a range is solved by choosing one split or one last action inside it: Burst Balloons, minimum cost to cut a stick, and palindrome partitioning all fill a range table the same way.