A greedy algorithm builds a solution one step at a time, always making the choice that looks best right now and never going back to change it. When it works, a greedy algorithm is usually the simplest and fastest solution available. The difficult part is knowing when it works, because on some problems the locally best choice leads to a worse overall answer. This lesson shows a problem where greedy is provably right and a familiar one where it can fail.
In this lesson you will learn
- The greedy-choice idea
- Activity selection: earliest finish time first
- Coin change and when greedy breaks
- The two properties that make greedy correct
- Greedy versus dynamic programming
You have already met greedy algorithms in Dijkstra’s algorithm and minimum spanning trees.
Activity Selection
#include <stdio.h>
#include <stdlib.h>
/* Activity selection: pick the most non-overlapping talks for one room */
struct Talk { const char *name; int start, end; };
int by_end(const void *a, const void *b) {
return ((const struct Talk *)a)->end - ((const struct Talk *)b)->end;
}
int main(void) {
struct Talk t[] = {
{"AI", 9, 11}, {"Web", 10, 12}, {"C", 11, 13}, {"SQL", 12, 14},
{"Java", 9, 10}, {"Cloud", 13, 15}, {"Git", 14, 16},
};
int n = sizeof t / sizeof t[0];
qsort(t, n, sizeof t[0], by_end); /* greedy choice: earliest finish first */
int last_end = 0, count = 0;
for (int i = 0; i < n; i++) {
if (t[i].start >= last_end) {
printf("choose %-5s %2d:00-%2d:00\n", t[i].name, t[i].start, t[i].end);
last_end = t[i].end;
count++;
}
}
printf("Talks scheduled: %d\n", count);
return 0;
}
Output
choose Java 9:00-10:00
choose Web 10:00-12:00
choose SQL 12:00-14:00
choose Git 14:00-16:00
Talks scheduled: 4
- The goal is to fit as many talks as possible into one room with no overlaps.
- The greedy rule is: among the talks that can still be scheduled, choose the one that finishes earliest. Sorting by end time makes this a single pass.
- A talk is accepted only if it starts at or after the end of the last chosen talk.
- Why it is correct: finishing as early as possible leaves the most time for everything else. Any optimal schedule can swap its first talk for the earliest-finishing one without losing a talk, and the same argument repeats for the rest.
- Sorting costs O(n log n) and the scan O(n).
Coin Change: When Greedy Fails
#include <stdio.h>
/* Coin change: greedy works for some coin systems, not all */
void greedy_coins(const int *coins, int n, int amount) {
printf("amount %d with {", amount);
for (int i = 0; i < n; i++) printf("%d%s", coins[i], i < n - 1 ? "," : "");
printf("}: ");
int used = 0;
for (int i = 0; i < n; i++) { /* coins listed largest first */
while (amount >= coins[i]) {
amount -= coins[i];
printf("%d ", coins[i]);
used++;
}
}
printf("-> %d coins\n", used);
}
int main(void) {
int taka[] = {10, 5, 2, 1}; /* Bangladeshi coins and notes */
greedy_coins(taka, 4, 28);
int odd[] = {4, 3, 1}; /* a coin system where greedy fails */
greedy_coins(odd, 3, 6);
printf("best for 6 with {4,3,1} is 3 3 -> 2 coins (greedy missed it)\n");
return 0;
}
Output
amount 28 with {10,5,2,1}: 10 10 5 2 1 -> 5 coins
amount 6 with {4,3,1}: 4 1 1 -> 3 coins
best for 6 with {4,3,1} is 3 3 -> 2 coins (greedy missed it)
- The greedy rule is to take the largest coin that fits, again and again.
- For denominations such as 10, 5, 2 and 1, this always gives the fewest coins: 28 = 10 + 10 + 5 + 2 + 1.
- For the coins {4, 3, 1}, greedy takes 4 first and then needs two 1s, using 3 coins, while 3 + 3 uses only 2. The early “best” choice blocked the better answer.
- For arbitrary coin systems, the correct method is dynamic programming, which tries every option for every smaller amount.
When Does Greedy Work?
| Property | Meaning |
|---|---|
| Greedy-choice property | A globally optimal solution can always start with the locally best choice |
| Optimal substructure | After making that choice, the rest is a smaller problem of the same kind |
Famous greedy algorithms include activity selection, fractional knapsack (take the best value per kilogram first), Huffman coding for compression, Kruskal’s and Prim’s MST algorithms, and Dijkstra’s shortest paths for non-negative weights. The 0/1 knapsack and general coin change are not solved correctly by greedy.
Common Mistakes
- Assuming greedy is correct without a proof or at least a search for counter-examples.
- Choosing the wrong greedy rule: for activity selection, “shortest talk first” or “earliest start first” both fail on some inputs.
- Forgetting to sort before the greedy scan.
- Using greedy for 0/1 knapsack, where items cannot be split.
Practice Exercises
Exercise 1. Show that “earliest start first” fails for activity selection.
Show answer
Talks: A 9–17, B 10–11, C 12–13. Earliest start chooses A and then nothing else fits (1 talk). Earliest finish chooses B and C (2 talks).
Exercise 2. Fractional knapsack: capacity 10 kg, items (value, weight) = (60, 5), (50, 10), (40, 4). What is the best value?
Show answer
Values per kg: 12, 5 and 10. Take all 5 kg of the first (60), all 4 kg of the third (40), then 1 kg of the second (5). Total 105.
Frequently Asked Questions
What is the difference between greedy and dynamic programming?
Greedy makes one choice per step and never reconsiders it. Dynamic programming considers every choice for each subproblem and keeps the best, so it is slower but works on more problems.
How can I tell if a greedy approach is right?
Try to prove the greedy-choice property with an exchange argument, and test small cases against a brute-force solution. A single counter-example shows that greedy is wrong.
Summary
- Greedy algorithms make the locally best choice and never undo it.
- Activity selection by earliest finish time is provably optimal.
- Greedy coin change works for standard coins but fails for some coin systems.
- Correctness needs the greedy-choice property and optimal substructure.
