Sub topic 4
Why a Linear Scan Is Not Enough
This chapter assumes you can read short Python and know Big-O notation. It also assumes you know the overlap test from the earlier chapter: closed intervals a and b overlap when a[0] <= b[1] and b[0] <= a[1].
The Obvious Solution
The simplest way to answer "which stored intervals overlap this query?" is to test every stored interval.
def scan_overlaps(intervals, lo, hi):
# Returns every stored interval that overlaps the closed interval [lo, hi].
return [iv for iv in intervals if iv[0] <= hi and lo <= iv[1]]
stored = [(1, 100), (2, 3), (4, 5), (6, 7), (8, 9)]
print(scan_overlaps(stored, 50, 60)) # [(1, 100)]This is correct, needs no extra structure, and inserting is just an append. Its cost is the problem. Each query examines all n intervals, so one query is O(n), even when only one interval matches.
| Stored intervals | Queries | Overlap tests |
|---|---|---|
| 1,000 | 1,000 | 1,000,000 |
| 1,000,000 | 1,000 | 1,000,000,000 |
If the set is small or you query it rarely, the scan is fine. It stops being fine when the set is large and queries are frequent, for example a calendar service checking every new booking against all existing ones.
Sorting by Low Endpoint Is Not Enough
A natural fix is to keep the intervals sorted by low endpoint, the same key a binary search tree would use. With that order you can binary search for the last interval whose lo is at most the query's hi. Every interval after that point starts too late to overlap, so you can ignore it.
That removes only one side of the problem. Among the intervals before the cut, you still need hi >= query lo. The high endpoints are not in order, so no binary search works on them.
from bisect import bisect_right
stored = [(1, 100), (2, 3), (4, 5), (6, 7), (8, 9)] # sorted by low endpoint
lows = [iv[0] for iv in stored]
lo, hi = 50, 60
cut = bisect_right(lows, hi) # intervals before index cut start at or before 60
print(cut) # 5, so every interval is still a candidate
print([iv for iv in stored[:cut] if lo <= iv[1]]) # [(1, 100)]The binary search discarded nothing here. The one matching interval, (1, 100), has the smallest low endpoint, so it is at the front. It could just as well be hidden among thousands of short intervals, and nothing in the sorted order says where. In the worst case you are back to O(n).
What the Fix Needs to Provide
The failure points to what is missing. While walking a sorted structure, we would like to ask of a whole group of intervals: "Can anything in here reach as far right as the query's low endpoint?" If the answer is no, we can skip the group without looking at it.
The largest high endpoint in a group answers that question. If it is smaller than the query's lo, no interval in the group can overlap. The next chapters store that value in each node of a binary search tree, which gives O(log n) search in a balanced tree.
Exercise
Intervals are sorted by low endpoint: (1, 50), (2, 3), (4, 5), (6, 7). The query is [40, 45]. A binary search on low endpoints finds that every interval has lo <= 45. Which interval overlaps, and why can the sorted order not point you to it directly?
Show answer
Only (1, 50) overlaps, since 1 <= 45 and 40 <= 50. The high endpoints (50, 3, 5, 7) are not sorted, so the sorted low endpoints say nothing about which interval reaches 40. You must check every candidate or keep extra information, such as the maximum high endpoint of a group.