Skip to content
Part IA Lent Term

Activity Selection and Interval Scheduling

Problem Statement

Given nn activities, each with a start time sis_i and finish time fif_i (with si<fis_i < f_i), select the maximum-size subset of mutually compatible activities. Two activities ii and jj are compatible if their intervals do not overlap: [si,fi)[s_i, f_i) and [sj,fj)[s_j, f_j) satisfy fisjf_i \le s_j or fjsif_j \le s_i. The half-open interval convention means an activity finishing at time tt permits another to start at time tt.

Greedy Strategy

Sort activities by finish time (ascending). Then:

  1. Select the first activity (earliest finish).
  2. Among activities that start at or after the finish time of the last selected activity, select the one with the earliest finish time.
  3. 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: Θ(nlogn)\Theta(n \log n) for sorting, then Θ(n)\Theta(n) for the greedy scan.

Worked Example

ActivityStartFinish
108:0008:30
209:0011:30
310:0011:00
410:4512:00

Sorted by finish time: 1(08:0008:30),3(10:0011:00),2(09:0011:30),4(10:4512:00)1(08:00\text{–}08:30), 3(10:00\text{–}11:00), 2(09:00\text{–}11:30), 4(10:45\text{–}12:00).

  • Select 1 (k=1k = 1).
  • i=2i = 2: activity 3. s3=10:00f1=8:30s_3 = 10:00 \ge f_1 = 8:30 → select 3 (k=3k = 3).
  • i=3i = 3: activity 2. s2=9:00<f3=11:00s_2 = 9:00 < f_3 = 11:00 → skip.
  • i=4i = 4: activity 4. s4=10:45<f3=11:00s_4 = 10:45 < f_3 = 11:00 → skip.

Result: {1,3}\{1, 3\}. Optimal (size 2). Alternative optimal solutions: {1,2}\{1, 2\}, {1,4}\{1, 4\} — 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 AA be an optimal solution. Let kk be the activity with the earliest finish time overall.

  • If AA contains kk, done.
  • If AA does not contain kk, let jj be the first activity in AA. Replace jj with kk. Since fkfjf_k \le f_j, activity kk finishes no later than jj, so kk is compatible with all other activities in AA. The modified solution A=(A{j}){k}A' = (A \setminus \{j\}) \cup \{k\} 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 p(i)p(i) be the largest index j<ij < i with fjsif_j \le s_i. Then:

OPT(i)=max(OPT(i1),  1+OPT(p(i)))\text{OPT}(i) = \max\big(\text{OPT}(i-1),\; 1 + \text{OPT}(p(i))\big)

This is Θ(n2)\Theta(n^2) to compute all p(i)p(i) values, or Θ(nlogn)\Theta(n \log n) 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

PropertyValue
ProblemMaximise number of non-overlapping intervals
Greedy ruleEarliest finish time first
TimeΘ(nlogn)\Theta(n \log n) (sort) + Θ(n)\Theta(n) (select)
ProofExchange argument
Weighted variantRequires DP; greedy fails
Alternative heuristicsShortest first, earliest start, fewest overlaps — all incorrect