Sub topic 4
Filling the Table by Interval Length
This chapter assumes you know basic tabulation, nested loops over 2D arrays, and the split-point recurrence from the previous chapter. In that recurrence, dp[i][j] is computed from dp[i][k] and dp[k+1][j] for every split k with i <= k < j. This chapter covers a practical question: in what order should you fill the cells so that every value you read already exists?
Why Order Matters
Bottom-up DP gives no warning when you read a cell too early. If you read a cell before it has been computed, you get its initial value, usually 0 or infinity, and the program quietly returns a wrong answer. In top-down memoization the recursion finds the order for you. In tabulation you have to choose it.
Look at what one cell needs. Both [i, k] and [k+1, j] lie strictly inside [i, j], so both are shorter than [i, j]. That gives a simple rule: if you compute every interval of length L - 1 before any interval of length L, all of a cell's dependencies are ready when you reach it.
The Standard Loop
The usual pattern has the length as the outer loop. Inside it, a loop over the start index i runs over every interval of that length, and j is computed from i and the length:
for length in range(2, n + 1):
for i in range(n - length + 1):
j = i + length - 1
# try every split k in [i, j)The upper bound n - length + 1 keeps j <= n - 1. Getting this bound wrong is a common off-by-one error. Too large a bound gives an IndexError, and too small a bound skips the last interval of each length, which is often the full range holding the answer.
Verifying an Order
The script below checks a fill order. It walks through the cells in sequence and confirms that every split reads only cells that were already filled. It then tests the length-first order and a natural-looking row-by-row order.
def fill_order(n):
order = []
for length in range(1, n + 1):
for i in range(n - length + 1):
j = i + length - 1
order.append((i, j))
return order
def check_order(order):
done = set()
for i, j in order:
for k in range(i, j):
if (i, k) not in done or (k + 1, j) not in done:
return False, (i, j, k)
done.add((i, j))
return True, None
n = 4
by_length = fill_order(n)
print(by_length)
# [(0, 0), (1, 1), (2, 2), (3, 3), (0, 1), (1, 2), (2, 3), (0, 2), (1, 3), (0, 3)]
print(check_order(by_length)) # (True, None)
row_major = [(i, j) for i in range(n) for j in range(i, n)]
print(check_order(row_major)) # (False, (0, 1, 0))The row-by-row order fails immediately. Computing dp[0][1] needs dp[1][1], which sits in a row that has not been filled yet.
Other Valid Orders
Length-first is not the only correct order. An order is valid when every cell comes after the cells it reads. The table below compares three options.
| Order | Outer loop | Inner loop | Valid for split recurrence? |
|---|---|---|---|
| Length-first | `length` ascending | `i` ascending | Yes |
| Bottom-up rows | `i` from `n-1` down to `0` | `j` from `i` up | Yes |
| Top-down rows | `i` ascending | `j` ascending | No |
Bottom-up rows works for a simple reason. Cell [i, k] is in the current row with a smaller j, so it was filled earlier in that row. Cell [k+1, j] is in a lower row, so it was filled in an earlier pass. The palindrome chapter uses this order. Length-first is the easiest order to reason about, and it works for any recurrence that reads only strictly shorter intervals.
Exercise
Change the row_major line so that i runs from n - 1 down to 0 while j still runs from i up to n - 1. What does check_order return, and why?
Show answer
It returns (True, None). When the order reaches [i, j], the left part [i, k] was filled earlier in the same row because k < j. The right part [k+1, j] was filled in an earlier row because k + 1 > i, and rows are processed from the bottom up.
Exercise
A student writes for i in range(n - length): in the standard loop. Which interval of each length is skipped, and what goes wrong at length == n?
Show answer
The interval starting at i = n - length is skipped, and that is the rightmost interval of each length. At length == n the loop body never runs, so dp[0][n-1] keeps its initial value and the function returns a wrong answer.