Interval DP: matrix chain, burst balloons, palindrome partitioning
Interval DP solves every subrange, shortest first, combining smaller ranges, as in matrix chain order and burst balloons.
Before you start: Space optimization: rolling rows and variables.
The idea
Two bracketings give the same product: 7500 or 75000 multiplications. Which bracketing is cheapest for a long chain?
Multiplying a × b by b × c costs a · b · c scalar multiplications. The order decides which sizes meet.
Intuition
Every bracketing ends with one last multiplication. It splits A1 … A4 into a left range and a right range.
The best bracketing of A1 … A4 uses the best bracketings of its two sides. So one table cell per range, (Ai, Aj), is enough.
A range only reads shorter ranges. Fill by length: the diagonal first, then each diagonal above it.
Cell (A1, A4) reads ranges of length 1 to 3. When every shorter range is done, all its inputs are ready.
Complexity
About n²/2 cells, each trying up to n splits: O(n³) time and O(n²) space. Listing every bracketing grows exponentially.
The number of bracketings of n matrices is a Catalan number, which grows like 4ⁿ. The table shares every sub-range between them.
Pitfalls
Filling row A1 left to right reads (A2, A3) before it exists. Loop over lengths, or run the row index from the bottom up.
Matrix Ai is p[i - 1] × p[i]. The final product of a split after Ak costs p[i - 1] · p[k] · p[j]; an index off by one gives wrong costs.
The snippet counts matrices from 0, so the same cost reads p[i] · p[k + 1] · p[j + 1]. Pick one convention and check it on two matrices.
Variations
Burst balloons uses the same table. Choose the balloon burst last in each range; the range's outer neighbours stay fixed.
Palindrome partitioning fills a yes/no table by length, then one array of fewest cuts for each prefix.
Code
Pseudocode
matrixChain(p):
n ← length(p) - 1
dp[i][i] ← 0 for every i
for len ← 2 to n
for i ← 0 to n - len
j ← i + len - 1
dp[i][j] ← min over k = i … j - 1 of
dp[i][k] + dp[k + 1][j] + p[i] · p[k + 1] · p[j + 1]
return dp[0][n - 1]
Python
def matrix_chain(p):
n = len(p) - 1
if n < 1:
return 0
dp = [[0] * n for _ in range(n)]
for length in range(2, n + 1):
for i in range(n - length + 1):
j = i + length - 1
dp[i][j] = min(
dp[i][k] + dp[k + 1][j] + p[i] * p[k + 1] * p[j + 1]
for k in range(i, j))
return dp[0][n - 1]
JavaScript
function matrixChain(p) {
const n = p.length - 1;
if (n < 1) return 0;
const dp = Array.from({ length: n }, () => new Array(n).fill(0));
for (let len = 2; len <= n; len++) {
for (let i = 0; i + len - 1 < n; i++) {
const j = i + len - 1;
dp[i][j] = Infinity;
for (let k = i; k < j; k++) {
const cost = dp[i][k] + dp[k + 1][j] + p[i] * p[k + 1] * p[j + 1];
dp[i][j] = Math.min(dp[i][j], cost);
}
}
}
return dp[0][n - 1];
}
Java
static long matrixChain(int[] p) {
int n = p.length - 1;
if (n < 1) return 0;
long[][] dp = new long[n][n];
for (int len = 2; len <= n; len++)
for (int i = 0; i + len - 1 < n; i++) {
int j = i + len - 1;
dp[i][j] = Long.MAX_VALUE;
for (int k = i; k < j; k++)
dp[i][j] = Math.min(dp[i][j],
dp[i][k] + dp[k + 1][j] + (long) p[i] * p[k + 1] * p[j + 1]);
}
return dp[0][n - 1];
}
Practice problems (4)
Matrix chain order: fill the table Warm-up
Four matrices have dimensions p = [4, 2, 3, 1, 3], so A1 is 4 × 2 and A4 is 1 × 3. Follow the table of fewest multiplications. Answer each question before the step plays.
Matrix chain multiplication Standard
dims describes a chain of matrices: matrix i (counting from 1) is dims[i - 1] × dims[i]. Multiplying an a × b matrix by a b × c matrix costs a · b · c scalar multiplications.
Return the fewest scalar multiplications needed to multiply the whole chain. With fewer than two entries in dims there are no matrices; return 0.
Burst balloons Standard
Balloons stand in a row with the values in nums. Bursting balloon i pays left · nums[i] · right coins, where left and right are the values of its current neighbours. A missing neighbour at either end counts as 1. After a burst, the two neighbours become adjacent.
Burst every balloon and return the most coins you can collect.
Palindrome partitioning II Standard
Cut the string s into pieces so that every piece is a palindrome (it reads the same both ways).
Return the fewest cuts needed.
Related topics
Next on the path: State machine DP: stock problems