Matrix Chain Multiplication with Dynamic Programming: Optimal Parenthesization for
We are given a matrix chain with dimension array . This means the matrices are2:
The goal of matrix chain multiplication is not to change the final product, since matrix multiplication is associative, but to minimize the total number of scalar multiplications required.2
If a matrix of size is multiplied by a matrix of size , then the cost is:
Dynamic programming is used because the problem has optimal substructure and overlapping subproblems.2
We define:
with recurrence2:
Base case:
The dynamic programming table-filling algorithm runs in time and uses space, where is the number of matrices.2
Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity.
Matrix chain multiplication - Wikipedia - Overview of the optimization problem and why parenthesization matters.
Matrix Chain Multiplication - GeeksforGeeks - Summary of recurrence, DP table method, and complexity.
Footnotes
-
Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity. ↩ ↩2 ↩3 ↩4 ↩5
-
Matrix chain multiplication - Wikipedia - Overview of the problem, associativity, and Catalan-number growth of parenthesizations. ↩ ↩2 ↩3
-
Matrix Chain Multiplication - GeeksforGeeks - Practical explanation of the recurrence and complexity bounds. ↩ ↩2 ↩3 ↩4 ↩5
Matrix Chain Multiplication - Dynamic Programming
Key Observation
The dimensions determine the multiplication cost, not the values inside the matrices. Different valid parenthesizations can have very different costs.2
Footnotes
-
Matrix chain multiplication - Wikipedia - Overview of the problem, associativity, and Catalan-number growth of parenthesizations. ↩
-
Matrix Chain Multiplication - GeeksforGeeks - Practical explanation of the recurrence and complexity bounds. ↩
Let us encode the matrices more formally using the dimension array :
| Matrix | Size |
|---|---|
Thus there are matrices. A brute-force strategy would examine all full parenthesizations, whose count grows according to the Catalan number sequence and is exponential in effect for this problem size growth. Dynamic programming avoids recomputing the same subchains by filling tables diagonally by chain length.2
We maintain two tables:
For this instance, we will compute all chain lengths from to .
Footnotes
-
Matrix chain multiplication - Wikipedia - Overview of the problem, associativity, and Catalan-number growth of parenthesizations. ↩
-
Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity. ↩ ↩2
-
Matrix Chain Multiplication - GeeksforGeeks - Practical explanation of the recurrence and complexity bounds. ↩
Dynamic Programming Solution: Fill the Cost Table
- 1Step 1
Set all diagonal entries to zero because multiplying one matrix requires no multiplication: for .2
Footnotes
-
Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity. ↩
-
Matrix Chain Multiplication - GeeksforGeeks - Practical explanation of the recurrence and complexity bounds. ↩
-
- 2Step 2
For each adjacent pair, compute one direct multiplication cost using . This gives: , , , and .
- 3Step 3
For each subchain of 3 matrices, try both possible split points and keep the minimum. For example, for , compare splitting after versus after .
- 4Step 4
For each subchain of 4 matrices, try all 3 split points. Use already-computed smaller subproblems from the table, which is the core dynamic programming idea.
Footnotes
-
Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity. ↩
-
- 5Step 5
For , try splits at and choose the smallest total. The corresponding split locations recorded in let us reconstruct the optimal parenthesization.
- 6Step 6
Starting from , recursively split the chain into left and right optimal subchains until single matrices remain. This prints the optimal parenthesization.
Footnotes
-
Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity. ↩
-
1. Base cases
Since a single matrix does not need multiplication:
2. Chains of length
For two matrices there is only one way to multiply.
3. Chains of length
For :
- Split at :
- Split at :
So,
For :
- Split at :
- Split at :
So,
For :
- Split at :
- Split at :
So,
All of these computations directly apply the standard recurrence for matrix-chain dynamic programming.2
Footnotes
-
Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity. ↩
-
Matrix Chain Multiplication - GeeksforGeeks - Practical explanation of the recurrence and complexity bounds. ↩
How to Read the Recurrence
The term is the best cost of solving the two subchains, and is the cost of multiplying the two resulting matrices together.
Footnotes
-
Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity. ↩
4. Chains of length
For , try :
- :
- :
- :
Therefore,
For , try :
- :
- :
- :
Therefore,
5. Full chain of length
Now compute by trying all splits :
- :
- :
- :
- :
Hence the optimal cost is:
and the best top-level split is:
This means the chain is optimally split as:
Then from , the right part is split as:
So the full optimal parenthesization is:
or equivalently
with minimum scalar multiplications:
Minimum Cost by Subchain Length
Optimal dynamic programming costs obtained for representative subchains
Dynamic Programming Tables
Cost table
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | 0 | 120 | 264 | 1080 | 1344 |
| 2 | - | 0 | 360 | 1320 | 1350 |
| 3 | - | - | 0 | 720 | 1140 |
| 4 | - | - | - | 0 | 1680 |
| 5 | - | - | - | - | 0 |
Split table
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | - | 1 | 2 | 2 | 2 |
| 2 | - | - | 2 | 2 | 2 |
| 3 | - | - | - | 3 | 4 |
| 4 | - | - | - | - | 4 |
| 5 | - | - | - | - | - |
The entries in the split table show where each optimal subproblem divides. This is the basis for backtracking the final parenthesization after the cost table is complete.2
Footnotes
-
Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity. ↩
-
Matrix-Chain Multiplication PDF, Columbia University - Lecture notes showing worked matrix-chain examples and parenthesization logic. ↩
1(A1A2)((A3A4)A5)
Detailed Reasoning and Complexity
Common Mistake
Do not multiply dimensions directly from the original matrices after each step without updating the intermediate result sizes. Each parenthesization creates new matrix dimensions that affect later costs.
Footnotes
-
Matrix-Chain Multiplication PDF, Columbia University - Lecture notes showing worked matrix-chain examples and parenthesization logic. ↩
Final Answer
The most efficient parenthesization of the matrix chain
for dimensions
is:
or fully parenthesized:
Total scalar multiplications
-
:
-
:
-
:
intermediate size is , so -
:
intermediate sizes are and , so
Therefore,
Complexity of the dynamic programming method
For a chain of matrices:
- Time complexity:
- Space complexity:
These are the standard complexity bounds for bottom-up matrix-chain multiplication dynamic programming.2
Footnotes
-
Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity. ↩
-
Matrix Chain Multiplication - GeeksforGeeks - Practical explanation of the recurrence and complexity bounds. ↩
Knowledge Check
What is the optimal parenthesization for the matrix chain with dimensions ?
Explore Related Topics
Dynamic Programming and Greedy Algorithms: Comparative Analysis, Failure Cases, and Core DP Properties
The article contrasts greedy algorithms with dynamic programming, shows when greedy fails and DP is necessary, and explains DP’s core properties of optimal substructure and overlapping subproblems.
- Greedy makes irrevocable local choices and works only with the property, while DP stores and reuses subproblem results to guarantee optimality.
- Counterexamples such as coin change ( for amount 6) and knapsack illustrate failures of greedy and the need for DP recurrences like and .
- DP relies on to form recurrences and on overlapping subproblems to justify caching.
- Design steps: recognize structure, detect repetition, formulate recurrence, compute once (memoization/tabulation), optionally reconstruct solution.
- Rule of thumb: use greedy if local choices can be proved globally safe; otherwise apply DP.
Memory Allocation with First-Fit and Best-Fit
Round Robin Scheduling Analysis for Five Processes with Time Quantum 3 ms