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

Greedy Algorithms

College Depth 92 in the knowledge graph ☐ I know this ☆ Set as goal
47topics build on this
473prerequisites beneath it
See this on the map →
Algorithm Design BasicsTime and Space Complexity+4 more→→Activity Selection Problem Using Greedy AlgorithmsApproximation Algorithms (LP Relaxation and Primal-Dual)+10 more
greedy algorithm-design optimization exchange-argument

Core Idea

A greedy algorithm makes the locally optimal choice at each step with the intent of finding a global optimum. Greedy algorithms work correctly when the problem has a greedy-choice property (a local optimum leads to a global optimum) and optimal substructure. Classic greedy problems include activity selection, fractional knapsack, Huffman coding, and minimum spanning trees. Correctness is typically proved via an exchange argument: showing that replacing a greedy choice with any alternative cannot improve the solution.

How It's Best Learned

Implement activity selection (interval scheduling maximization) and Huffman encoding. For each problem, attempt a formal exchange argument for correctness before coding. Contrast greedy with DP: the 0/1 knapsack requires DP, while the fractional knapsack is solvable greedily.

Common Misconceptions

Explainer

A greedy algorithm builds a solution one step at a time, always making the choice that looks best right now without reconsidering past decisions. The appeal is efficiency: greedy algorithms are typically fast and simple to implement. The danger is incorrectness: making the locally optimal choice at each step does not automatically yield a globally optimal solution unless the problem has special structure.

The two properties that guarantee correctness are greedy-choice property and optimal substructure. Optimal substructure — which you've seen in dynamic programming — means that an optimal solution to the whole problem contains optimal solutions to its subproblems. The greedy-choice property is stronger: it says that a locally optimal (greedy) choice at each step is always part of *some* globally optimal solution. When both hold, you can safely commit to each greedy decision and recurse on what remains.

The canonical example is the activity selection problem: given a set of intervals (events with start and end times), select the maximum number of non-overlapping events. The greedy strategy — always pick the event that ends earliest — is provably optimal. Why? After choosing the earliest-ending event, the remaining problem is identical in structure, and no other first choice could leave more room for future events. This is the greedy-choice property in action. A formal proof uses an exchange argument: take any optimal schedule that doesn't start with the earliest-ending event; swap in the earliest-ending event instead; the result is no worse (it still fits, and the remaining time is at least as large).

Contrast this with the 0/1 knapsack problem, where items cannot be split. Greedy (take the highest value-per-weight item first) fails because committing to one item irrevocably forecloses combinations that would have been better. The fractional knapsack, where items can be broken into pieces, *does* have the greedy-choice property — you always take as much as possible of the highest ratio item — which is why it is solvable greedily while 0/1 requires dynamic programming. This contrast illustrates that the difference between greedy and DP is not about complexity but about problem structure.

In practice, many greedy algorithms require a sorted order or a priority queue to efficiently find the next best choice. Huffman coding builds an optimal prefix-free code by repeatedly merging the two lowest-frequency symbols — each merge requires finding the minimum, which is O(log n) with a min-heap, making the full algorithm O(n log n). Dijkstra's shortest-path algorithm is another greedy algorithm using a priority queue to always extend the currently shortest known path. Understanding greedy algorithms is therefore not just about knowing when they work, but about connecting the proof of correctness to the right data structure for implementation.

Practice Questions 3 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

Longest path: 93 steps · 473 total prerequisite topics

Prerequisites (6)

Leads To (12)