Sub topic 1
Where DP Memory Goes: States, Transitions, and Tables
Dynamic programming (DP) trades memory for time. It stores the answers to subproblems so it never computes them twice. That storage is usually the largest memory cost in a DP solution. Before you can cut it down, you need to see exactly where it comes from.
Prerequisites
This chapter assumes you already know:
- Basic dynamic programming: recurrences, memoization, and bottom-up tabulation
- Big-O analysis of time and space
- Arrays, nested loops, and recursion in Python or a similar language
- Classic problems such as Fibonacci, knapsack, and longest common subsequence (LCS)
States, Transitions, and Tables
A DP solution has three parts:
- A state is a tuple of parameters that identifies one subproblem, such as
(i, j)in LCS: "the LCS length of the firsticharacters ofxand the firstjcharacters ofy." - A transition is the rule that computes a state's value from other states. This is the recurrence.
- A table is the storage that holds one value per state.
The table has one cell for every possible combination of the state's parameters. So the memory it uses is the product of the ranges of those parameters.
| Problem | State | Table cells | Space |
|---|---|---|---|
| Fibonacci | `i` | n + 1 | O(n) |
| LCS | `(i, j)` | (m + 1)(n + 1) | O(mn) |
| 0/1 knapsack | `(item, capacity)` | (n + 1)(W + 1) | O(nW) |
| Job assignment | `(worker, set of jobs)` | n · 2^n | O(n · 2^n) |
Each parameter you add to the state multiplies the memory cost.
A Full Table, Measured
Here is the standard LCS tabulation, which keeps every cell:
def lcs_full(x, y):
m, n = len(x), len(y)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if x[i - 1] == y[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
cells = (m + 1) * (n + 1)
return dp[m][n], cells
length, cells = lcs_full("ABCBDAB", "BDCABA")
print(length, cells) # 4 56With two strings of 10,000 characters, the table has about 10^8 cells. In a language with 4-byte integers that is about 400 MB. In Python, each list slot is an 8-byte pointer, so it is at least 800 MB.
Common Errors in Estimating Memory
- Counting only time. O(mn) time is fine for many inputs. O(mn) space often is not, because memory runs out before time does.
- Ignoring per-cell overhead. In Python a list of lists stores pointers to integer objects. Real usage is several times the cell count multiplied by the integer width.
- Forgetting the recursion stack. Top-down memoization stores the same table plus a call stack that can be as deep as the longest chain of dependencies. A later chapter covers this cost.
- Assuming every cell is needed. The table stores all states, but a transition usually reads only a few of them. In LCS, cell
(i, j)reads only rowi - 1and rowi.
What You Can and Cannot Discard
The final answer often lives in a single cell, such as dp[m][n]. If you only need that value, most of the table is scaffolding you can throw away. If you need the solution itself, for example the actual common subsequence, you usually trace back through the full table. Throwing cells away then has a cost, which a later chapter deals with.
Exercise
A 0/1 knapsack solution uses a table indexed by (item, capacity) with 200 items and capacity 50,000. How many cells does the full table have, and roughly how much memory does it use at 8 bytes per cell?
Show answer
(200 + 1) × (50,000 + 1) ≈ 10,050,201 cells. At 8 bytes each that is about 80 MB, before any language-specific overhead such as Python's integer objects.
Exercise
In the LCS transition, which cells does dp[i][j] read? What does that suggest about how many rows must be in memory at once?
Show answer
It reads dp[i-1][j-1], dp[i-1][j], and dp[i][j-1]. These come only from the current row and the previous row, so in principle only two rows need to be kept at a time.