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 moreActivity 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 ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsDefining and Calling FunctionsFunctions: Decomposing ProblemsFunction Parameters and Argument PassingReturn ValuesVariable ScopeIntroduction to ClassesObjects and InstancesMethods and AttributesAlgorithm Design BasicsTree Structure and Node PropertiesBinary TreesTree TraversalsDepth-First Search (DFS)Depth-First Search: Implementation and ApplicationsTopological SortDynamic ProgrammingLongest Common Subsequence (LCS) ProblemEdit Distance: Levenshtein Distance and DP0/1 Knapsack Problem: Bounded Capacity DPGreedy Algorithms

Longest path: 93 steps · 473 total prerequisite topics

Prerequisites (6)

Leads To (12)