Sub topic 4
Activity selection by earliest finish time
This chapter assumes you know Big-O notation, can sort an array in O(n log n), and can read short Python. It also assumes you have met the idea of a greedy choice from the previous chapters.
The problem
You are given n activities. Activity i has a start time s_i and a finish time f_i, and it occupies the half-open interval [s_i, f_i). Two activities are compatible if their intervals do not overlap. One activity may start exactly when another finishes. The goal is to select the largest possible set of pairwise compatible activities. The goal is to maximize the count of activities, not the total time used.
The same problem appears as "schedule the most meetings in one room" and "interval scheduling".
The greedy rule
Sort the activities by finish time. Then scan them in that order. Take an activity if its start is at least the finish time of the last activity you took.
The rule works because the activity that finishes earliest leaves the most room for everything that comes after it.
def max_activities(intervals):
count = 0
last_end = float('-inf')
for start, finish in sorted(intervals, key=lambda x: x[1]):
if start >= last_end:
count += 1
last_end = finish
return count
print(max_activities([(1, 4), (3, 5), (0, 6), (5, 7), (3, 9), (5, 9), (6, 10), (8, 11), (8, 12), (2, 14), (12, 16)])) # 4The comparison is start >= last_end, not >. Using > would wrongly reject an activity that begins at the moment the previous one ends. In GATE questions, check whether the problem treats touching intervals as compatible.
Tracing by hand
Sorted by finish time, the eleven activities appear in this order: (1,4), (3,5), (0,6), (5,7), (3,9), (5,9), (6,10), (8,11), (8,12), (2,14), (12,16).
| Activity | last_end before | Decision | last_end after |
|---|---|---|---|
| (1,4) | -inf | take | 4 |
| (3,5) | 4 | skip, 3 < 4 | 4 |
| (0,6) | 4 | skip | 4 |
| (5,7) | 4 | take | 7 |
| (3,9), (5,9), (6,10) | 7 | skip, all start before 7 | 7 |
| (8,11) | 7 | take | 11 |
| (8,12), (2,14) | 11 | skip | 11 |
| (12,16) | 11 | take | 16 |
The answer is 4: (1,4), (5,7), (8,11), (12,16).
Why it is correct: the exchange argument
Let the activities be sorted by finish time, and let a1 be the one that finishes first. Take any optimal solution S, with its activities ordered by finish time.
- If
Scontainsa1, the greedy choice already agrees withS. - If
Sdoes not containa1, letbbe the first activity inS. Becausea1finishes no later thanb, replacingbwitha1cannot create an overlap with the rest ofS. The new set has the same size, so it is also optimal.
So some optimal solution contains the greedy choice. This is the greedy-choice property. After taking a1, the remaining problem is the same problem restricted to activities that start at or after f1. That is optimal substructure. Induction on the number of activities finishes the proof.
Complexity
- Sorting costs O(n log n).
- The scan makes one pass and costs O(n).
- Total time is O(n log n). Extra space is O(n) for the sorted copy, or O(1) if you sort in place.
If the input is already sorted by finish time, the algorithm runs in O(n). GATE questions often state this assumption.
Returning the chosen activities
Counting is often not enough. This version returns the selected intervals.
def select_activities(intervals):
chosen = []
last_end = float('-inf')
for start, finish in sorted(intervals, key=lambda x: x[1]):
if start >= last_end:
chosen.append((start, finish))
last_end = finish
return chosen
print(select_activities([(1, 4), (3, 5), (0, 6), (5, 7), (3, 9), (5, 9), (6, 10), (8, 11), (8, 12), (2, 14), (12, 16)]))
# [(1, 4), (5, 7), (8, 11), (12, 16)]The optimal set is not always unique. Ties in finish time can lead to a different set of the same size, but the size never changes.
Common errors: rules that look plausible but fail
Each of these rules fails on a small counterexample.
| Greedy rule | Counterexample | Rule picks | Optimum |
|---|---|---|---|
| Earliest start time | (0,10), (1,2), (3,4) | 1 activity | 2 |
| Shortest duration | (0,5), (4,7), (6,10) | 1 activity, since (4,7) blocks both others | 2 |
| Latest start time, scanning backward | works, by symmetry with earliest finish | optimal | optimal |
The last row is a valid variant. Choosing the latest start time and scanning backward is the mirror image of the earliest-finish rule. Any rule you propose needs an exchange argument. A rule that merely seems sensible is not evidence.
Other mistakes to avoid:
- Sorting by start time and then applying the finish-time test. The scan order must be finish time.
- Updating
last_endwhen you skip an activity. Update it only when you take one. - Confusing this problem with weighted interval scheduling. If each activity has a profit and you maximize total profit, the greedy rule fails and you need dynamic programming.
Exercise
Activities given as (start, finish): (1,3), (2,5), (4,7), (1,8), (5,9), (8,10), (9,11). Using earliest finish time, how many activities are selected, and which ones?
Show answer
Sorted by finish: (1,3), (2,5), (4,7), (1,8), (5,9), (8,10), (9,11). Take (1,3), so lastend = 3. Skip (2,5) because 2 < 3. Take (4,7), so lastend = 7. Skip (1,8) and (5,9). Take (8,10), so last_end = 10. Skip (9,11) because 9 < 10. The result is 3 activities: (1,3), (4,7), (8,10).
Exercise
Give a counterexample showing that choosing the shortest activity first is not optimal, and state the running time of the earliest-finish algorithm on n unsorted activities.
Show answer
Take (0,5), (4,7), (6,10). The shortest activity is (4,7), and it overlaps both others, so the rule picks 1 activity. The optimum is 2, namely (0,5) and (6,10). The earliest-finish algorithm runs in O(n log n), dominated by sorting, because the scan itself is O(n).