Introduction to Greedy Algorithms
Table of Contents + β
Say you are standing at a shop counter. The bill is 70 rupees. You hand over 100. So the shopkeeper has to give you 30 back. What do they do? They reach for the biggest coin or note that still fits. Then the next biggest. And so on. Nobody sits and plans the perfect combination. They just keep grabbing the largest one that works, right now, at each step.
That little habit is the whole idea behind a greedy algorithm. So let us slow down and understand it properly.
πͺ What Is a Greedy Algorithm?
A greedy algorithm is a way of solving a problem by making the choice that looks best at this exact moment. And it never goes back to change that choice.
The thing is, it does not look at the full picture. It does not think about what happens five steps later. It just asks one small question at each step. βOf all the moves I can make right now, which one looks best?β Then it takes that move and moves on.
We call this a local choice. A local choice is the best option you can see at this single step. You do not worry about the steps that come after.
Here is the hope behind greedy. If you keep picking the best local choice every time, maybe all those small best choices add up to the best final answer. Sometimes that hope is true. Sometimes it is not. That is the part you have to be careful about. We will get to it.
Note
Greedy means βdecide now, never undo.β It does not try every path like brute force does. It commits to one choice at each step and keeps walking forward.
π° The Real-World Analogy: Giving Change
Let us go back to the shopkeeper giving you 30 rupees in change. Suppose the coins available are 20, 10, 5, and 1.
The shopkeeper thinks like this, one step at a time:
- I need to make 30. The biggest coin that fits is 20. Take it. Now 10 is left.
- I need 10 more. The biggest coin that fits is 10. Take it. Now 0 is left.
- Done. I gave a 20 and a 10. That is two coins.
See what happened? At every step the shopkeeper grabbed the largest coin that still fit. They never paused to plan. And for these coins, that simple habit gives the fewest coins possible. That is greedy working beautifully.
π§© When Does Greedy Actually Work?
Greedy does not work for every problem. It only gives the correct answer when the problem has two special properties. Let me explain both in plain words.
The first one is the greedy-choice property. This means the best local choice at each step really does belong to some best overall answer. In other words, grabbing the biggest coin now never traps you into a worse result later.
The second one is optimal substructure. This means once you make a choice, the smaller problem that is left over is the same kind of problem. After the shopkeeper gives the 20, the leftover task βmake 10 in changeβ is just a smaller version of the original task.
Here is the short version:
- Greedy-choice property β the best move right now is safe. It leads toward the best final answer.
- Optimal substructure β after the move, what remains is a smaller copy of the same problem.
Tip
If both properties hold, greedy is wonderful. It is fast and simple. If even one of them fails, greedy can confidently give you a wrong answer. So you must check.
β οΈ When Greedy Fails: A Coin That Breaks It
Now here is the part students must really see. This is where greedy bites you.
Greedy worked for coins of 20, 10, 5, 1. But coins are not always so friendly. Imagine a coin system of 1, 3, and 4. And you need to make 6.
Watch greedy go to work:
- I need 6. The biggest coin that fits is 4. Take it. Now 2 is left.
- I need 2. No 3 fits, no 4 fits, so take a 1. Now 1 is left.
- I need 1. Take a 1. Now 0 is left.
- Greedy used three coins: 4 + 1 + 1.
But wait. Look closer. You could have used 3 + 3. That is only two coins. Greedy gave you three. It lost.
Why did greedy fail here? Because grabbing the 4 looked best at that moment, but it actually trapped you. The greedy-choice property does not hold for the coin set 1, 3, 4. The best local move was not part of the best overall answer.
Caution
The same greedy rule βtake the biggest coin firstβ was correct for one coin set and wrong for another. That is the danger. Greedy being fast does not mean greedy is correct. You must prove it works for your exact problem before you trust it.
π» Greedy Coin Change in Code
Let me show you the greedy coin-change idea in code. We give it a list of coin values from biggest to smallest and a target amount. It keeps taking the biggest coin that fits. Then it counts how many coins it used.
Remember, this code is only correct when the coin set has the greedy-choice property. We run it on the friendly set 20, 10, 5, 1 so the answer comes out right.
# Take the biggest coin that fits, again and again, until amount is 0def greedy_coins(coins, amount): count = 0 for coin in coins: # coins are sorted biggest first while amount >= coin: # grab this coin while it still fits amount -= coin count += 1 return count
coins = [20, 10, 5, 1] # friendly coin setamount = 30print("Coins used:", greedy_coins(coins, amount))The output of the above code will be:
Coins used: 2Output
For the coin set 20, 10, 5, 1 and amount 30, greedy gives 2 coins, which is correct. Feed it the set 1, 3, 4 with amount 6 and it would return 3 coins, which is wrong. Same code, different coins, different luck.
π Greedy vs Brute Force vs Dynamic Programming
Students often mix up these ideas. So here is a quick side-by-side to keep them clear.
| Approach | How it decides | Speed | Always correct? |
|---|---|---|---|
| Greedy | Best local choice, never undo | Fast | Only if the problem fits |
| Brute force | Try every possible combination | Very slow | Yes |
| Dynamic programming | Build answers from smaller solved subproblems | Medium | Yes |
So greedy is the fastest of the three. But it pays for that speed with risk. Dynamic programming solves the problem by combining answers to smaller subproblems and storing them. It is slower than greedy, but it is always correct for coin change. When greedy is proven safe, use it. When it is not, reach for dynamic programming.
Tip
A good habit: write the greedy solution, then ask yourself βcan I find one example where this gives the wrong answer?β If you cannot, and you can argue why, greedy is probably safe. If you can, drop greedy.
π Common Mistakes Students Make
Here are the slips that trip people up the most:
- Assuming greedy is correct just because the code runs and gives some answer. A wrong answer is still an answer.
- Picking greedy because it is the easiest to write, without checking the greedy-choice property.
- Forgetting that the same greedy rule can be right for one input and wrong for another, like our two coin sets.
- Mixing up greedy with dynamic programming. Greedy commits and never looks back. Dynamic programming weighs smaller solved pieces.
- Skipping the proof. If you cannot argue why the local choice is safe, you do not really know your answer is right.
π§© What Youβve Learned
- β A greedy algorithm makes the best local choice at each step and never undoes it.
- β Greedy is correct only when the problem has the greedy-choice property and optimal substructure.
- β Giving change with the largest coins first is greedy, and it works for friendly coin sets.
- β For the coin set 1, 3, 4 making 6, greedy picks 4 + 1 + 1, but 3 + 3 is better, so greedy fails there.
- β Greedy is fast but not always correct, so you must prove it works before trusting it.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does a greedy algorithm do at each step?
Why: Greedy commits to the best local choice at each step and does not go back to change it.
- 2
Which two properties must hold for greedy to give a correct answer?
Why: Greedy is correct only when the best local choice is safe (greedy-choice property) and the leftover is a smaller copy of the same problem (optimal substructure).
- 3
For coins 1, 3, 4 and amount 6, why does greedy give the wrong answer?
Why: Greedy grabs the 4 because it looks best, but that traps it into three coins, while 3 + 3 needs only two.
- 4
Compared to dynamic programming, greedy is usually...
Why: Greedy is faster because it commits at each step, but it is only correct when the problem fits, so you must prove it works.