Sub topic 3
The Problem: Overlap and Stabbing Queries
This chapter defines the queries an interval tree answers. It does not build the tree yet. You will pin down each query precisely, write a reference implementation, and see which properties a faster structure has to keep.
I assume you know the closed-interval overlap test from the previous chapter, and that you can read short Python.
The Setting: A Dynamic Set of Intervals
We keep a collection of closed intervals (low, high) with low <= high. Two things make the problem interesting:
- The collection is dynamic. Intervals are inserted and deleted over time, so a structure that is built once and never changed is not enough.
- Queries arrive repeatedly against the current collection. We want each query to be cheap compared with the size of the collection.
Typical examples are meeting bookings on a calendar, ranges of memory addresses held by allocations, and time spans during which a sensor was active.
Overlap Queries
An overlap query gives you a query interval q = (lo, hi) and asks which stored intervals overlap it. "Overlap" uses the same test as before: a stored interval s overlaps q when s.low <= q.high and q.low <= s.high.
The question comes in several forms, and they have different costs:
| Form | Question | Result |
|---|---|---|
| Existence | Does any stored interval overlap `q`? | `True` or `False` |
| Find one | Give me any one stored interval that overlaps `q`. | One interval or `None` |
| Report all | Give me every stored interval that overlaps `q`. | A list of `k` intervals |
| Count | How many stored intervals overlap `q`? | An integer |
The find-one form is the one the main interval tree search answers directly. Report-all is built on top of it, and its cost depends on how many answers exist.
Stabbing Queries
A stabbing query gives you a single point x and asks which stored intervals contain it. An interval (low, high) contains x when low <= x <= high.
A stabbing query is an overlap query in disguise. A point is a degenerate interval (x, x), and the overlap test with (x, x) reduces to low <= x and x <= high, which is the containment test. So any structure that answers overlap queries answers stabbing queries for free.
The reverse is not true in general. Some structures handle stabbing queries efficiently but handle general overlap queries awkwardly, and Chapter 16 returns to this when comparing designs.
A Reference Implementation
Before optimizing anything, write the simplest version that is obviously correct. It defines what every faster structure must return, and you can test against it later.
def overlaps(a, b):
# a and b are closed intervals given as (low, high) with low <= high
return a[0] <= b[1] and b[0] <= a[1]
def overlap_all(intervals, q):
# Every stored interval that overlaps the query interval q.
return [s for s in intervals if overlaps(s, q)]
def overlap_one(intervals, q):
# Any one overlapping interval, or None.
for s in intervals:
if overlaps(s, q):
return s
return None
def stab(intervals, x):
# Every stored interval that contains the point x.
return overlap_all(intervals, (x, x))
stored = [(15, 20), (10, 30), (17, 19), (5, 20), (12, 15), (30, 40)]
print(overlap_all(stored, (21, 23))) # [(10, 30)]
print(overlap_all(stored, (14, 16))) # [(15, 20), (10, 30), (5, 20), (12, 15)]
print(overlap_one(stored, (45, 50))) # None
print(stab(stored, 15)) # [(15, 20), (10, 30), (5, 20), (12, 15)]
print(stab(stored, 30)) # [(10, 30), (30, 40)]Notice that stab is one line. Notice also that the results come back in storage order, not sorted. When you compare two implementations later, sort both lists first. Which interval overlap_one returns is not specified. Any overlapping interval is a correct answer, so a test should check that the returned interval overlaps q, not that it equals a particular one.
Edge Cases That Change the Answer
Details of the definition decide the results, so settle them before writing any tree code.
- Shared endpoints count. With closed intervals,
(5, 9)and(9, 12)overlap at the single point 9. Stabbing at 30 returns both(10, 30)and(30, 40)above. - Degenerate stored intervals. An interval
(7, 7)is legal. It is stabbed only byx = 7. - Duplicates. The same interval may be stored twice. Report-all then returns it twice unless you decide otherwise. Decide this early, because it affects deletion too.
- Empty set. Every query returns
None, an empty list, or 0. - Half-open intervals. Many real systems use
[low, high). Then touching intervals do not overlap, and the test becomesa[0] < b[1] and b[0] < a[1]. The tree structure in this tutorial does not change, but every<=in the comparisons must be reconsidered.
What a Good Answer Looks Like
The number of results k can be anywhere from 0 to n, the number of stored intervals. A query whose query interval covers everything must return all n intervals, so no structure can answer report-all in less than k steps. That makes the target cost depend on both sizes.
- For existence and find-one, which return at most one interval, we want time that grows like
log n. - For report-all, we want time that grows like
log n + k. This is called output-sensitive: you pay for the work of finding answers plus the work of handing them back, and nothing more.
The reference implementation above costs about n overlap tests per query regardless of k. The next chapter looks at what that costs and why sorting the intervals by low endpoint does not help.
Summary of Terms
- Overlap query: given an interval, find stored intervals that share at least one point with it.
- Stabbing query: given a point, find stored intervals that contain it. It is an overlap query with
(x, x). - Output-sensitive: a cost that includes the size
kof the answer. - Dynamic set: a collection that supports inserts and deletes between queries.
Exercise
The stored intervals are (1, 4), (4, 6), (7, 9) and (2, 8). Which of them does the stabbing query x = 4 return, and what overlap query is it equivalent to?
Show answer
It returns (1, 4), (4, 6) and (2, 8). The interval (7, 9) starts after 4. Closed intervals include their endpoints, so both (1, 4) and (4, 6) contain 4. The query is equivalent to the overlap query with the degenerate interval (4, 4).
Exercise
Why can no algorithm answer a report-all overlap query in O(log n) time for every input, no matter how the intervals are stored?
Show answer
A query can overlap all n stored intervals, so k can equal n. The algorithm has to output n intervals, which takes at least n steps. The best possible bound therefore depends on k, such as O(log n + k).