Greedy algorithms
Take the best-looking option at every step and never look back. Seven steps through coins, a meeting room and Huffman codes show when that is enough, and when it is not.
In 30 seconds
A greedy algorithm builds an answer one choice at a time. At each step it takes whatever looks best right now and never changes its mind.
That is fast, and for some problems it gives the best possible answer: booking the most meetings into one room, or building Huffman codes for compression.
For others it does not. With coins of 4, 3 and 1, greedy pays 6 with three coins when two would do. Greedy is only safe where someone has proved that the local best choice never hurts.
Greedy algorithms, step by step
Meetings that end and start at the same time count as not overlapping. Dijkstra’s algorithm is greedy and exact only when no edge has a negative weight.
- Step 1: Biggest coin first. To hand over 63 cents, take the largest coin that still fits, subtract it, and repeat. That is a greedy algorithm: at every step it makes the choice that looks best right now and never goes back on it. With coins of 25, 10, 5 and 1, the six coins it finds are the fewest possible.
- Step 2: Same rule, awkward coins. Change the coins to 4, 3 and 1 and ask for 6. Greedy grabs the 4, which leaves 2, and only two 1s can fill that: three coins. Two 3s would have done it. The best first step led to a worse whole, and greedy cannot undo it.
- Step 3: One room, six requests. A second problem. Six meetings want the same room, and overlapping ones cannot share it. We want to fit in as many as possible. There are many greedy rules to try, such as shortest first or earliest start first, and most of them can be fooled.
- Step 4: Earliest finish first. The rule that works: among the meetings still possible, pick the one that ends first. The bars are listed in order of finishing time, so that is A. It frees the room at 11:00, sooner than any other choice could. B overlaps A, so B is out.
- Step 5: Repeat until nothing fits. Repeat: C ends first among what is left, then D, then F, while E clashes and is dropped. Four meetings, and no schedule fits more. The reason: in any best schedule you can swap its first meeting for A, which ends no later, and nothing after it is disturbed. The same swap works at every later pick.
- Step 6: Huffman: merge the rarest. Huffman coding is greedy too. To give common letters short codes, join the two rarest items into one, add their counts, and repeat until one tree is left. Reading 0 and 1 down the branches gives each code. Here 100 letters take 185 bits instead of 200, and no other such code does better.
- Step 7: When is greedy safe?. Greedy is fast and simple, but only correct for problems where the locally best choice is always part of some best overall answer. That has to be proved, as the swap argument did for meetings. Where it fails, slower methods that compare whole combinations, such as dynamic programming, are needed.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Spotted a mistake on this page?