Greedy algorithms explained in plain terms means understanding what happens when a program always takes the best option in front of it and never looks back. The central question is whether such short-sighted choices can ever produce the best overall answer, and the surprising answer is that sometimes they can, and the trick is knowing when.
A greedy algorithm builds a solution step by step, choosing the locally best option at each step and never reconsidering it. When the problem has the right structure, those local choices add up to a globally optimal answer, and the algorithm runs far faster than any method that explores alternatives.
The idea is the simplest approach in all of algorithm design, and it is also the easiest to get wrong, because the difference between a brilliant greedy solution and a broken one is a proof, not a feeling. This article explains how greedy algorithms work, where they shine, and where they fail.
Key takeaways
- A greedy algorithm always takes the locally best option and never revisits it.
- It is optimal only when the problem has greedy choice and optimal substructure.
- Greedy solutions are usually far faster than exhaustive search.
- Classic wins include Huffman coding and minimum spanning trees.
- Many famous problems defeat greedy choices and need other methods.
What a greedy algorithm is
A greedy algorithm makes a sequence of choices, and at each step it picks whichever option looks best right now, without worrying about the future. It never backtracks, never reconsiders, and never explores alternatives, which is what makes it so fast and so risky at the same time. The whole method can often be written in a few lines, and the hard part is proving that those few lines are correct.
Two properties decide whether a greedy approach works. The first is the greedy choice property, which means a locally optimal choice can always be part of some globally optimal solution, and the second is optimal substructure, which means the best answer to the whole problem contains the best answers to its smaller parts. When both hold, the greedy algorithm is correct, and when either fails, it can produce a plausible-looking answer that is simply wrong.
Coin change is the example everyone meets first, and it shows both sides. With British coins, always taking the largest coin that fits gives the fewest coins, because the denominations are designed to make it work. With a made-up set of coins, like one, three, and four, the same greedy rule fails: six pence becomes four plus one plus one, three coins, when three plus three would have used two. Our guide to hashing covers another technique where a simple local rule delivers outsized speed.
Where greedy algorithms win
Minimum spanning trees are the great triumph of greedy thinking. Given a network of places joined by roads of different lengths, the cheapest network that connects them all is found by repeatedly adding the cheapest road that does not form a cycle, which is Kruskal’s algorithm, or by repeatedly growing the tree with the cheapest edge to a new place, which is Prim’s algorithm. Both are greedy, both are provably optimal, and both run fast.
Huffman coding is another classic win. To compress a file, the algorithm repeatedly merges the two least frequent symbols into a combined one, building a tree that assigns the shortest codes to the most common symbols. The result is an optimal prefix code, and the greedy merging is exactly what makes it optimal, because the two rarest symbols can always share the longest code in some optimal tree.
Interval scheduling is the cleanest example of all. Given a set of meetings with start and end times, the largest number that can be attended is found by always picking the meeting that ends earliest among those that do not clash with the ones already chosen. The proof is a simple exchange argument: the earliest-ending meeting leaves the most room for everything else, so it can always be part of an optimal schedule.
Dijkstra’s algorithm is greedy too, in a sense that surprises many people. At each step it settles the nearest unsettled node, a local choice that turns out to be globally safe because of the non-negative weights, and that single greedy insight is what makes the whole algorithm work.

Greedy algorithms versus other approaches
A greedy algorithm is one of several ways to build a solution, and each has a different balance of speed and safety. Dynamic programming explores subproblems systematically and is always optimal when it applies, brute force tries everything and is always optimal but far too slow, and backtracking explores choices with the option to retreat. Greedy is the fastest of the group and the least safe.
The table below sets out the main contrasts between the leading approaches.
| Aspect | Greedy algorithm | Dynamic programming | Brute force |
|---|---|---|---|
| How it decides | Best local choice, never revisits | Best of all subproblem answers | Tries every possibility |
| Correctness | Optimal only with the right structure | Optimal when structure holds | Always optimal |
| Typical speed | Fast, often linearithmic | Polynomial after reuse | Exponential, often unusable |
| Memory used | Almost none | Table of subproblem answers | Almost none |
| Best for | Problems with greedy choice | Optimisation with repetition | Tiny inputs, verification |
The practical result is that greedy is the first approach to try when a problem looks like it might have the greedy choice property, and the proof, or a counterexample, decides whether it stays.
Where greedy algorithms are used
Network design relies on greedy algorithms every day. Laying the cheapest cable network to connect a set of buildings, or the cheapest set of links to keep a data centre connected, is a minimum spanning tree problem, and Kruskal’s and Prim’s algorithms solve it greedily in near-linear time. Our guide to Bloom filters explains another lightweight structure that networks use to make fast local decisions.
Compression tools use Huffman coding, a greedy algorithm, to shrink files before storage or transmission. The method is fast enough to run on every file a system handles, and its optimality means no other prefix code can do better, which is why it appears inside formats from ZIP archives to JPEG images.
Scheduling systems use greedy rules to assign tasks, rooms, and people. A university timetabling the most lectures into the fewest rooms, or a conference fitting the most talks into a day, is solving an interval scheduling problem, and the earliest-finishing rule gives the optimal answer with a proof that fits on a page.
Approximation algorithms use greedy choices when the exact answer is too expensive to compute. The travelling salesman problem, the set cover problem, and many scheduling problems have greedy approximations with proven guarantees, which means the answer is not perfect but is never worse than a known factor from perfect, and that is often good enough in practice.
Strengths and limits
The greatest strength of a greedy algorithm is its speed and simplicity. A greedy solution is usually a short loop with a sort, it runs in near-linear time, and it uses almost no memory, which makes it the easiest algorithm to implement, test, and deploy. When it is correct, nothing else comes close on efficiency.
It is also a strength that greedy algorithms are easy to reason about in the small. Each step is a single local decision, so the behaviour is transparent, and a failure is usually easy to see in a counterexample, which is more than can be said for many cleverer methods.
The limits are real, though. A greedy algorithm gives no warning when it is wrong, and the answer it produces looks just as confident as a correct one, which makes an unproven greedy solution a genuine risk. Our guide to consistent hashing shows how distributed systems make local decisions that stay correct as the system grows, and the lesson is the same: a local rule needs a proof before it can be trusted.
Greedy algorithms checklist
- Use a greedy algorithm when a local choice is provably safe.
- Look for the greedy choice property before trusting the method.
- Test the rule on small counterexamples first.
- Remember coin change fails for some coin sets.
- Prefer dynamic programming when greedy is not provably correct.
- Use Kruskal’s or Prim’s algorithm for minimum spanning trees.
- Use Huffman coding when the most common symbols need short codes.
- Sort by finishing time for interval scheduling problems.
- Never ship a greedy solution without a proof or a test.
Frequently asked questions
What is a greedy algorithm in simple terms?
It is a method that builds a solution by always taking the best option available at each step, without ever looking back or reconsidering.
When is a greedy algorithm correct?
When the problem has the greedy choice property and optimal substructure, which means a locally best choice can always be part of a globally best answer.
What is an example of a greedy algorithm that works?
Kruskal’s algorithm for minimum spanning trees, Huffman coding for compression, and earliest-finishing-time scheduling are all greedy and provably optimal.
What is an example where greedy fails?
Coin change with coin values of one, three, and four fails for six pence, and the knapsack problem defeats greedy choices in general.
How is a greedy algorithm different from dynamic programming?
A greedy algorithm makes one local choice per step and never revisits it, while dynamic programming considers many subproblem answers and combines the best ones.
When should I not use a greedy algorithm?
When the greedy choice property cannot be proved, because the answer may look right and still be wrong, and a systematic method is safer.
The bottom line
Greedy algorithms explained in one line is this: take the best option in front of you, and if the problem is built for it, the best options add up to the best answer.
It is the fastest tool in algorithm design and the one that most demands a proof, and knowing which problems it suits is a skill in itself.
This article is general information about how greedy algorithms work, not advice about choosing an algorithm for a particular system.







