Interval DP
Many problems ask for the best way to combine a contiguous range of items, and the answer is a dynamic program over subintervals that chooses a split point . We derive the pattern from matrix-chain multiplication — parenthesising a product to minimize scalar multiplications in — distil it into a reusable template filled by increasing interval length, and then meet its sharpest variant: the "last operation" trick behind Burst Balloons and cutting a stick, where fixing the last move (not the first) makes the two sides independent.
╌╌╌╌
The sequence DPs we have met so far walked a string or array from one end and let
the subproblem be a prefix: the state was the best answer using the first items.
A second, equally common family does not decompose by prefix at all. It
decomposes by contiguous range. The natural subproblem is "the best answer
for the slice ," and the recurrence builds a long range out of two shorter
ranges by guessing where they meet: a split point inside . This
is interval dynamic programming, one of the core DP
patterns, and once you see its
shape you will find it everywhere: parenthesising a product, building an optimal
search tree, bursting balloons, cutting a stick, partitioning a string.
The archetype, and the cleanest place to learn the pattern, is the problem of parenthesising a matrix product.
Matrix-chain multiplication
Multiplying a matrix by a matrix takes scalar multiplications. Matrix multiplication is associative, so for a chain the answer is the same however we parenthesise, but the cost is not. Given a chain of matrices where has dimensions (so the dimension sequence is ), we want the parenthesisation that minimizes the total number of scalar multiplications.1
The number of parenthesisations grows like the Catalan numbers, exponentially, so brute force is hopeless. But the problem has the two features every DP needs. Optimal substructure: in the optimal parenthesisation of , the outermost multiplication splits the chain at some into , and each side must itself be optimally parenthesised: if a cheaper parenthesisation of a side existed, substituting it would lower the total. Overlapping subproblems: both sides are again contiguous subchains, and the same subchain is reached by many different outer choices.
Let be the minimum number of scalar multiplications needed to compute . A single matrix needs no multiplications, and otherwise we try every split:
The term is the cost of the final multiplication: the left block is a matrix, the right block is , and combining them costs .
Filling the table
The recurrence for depends only on intervals strictly shorter than (the left side has length , the right side ). So if we fill the table in order of increasing interval length , every value we read is already computed. We also record, in a split table , the that achieved the minimum, so we can reconstruct the parenthesisation afterwards.
- 1
- 2for to do
- 3
- 4for to do= chain length
- 5for to do
- 6
- 7
- 8for to dotry every split
- 9
- 10if then
- 11
- 12
- 13return
There are entries and each costs to fill (the inner loop over ), so the algorithm runs in time and space. The answer is ; the optimal parenthesisation is read back from by recursing: gives the outermost split, then and give the next ones, and so on down to single matrices.
The diagonal (length ) is the base case; each successive diagonal moving toward the top-right corner holds longer intervals, and cell draws on cells in its own row to the left (the terms) and its own column below (the terms). That dependency shape, left along the row and down the column, is the signature of an interval DP.
The interval-DP recipe
Strip matrix-chain of its specifics and a reusable pattern remains.
The art in any specific problem is steps 1 and 3: defining the slice so that the two sides really are independent, and finding the cost term that depends only on the endpoints and the split. The rest is bookkeeping.
Optimal binary search tree
A first variation keeps the split idea but changes what the cost term measures. Given sorted keys with search probabilities , an optimal binary search tree is the BST minimizing the expected search cost . Choosing key as the root of the subtree on keys splits the rest into a left subtree on and a right subtree on , again two independent ranges combined at a pivot. Making the root pushes every key in down one level, which adds the total weight to the cost:
Picking root hangs the two solved subtrees beneath it, and the act of adding a root pushes every key in one level deeper, so its whole weight is charged once more:
The shape is identical to matrix-chain, with the same diagonals and the same fill, the combine term now being the swept-down weight of the whole interval rather than a product of dimensions.2
The last operation
trick: Burst Balloons and cutting a stick
Some problems resist the naive split because the cost of a piece depends on what is adjacent to it, and the first split severs exactly the adjacency that sets the cost. The fix is to guess the last operation instead of the first.
Take Burst Balloons: balloons have values , and bursting balloon earns where left and right are its current surviving neighbors; bursting removes and rejoins the neighbors. We want the maximum total earnings. If we try to fix the first balloon burst in , the two halves are not independent: a balloon in the left half can later have a right neighbor in the right half, so the halves keep interacting through the shared boundary.
Now fix the balloon that is burst last in the open interval (using sentinels just outside the range that are never burst). When is burst last, every other balloon in is already gone, so its two neighbors at that moment are exactly the boundaries and . The earnings for that final burst are , fixed and independent of order; the balloons in were all burst before while and stood as fixed walls, and likewise played out against walls and . The two sides never touch. They are independent:
Minimum Cost to Cut a Stick is the same idea in dual form. A stick has cut positions inside it; making a cut costs the current length of the piece being cut. Fix which cut in is performed last: at that moment the piece runs uncut from wall to wall , so the cost is exactly the length , fixed; and the cuts in and were made earlier, each within its own sub-piece, independently. Same recurrence, over the cut positions. The lesson generalizes: when the per-step cost depends on neighbors, ask which step is last, since the last step is the one whose context is fully determined by the interval endpoints.
Palindrome partitioning over a string
Interval DP also runs over strings. In Palindrome Partitioning II we cut a string into pieces that are each palindromes, minimizing the number of cuts. Precompute , whether is a palindrome, itself an interval DP, since is a palindrome iff and is (a length- shorter interval). Then let be the fewest cuts for the prefix ; for each we look back to the last palindromic piece:
The palindrome table is the interval DP (filled by increasing length); the cut count is then a sweep over it, a clean example of one interval DP feeding a second, simpler one.
When is too slow
The cost comes from the inner scan over all splits . For a class of interval DPs (matrix-chain, optimal BST, and others whose cost obeys the quadrangle inequality) the optimal split point is monotone in the endpoints, so the search for can be confined to a shrinking window. This is Knuth's optimization, and it drops the running time to . We treat the conditions and the proof in the lesson on DP optimizations; for now, note only that the here is not always the last word.3
Takeaways
- Interval DP solves problems over a contiguous range: the state is a slice , the recurrence picks a split or pivot that breaks the range into two independent parts, and the table is filled by increasing interval length so both parts are ready, typically states splits .
- Matrix-chain multiplication is the archetype: , , with a split table to reconstruct the parenthesisation in time and space.
- Optimal BSTs reuse the same shape, replacing the combine term with the swept weight of the interval.
- The
last operation
trick (Burst Balloons, Minimum Cost to Cut a Stick) fixes which move happens last in , not first, pinning the moving neighbor cost to the fixed interval walls and making the two sides truly independent. - Palindrome Partitioning II runs interval DP over a string: a palindrome table by increasing length, then a linear cut-count sweep.
- Knuth's optimization cuts matrix-chain-style DPs to when the quadrangle inequality makes the optimal split monotone — see the DP-optimizations lesson.
Footnotes
- CLRS, Ch. 15 — Dynamic Programming (§15.2): matrix-chain multiplication, the recurrence, and reconstruction from the split table in . ↩
- CLRS, Ch. 15 — Dynamic Programming (§15.5): optimal binary search trees as the same interval DP with the interval-weight combine term. ↩
- Skiena, § — Dynamic Programming: interval DPs as range subproblems combined at a split, and when monotonicity (Knuth) lowers the cubic cost. ↩
╌╌ END ╌╌