Sub topic 2
Testing Whether Two Intervals Overlap
This chapter assumes you can read short Python and know that an interval is a pair (low, high) with low <= high. All intervals here are closed, so both endpoints belong to the interval.
The Test
Two closed intervals overlap if they share at least one point. You could compare all six orderings of the four endpoints. It is simpler to look at when they do not overlap. There are only two cases:
- Interval
aends beforebstarts:a.high < b.low. - Interval
bends beforeastarts:b.high < a.low.
If neither case holds, the intervals overlap. Negating both conditions gives the test:
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]
print(overlaps((15, 20), (10, 30))) # True
print(overlaps((5, 8), (9, 12))) # False
print(overlaps((5, 9), (9, 12))) # True, closed intervals sharing an endpoint overlapIn words, each interval must start no later than the other one ends. The test needs two comparisons and works for every arrangement: partial overlap, one interval containing the other, and identical intervals.
Why Both Comparisons Are Needed
A common mistake is to check only one side, such as a[0] <= b[1]. That condition says only that a does not start after b ends. It is also true when a lies entirely after b's start and... entirely before it, as in a = (1, 2) and b = (5, 9). Here 1 <= 9 holds, yet the intervals are disjoint. The second comparison, 5 <= 2, is false and rejects the pair.
Closed Versus Half-Open Intervals
The endpoint convention changes the comparison operators. Check the convention before you write the test.
| Convention | Example | Overlap test |
|---|---|---|
| Closed `[low, high]` | `(5, 9)` and `(9, 12)` share the point 9 | `a.low <= b.high and b.low <= a.high` |
| Half-open `[low, high)` | `(5, 9)` and `(9, 12)` do not overlap | `a.low < b.high and b.low < a.high` |
Half-open intervals are common for time ranges, because back-to-back meetings should not conflict. The rest of this tutorial uses closed intervals, which is why the code above uses <=.
Common Errors
- Using
<instead of<=for closed intervals. This misses intervals that touch at one point. - Passing a pair with
low > high. The test assumeslow <= highand gives meaningless answers otherwise. Validate or normalize at the point where intervals enter your program. - Mixing conventions. If some intervals are closed and others half-open, no single test is right for both.
The overlap test is the base for everything that follows. Every interval tree operation decides whether to keep a stored interval using this check.
Exercise
Using closed intervals, does (3, 7) overlap (7, 9)? Does it overlap (8, 9)? What would the first answer be if the intervals were half-open?
Show answer
(3, 7) and (7, 9) overlap, because 3 <= 9 and 7 <= 7, so they share the point 7. (3, 7) and (8, 9) do not overlap, because 8 <= 7 is false. If the intervals were half-open, the first pair would not overlap, because the test 7 < 7 is false.
Exercise
A programmer writes return a[0] <= b[1] as the overlap test. Give a pair of intervals where this returns True but the intervals are disjoint.
Show answer
For a = (1, 2) and b = (5, 9), 1 <= 9 is True, but the intervals are disjoint. The missing check is b[0] <= a[1], which is 5 <= 2 and False.