DSA Notes
Master matrix chain multiplication as the gateway to interval DP — why multiplication order affects cost, the O(n³) interval DP solution, building the DP table from small intervals to large, reconstructing optimal parenthesization, and other interval DP problems like burst balloons and palindrome partitioning.
Why Order of Multiplication Matters
Matrix multiplication is associative: (A×B)×C = A×(B×C). The result is the same, but the number of scalar multiplications depends on the order.
Example: Three matrices: A(10×30), B(30×5), C(5×60)
| Option 1 | (A×B)×C |
| A×B: 10×30×5 = 1500 multiplications | result is 10×5 matrix |
| (A×B)×C | 10×5×60 = 3000 multiplications |
| Total | 4500 |
| Option 2 | A×(B×C) |
| B×C: 30×5×60 = 9000 multiplications | result is 30×60 matrix |
| A×(B×C) | 10×30×60 = 18000 multiplications |
| Total | 27000 |
Problem: Given n matrices, find the optimal parenthesization that minimizes scalar multiplications.
The Interval DP Solution
dp[i][j] = minimum cost to multiply matrices Aᵢ through Aⱼ.
Recurrence:
For all k from i to j-1
dp[i][j] = min(dp[i][k] + dp[k+1][j] + p[i-1] × p[k] × p[j])
Where
dp[i][k] = cost of left group: matrices i..k
dp[k+1][j] = cost of right group: matrices k+1..j
p[i-1]×p[k]×p[j] = cost of multiplying the two resulting matrices
Base case: dp[i][i] = 0 (single matrix, no multiplication needed)
Fill order: From shorter chains to longer chains (bottom-up by length).
Complete Implementation
DP Table Filling Visualization
For p = [10, 30, 5, 60] (3 matrices: A₁=10×30, A₂=30×5, A₃=5×60):
| Fill length=1 (single matrices) | dp[i][i] = 0 |
| dp[0][1] | Multiply A₁A₂ |
| k=0 | dp[0][0] + dp[1][1] + p[0]×p[1]×p[2] = 0+0+10×30×5 = 1500 |
| dp[1][2] | Multiply A₂A₃ |
| k=1 | dp[1][1] + dp[2][2] + p[1]×p[2]×p[3] = 0+0+30×5×60 = 9000 |
| dp[0][2] | Multiply A₁A₂A₃ |
| k=0 | (A₁)(A₂A₃) = dp[0][0]+dp[1][2]+p[0]×p[1]×p[3] = 0+9000+10×30×60 = 27000 |
| k=1 | (A₁A₂)(A₃) = dp[0][1]+dp[2][2]+p[0]×p[2]×p[3] = 1500+0+10×5×60 = 4500 |
Reconstruct Optimal Parenthesization
Other Interval DP Problems
Burst Balloons
Palindrome Partitioning (Minimum Cuts)
When to Use Interval DP
Interval DP fits problems where:
- You're deciding the last operation within a range [i, j]
- The solution for [i, j] depends on sub-ranges [i, k] and [k+1, j]
- You fill the DP table by increasing interval length
Pattern:
Complexity
| Problem | Time | Space |
|---|---|---|
| Matrix Chain Multiplication | O(n³) | O(n²) |
| Burst Balloons | O(n³) | O(n²) |
| Palindrome Partitioning | O(n²) | O(n²) |
| Optimal BST | O(n³) | O(n²) |
All interval DP problems have O(n³) time due to the three nested loops: two for the range and one for the split point.
Summary
Matrix chain multiplication teaches interval DP — the technique for problems where you split a range at every possible point and combine results:
- State:
dp[i][j]= answer for subproblem on range [i, j] - Transition: try all split points k in [i, j-1]
- Fill order: by increasing interval length
- Time: O(n³) — three nested loops
This pattern appears in: matrix chaining, balloon bursting, palindrome partitioning, optimal BST construction, polygon triangulation, and more.
*Next Lesson: DP Interview Problems*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Matrix Chain Multiplication — Interval DP with Complete Analysis.
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