Computer ScienceAlgorithmsMedium

Greedy Algorithm

Also known as:Greedy HeuristicGreedy Method

A Greedy Algorithm is an algorithmic paradigm that makes the locally optimal choice at each step with the hope of finding a globally optimal solution, without reconsidering previous choices. Greedy algorithms are efficient and simple, but they do not always produce the globally optimal solution — they work correctly only when the problem satisfies the greedy-choice property and optimal substructure. Classic examples include Kruskal's and Prim's algorithms for minimum spanning trees, Huffman coding, and activity selection.

Greedy vs Dynamic Programming Paradigm Comparison

PropertyGreedyDynamic Programming
ApproachLocally optimal at each stepAll subproblems solved and stored
ReconsiderationNo backtrackingConsiders all subproblems
OptimalityNot always globally optimalGlobally optimal
Time ComplexityUsually faster (O(n log n))Often polynomial O(n^2) or more
SpaceO(1) to O(n)O(n) to O(n^2)
ExampleActivity Selection, HuffmanKnapsack, LCS, Edit Distance

Interactive Tools

Brilliant.org – Greedy Algorithms

Problem sets illustrating when greedy works and when it fails

Open Tool

Khan Academy – Greedy Algorithms

Introduction to greedy strategy within algorithm fundamentals

Open Tool

VisuAlgo – Greedy

Interactive visualization of greedy MST algorithms

Open Tool
Illustration of greedy coin-change algorithm selecting largest coins first

Wikimedia Commons, CC BY-SA

Related Terms

The word "greedy" comes from Old English graedig, meaning eager or hungry. In computer science, it was adopted metaphorically in the 1950s–1960s to describe algorithms that greedily grab the best-looking option at each step. The formal theory was developed by Jack Edmonds and others in the 1960s–1970s.

optimizationgraphheuristicalgorithmscomputer-sciencecombinatorics