14.1 Introduction to dynamic programming¶
Dynamic programming is an important algorithmic paradigm that decomposes a problem into a series of smaller subproblems, and stores the solutions of these subproblems to avoid redundant computations, thereby significantly improving time efficiency.
In this section, we start with a classic problem, first presenting its brute force backtracking solution, observing the overlapping subproblems contained within, and then gradually deriving a more efficient dynamic programming solution.
Climbing stairs
Given a staircase with \(n\) steps, where you can climb \(1\) or \(2\) steps at a time, how many different ways are there to reach the top?
As shown in Figure 14-1, there are \(3\) ways to reach the top of a \(3\)-step staircase.
Figure 14-1 Number of ways to reach the 3rd step
The goal of this problem is to determine the number of ways, considering using backtracking to exhaust all possibilities. Specifically, imagine climbing stairs as a multi-round choice process: starting from the ground, choosing to go up \(1\) or \(2\) steps each round, adding one to the count of ways upon reaching the top of the stairs, and pruning the process when exceeding the top. The code is as follows:
def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:
"""Backtracking"""
# When climbing to the nth step, add 1 to the number of solutions
if state == n:
res[0] += 1
# Traverse all choices
for choice in choices:
# Pruning: do not allow climbing beyond the nth step
if state + choice > n:
continue
# Attempt: make a choice, update the state
backtrack(choices, state + choice, n, res)
# Retract
def climbing_stairs_backtrack(n: int) -> int:
"""Climbing stairs: Backtracking"""
choices = [1, 2] # Can choose to climb up 1 step or 2 steps
state = 0 # Start climbing from the 0th step
res = [0] # Use res[0] to record the number of solutions
backtrack(choices, state, n, res)
return res[0]
/* Backtracking */
void backtrack(List<Integer> choices, int state, int n, List<Integer> res) {
// When climbing to the nth step, add 1 to the number of solutions
if (state == n)
res.set(0, res.get(0) + 1);
// Traverse all choices
for (Integer choice : choices) {
// Pruning: do not allow climbing beyond the nth step
if (state + choice > n)
continue;
// Attempt: make a choice, update the state
backtrack(choices, state + choice, n, res);
// Retract
}
}
/* Climbing stairs: Backtracking */
int climbingStairsBacktrack(int n) {
List<Integer> choices = Arrays.asList(1, 2); // Can choose to climb up 1 step or 2 steps
int state = 0; // Start climbing from the 0th step
List<Integer> res = new ArrayList<>();
res.add(0); // Use res[0] to record the number of solutions
backtrack(choices, state, n, res);
return res.get(0);
}
14.1.1 Method 1: Brute force search¶
Backtracking algorithms do not explicitly decompose the problem but treat solving the problem as a series of decision steps, searching for all possible solutions through exploration and pruning.
We can try to analyze this problem from the perspective of decomposition. Let \(dp[i]\) be the number of ways to reach the \(i^{th}\) step, then \(dp[i]\) is the original problem, and its subproblems include:
Since each round can only advance \(1\) or \(2\) steps, when we stand on the \(i^{th}\) step, the previous round must have been either on the \(i-1^{th}\) or the \(i-2^{th}\) step. In other words, we can only step from the \(i-1^{th}\) or the \(i-2^{th}\) step to the \(i^{th}\) step.
This leads to an important conclusion: the number of ways to reach the \(i-1^{th}\) step plus the number of ways to reach the \(i-2^{th}\) step equals the number of ways to reach the \(i^{th}\) step. The formula is as follows:
This means that in the stair climbing problem, there is a recursive relationship between the subproblems, the solution to the original problem can be constructed from the solutions to the subproblems. Figure 14-2 shows this recursive relationship.
Figure 14-2 Recursive relationship of solution counts
We can obtain the brute force search solution according to the recursive formula. Starting with \(dp[n]\), recursively decompose a larger problem into the sum of two smaller problems, until reaching the smallest subproblems \(dp[1]\) and \(dp[2]\) where the solutions are known, with \(dp[1] = 1\) and \(dp[2] = 2\), representing \(1\) and \(2\) ways to climb to the first and second steps, respectively.
Observe the following code, which, like standard backtracking code, belongs to depth-first search but is more concise:
Figure 14-3 shows the recursive tree formed by brute force search. For the problem \(dp[n]\), the depth of its recursive tree is \(n\), with a time complexity of \(O(2^n)\). Exponential order represents explosive growth, and entering a long wait if a relatively large \(n\) is input.
Figure 14-3 Recursive tree for climbing stairs
Observing Figure 14-3, the exponential time complexity is caused by 'overlapping subproblems'. For example, \(dp[9]\) is decomposed into \(dp[8]\) and \(dp[7]\), \(dp[8]\) into \(dp[7]\) and \(dp[6]\), both containing the subproblem \(dp[7]\).
Thus, subproblems include even smaller overlapping subproblems, endlessly. A vast majority of computational resources are wasted on these overlapping subproblems.
14.1.2 Method 2: Memoized search¶
To enhance algorithm efficiency, we hope that all overlapping subproblems are calculated only once. For this purpose, we declare an array mem
to record the solution of each subproblem, and prune overlapping subproblems during the search process.
- When \(dp[i]\) is calculated for the first time, we record it in
mem[i]
for later use. - When \(dp[i]\) needs to be calculated again, we can directly retrieve the result from
mem[i]
, thus avoiding redundant calculations of that subproblem.
The code is as follows:
def dfs(i: int, mem: list[int]) -> int:
"""Memoized search"""
# Known dp[1] and dp[2], return them
if i == 1 or i == 2:
return i
# If there is a record for dp[i], return it
if mem[i] != -1:
return mem[i]
# dp[i] = dp[i-1] + dp[i-2]
count = dfs(i - 1, mem) + dfs(i - 2, mem)
# Record dp[i]
mem[i] = count
return count
def climbing_stairs_dfs_mem(n: int) -> int:
"""Climbing stairs: Memoized search"""
# mem[i] records the total number of solutions for climbing to the ith step, -1 means no record
mem = [-1] * (n + 1)
return dfs(n, mem)
/* Memoized search */
int dfs(int i, int[] mem) {
// Known dp[1] and dp[2], return them
if (i == 1 || i == 2)
return i;
// If there is a record for dp[i], return it
if (mem[i] != -1)
return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1, mem) + dfs(i - 2, mem);
// Record dp[i]
mem[i] = count;
return count;
}
/* Climbing stairs: Memoized search */
int climbingStairsDFSMem(int n) {
// mem[i] records the total number of solutions for climbing to the ith step, -1 means no record
int[] mem = new int[n + 1];
Arrays.fill(mem, -1);
return dfs(n, mem);
}
Observe Figure 14-4, after memoization, all overlapping subproblems need to be calculated only once, optimizing the time complexity to \(O(n)\), which is a significant leap.
Figure 14-4 Recursive tree with memoized search
14.1.3 Method 3: Dynamic programming¶
Memoized search is a 'top-down' method: we start with the original problem (root node), recursively decompose larger subproblems into smaller ones until the solutions to the smallest known subproblems (leaf nodes) are reached. Subsequently, by backtracking, we collect the solutions of the subproblems, constructing the solution to the original problem.
On the contrary, dynamic programming is a 'bottom-up' method: starting with the solutions to the smallest subproblems, iteratively construct the solutions to larger subproblems until the original problem is solved.
Since dynamic programming does not include a backtracking process, it only requires looping iteration to implement, without needing recursion. In the following code, we initialize an array dp
to store the solutions to the subproblems, serving the same recording function as the array mem
in memoized search:
def climbing_stairs_dp(n: int) -> int:
"""Climbing stairs: Dynamic programming"""
if n == 1 or n == 2:
return n
# Initialize dp table, used to store subproblem solutions
dp = [0] * (n + 1)
# Initial state: preset the smallest subproblem solution
dp[1], dp[2] = 1, 2
# State transition: gradually solve larger subproblems from smaller ones
for i in range(3, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[n]
/* Climbing stairs: Dynamic programming */
int climbingStairsDP(int n) {
if (n == 1 || n == 2)
return n;
// Initialize dp table, used to store subproblem solutions
int[] dp = new int[n + 1];
// Initial state: preset the smallest subproblem solution
dp[1] = 1;
dp[2] = 2;
// State transition: gradually solve larger subproblems from smaller ones
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
Figure 14-5 simulates the execution process of the above code.
Figure 14-5 Dynamic programming process for climbing stairs
Like the backtracking algorithm, dynamic programming also uses the concept of "states" to represent specific stages in problem solving, each state corresponding to a subproblem and its local optimal solution. For example, the state of the climbing stairs problem is defined as the current step number \(i\).
Based on the above content, we can summarize the commonly used terminology in dynamic programming.
- The array
dp
is referred to as the DP table, with \(dp[i]\) representing the solution to the subproblem corresponding to state \(i\). - The states corresponding to the smallest subproblems (steps \(1\) and \(2\)) are called initial states.
- The recursive formula \(dp[i] = dp[i-1] + dp[i-2]\) is called the state transition equation.
14.1.4 Space optimization¶
Observant readers may have noticed that since \(dp[i]\) is only related to \(dp[i-1]\) and \(dp[i-2]\), we do not need to use an array dp
to store the solutions to all subproblems, but can simply use two variables to progress iteratively. The code is as follows:
Observing the above code, since the space occupied by the array dp
is eliminated, the space complexity is reduced from \(O(n)\) to \(O(1)\).
In dynamic programming problems, the current state is often only related to a limited number of previous states, allowing us to retain only the necessary states and save memory space by "dimension reduction". This space optimization technique is known as 'rolling variable' or 'rolling array'.