Input: A set of intervals I = \{I_1, I_2, \dots, I_n\}. Each interval I_i = [s_i, f_i) with start s_i and finish f_i.
Compatibility: Two intervals are compatible if they do not overlap (f_i \le s_j or f_j \le s_i).
Output: A maximum-size subset of mutually compatible intervals.
CLRS (T1) refers to this problem as Activity Selection. The name Interval Scheduling (Kleinberg & Tardos) is more common.
Compatible Subsets:
Goal: find a subset of maximum size.
Sort intervals by finish time: f_1 \le f_2 \le \dots \le f_n.
Define S_{ij} = \{I_k : f_i \le s_k < f_k \le s_j\}.
Let c[i,j] be the maximum number of compatible intervals in S_{ij}:
c[i,j] = \begin{cases} 0 & \text{if } S_{ij} = \emptyset \\[4pt] \displaystyle\max_{i < k < j} \big(c[i,k] + c[k,j] + 1\big) & \text{if } S_{ij} \ne \emptyset \end{cases}
A DP solution would be O(n^3) — but a greedy approach is simpler and faster.
Intuition: Choose the interval that leaves the resource available for as many others as possible.
Which interval should we pick first?
Greedy choice: Always pick the interval with the earliest finish time among the remaining compatible intervals.
Pick the interval that starts first.
Counterexample:
Among all intervals, one must finish first in any optimal solution.
Picking the earliest finish leaves the maximum remaining time for other intervals.
After sorting by finish time, the greedy choice picks I_1 first.
Greedy-Interval-Scheduler(s, f):
// Assumes f[1..n] is sorted in increasing order
n = length(s)
A = {I_1}
k = 1
for m = 2 to n:
if s[m] >= f[k]:
A = A ∪ {I_m}
k = m
return ATwo parts:
Lemma
There exists an optimal solution A such that the greedy choice I_1 (earliest finish) is in A.
Proof. Let A be an optimal solution, ordered by finish time. Let k_1 be the first interval in A.
Note
Lemma: If A is an optimal solution, then A' = A \setminus \{I_1\} is an optimal solution to S' = \{I_i \in I : s_i \ge f_1\}.
Proof (Contradiction).
These two properties must be proved for every greedy algorithm.
CS F364: Design & Analysis of AlgorithmsTulasimohan Molli