DSA Notes
Master the Activity Selection Problem completely — problem definition, why sorting by end time is the correct greedy criterion, exchange argument proof, implementation variants (maximum activities, weighted interval scheduling, minimum rooms), and interval scheduling interview problems.
Problem Statement
Given n activities, each with a start time s[i] and finish time f[i], select the maximum number of non-overlapping activities that a single machine can perform.
Two activities i and j are compatible if they don't overlap: f[i] ≤ s[j] or f[j] ≤ s[i].
| Activities | [(1,4),(3,5),(0,6),(5,7),(3,9),(5,9),(6,10),(8,11),(8,12),(2,14),(12,16)] |
| Start | 1 3 0 5 3 5 6 8 8 2 12 |
| Finish | 4 5 6 7 9 9 10 11 12 14 16 |
| Optimal selection | 4 non-overlapping activities |
Implementation
Proof of Correctness — Exchange Argument
Theorem: Sorting by earliest finish time and greedily selecting compatible activities yields a maximum-size set.
Proof:
Let G = greedy solution, OPT = any optimal solution.
Order both sets by finish time:
- G: g₁, g₂, ..., g_k (greedy selections)
- OPT: o₁, o₂, ..., o_m (optimal selections)
Claim: k = m (greedy selects as many as optimal).
Proof by induction on i: After i selections, greedy's i-th selected activity finishes no later than optimal's i-th activity.
Base case (i=1): Greedy picks g₁ with the earliest finish. f(g₁) ≤ f(o₁) because g₁ has the globally minimum finish time. ✓
Inductive step: Assume f(gᵢ) ≤ f(oᵢ). Then:
- oᵢ₊₁ starts after oᵢ finishes: s(oᵢ₊₁) ≥ f(oᵢ) ≥ f(gᵢ)
- So oᵢ₊₁ is compatible with gᵢ
- Greedy considers all activities compatible with gᵢ (including oᵢ₊₁)
- Greedy picks the earliest-finishing one: f(gᵢ₊₁) ≤ f(oᵢ₊₁) ✓
Since OPT selects m activities and at each step greedy leaves at least as much room, greedy also selects m activities. ∎
Variant 1 — Minimum Number of Rooms (Interval Partitioning)
Variant 2 — Non-Overlapping Intervals (Minimum Removals)
Variant 3 — Weighted Interval Scheduling (Need DP)
When each activity has a value and you want to maximize total value:
Variant 4 — Merge Overlapping Intervals
Complexity Summary
| Problem | Greedy Criterion | Time | Space |
|---|---|---|---|
| Max activities | Earliest finish first | O(n log n) | O(n) |
| Min rooms | Earliest start, reuse earliest-free | O(n log n) | O(n) |
| Min removals | Earliest finish first | O(n log n) | O(1) |
| Weighted (max) | DP required | O(n log n) | O(n) |
| Merge intervals | Sort by start | O(n log n) | O(n) |
Summary
Activity Selection is the canonical greedy problem:
- Greedy choice: Always pick the activity that finishes earliest
- Why correct: Leaves maximum remaining time for other activities
- Proof: Exchange argument — earliest-finishing activity can always replace any other first choice without reducing the total count
- Weighted variant: Greedy fails — use dynamic programming with binary search
*Next Lesson: Fractional Knapsack*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Activity Selection Problem — Greedy Scheduling with Proof of Correctness.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, greedy, activity, selection
Related Data Structures & Algorithms Topics