Sub topic 3
The Problem: Overlap and Stabbing Queries
This chapter assumes you can read short Python, know what a binary search tree is, and are comfortable with Big-O notation. It uses the closed-interval overlap test from the previous chapter: a[0] <= b[1] and b[0] <= a[1].
The Stored Set
An interval tree holds a dynamic set of closed intervals. Dynamic means the set changes over time, so the structure must support three operations:
- Insert an interval.
- Delete an interval.
- Query the set for intervals that match a given probe.
Calendars, genomic ranges, and scheduling systems all fit this shape. Intervals arrive and leave, and you repeatedly ask what is "at" some time or position.
Two Kinds of Query
An overlap query takes a probe interval [lo, hi]. It asks which stored intervals overlap the probe.
A stabbing query takes a single point x. It asks which stored intervals contain x. The name comes from stabbing a line through the intervals at position x and seeing which ones it hits.
A stabbing query is an overlap query whose probe is the degenerate interval [x, x]. One overlap routine therefore answers both.
| Query | Input | Result |
|---|---|---|
| Overlap | interval `[lo, hi]` | stored intervals that share a point with it |
| Stabbing | point `x` | stored intervals `[a, b]` with `a <= x <= b` |
Each query also comes in two forms. The first form asks for any one match, which is enough for questions like "is this time slot free?". The second asks for every match. Later chapters give them different algorithms and different costs.
A Reference Implementation
The simplest correct solution checks every stored interval. It is also a useful test oracle for faster code later.
def overlaps(a, b):
# a and b are closed intervals (low, high) with low <= high
return a[0] <= b[1] and b[0] <= a[1]
def overlap_query(intervals, lo, hi):
return [iv for iv in intervals if overlaps(iv, (lo, hi))]
def stab_query(intervals, x):
return overlap_query(intervals, x, x)
data = [(15, 20), (10, 30), (17, 19), (5, 20), (12, 15), (30, 40)]
print(overlap_query(data, 21, 23)) # [(10, 30)]
print(stab_query(data, 16)) # [(15, 20), (10, 30), (5, 20)]
print(stab_query(data, 30)) # [(10, 30), (30, 40)]The last line shows the closed-interval rule. The point 30 is the high end of (10, 30) and the low end of (30, 40), so it stabs both.
Each query touches all n intervals, so it costs O(n) even when it returns nothing. The next chapter looks at why that cost becomes a problem.
Common Errors
Two other mistakes are common:
- Storing an interval with
low > high. The overlap test assumeslow <= high, so reject or swap such input on insertion. - Treating "no match" as an error. An empty list, or
Nonefor the one-match form, is a normal answer.
Exercise
Using the data list above, what does overlap_query(data, 18, 18) return, and why is it the same as stab_query(data, 18)?
Show answer
It returns [(15, 20), (10, 30), (17, 19), (5, 20)]. A probe of (18, 18) is a single point, and stab_query is defined as overlap_query(intervals, x, x), so the two calls run the same code. Intervals (12, 15) and (30, 40) are excluded because 18 lies outside them.