Sub topic 3
The O(n^2) Dynamic Program
This chapter assumes you can read Python, know Big-O notation, and have seen basic dynamic programming, meaning a problem broken into subproblems linked by a recurrence. The last chapter showed that checking every subsequence costs 2^n. Here we get that down to n^2 by choosing a better subproblem.
Choosing the Subproblem
The obvious subproblem is "the LIS of the first i elements." It does not work well. Knowing the length of the best subsequence in a prefix tells you nothing about its last value, so you cannot tell whether a[i] can extend it.
The fix is to fix where the subsequence ends:
dp[i]is the length of the longest strictly increasing subsequence that ends at index i.
Since a[i] is the last element, any earlier element a[j] with j < i and a[j] < a[i] can sit directly before it. That gives the recurrence:
dp[i] = 1 + max(dp[j])over allj < iwitha[j] < a[i]dp[i] = 1if no suchjexists, becausea[i]alone is a subsequence of length 1.
The answer is max(dp), because the longest subsequence can end at any index.
Watching the Dependencies
Take a = [10, 9, 2, 5, 3, 7, 101, 18]. To fill in dp[5] (value 7), we look back at every smaller earlier value.
The values 10 and 9 are skipped because they are not smaller than 7. Here is the full table:
| i | a[i] | valid predecessors (values) | dp[i] |
|---|---|---|---|
| 0 | 10 | none | 1 |
| 1 | 9 | none | 1 |
| 2 | 2 | none | 1 |
| 3 | 5 | 2 | 2 |
| 4 | 3 | 2 | 2 |
| 5 | 7 | 2, 5, 3 | 3 |
| 6 | 101 | 10, 9, 2, 5, 3, 7 | 4 |
| 7 | 18 | 10, 9, 2, 5, 3, 7 | 4 |
The maximum is 4, and one subsequence that achieves it is [2, 5, 7, 101].
The Implementation
def lis_length_quadratic(a):
if not a:
return 0
dp = [1] * len(a) # dp[i] = length of LIS ending at index i
for i in range(len(a)):
for j in range(i):
if a[j] < a[i] and dp[j] + 1 > dp[i]:
dp[i] = dp[j] + 1
return max(dp)
print(lis_length_quadratic([10, 9, 2, 5, 3, 7, 101, 18])) # 4The outer loop goes left to right, so every dp[j] with j < i is final before dp[i] reads it. The inner loop does up to i comparisons, which gives 0 + 1 + ... + (n−1) = n(n−1)/2 steps. Time is O(n^2) and space is O(n).
Common Errors
| Mistake | Symptom | Fix |
|---|---|---|
| Returning `dp[-1]` | Wrong when the LIS does not end at the last element, e.g. `[1, 2, 0]` gives 1 instead of 2 | Return `max(dp)` |
| Using `a[j] <= a[i]` | Counts equal values, so `[2, 2, 2]` returns 3 | Use `<` for strictly increasing |
| Initializing `dp` to 0 | Every length comes out one too short | Start each entry at 1 |
| Calling `max` on an empty `dp` | `ValueError` on empty input | Return 0 early, as above |
This version only returns the length. The next chapter adds parent pointers so you can recover the subsequence itself.
Exercise
Fill in the dp array for a = [3, 1, 4, 1, 5, 9, 2, 6] and give the LIS length.
Show answer
dp = [1, 1, 2, 1, 3, 4, 2, 4]. The length is 4, for example [3, 4, 5, 9] or [1, 4, 5, 6].
Exercise
For a = [5, 6, 7, 1], what does returning dp[-1] give instead of max(dp), and why is that wrong?
Show answer
dp = [1, 2, 3, 1], so dp[-1] is 1 while the correct answer is 3. dp[-1] only measures subsequences that end at the value 1, but the longest one, [5, 6, 7], ends earlier.