Sub topic 15
Choosing the Right Reduction for a Recurrence
This chapter assumes you have worked through the earlier chapters. You should be comfortable with dependency analysis, rolling variables and rows, loop direction, diagonal saving, modulo indexing, removing dimensions, state machines, bitmasks, sparse states, memoization cost, and reconstruction. Here those pieces become one decision procedure.
Start from the Dependency Pattern
Every reduction depends on one fact: which earlier states each transition reads. Before you pick a technique, write the recurrence and mark every term on its right-hand side. Then answer these questions in order:
- How far back does each dimension look?
- Within the current layer, which cells are read before they are overwritten?
- Is any state component computable from the others?
- How many states are actually reachable?
- Do you need the solution itself, or only its value?
Each answer points to a technique.
Matching Patterns to Techniques
| Dependency pattern | Technique | Space after | Main risk |
|---|---|---|---|
| `f(i)` reads `f(i-1)`, `f(i-2)` | Rolling variables | O(1) | Updating in the wrong order |
| `f(i)` reads last k values | Modulo buffer of size k | O(k) | Reading a slot already overwritten |
| Row i reads row i-1 | Two rows | O(n) | Forgetting to swap or reset |
| Row i reads `i-1` at `j` and `j-w` | One array, downward loop | O(n) | An upward loop reuses items |
| Row i reads row i at `j-w` | One array, upward loop | O(n) | A downward loop forbids reuse |
| Reads `(i-1, j-1)` too | One row plus `diag` | O(n) | Losing the diagonal value |
| A dimension equals a function of others | Drop the dimension | Divides by that size | Deriving it incorrectly |
| Few reachable states in a huge space | Hash map | O(reachable) | Per-entry overhead of the hash map |
Weighing the Trade-offs
Space reduction is rarely free. Keep these costs in mind.
- Reconstruction. Rolling arrays throw away the information needed to trace back choices. If you need the path, you can store a compact choice table, which often takes bits rather than integers. You can also run Hirschberg's method, which doubles the time but keeps linear space.
- Readability. A single-row edit distance is harder to review than a full table. Write the full-table version first, test it, then compress it and compare outputs on random inputs.
- Constant factors. A hash map can use more memory than a dense array when most states are reachable. Measure before you switch.
- Recursion. Top-down memoization stores every visited state and also uses stack frames. If depth or cache size is a problem, convert to bottom-up and then apply the reductions above.
A Worked Choice
Take the longest palindromic subsequence: L(i, j) reads L(i+1, j-1), L(i+1, j), and L(i, j-1). Row i reads row i+1 and itself, so you can keep two rows while iterating i downward. The term (i+1, j-1) is a diagonal, so a single row also works if you save diag before overwriting each cell. If you also need the palindrome itself, keep the full table or use a divide-and-conquer reconstruction.
Exercise
A recurrence is dp[i][c] = max(dp[i-1][c], dp[i][c - wi] + vi) for unlimited copies of each item. Which reduction applies, and which loop direction is required?
Show answer
A single array indexed by c. Loop c upward, so dp[c - w_i] already reflects item i and the item can be reused. A downward loop would allow each item only once.
Exercise
You need the actual edit script between two 100,000-character strings, and memory allows only O(n). Which approach do you choose, and what does it cost in time?
Show answer
Hirschberg's divide-and-conquer method. It computes single-row forward and backward passes to find split points, which keeps O(n) space and reconstructs the full alignment in about twice the O(mn) time of one pass.