DSA Notes
Master the matrix chain multiplication problem with optimal parenthesization, gap-based DP table filling, and complete implementation.
The Problem
Given a chain of matrices A1, A2, ..., An, find the most efficient way to multiply them. Matrix multiplication is associative — you can parenthesize in any order and get the same result, but different orderings require vastly different numbers of scalar multiplications.
Example: Matrices A(10×30), B(30×5), C(5×60)
- (AB)C: (10×30×5) + (10×5×60) = 1500 + 3000 = 4500 operations
- A(BC): (30×5×60) + (10×30×60) = 9000 + 18000 = 27000 operations
Same result, but (AB)C is 6x faster! For long chains, the difference is astronomical.
Problem Formulation
Given dimensions: p = [p0, p1, p2, ..., pn] where matrix Ai has dimensions p[i-1] × p[i].
Find the minimum number of scalar multiplications needed to compute the product A1 × A2 × ... × An.
The DP Approach
State: dp[i][j] = minimum cost to multiply matrices from i to j (inclusive)
Transition: Try every possible split point k between i and j:
dp[i][k]: cost to compute left portion (matrices i through k)dp[k+1][j]: cost to compute right portion (matrices k+1 through j)p[i-1] * p[k] * p[j]: cost to multiply the two resulting matrices
Base: dp[i][i] = 0 (single matrix, no multiplication needed)
Answer: dp[1][n]
Gap-Based Table Filling
This is an interval DP problem. We must fill the table by increasing gap (length of chain):
- Gap 1: chains of length 1 (base case = 0)
- Gap 2: chains of length 2 (one split point)
- Gap 3: chains of length 3 (two split points)
- ... up to gap n
Dry Run
| Dimensions: p = [10, 30, 5, 60] | 3 matrices |
| A1 | 10×30, A2: 30×5, A3: 5×60 |
| Base | dp[1][1] = dp[2][2] = dp[3][3] = 0 |
| dp[1][2]: k=1 | dp[1][1] + dp[2][2] + 10*30*5 = 0+0+1500 = 1500 |
| dp[2][3]: k=2 | dp[2][2] + dp[3][3] + 30*5*60 = 0+0+9000 = 9000 |
| k=1 | dp[1][1] + dp[2][3] + 10*30*60 = 0+9000+18000 = 27000 |
| k=2 | dp[1][2] + dp[3][3] + 10*5*60 = 1500+0+3000 = 4500 |
| Answer | dp[1][3] = 4500 |
Python Implementation
C++ Implementation
Time and Space Complexity
- Time: O(n³) — O(n²) subproblems, each examining O(n) split points
- Space: O(n²) — for the DP table
Understanding the Gap-Based Filling Order
| Matrix indices | 1 2 3 4 |
| Gap 1 | dp[1][1], dp[2][2], dp[3][3], dp[4][4] (all 0) |
| Gap 2 | dp[1][2], dp[2][3], dp[3][4] |
| Gap 3 | dp[1][3], dp[2][4] |
| Gap 4 | dp[1][4] ← ANSWER |
Interval DP Pattern
Matrix Chain Multiplication is the canonical example of interval DP. The pattern appears in many problems:
- Burst Balloons: Split array, choose optimal burst order
- Palindrome Partitioning: Minimum cuts to make all substrings palindromes
- Optimal BST: Construct BST with minimum search cost
- Stone Game: Merge piles with minimum cost
The common structure:
Practical Insight
In practice, matrix chain multiplication matters for:
- Scientific computing (choosing multiplication order for large matrices)
- Database query optimization (join ordering)
- Compiler optimization (expression evaluation order)
- Any problem where combining two sub-results has a cost that depends on their sizes
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Matrix Chain Multiplication.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, dynamic, programming, matrix
Related Data Structures & Algorithms Topics