A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Activity Selection Problem Using Greedy Algorithms

College Depth 93 in the knowledge graph ☐ I know this ☆ Set as goal
30topics build on this
474prerequisites beneath it
See this on the map →
Greedy Algorithms→→Dijkstra's Algorithm
greedy activity-selection interval-scheduling optimization correctness-proof

Core Idea

The activity selection problem: given activities with start and end times, select the maximum number of non-overlapping activities. Greedy solution: sort by end time and greedily select activities that don't overlap with the last selected. This achieves optimality in O(n log n) time and demonstrates that greedy algorithms can be optimal when the problem has greedy-choice property and optimal substructure.

How It's Best Learned

Trace the greedy algorithm by hand on a small activity set. Prove optimality via the greedy-choice property: the first activity to finish is always in some optimal solution. Contrast with other orderings (start time, duration) to see why they fail.

Common Misconceptions

Explainer

From your study of greedy algorithms, you know the core idea: make the locally optimal choice at each step and hope it leads to a globally optimal solution. The activity selection problem is the textbook example where this strategy provably works. The setup is simple: you have a set of activities, each with a start time and an end time, and you want to attend as many non-overlapping activities as possible. Think of scheduling meetings in a conference room — once you commit to a meeting, you cannot attend any other meeting that overlaps with it.

The greedy strategy is to sort activities by end time and then iterate through them, selecting each activity whose start time is at or after the end time of the last activity you selected. The first activity you pick is the one that finishes earliest. This is counterintuitive — why not pick the shortest activity, or the one that starts earliest? The answer is that finishing earliest leaves the maximum remaining time for subsequent activities. Picking the shortest activity can fail because a short activity might sit right in the middle of the timeline, blocking two others. Picking the earliest start time can fail because an activity that starts early might run very long, blocking everything else. Only the earliest-finishing criterion guarantees you never waste future capacity.

The proof of optimality uses the greedy-choice property: the earliest-finishing activity is always part of some optimal solution. Here is the intuition. Suppose an optimal solution does not include the earliest-finishing activity a₁. It must include some other activity a' that overlaps with a₁. Since a₁ finishes no later than a', you can swap a' for a₁ without creating any new conflicts — the resulting solution has the same number of activities and is still valid. By induction, the greedy algorithm builds a solution that is as large as any optimal one. This property combined with optimal substructure (once you select an activity, the remaining subproblem is the same type of problem on the remaining compatible activities) is what makes the greedy approach work.

The algorithm runs in O(n log n) time, dominated by the sort. The selection pass itself is O(n) — a single scan through the sorted list. This is a dramatic improvement over a brute-force approach that would consider all 2ⁿ subsets of activities. The activity selection problem is also the foundation for more complex interval scheduling variants: weighted activity selection (where each activity has a value and you maximize total value — this requires DP, not greedy), interval partitioning (minimizing the number of rooms needed), and job scheduling with deadlines. Recognizing that a problem has the structure of activity selection is often the key insight that makes a seemingly complex scheduling task tractable.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Tree Structure and Node Properties → Binary Trees → Tree Traversals → Depth-First Search (DFS) → Depth-First Search: Implementation and Applications → Topological Sort → Dynamic Programming → Longest Common Subsequence (LCS) Problem → Edit Distance: Levenshtein Distance and DP → 0/1 Knapsack Problem: Bounded Capacity DP → Greedy Algorithms → Activity Selection Problem Using Greedy Algorithms

Longest path: 94 steps · 474 total prerequisite topics

Prerequisites (1)

Leads To (1)