Greedy optimization · Algorithm 12 of 15
Interval Scheduling
Choose the largest compatible set of activities by earliest finish time. Citation needed
The whole, before the parts
Interval scheduling chooses as many non-overlapping activities as possible for one shared resource. A meeting room provides the whole problem: selecting one booking limits which other bookings can fit. Citation needed
Terminology and core concepts
Given activities with start and finish times, choose as many non-overlapping activities as possible. Greedy interval scheduling chooses the available activity that finishes first. The exchange argument explains why: replace the first activity in an optimal solution with the earliest-finishing activity. This leaves at least as much room for every later activity, so the replacement cannot reduce the count. Repeating the argument proves the greedy rule. Intervals here are half-open: an activity may start exactly when another finishes. Citation needed
Learning path
Trace an example
activities: (0,3), (1,2), (2,4), (3,5), (4,6) sort by finish: (1,2), (0,3), (2,4), (3,5), (4,6) take (1,2) current finish 2 skip (0,3) overlaps take (2,4) current finish 4 skip (3,5) overlaps take (4,6) three compatible activities
Implement it in Python
Read the loop alongside the trace, then run this self-contained example with Python 3. The assertions show expected results. Inputs use ordinary Python lists and dictionaries; comparisons and numeric operations are assumed to behave normally. Citation needed
def interval_schedule(intervals):
# Each interval has start < finish; the goal is maximum count, not value.
if any(start >= finish for start, finish in intervals):
raise ValueError('Activities must have positive duration')
chosen = []
last_finish = float('-inf')
for start, finish in sorted(intervals, key=lambda pair: pair[1]):
if start >= last_finish:
chosen.append((start, finish))
last_finish = finish
return chosen
assert interval_schedule([(0, 3), (1, 2), (2, 4), (3, 5), (4, 6)]) == [
(1, 2), (2, 4), (4, 6)]
assert interval_schedule([]) == []Citation needed
Analyze the cost
Sorting n activities takes O(n log n); the scan takes O(n). This version uses O(n) extra space for the sorted copy and selected output. If activities are already sorted by finish time, selection alone is linear. Citation needed
A mistake to watch for
Choosing earliest start or shortest duration does not generally maximize the count. If activities carry different rewards, maximizing total reward is a different problem that needs more than this greedy rule. Citation needed
Practice and recall
-
Why is choosing earliest start wrong for [(0, 10), (1, 2), (2, 3)]?
Work it out before opening the solution. Citation needed
Show the reasoning
- Earliest start chooses (0, 10), blocking both shorter activities.
- Earliest finish chooses (1, 2), then (2, 3).
- Two activities beat one, so earliest start is not a safe rule.
Citation needed
- Which compatible activity does the greedy rule choose next?
- The one with the earliest finish time. Citation needed
To check your understanding, reproduce the implementation without looking, explain why its main step is safe, and test an edge case before moving to the next algorithm. Citation needed
Sources
- Princeton: Kleinberg–Tardos greedy algorithms lecture slides (PDF) — further study of the underlying algorithm and analysis.
- Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein, Introduction to Algorithms — correctness arguments and complexity analysis.