Skip to main content
Interactive Algorithm Education

Visualize & Master Algorithms & Data Structures

Explore classic & modern sorting algorithms, efficient searching techniques, and interactive data structure visualizations — all with real-time step-by-step animation, comparisons, swaps, and Big-O metrics.

Greedy Visualizer

Activity Selection

Step 0 / 0
Speed 100ms
Step Progress 0 / 0
Selected 0
Rejected 0
Status Ready
Unconsidered
Considering
Selected
Rejected
Step Explanation

Select an algorithm and press Play to watch the greedy choices unfold.

—
Pseudocode
 

Activity Selection

Elementary (2/5) ~30 minutes Interval scheduling Greedy choice: earliest finish time Exchange argument (optimality) Non-overlapping selection Prereqs: Sorting, Basic greedy intuition
Quick Reference

Activity Selection

Activity Selection picks the maximum number of non-overlapping activities given start and end times. The greedy strategy — always choose the activity that finishes earliest — is provably optimal.

Difficulty: Elementary (2/5) greedy

Complexity

Best Time
O(n log n)
Average Time
O(n log n)
Worst Time
O(n log n)
Space
O(n)

When to Use

For interval scheduling: meeting-room allocation, classroom scheduling, and resource booking with time conflicts.

Pros

  • Greedy choice is provably optimal
  • O(n log n) dominated by sorting
  • Simple and easy to reason about

Cons

  • Requires non-overlapping constraint
  • Only works for the interval-scheduling structure
  • Tie-breaking on equal end times needs care

History

The activity-selection problem is a canonical greedy algorithm taught since the 1970s and appears in every major algorithms textbook as the introductory greedy example.

You have one meeting room and a set of activities, each with a start and end time. What’s the largest number of non-overlapping activities you can schedule?

The answer: always book the activity that finishes earliest, then repeat. It’s one of the cleanest proofs that a simple greedy rule can be provably optimal — and the foundation for interval scheduling everywhere.

How It Works

  1. Sort activities by finish time, ascending.
  2. Select the activity that finishes first.
  3. Scan the rest in order — keep any activity whose start time is not earlier than the end of the last selected activity.
  4. Done: the selected set is maximal.

The greedy choice is deliberate: the earliest finisher leaves the most room afterward, so no other activity can block it.

An exchange argument proves optimality: take any optimal solution and swap its first activity for the earliest-finishing one. The swap can’t overlap anything the original could, and the remaining problem is unchanged — so the greedy first pick is safe.

Key Insight

Greedy works here because the constraint is purely about time overlap — once you pick a finisher, the future depends only on the end time, not on which activity it was. That single observation collapses the whole search space. If a later activity started earlier and finished later, it would only ever be worse.

Worked Example

The visualizer uses activities A(1–3), B(2–5), C(4–6), D(6–8), E(5–7):

StepCandidateStarts ≥ last end?Chosen?
1A (1–3)— (first)✓ A
2B (2–5)2 < 3✗
3C (4–6)4 ≥ 3✓ C
4E (5–7)5 < 6✗
5D (6–8)6 ≥ 6✓ D

Selected set: A, C, D — 3 activities. Watch the visualizer step through exactly this: each grayed-out candidate fails the start-time check against the current end.

Edge Cases & Pitfalls

  • Ties in finish time — any earliest finisher works; pick either.
  • Back-to-back intervals — an activity ending at 6 and another starting at 6 are compatible (start ≥ end).
  • Overlapping candidates — B and E both conflict with A’s end; the scan simply skips them.
  • Sorting matters — without sorting by finish time, the greedy rule breaks. This is a preprocessing requirement, not a choice.

Comparison: Activity Selection vs Other Interval Problems

ProblemGreedy ruleOptimal?
Max non-overlappingEarliest finishYes
Min rooms (interval partitioning)Sort by start + heapYes
Min points to cover intervalsGreedy by endYes
Weighted interval schedulingEarliest finishNo → use DP

Applications

  • Meeting room scheduling — booking a single room
  • Classroom allocation — packing lectures into one hall
  • Resource time-slicing — CPU/job scheduling around deadlines

Practice Trajectory

  1. Hand-sort the visualizer’s activities by finish time and run the scan yourself.
  2. Explain why skipping B (2–5) at step 2 can never cost you a solution.
  3. Construct two activities where picking the earliest start (instead of earliest finish) fails.
  4. Extend to the min-rooms problem and see why a heap replaces the single pointer.
  5. Prove the exchange argument for earliest-finish in your own words.