Greedy Algorithms: When the Locally Best Choice Is Enough

← Back to Blog & Articles

A greedy algorithm makes the best-looking choice at each step and never looks back. It is fast and simple, but it is only correct when the problem has the right structure.

A classic that works: activity selection

Suppose you have a list of meetings with start and finish times and want to schedule as many non-overlapping meetings as possible. Sort them by finish time, take the first, then repeatedly take the next meeting that starts after the previous one ends. Choosing the earliest finish leaves the most room for everything else.

A classic that fails: unusual coin systems

With coins worth 1, 3, and 4, and a target of 6, greedy takes 4 + 1 + 1, which is three coins. The better answer is 3 + 3, which is two. Greedy happens to work for many real currencies, but not for every coin set.

How to check whether greedy is safe

Two properties matter. The greedy-choice property says that some optimal solution begins with the greedy choice. Optimal substructure says that what remains after that choice is a smaller problem of the same kind. A common proof technique is the exchange argument: show that swapping the first choice of any optimal solution for the greedy choice does not make it worse.

Greedy algorithms you already know

Kruskal's and Prim's algorithms for minimum spanning trees are greedy and correct. Dijkstra's shortest-path algorithm is greedy too, and it is correct when edge weights are non-negative.

Practical advice

Test greedy ideas against small counterexamples before trusting them. If one appears, the problem usually needs dynamic programming instead.

Continue with the book

Explore the full treatment, worked examples, and exercises in Algorithms Made Simple.


Adapted from Algorithms Made Simple, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top