Sub topic 8
Strict vs Non-Decreasing: bisect_left vs bisect_right
This chapter assumes you know the O(n log n) tails-array algorithm from the previous chapter. You should also know how Python's bisect module searches a sorted list. The only new idea here is one character in a function name, and it changes which problem the algorithm solves.
Two problems that differ only on ties
A strictly increasing subsequence requires each element to be greater than the one before it: a[i1] < a[i2] < .... A non-decreasing subsequence also allows equal neighbours: a[i1] <= a[i2] <= ....
On input with no repeated values the two answers are the same. Once values repeat, the answers can differ:
| Input | Longest strictly increasing | Longest non-decreasing |
|---|---|---|
| `[1, 3, 3, 2, 3]` | 3 (`1, 2, 3`) | 4 (`1, 3, 3, 3`) |
| `[2, 2, 2, 2]` | 1 | 4 |
| `[4, 1, 5, 2]` | 2 | 2 |
What the two bisect functions do
In a sorted list tails, both functions return the position where x would be inserted to keep the list sorted. They give different results only when x is already in the list:
bisect_left(tails, x)returns the index of the first element>= x. The new value lands before any copies of itself.bisect_right(tails, x)returns the index of the first element> x. The new value lands after any copies of itself.
In the tails algorithm, the returned index i means "x extends a subsequence of length i". The element at tails[i - 1] is the tail that x follows, so the bound you choose sets which comparison counts as an extension.
- With
bisect_left, every tail before indexiis strictly less thanx. Soxextends only strictly smaller tails, which gives the strict LIS. - With
bisect_right, every tail before indexiis<= x. An equal tail counts as an extension, which gives the non-decreasing version.
The non-decreasing version
from bisect import bisect_right
def longest_non_decreasing_length(a):
tails = []
for x in a:
i = bisect_right(tails, x)
if i == len(tails):
tails.append(x)
else:
tails[i] = x
return len(tails)
print(longest_non_decreasing_length([1, 3, 3, 2, 3])) # 4Trace both bounds on [1, 3, 3, 2, 3]:
| x | tails with bisect_left | tails with bisect_right |
|---|---|---|
| 1 | `[1]` | `[1]` |
| 3 | `[1, 3]` | `[1, 3]` |
| 3 | `[1, 3]` (replace) | `[1, 3, 3]` (append) |
| 2 | `[1, 2]` | `[1, 2, 3]` |
| 3 | `[1, 2, 3]` | `[1, 2, 3, 3]` |
The second 3 is where the two runs split. bisect_left overwrites the existing 3 with an equal value, so nothing changes. bisect_right places it after the existing 3 and makes the list longer.
The tails list is still sorted in the non-decreasing version, but it can now contain duplicates. The invariant still holds: tails[k] is the smallest tail of any non-decreasing subsequence of length k + 1.
Applying the same switch elsewhere
The change carries over to the other versions in this tutorial:
- O(n²) DP: change
a[j] < a[i]toa[j] <= a[i]. - O(n log n) reconstruction: in
lis_sequence, swapbisect_leftforbisect_right. The parent links work unchanged. - Decreasing variants: negate every value first. To get the strictly decreasing version, run the strict algorithm on the negated values. To get the non-increasing version, run the non-decreasing one.
Common errors
- Using
<=in the DP butbisect_leftin the fast version. The two then answer different questions and disagree on input with repeats. - Assuming
tailsholds unique values. This is true only for the strict version. - Testing only on distinct values. Ties are the one case that tells the two bounds apart.
Exercise
What do the strict and non-decreasing tails algorithms each return for [2, 2, 2, 2], and what does each tails list look like at the end?
Show answer
Strict (bisectleft) returns 1. Every 2 lands at index 0 and replaces the one before it, so tails ends as [2]. Non-decreasing (bisectright) returns 4. Every 2 lands after the existing copies and is appended, so tails ends as [2, 2, 2, 2].
Exercise
You need the length of the longest non-increasing subsequence of [5, 3, 3, 1, 4]. Which transformation and which bisect function do you use, and what is the answer?
Show answer
Negate the values to get [-5, -3, -3, -1, -4]. A non-increasing subsequence of the original is a non-decreasing subsequence of the negated list, so use bisect_right. The answer is 4, from 5, 3, 3, 1.