Activity Selection Problem
Select the maximum number of non-overlapping activities from a set, where each activity has a start and end time. This classic greedy algorithm demonstrates the greedy choice property: always selecting the activity that finishes earliest leaves the most room for remaining activities. Used in scheduling problems, resource allocation, and interval management. Achieves optimal solution with O(n log n) time complexity.
Visualization
Interactive visualization for Activity Selection Problem
Activity Selection (Greedy)
• Time: O(n log n) - sorting
• Greedy: select activity with earliest finish time
• Optimal solution for max activities
Interactive visualization with step-by-step execution
Implementation
1interface Activity {
2 start: number;
3 end: number;
4}
5
6function activitySelection(activities: Activity[]): Activity[] {
7 activities.sort((a, b) => a.end - b.end);
8 const selected: Activity[] = [activities[0]];
9 let lastEnd = activities[0].end;
10
11 for (let i = 1; i < activities.length; i++) {
12 if (activities[i].start >= lastEnd) {
13 selected.push(activities[i]);
14 lastEnd = activities[i].end;
15 }
16 }
17
18 return selected;
19}Deep Dive
Theoretical Foundation
Activity Selection uses the greedy strategy of always choosing the activity with the earliest finish time. Proof of optimality: suppose we have an optimal solution that doesn't include the earliest-finishing activity. We can replace the first activity in that solution with the earliest-finishing one without conflict, proving greedy choice is optimal. After selecting an activity, recursively solve for remaining compatible activities. Time: O(n log n) for sorting + O(n) for selection. This is the foundation for weighted interval scheduling (DP version).
Complexity
Time
O(n log n)
O(n log n)
O(n log n)
Space
O(1)
Applications
Industry Use
Meeting room scheduling
CPU task scheduling
Classroom assignment
Manufacturing job scheduling
TV commercial time slots
Conference talk scheduling
Resource allocation in project management
Use Cases
Related Algorithms
Huffman Coding
Huffman Coding is a lossless data compression algorithm that creates optimal prefix-free variable-length codes based on character frequencies. Developed by David A. Huffman in 1952 as a student at MIT, it uses a greedy approach to build a binary tree where frequent characters get shorter codes. This algorithm is fundamental in ZIP, JPEG, MP3, and many compression formats.
Fractional Knapsack Problem
Given items with values and weights, and a knapsack with capacity, select items (or fractions thereof) to maximize total value. Unlike the 0/1 knapsack where items must be taken whole, the fractional knapsack allows taking fractions of items. The greedy approach of taking items in order of value-to-weight ratio yields the optimal solution in O(n log n) time. This demonstrates when greedy algorithms work vs. when dynamic programming is needed.
Job Sequencing with Deadlines
Schedule jobs with deadlines and profits to maximize total profit. Each job takes 1 unit time and has a deadline and profit. The greedy strategy is to sort jobs by profit (descending) and greedily schedule each job as late as possible before its deadline. This maximizes profit while respecting constraints. Used in task scheduling, CPU job management, and project planning.
Minimum Platforms Problem
Find the minimum number of platforms required for a railway station given arrival and departure times of trains. No train should wait. The elegant solution uses the sorted merge technique: sort arrivals and departures separately, then use two pointers to track platforms needed at each moment. This greedy approach simulates the timeline efficiently in O(n log n) time, commonly asked in interviews.