Sub topic 7
Activity Selection: Earliest Finish Time First
This chapter assumes you can read Python with loops and sorted, know that sorting costs O(n log n), and have seen the greedy-stays-ahead proof technique from the previous chapter.
The problem
You have one resource, such as a lecture hall, and a list of requests. Each request is an interval (start, end). Two requests conflict if their times overlap. The goal is to accept as many requests as possible with no two overlapping.
The intervals here are half-open: (1, 4) ends exactly when (4, 6) can begin, so those two are compatible.
The greedy rule
Sort the intervals by finish time. Walk through them in that order and accept each one that starts at or after the end of the last accepted interval.
The reasoning is that the interval that finishes first leaves the most remaining time for everything else. Any other first choice ends later, so it can only rule out more of the future.
def select_activities(intervals):
chosen = []
last_end = float("-inf")
for start, end in sorted(intervals, key=lambda iv: iv[1]):
if start >= last_end:
chosen.append((start, end))
last_end = end
return chosen
print(select_activities([(1, 4), (3, 5), (0, 6), (5, 7), (3, 9), (5, 9), (6, 10), (8, 11)]))
# [(1, 4), (5, 7), (8, 11)]Sorting takes O(n log n) and the scan takes O(n), so the whole algorithm runs in O(n log n).
Why it is optimal
Let the greedy picks be g1, g2, …, gk and let any optimal solution, sorted by time, be o1, o2, …, om. Greedy stays ahead: by induction, end(gi) ≤ end(oi) for every i. The base case holds because g1 has the earliest finish time of all intervals. For the inductive step, oi+1 starts after oi ends, which is at or after the end of gi. That makes oi+1 a candidate when greedy chooses gi+1, so greedy picks something that ends no later. If m were greater than k, then ok+1 would still be available after gk, and greedy would not have stopped. So k = m.
Common errors
Several rules look reasonable but are wrong. Each one fails on a small input:
| Rule | Counterexample | Greedy gets | Optimal |
|---|---|---|---|
| Earliest start | (0, 10), (1, 2), (3, 4) | 1 | 2 |
| Shortest duration | (0, 5), (4, 6), (5, 10) | 1 | 2 |
| Earliest finish | none exists | always optimal |
Two other errors are common. One is sorting the list but then comparing each interval against the previous interval in the list instead of the last accepted one. The other is starting last_end at 0, which rejects intervals with negative start times.
Exercise
Run the shortest-duration rule on (0, 5), (4, 6), (5, 10), then run earliest finish time on the same input. Which intervals does each rule choose?
Show answer
Shortest duration picks (4, 6), which has length 2 and overlaps both other intervals, so it ends with 1 interval. Earliest finish sorts the intervals as (0, 5), (4, 6), (5, 10). It takes (0, 5), skips (4, 6) because 4 < 5, and takes (5, 10) because 5 >= 5. That gives 2 intervals, which is optimal.
Exercise
Suppose every activity has a weight, and the goal is to maximize the total weight instead of the number of activities. Does earliest finish time still work?
Show answer
No. With (0, 10) of weight 100 and (1, 2) of weight 1, earliest finish picks (1, 2) and loses the heavy interval. Weighted interval scheduling needs dynamic programming.