Sub topic 8
Interval Partitioning: Scheduling with the Fewest Rooms
This chapter assumes you know activity selection from the previous chapter, Python's heapq module, and proof by contradiction.
The problem
You have a set of lectures, each with a start and an end time. Every lecture must be scheduled, and two lectures that overlap cannot share a room. What is the smallest number of rooms that holds them all?
Activity selection keeps one room and drops lectures. Interval partitioning keeps every lecture and adds rooms. We treat each interval as half-open, [start, end). A lecture ending at 10 can therefore share a room with one starting at 10.
The lower bound: depth
The depth of a set of intervals is the largest number of intervals that contain any single point in time. If three lectures are all running at 9:30, you need at least three rooms. No algorithm can do better than the depth, so a schedule that uses exactly depth rooms is optimal.
The greedy rule
- Sort the intervals by start time.
- For each interval, look at the room that frees up earliest.
- If that room is already free, reuse it. If not, open a new room.
A min-heap of end times gives you the earliest-free room in O(1) and updates it in O(log n). The total cost is O(n log n).
import heapq
def min_rooms(intervals):
ends = []
for start, end in sorted(intervals):
if ends and ends[0] <= start:
heapq.heapreplace(ends, end)
else:
heapq.heappush(ends, end)
return len(ends)
print(min_rooms([(0, 30), (5, 10), (15, 20)])) # 2Trace
| Interval | Heap before | Action | Heap after |
|---|---|---|---|
| (0, 30) | [] | open room | [30] |
| (5, 10) | [30] | 30 > 5, open room | [10, 30] |
| (15, 20) | [10, 30] | 10 <= 15, reuse | [20, 30] |
The algorithm uses two rooms. The depth is also 2, because (0, 30) and (5, 10) overlap at time 5.
Why it is optimal
Suppose the greedy opens room number d while processing interval I, which starts at time s. A new room opens only when every one of the d − 1 rooms in use has an end time greater than s. Each interval in those rooms also started at or before s, because the input is sorted by start time. So all d − 1 of them, plus I, contain the moment just after s. That makes the depth at least d. The greedy never uses more rooms than the depth, and no schedule can use fewer, so the greedy is optimal.
This proof is a structural bound. The proof shows the greedy meets a lower bound that every solution must respect, which is a common pattern in greedy proofs.
Exercise
How many rooms does min_rooms return for [(1, 4), (2, 5), (3, 6), (5, 7), (6, 8)]? Name a time point that proves this many rooms is necessary.
Show answer
3. At time 3.5, the intervals (1, 4), (2, 5) and (3, 6) all overlap, so the depth is at least 3. The greedy reuses rooms for (5, 7) and (6, 8), so it never needs a fourth.
Exercise
The rules change so that a meeting ending at 10 cannot share a room with one starting at 10. What single change to min_rooms handles this?
Show answer
Change the test ends[0] <= start to ends[0] < start. A room is then reused only if it freed up strictly before the new meeting begins.