Sub topic 9
Counting States and Transitions: O(n^2) Space, O(n^3) Time
This chapter assumes you know Big-O notation and have seen the split-point and endpoint-shrinking recurrences from earlier chapters. To find the cost of any interval DP, count two things: how many states the table holds, and how much work each state does.
Counting the States
A state dp[i][j] exists for every pair with 0 <= i <= j < n. That gives n(n+1)/2 intervals, which is O(n^2). The table is usually allocated as a full n by n grid. Half of it stays unused, but the space is still O(n^2).
A separate split table for reconstruction doubles the constant factor. It does not change the order of growth.
Counting the Transitions
Total time equals the number of states multiplied by the average work per state.
| Recurrence shape | Choices per state | Total time |
|---|---|---|
| Split point (matrix chain, stone merging) | j - i values of k | O(n^3) |
| Endpoint shrinking (palindromic subsequence) | Constant, at most 3 | O(n^2) |
For the split recurrence, an interval of length L has L - 1 split points, and there are n - L + 1 intervals of that length. Summed over all lengths, this gives exactly (n^3 - n) / 6 transitions. That is O(n^3) with a small constant of 1/6. The script below counts the transitions directly and checks them against the formula.
def count_transitions(n):
total = 0
for length in range(2, n + 1):
for i in range(n - length + 1):
j = i + length - 1
total += j - i # number of k values in range(i, j)
return total
for n in [3, 10, 100]:
print(n, count_transitions(n), (n**3 - n) // 6)
# 3 4 4
# 10 165 165
# 100 166650 166650What This Means in Practice
Each doubling of n multiplies the work of a split-point DP by about 8. Rough limits:
- n up to about 500 gives roughly 2 x 10^7 inner steps. Compiled languages handle this quickly. Pure Python takes several seconds.
- n around 2000 gives about 1.3 x 10^9 steps. This is too slow in Python and tight even in C++.
- n of 5000 or more needs a better algorithm, such as Knuth's optimization (covered later) or a different formulation.
Reducing Space
The split recurrence for dp[i][j] reads values from the whole row i and the whole column j. In general it needs the full table. If you also want reconstruction, you must keep split as well.
The endpoint recurrence reads only row i + 1 and the current row i. When you iterate i downward, you can keep just two rows, which brings space down to O(n):
def lps_linear_space(s):
n = len(s)
if n == 0:
return 0
prev = [0] * n # row i + 1
for i in range(n - 1, -1, -1):
cur = [0] * n
cur[i] = 1
for j in range(i + 1, n):
if s[i] == s[j]:
cur[j] = prev[j - 1] + 2
else:
cur[j] = max(prev[j], cur[j - 1])
prev = cur
return prev[n - 1]
print(lps_linear_space("bbbab")) # 4Exercise
How many split-point transitions does matrix chain multiplication perform for n = 4 matrices? Verify by listing the intervals by length.
Show answer
(4^3 - 4) / 6 = 10. Length 2: three intervals with 1 split each, giving 3. Length 3: two intervals with 2 splits each, giving 4. Length 4: one interval with 3 splits, giving 3. Total 3 + 4 + 3 = 10.
Exercise
A problem has n = 3000 and uses the endpoint-shrinking recurrence. What are the time and space costs with a full table and with rolling rows? Is the approach feasible?
Show answer
Time is O(n^2), about 4.5 x 10^6 states with constant work each, so it is feasible. A full table needs 9 x 10^6 cells (O(n^2) space). Rolling rows need about 2 x 3000 cells (O(n) space).