Sub topic 4
Recovering the Subsequence with Parent Pointers
This chapter assumes you know the O(n^2) dynamic program from the previous chapter, where dp[i] is the length of the longest strictly increasing subsequence that ends at index i. It also assumes you can read Python lists and loops comfortably.
The quadratic DP tells you how long the LIS is, but it throws away which elements form it. Many real tasks need the elements themselves, for example to show which versions of a file stayed in order. You can get them back with one extra array and a backward walk.
The idea: remember who extended you
When the inner loop finds that dp[j] + 1 > dp[i], it has learned that the best subsequence ending at i comes from appending a[i] to the best subsequence ending at j. Store that j in parent[i]. An index whose subsequence is just itself keeps parent[i] = -1, which marks the start of a chain.
After the loops finish, each index points to its predecessor in an optimal subsequence ending there. Start at the index with the largest dp value and follow the pointers until you reach -1. That gives the subsequence in reverse order.
def lis_sequence_quadratic(a):
if not a:
return []
n = len(a)
dp = [1] * n
parent = [-1] * n
for i in range(n):
for j in range(i):
if a[j] < a[i] and dp[j] + 1 > dp[i]:
dp[i] = dp[j] + 1
parent[i] = j
i = max(range(n), key=lambda k: dp[k])
seq = []
while i != -1:
seq.append(a[i])
i = parent[i]
return seq[::-1]
print(lis_sequence_quadratic([10, 9, 2, 5, 3, 7, 101, 18])) # [2, 5, 7, 101]The extra cost is O(n) space for parent and O(n) time for the walk. The total stays O(n^2) time.
Tracing the example
| index | value | dp | parent |
|---|---|---|---|
| 0 | 10 | 1 | -1 |
| 1 | 9 | 1 | -1 |
| 2 | 2 | 1 | -1 |
| 3 | 5 | 2 | 2 |
| 4 | 3 | 2 | 2 |
| 5 | 7 | 3 | 3 |
| 6 | 101 | 4 | 5 |
| 7 | 18 | 4 | 5 |
Indices 6 and 7 both reach length 4. max returns the first maximum it sees, which is index 6. The walk then visits 6 → 5 → 3 → 2 → -1, and collecting the values gives [101, 7, 5, 2]. Reversing that list gives [2, 5, 7, 101].
Ties and which answer you get
An array often has more than one LIS. In the example, [2, 3, 7, 18] is just as long as [2, 5, 7, 101]. Your tie-breaking decides which one the function returns:
- The strict
>in the update keeps the earliestjthat reaches the best length. At index 5, both index 3 (value 5) and index 4 (value 3) give length 3, and index 3 wins because it comes first. maxover indices picks the first end index with the maximumdp.
Each of these choices still produces a valid LIS. If a problem asks for a specific one, such as the lexicographically smallest, you have to choose the tie-breaking rule on purpose.
Rebuilding without a parent array
You can also reconstruct the subsequence from dp alone. Start at the best index i, then scan left for any j < i with a[j] < a[i] and dp[j] == dp[i] - 1, and repeat from j. Such a j always exists, because the DP got dp[i] from one. In the worst case this costs O(n^2), the same as the DP. The parent array is simpler and makes the walk O(n).
Exercise
What does lissequencequadratic return for [3, 4, 1, 2, 5]? Give the dp and parent values for index 4.
Show answer
At index 4 (value 5), j=0 gives length 2, then j=1 gives length 3 with parent 1. j=3 also gives length 3, but 3 is not greater than 3, so the parent stays 1. That makes dp[4]=3 and parent[4]=1. The walk is 4 → 1 → 0, so the function returns [3, 4, 5].
Exercise
In the same function, change the condition to dp[j] + 1 >= dp[i]. What does it now return for [3, 4, 1, 2, 5], and why?
Show answer
It returns [1, 2, 5]. With >=, a later j that ties the best length replaces the earlier one. At index 4, j=3 (value 2) also gives length 3, so parent[4] becomes 3. The walk is 4 → 3 → 2. The result is still a valid LIS. Only the tie-breaking changed.