Sub topic 2
Reading the Dependency Pattern of a Recurrence
This chapter assumes you can write a bottom-up DP table and analyze its space in Big-O terms. It also assumes you know the Fibonacci, knapsack, and longest common subsequence (LCS) recurrences. Every memory reduction later in this tutorial starts from one question: when the program computes a cell, which earlier cells does it read?
The dependency pattern
A transition is the rule that computes one state from others. The dependency pattern of a recurrence is the set of offsets from the current cell to the cells it reads. In Fibonacci, f[i] = f[i-1] + f[i-2], so the offsets are -1 and -2. In LCS, cell (i, j) reads (i-1, j-1), (i-1, j), and (i, j-1), so the offsets are (-1,-1), (-1,0), and (0,-1).
The pattern tells you how much of the table is still live at any moment. A cell is live if some cell that has not been computed yet will still read it. Once no future cell can read a value, you can throw that value away.
Making the pattern visible
You can work out the pattern by reading the recurrence. You can also check it by instrumenting the code. The version below fills the full LCS table and records the offset of every read:
def lcs_with_reads(x, y):
m, n = len(x), len(y)
dp = [[0] * (n + 1) for _ in range(m + 1)]
offsets = set()
def read(i, j, ci, cj):
offsets.add((i - ci, j - cj))
return dp[i][j]
for i in range(1, m + 1):
for j in range(1, n + 1):
if x[i - 1] == y[j - 1]:
dp[i][j] = read(i - 1, j - 1, i, j) + 1
else:
dp[i][j] = max(read(i - 1, j, i, j), read(i, j - 1, i, j))
return dp[m][n], sorted(offsets)
print(lcs_with_reads("ABCBDAB", "BDCABA"))
# (4, [(-1, -1), (-1, 0), (0, -1)])No read reaches back more than one row. So after row i is done, every row before i-1 is dead weight.
What to record for each recurrence
For each recurrence, write down three things:
- How far back it reaches along each dimension. This tells you how many layers you must keep.
- Which direction it reads within the current layer. It may read only the layer before, or also cells already written in the current layer, to the left or right.
- Any value that gets overwritten before its last read. The diagonal in LCS is the usual example.
| Recurrence | Reads | Live data at any moment |
|---|---|---|
| Fibonacci | `i-1`, `i-2` | two values |
| Tribonacci | `i-1`, `i-2`, `i-3` | three values |
| LCS | `(i-1,j-1)`, `(i-1,j)`, `(i,j-1)` | previous row plus current row |
| 0/1 knapsack | `(i-1,c)`, `(i-1,c-w)` | previous row only |
| Unbounded knapsack | `(i-1,a)`, `(i,a-coin)` | previous row plus current row |
Common errors
- Reading the code instead of the math. An index such as
c - wcan reach far to the left. The reach is "one row back, any column to the left", not "one step back". - Ignoring the order cells are computed in. Whether a cell is live depends on the order the loops visit cells as well as on the offsets. Changing the loop order changes which values must survive.
- Missing a hidden reader. Base cases, final answers, and code that reconstructs the solution may read cells that the main transition never touches. Include them in your analysis.
Exercise
The 0/1 knapsack recurrence is dp[i][c] = max(dp[i-1][c], dp[i-1][c-wi] + vi), with rows i for items and columns c for capacity. List its dependency offsets and state the smallest amount of the table that must stay live.
Show answer
The offsets are (-1, 0) and (-1, -wi): one row up, in the same column and wi columns to the left. No read touches the current row, so only the previous row must stay live while row i is computed. That is O(capacity) memory instead of O(items × capacity).
Exercise
A recurrence computes dp[i] = dp[i-2] + dp[i-5]. How many earlier values must you keep while computing dp[i] in increasing order of i?
Show answer
Five: dp[i-1] through dp[i-5]. The value dp[i-1] is not read for dp[i], but dp[i+1] will read it, so it is still live.