Introduction to Dynamic Programming¶
The essence of dynamic programming is to avoid repeated calculation. Often, dynamic programming problems are naturally solvable by recursion. In such cases, it's easiest to write the recursive solution, then save repeated states in a lookup table. This process is known as top-down dynamic programming with memoization. That's read "memoization" (like we are writing in a memo pad) not memorization.
One of the most basic, classic examples of this process is the fibonacci sequence. Its recursive formulation is
int f(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return f(n - 1) + f(n - 2);
}
The runtime of this recursive function is exponential - approximately
Speeding up Fibonacci with Dynamic Programming (Memoization)¶
Our recursive function currently solves fibonacci in exponential time. This means that we can only handle small input values before the problem becomes too difficult. For instance,
To increase the speed, we recognize that the number of subproblems is only
Each recursive call will check against a lookup table to see if the value has been calculated. This is done in
const int MAXN = 100;
bool found[MAXN];
int memo[MAXN];
int f(int n) {
if (found[n]) return memo[n];
if (n == 0) return 0;
if (n == 1) return 1;
found[n] = true;
return memo[n] = f(n - 1) + f(n - 2);
}
With our new memoized recursive function,
Typically, we try to save states in arrays, if possible, since the lookup time is map
in C++) or hash tables (unordered_map
in C++).
An example of this might be:
unordered_map<int, int> memo;
int f(int n) {
if (memo.count(n)) return memo[n];
if (n == 0) return 0;
if (n == 1) return 1;
return memo[n] = f(n - 1) + f(n - 2);
}
Or analogously:
map<int, int> memo;
int f(int n) {
if (memo.count(n)) return memo[n];
if (n == 0) return 0;
if (n == 1) return 1;
return memo[n] = f(n - 1) + f(n - 2);
}
Both of these will almost always be slower than the array-based version for a generic memoized recursive function. These alternative ways of saving state are primarily useful when saving vectors or strings as part of the state space.
The layman's way of analyzing the runtime of a memoized recursive function is:
Using a binary search tree (map in C++) to save states will technically result in
This approach is called top-down, as we can call the function with a query value and the calculation starts going from the top (queried value) down to the bottom (base cases of the recursion), and makes shortcuts via memoization on the way.
Bottom-up Dynamic Programming¶
Until now you've only seen top-down dynamic programming with memoization. However, we can also solve problems with bottom-up dynamic programming. Bottom-up is exactly the opposite of top-down, you start at the bottom (base cases of the recursion), and extend it to more and more values.
To create a bottom-up approach for fibonacci numbers, we initialize the base cases in an array. Then, we simply use the recursive definition on array:
const int MAXN = 100;
int fib[MAXN];
int f(int n) {
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i <= n; i++) fib[i] = fib[i - 1] + fib[i - 2];
return fib[n];
}
Of course, as written, this is a bit silly for two reasons:
Firstly, we do repeated work if we call the function more than once.
Secondly, we only need to use the two previous values to calculate the current element. Therefore, we can reduce our memory from
An example of a bottom-up dynamic programming solution for fibonacci which uses
const int MAX_SAVE = 3;
int fib[MAX_SAVE];
int f(int n) {
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i <= n; i++)
fib[i % MAX_SAVE] = fib[(i - 1) % MAX_SAVE] + fib[(i - 2) % MAX_SAVE];
return fib[n % MAX_SAVE];
}
Note that we've changed the constant from MAXN
TO MAX_SAVE
. This is because the total number of elements we need to access is only 3. It no longer scales with the size of input and is, by definition,
That's it. That's the basics of dynamic programming: Don't repeat the work you've done before.
One of the tricks to getting better at dynamic programming is to study some of the classic examples.
Classic Dynamic Programming Problems¶
Name | Description/Example |
---|---|
0-1 Knapsack | Given |
Subset Sum | Given |
Longest Increasing Subsequence (LIS) | You are given an array containing |
Counting Paths in a 2D Array | Given |
Longest Common Subsequence | You are given strings |
Longest Path in a Directed Acyclic Graph (DAG) | Finding the longest path in Directed Acyclic Graph (DAG). |
Longest Palindromic Subsequence | Finding the Longest Palindromic Subsequence (LPS) of a given string. |
Rod Cutting | Given a rod of length |
Edit Distance | The edit distance between two strings is the minimum number of operations required to transform one string into the other. Operations are ["Add", "Remove", "Replace"] |
Related Topics¶
- Bitmask Dynamic Programming
- Digit Dynamic Programming
- Dynamic Programming on Trees
Of course, the most important trick is to practice.
Practice Problems¶
- LeetCode - 1137. N-th Tribonacci Number
- LeetCode - 118. Pascal's Triangle
- LeetCode - 1025. Divisor Game
- Codeforces - Vacations
- Codeforces - Hard problem
- Codeforces - Zuma
- LeetCode - 221. Maximal Square
- LeetCode - 1039. Minimum Score Triangulation of Polygon