Greedy Algorithms
Greedy Fundamentals
Master Greedy foundations: greedy choice property, optimal substructure, and Greedy vs Dynamic Programming vs Backtracking.
Last Updated: August 2, 2026
•
15 min read
1. Introduction
What is a Greedy Algorithm?
A Greedy Algorithm is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. It makes locally optimal choices at each stage with the hope of finding a global optimum.Why study them?
Greedy algorithms are highly optimal, often running inO(N) or O(N log N) time with O(1) space. However, they do not work for all optimization problems. Understanding how to prove greedy correctness and identify when a problem requires Dynamic Programming (DP) is a core interview capability.
Where is it Used?
2. Mental Model: The Greedy Shopper
Imagine you are in a supermarket and are allowed to grab 5 items for free:
3. Core Properties & Trade-offs
To apply a greedy approach, a problem must satisfy two properties: 1. Greedy Choice Property: A global optimal solution can be arrived at by making locally optimal (greedy) choices. 2. Optimal Substructure: An optimal solution to the problem contains optimal solutions to its subproblems.
Greedy vs Dynamic Programming vs Backtracking
Let's look at the Coin Change problem (making change for target T with fewest coins):
[1, 2, 5, 10] (standard): Greedy works. To make change for 16, we greedily take 10, then 5, then 1 (3 coins).[1, 3, 4] (arbitrary): Greedy fails. To make change for 6, greedy picks 4 followed by two 1s (4 + 1 + 1 = 3 coins). However, the global optimum is two 3s (3 + 3 = 2 coins). This requires Dynamic Programming.4. Visual Decision Choice Comparison
Comparing decision path choices for target 6 with coins [1, 3, 4]:
5. Real-World Examples
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers test structural assumptions:[1, 3, 4] or Knapsack constraints.Common Mistakes
Warning: 1. Assuming Greedy Always Works: Implementing a greedy strategy without mathematically proving that local choices lead to global optimums, leading to failing edge cases.
> 2. Forgetting to Sort: Greedy algorithms frequently require sorted input data (e.g. sorting by weights, values, start times, or end times). Skipping sorting violates greedy assumptions.
7. Summary
8. Quiz
Question 1: Why is Dijkstra's algorithm classified as a Greedy algorithm?
Answer: Dijkstra's is greedy because at each step it extracts the vertex with the absolute minimum distance from the priority queue and finalizes its path cost, assuming no future paths can reduce its cost.Question 2: Does the greedy choice property guarantee that a greedy algorithm will find the optimal solution?
Answer: Yes. If a problem mathematically satisfies the greedy choice property and optimal substructure, a greedy algorithm is guaranteed to yield the global optimum.Question 3: If target is 11 and coin denominations are [1, 5, 6, 8], does greedy make optimal change?
Answer: No. Greedy picks8 + 1 + 1 + 1 (4 coins), whereas the optimal selection is 6 + 5 (2 coins).
Question 4: True or False: Greedy algorithms generally use more memory than Dynamic Programming.
Answer: False. Greedy algorithms only track the current state, running inO(1) auxiliary space. DP stores solutions to all subproblems in matrices/arrays, requiring O(N) or O(N²) space.