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
Complexity Analysis: Best Case, Worst Case, and Average Case
The material introduces best‑case, worst‑case, and average‑case complexity as three distinct functions describing an algorithm’s running time on inputs of size , explains how they are formally defined, and shows why worst‑case analysis is usually preferred.
- Best case: , the minimum time over all inputs of size .
- Worst case: , giving a guaranteed upper bound.
- Average case: , requiring an explicit input probability model.
- Linear search illustrates the three cases: best, worst, and average (expected comparisons).
- Worst‑case analysis is favored because it needs no probabilistic assumptions and ensures reliability for all inputs, especially in real‑time or safety‑critical systems.
Banker's Algorithm Safe-State Analysis for Processes $P_0$ Through $P_4$
The course walks through a full Banker's‑algorithm safety analysis for processes –, computing the Need matrix and using the work‑vector test to verify a safe state and a valid completion order.
- Need matrix: , , , , .
- Starting work ; each step selects a process with , releases its allocation, and updates .
- All processes finish, yielding a safe sequence .
- A safe state requires only one such sequence; it does not mean every immediate allocation is safe.
- Exam tip: test processes with minimal or zero Need first to unlock the rest.
Syntax-Directed Translation: Infix to Prefix Notation
The module shows how a syntax‑directed translation scheme using only synthesized attributes can convert infix arithmetic expressions into prefix (Polish) notation while preserving operator precedence and left‑associativity.
- Grammar: E → E + T | E - T | T; T → T * F | F; F → digit, enforcing precedence ( * > + / - ).
- Semantic actions compute a
valstring for each non‑terminal, concatenating the operator before its operand strings. - Example results:
9 - 5 + 2→+ - 9 5 2;9 - 5 * 2→- 9 * 5 2. - Synthesized (S‑attributed) attributes allow immediate bottom‑up evaluation during LR‑style parsing.
- Left‑recursive rules enable left‑associativity; to use LL parsers the grammar must be transformed and inherited attributes introduced.