There are two ways to implement a DP algorithm:
- Bottom-up, also known as tabulation
- Top-down, also known as memoization
Bottom-up (Tabulation) (可以理解成顺思维)
Bottom-up is implemented with iteration and starts at the base cases. Using the Fibonacci sequence as an example again. The base cases for the Fibonacci sequence are F(0) = 0 and F(1) = 1. With bottom-up, we would see these base cases to calculate F(2), and then use the result to calculate F(3), and so on all the way up F(n).
// Pseudocode example for bottom-up
F = array of length (n+1)
F[0] = 0
F[1] = 1
for i from 2 to n:
F[i] = F[i - 1] + F[i - 2]
Top-down (Memoization) (可以理解成逆思维)
Top-down is implemented with recursion and made efficient with memoization. If we wanted to find the nth Fibonacci number F(n), we try to compute this by finding F(n-1) and F(n-2). This defines a recursive pattern that will continue on until we reach the base cases F(0) = F(1) = 1. The problem with just implementing it recursively is that there is a ton of unnecessary repeated computation.
memoizing a result means to store the result of a function call, usually in a hashmap or an array, so that when the same function call is made again, we can simply return the memoized result instead of recalculating the result.
// Pseudocode example for top-down
memo = hashmap
Function F(integer i):
if i is 0 or 1:
return i
if i doesn't exist in memo:
memo[i] = F(i - 1) + F(i - 2)
return memo[i]
Which is better?
Any DP algorithm can be implemented with either method, and there are reasons for choosing either over the other. However, each method has one main advantage that stands out:
- A bottom-up implementation’s runtime is usually faster, as iteration does not have the overhead that recursion does.
- A top-down implementation is usually much easier to write. This is because with recursion, the ordering of subproblems does not matter, whereas with tabulation, we need to go through a logical ordering of solving subproblems.
总结:
Top-down uses recursion
Bottom-up uses iteration