Sub topic 5
The Tails Array: Smallest Ending Value per Length
This chapter assumes you know the O(n^2) dynamic program from earlier, where dp[i] is the length of the longest strictly increasing subsequence ending at index i. It also assumes you can read basic Python and are comfortable with Big-O notation. Binary search comes in the next chapter. Here the goal is to understand the data structure that makes binary search possible.
A Different Question to Ask
The quadratic DP asks, for each index, how long the best subsequence ending there is. The tails method turns the question around and asks, for each length, which value ends that length most cheaply.
Define tails[k] as the smallest possible last value of any strictly increasing subsequence of length k + 1 among the elements seen so far.
A small ending value is better because it leaves more room for future elements. If two subsequences of length 3 end in 7 and 50, only the one ending in 7 can be extended by 20. The one ending in 50 can never do better, so you can forget it.
The Sorted Invariant
The tails array is always strictly increasing. Here is the argument. Take the increasing subsequence of length k + 1 that ends at tails[k]. Its first k elements form an increasing subsequence of length k, and that shorter subsequence ends at some value v < tails[k]. Because tails[k-1] is the smallest ending value over all length-k subsequences, tails[k-1] <= v < tails[k].
This invariant is the whole reason the method is fast. A sorted array can be searched with binary search.
The Update Rule
When a new element x arrives, find the first position k with tails[k] >= x.
- If no such position exists,
xis larger than every tail. It extends the longest subsequence, so append it. - Otherwise, set
tails[k] = x. Every tail beforekis less thanx, soxextends a length-ksubsequence into a length-k + 1one that ends inx <= tails[k].
No other slot changes. Slots before k already hold values smaller than x. Slots after k would need a length-k + 1 subsequence ending below x, but tails[k] >= x rules that out.
The answer is len(tails) once every element has been processed.
A Linear-Scan Version
The version below finds k with a plain scan, which keeps the logic easy to see. It runs in O(n^2) in the worst case. The next chapter replaces the scan with binary search.
def lis_length_linear(a):
tails = []
for x in a:
k = 0
while k < len(tails) and tails[k] < x:
k += 1
if k == len(tails):
tails.append(x)
else:
tails[k] = x
print(f"x={x:>3} tails={tails}")
return len(tails)
print(lis_length_linear([10, 9, 2, 5, 3, 7, 101, 18])) # 4The trace for [10, 9, 2, 5, 3, 7, 101, 18]:
| x | Action | tails after |
|---|---|---|
| 10 | append | [10] |
| 9 | replace slot 0 | [9] |
| 2 | replace slot 0 | [2] |
| 5 | append | [2, 5] |
| 3 | replace slot 1 | [2, 3] |
| 7 | append | [2, 3, 7] |
| 101 | append | [2, 3, 7, 101] |
| 18 | replace slot 3 | [2, 3, 7, 18] |
The final length is 4, which matches the DP.
Common Errors
- Using
>instead of>=when searching. For strict LIS, anxequal to an existing tail must replace it rather than land after it. Otherwise duplicates such as[2, 2]would count as length 2. - Replacing the wrong slot. Overwriting any slot other than the first one with
tails[k] >= xbreaks the sorted invariant, and every later search will return wrong results. - Expecting the length to shrink. A replacement never changes
len(tails). Only an append changes it.
Exercise
Trace the tails array for [4, 10, 4, 3, 8, 9]. What is the final tails array, and what is the LIS length?
Show answer
4 gives [4]. 10 gives [4, 10]. The second 4 replaces slot 0 because tails[0] = 4 >= 4, so tails stays [4, 10]. 3 gives [3, 10]. 8 replaces 10 to give [3, 8]. 9 appends to give [3, 8, 9]. The LIS length is 3, for example 4, 8, 9.
Exercise
Why must tails[k] be strictly greater than tails[k-1] for a strictly increasing LIS?
Show answer
The subsequence of length k+1 ending at tails[k] has a length-k prefix ending at some v < tails[k]. Since tails[k-1] is the minimum ending value for length k, tails[k-1] <= v < tails[k].