Sub topic 3
Choosing a Split Point
This chapter assumes you know basic dynamic programming, recursion, and the inclusive interval state dp[i][j] from the earlier chapters. It covers the most common recurrence shape in interval DP: cutting a range into two smaller ranges at a split point.
The core idea
Many interval problems have this property: any solution for the range i..j ends with one final step that joins a left part and a right part. If you knew where that boundary was, the answer would be:
- the best answer for the left part,
- plus the best answer for the right part,
- plus the cost of joining them.
You don't know where the boundary is. So you try every possible position k and keep the best result. That position is the split point.
With inclusive bounds, the recurrence for a minimization problem is:
dp[i][j] = min over k in [i, j-1] of ( dp[i][k] + dp[k+1][j] + join(i, k, j) )The left part is i..k and the right part is k+1..j. Both are strictly shorter than i..j, so the recurrence always moves toward the base cases.
The join cost
The join term is where problems differ. It can depend on all three indices or on only some of them:
| Problem | Join cost |
|---|---|
| Matrix chain multiplication | `dims[i] * dims[k+1] * dims[j+1]` (depends on `k`) |
| Merging adjacent piles | sum of `i..j` (independent of `k`) |
| Optimal binary search tree | sum of frequencies in `i..j` |
When the join cost doesn't depend on k, you can add it once, outside the minimum. That small fact matters later for prefix sums and Knuth's optimization.
The recurrence as code
The function below is a direct translation of the recurrence. It has no caching, so it runs in exponential time. Its purpose is to show the shape. Later chapters make it efficient.
def best(i, j, join):
# Inclusive interval i..j. A single element needs no joining.
if i == j:
return 0
result = float("inf")
for k in range(i, j): # k = i .. j-1
result = min(result, best(i, k, join) + best(k + 1, j, join) + join(i, k, j))
return result
dims = [10, 30, 5, 60] # three matrices: 10x30, 30x5, 5x60
matrix_join = lambda i, k, j: dims[i] * dims[k + 1] * dims[j + 1]
print(best(0, 2, matrix_join)) # 4500Common errors
- Off-by-one in the
krange. With inclusive bounds,kruns fromitoj-1. If you letk = j, the right partj+1..jis empty, and you may read garbage or count an extra term. - Mixing bound conventions. With exclusive bounds
(left, right), as in burst balloons,kruns overleft+1..right-1and both sides sharekas an endpoint. Pick one convention and write it down before you code. - Wrong initial value. Start a minimum at infinity and a maximum at negative infinity (or 0 if every answer is non-negative). If you start a minimum at 0, it never changes.
- Assuming the split is the last operation. In matrix chain, the split is the last multiplication. Some problems need you to rethink what the split means. The burst balloons chapter covers this.
Exercise
For an inclusive interval i..j of length L = j - i + 1, how many split points does the recurrence try, and what are the left and right parts when k = j - 1?
Show answer
It tries L - 1 split points (k = i through j - 1). When k = j - 1, the left part is i..j-1 and the right part is the single element j..j.
Exercise
In merging adjacent piles, the join cost is the sum of piles i..j. Rewrite the recurrence so the join cost is computed once instead of inside the loop.
Show answer
dp[i][j] = sum(i..j) + min over k in [i, j-1] of (dp[i][k] + dp[k+1][j]). The sum does not depend on k, so it moves outside the minimum.