Dynamic programming
The recursive Fibonacci from Recursion made two and a
half million calls for fib(30) because it kept recomputing the same
subproblems. Dynamic programming is the name for fixing that: when a
problem breaks into overlapping smaller problems, solve each small problem
once, remember the answer, and build up. The name is historical and means
nothing; think "recursion plus a notebook".
It is the most feared topic in interviews and the most mechanical once you see that every DP problem is answered by the same three questions.
Memoisation: recursion plus a dictionary
Keep the recursive function, add a cache:
memo = {}
def fib(n):
if n in memo:
return memo[n]
if n < 2:
return n
memo[n] = fib(n - 1) + fib(n - 2)
return memo[n]
print(fib(30), fib(90))
print(len(memo), "answers remembered")
Each n is computed once and looked up ever after, so fib(90) takes
about ninety calls instead of trillions. This is the
remember answers pattern from hashing applied to a
function's own arguments. Python has it built in: put
@functools.lru_cache(maxsize=None) above a function and its results are
cached automatically.
Memoisation changes nothing about how you think; you write the natural
recursion and add three lines. Its only limit is recursion depth, which
for fib(100000) would overflow the stack.
Tables: build up from the bottom
Instead of starting at n and recursing down, start at the base cases
and fill a list upwards. Same subproblems, same answers, no recursion:
def fib(n):
if n < 2:
return n
table = [0] * (n + 1)
table[1] = 1
for i in range(2, n + 1):
table[i] = table[i - 1] + table[i - 2]
return table[n]
print(fib(30), fib(90))
table[i] holds the answer to the subproblem "fib of i". The loop runs in
the order that guarantees the two entries each step needs are already
filled. Since only the last two entries are ever read, two variables would
do instead of the whole table; that space saving is a common follow-up.
Memoisation and tables are the same algorithm in different clothing. Start with memoisation when thinking, because it follows the recursion; convert to a table when depth or speed matters.
The three questions
Every DP problem is set up by answering:
- What is the subproblem? Define a function of a small number of
parameters whose value you want, like
fib(n),ways(n),fewest(amount),best(i). The parameters are usually "how much of the input is left". - How does a subproblem's answer come from smaller ones? This is the recursive case, and the whole difficulty of the problem lives here.
- What are the base cases? The smallest inputs with obvious answers.
With those three in hand, the memoised version is a direct transcription and the table version follows. The rest of the page is the three questions answered for the classic starter problems.
Climbing stairs
You can climb 1 or 2 steps at a time. How many different ways up n
steps? Subproblem: ways(n). The last move was either a 1-step from
n - 1 or a 2-step from n - 2, so ways(n) = ways(n - 1) + ways(n - 2).
Base: ways(0) = 1 (one way to stand still), ways(1) = 1.
def ways(n):
table = [1, 1] + [0] * (n - 1)
for i in range(2, n + 1):
table[i] = table[i - 1] + table[i - 2]
return table[n]
print([ways(n) for n in range(8)])
It is Fibonacci in disguise, which is common: many DP problems share a recurrence and differ only in the story.
Fewest coins, where greedy failed
The 30 with coins 25, 10, 1 from the greedy page.
Subproblem: fewest(amount), the fewest coins making exactly amount.
The last coin used was some c, so fewest(amount) = 1 + fewest(amount - c)
for the best c. Base: fewest(0) = 0; amounts that cannot be made are
infinite.
def fewest_coins(amount, coins):
INF = float("inf")
table = [0] + [INF] * amount
for a in range(1, amount + 1):
for c in coins:
if c <= a and table[a - c] + 1 < table[a]:
table[a] = table[a - c] + 1
return table[amount] if table[amount] != INF else -1
print(fewest_coins(30, [25, 10, 1]))
print(fewest_coins(67, [25, 10, 5, 1]))
print(fewest_coins(3, [2]))
Trying every last coin is what greedy refused to do, and it is exactly
what makes this correct. The cost is amount × len(coins), which for
realistic amounts is tiny.
House robber: take or skip
A row of houses with cash in each; you cannot rob two adjacent ones. The
most you can take? Subproblem: best(i), the most from the first i
houses. For house i, either skip it (best(i - 1)) or take it, in
which case house i - 1 is out and you add best(i - 2). Base: zero
houses give 0.
def rob(houses):
prev2, prev1 = 0, 0
for cash in houses:
prev2, prev1 = prev1, max(prev1, prev2 + cash)
return prev1
print(rob([2, 7, 9, 3, 1]))
print(rob([1, 2, 3, 1]))
Written with two variables instead of a table, since only the previous two answers are needed. The "take it or skip it" choice is the heart of a large family: knapsack, subset sums, longest increasing subsequence.
Two dimensions: paths through a grid
From the top-left of an rows × cols grid, moving only right or down,
how many routes reach the bottom-right? Subproblem: paths(r, c), the
routes to cell (r, c). You arrived from above or from the left, so
paths(r, c) = paths(r - 1, c) + paths(r, c - 1). Base: the first row and
first column have one route each.
def grid_paths(rows, cols):
table = [[1] * cols for _ in range(rows)]
for r in range(1, rows):
for c in range(1, cols):
table[r][c] = table[r - 1][c] + table[r][c - 1]
return table[rows - 1][cols - 1]
print(grid_paths(3, 3), grid_paths(3, 7))
Two parameters, a two-dimensional table, and the fill order (row by row)
again makes sure what each cell needs is ready. Blocked cells are the
natural extension: set their entry to 0. Edit distance and longest common
subsequence between two strings are the same shape with i characters of
one string and j of the other as the two parameters.
Spotting a DP problem
- The question asks for a count of ways, a minimum or maximum over many choices, or whether something is possible, and the choices interact.
- A greedy rule is tempting but you can break it with a small example.
- Brute force is "try every combination", which is exponential, and the combinations share sub-parts.
Then answer the three questions, write the memoised recursion, test it on tiny inputs by hand, and only then convert to a table if you need to.
Try it
Fibonacci, fast
Write fib(n) that returns the n-th Fibonacci number, with
fib(0) = 0 and fib(1) = 1, for n up to a few thousand.
fib(10) -> 55
fib(90) -> 2880067194370816120
The point is to practise replacing an exponential recursion with
memoisation or a table. The checks go to n = 3000; the plain recursive
version from the recursion page would never finish.
def fib(n):
...
Climbing stairs
You climb a staircase of n steps taking 1 or 2 steps at a time. Write
ways(n) that returns how many distinct sequences of steps reach the top.
ways(0) is 1 (do nothing).
ways(1) -> 1
ways(2) -> 2 (1+1, 2)
ways(3) -> 3 (1+1+1, 1+2, 2+1)
ways(4) -> 5
The point is to practise the three DP questions: the subproblem is
ways(k), the recurrence is ways(k - 1) + ways(k - 2), and the bases are
ways(0) = ways(1) = 1.
def ways(n):
...
Fewest coins, any coins
Write fewest_coins(amount, coins) that returns the fewest coins from the
given denominations that add up exactly to amount, using each
denomination as many times as needed. Return -1 if it cannot be made.
amount is zero or more.
fewest_coins(30, [25, 10, 1]) -> 3 (10 + 10 + 10)
fewest_coins(67, [25, 10, 5, 1]) -> 6
fewest_coins(3, [2]) -> -1
fewest_coins(0, [7]) -> 0
The point is to practise a one-dimensional DP table where greedy fails.
def fewest_coins(amount, coins):
...
House robber
houses lists the cash in each house along a street. You may not take
from two adjacent houses. Write most_cash(houses) that returns the most
you can take.
most_cash([2, 7, 9, 3, 1]) -> 12 (2 + 9 + 1)
most_cash([1, 2, 3, 1]) -> 4 (1 + 3)
most_cash([]) -> 0
The point is to practise the take-or-skip recurrence.
def most_cash(houses):
...
Paths through a grid
Write grid_paths(rows, cols) that returns how many routes go from the
top-left cell to the bottom-right cell of a rows × cols grid, moving
only right or down. Both dimensions are at least 1.
grid_paths(1, 1) -> 1
grid_paths(2, 2) -> 2
grid_paths(3, 3) -> 6
grid_paths(3, 7) -> 28
The point is to practise a two-dimensional table. Grids up to 18 × 18
are checked, where trying every path would take far too long.
def grid_paths(rows, cols):
...
Next: Backtracking.