Sub topic 2
From Moves to Recurrences
This chapter assumes you can read nested Python loops, index a 2D list as grid[i][j], and already know what dp[i][j] means for a problem, as covered in the previous chapter. Here you will learn how to turn the moves a problem allows into a recurrence. A recurrence is a formula that computes one cell's value from the values of other cells.
Reverse the Moves
A move rule says where you can go from a cell. A recurrence needs the opposite: where you could have come from. To get it, reverse every allowed move.
If the walker can step right or down, the walker can reach (i, j) only from:
(i - 1, j), the cell above, by moving down(i, j - 1), the cell to the left, by moving right
These cells are the predecessors of (i, j). Each allowed move gives one predecessor, so the recurrence has one term per move.
Pick the Combining Operation
The predecessors tell you which cells to read. The question tells you how to combine them.
| Question asked | Combine with | Recurrence for right/down moves |
|---|---|---|
| How many paths? | sum | `dp[i][j] = dp[i-1][j] + dp[i][j-1]` |
| Cheapest path? | min, then add this cell's cost | `dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1])` |
| Most valuable path? | max, then add this cell's value | `dp[i][j] = grid[i][j] + max(dp[i-1][j], dp[i][j-1])` |
| Is the goal reachable? | logical or | `dp[i][j] = dp[i-1][j] or dp[i][j-1]` |
Counting uses a sum because the paths through different predecessors are disjoint: no path reaches (i, j) from the left and from above at once. Optimization uses min or max because only the best predecessor matters.
Adding a Move Adds a Term
Suppose the walker may also step diagonally down-right. That move reverses to the predecessor (i - 1, j - 1), so the counting recurrence gets a third term:
def count_paths_with_diagonal(m, n):
dp = [[0] * n for _ in range(m)]
dp[0][0] = 1
for i in range(m):
for j in range(n):
if i == 0 and j == 0:
continue
total = 0
if i > 0:
total += dp[i - 1][j] # came down
if j > 0:
total += dp[i][j - 1] # came right
if i > 0 and j > 0:
total += dp[i - 1][j - 1] # came diagonally
dp[i][j] = total
return dp[m - 1][n - 1]
print(count_paths_with_diagonal(3, 3)) # 13The if i > 0 guards keep the code from reading outside the grid. The next chapter covers cleaner ways to handle these edges.
Common Errors
- Forgetting to reverse the moves. If moves go right and down, reading
dp[i+1][j]reads a cell the walker reaches later, so its value is not ready yet. - Counting cell cost twice. In min-cost problems, add
grid[i][j]once, outside themin. Do not add it inside each term. - Using sum where you need max. Before writing code, ask whether the problem wants all paths or the best path.
- Missing a move. Write down every allowed move and check that each one has a term in the recurrence.
Exercise
A walker starts in the bottom-left corner and may move only right or up. Each cell has a cost. Which predecessors does cell (i, j) have, where row 0 is the top row, and what is the min-cost recurrence?
Show answer
Moving right means the walker came from the left, (i, j-1). Moving up means it came from the row below, (i+1, j). The recurrence is dp[i][j] = grid[i][j] + min(dp[i+1][j], dp[i][j-1]).
Exercise
Moves are right, down, and down-right diagonal. Write the recurrence for the maximum total value collected along a path.
Show answer
dp[i][j] = grid[i][j] + max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]), where any predecessor that falls outside the grid is skipped.