Activity Selection and Interval Scheduling
Problem Statement
Given activities, each with a start time and finish time (with ), select the maximum-size subset of mutually compatible activities. Two activities and are compatible if their intervals do not overlap: and satisfy or . The half-open interval convention means an activity finishing at time permits another to start at time .
Greedy Strategy
Sort activities by finish time (ascending). Then:
- Select the first activity (earliest finish).
- Among activities that start at or after the finish time of the last selected activity, select the one with the earliest finish time.
- Repeat until no more compatible activities remain.
sort activities by finish time
selected = {1}
k = 1
for i = 2 to n:
if s[i] >= f[k]:
selected = selected ∪ {i}
k = i
Running time: for sorting, then for the greedy scan.
Worked Example
| Activity | Start | Finish |
|---|---|---|
| 1 | 08:00 | 08:30 |
| 2 | 09:00 | 11:30 |
| 3 | 10:00 | 11:00 |
| 4 | 10:45 | 12:00 |
Sorted by finish time: .
- Select 1 ().
- : activity 3. → select 3 ().
- : activity 2. → skip.
- : activity 4. → skip.
Result: . Optimal (size 2). Alternative optimal solutions: , — all achieve the same maximum cardinality.
Why Earliest Finish Time Works
The greedy choice is the activity with the earliest finish time. Intuition: this leaves the maximum remaining time for other activities, giving the greatest opportunity to schedule more.
Any other first choice would finish later and could only reduce (or at best leave unchanged) the amount of remaining time. There is never a reason to prefer a later-finishing activity.
Proof (Exchange Argument)
Let be an optimal solution. Let be the activity with the earliest finish time overall.
- If contains , done.
- If does not contain , let be the first activity in . Replace with . Since , activity finishes no later than , so is compatible with all other activities in . The modified solution has the same size and is also optimal.
By induction, the greedy algorithm produces an optimal schedule.
DP Formulation (Unweighted)
The problem can also be solved with DP. Sort by finish time. Let be the largest index with . Then:
This is to compute all values, or with binary search on finish times. The greedy approach is simpler and equally optimal.
Weighted Variant
If each activity has a weight (value) and the goal is to maximise total weight rather than count, the greedy strategy fails. The DP formulation above remains correct with minor modification (using weight instead of 1), and no greedy shortcut exists. This is the weight-independent vs. weight-dependent distinction.
Discussion: Incorrect Heuristics
- Shortest activity first: can select a short activity that blocks two longer, mutually compatible ones.
- Earliest start time first: a very long activity starting early blocks everything else.
- Fewest overlaps first: a counterexample exists where the activity with fewest overlaps still prevents a better combination.
Only earliest finish time greedily guarantees optimality for the unweighted case.
Summary
| Property | Value |
|---|---|
| Problem | Maximise number of non-overlapping intervals |
| Greedy rule | Earliest finish time first |
| Time | (sort) + (select) |
| Proof | Exchange argument |
| Weighted variant | Requires DP; greedy fails |
| Alternative heuristics | Shortest first, earliest start, fewest overlaps — all incorrect |