Intermediate Topic 24 of 51 Algorithms
Greedy Algorithms
Making the locally best choice at every step.
How it works
A greedy algorithm builds a solution step by step, always taking the choice that looks best at the moment and never reconsidering. It is correct only when the problem has the greedy-choice property and optimal substructure, usually proved by an exchange argument. Greedy methods are typically simple and fast, often O(n log n) because of sorting.
Step-by-step walkthrough
- Make change for 41 using the fewest coins.
- Always take the largest coin that fits: 25 leaves 16.
- Then 10 leaves 6.
- Then 5 leaves 1.
- Then 1 gives total 4 coins, greedy works for this coin system but not for every one.
Where it is used
Huffman coding, Kruskal's and Prim's minimum spanning trees, Dijkstra's shortest paths, interval scheduling, coin change with canonical denominations and fractional knapsack use greedy strategies.
Common pitfall
Many problems look greedy but are not: 0/1 knapsack and coin change with unusual coins give wrong answers. Always prove correctness or test against brute force on small cases. Ties need consistent ordering, and the sorting criterion must match the problem exactly.
Where it fits in the course
Greedy Algorithms is topic 24 of 51 in Algorithms, rated Intermediate. It follows Master Theorem. The next topic is Dynamic Programming.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Algorithms quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Greedy Algorithms?
Where do people use Greedy Algorithms?
What is the most common mistake with Greedy Algorithms?
What should I learn before this topic?
How can I practice it?
Is Greedy Algorithms hard to learn?
How long does it take to learn Greedy Algorithms?
Which topics are related to Greedy Algorithms?
What should I study after Greedy Algorithms?
Related topics
- Master Theorem (Algorithms)
- Dynamic Programming (Algorithms)
- Divide and Conquer (Algorithms)
- Knapsack Problem (Algorithms)
- Line Drawing Algorithms (Graphics, Vision & Media)
- Load Balancing Algorithms (Distributed Systems & Cloud)
- Wait-Free Algorithms (Concurrency & Parallelism)