Algorithms · Visualized

Greedy

Greedy

Make the locally optimal choice at each step, hoping it leads to a globally optimal solution.

Big-O: Works when the problem has optimal substructure + greedy-choice property — e.g. intervals, Huffman, fractional knapsack.

📖 Family Introduction

Greedy

Commit to the locally best choice — but only when you can prove it stays globally best.

A greedy algorithm makes the choice that looks best right now and never revisits it. That irreversibility is its power (one pass, usually O(n log n) from a sort) and its danger: greedy is only correct when the problem has the greedy-choice property — some optimal solution uses the local best choice — plus optimal substructure. The standard proof is an exchange argument: show any optimal solution can be transformed into the greedy one without losing value.

The general recipe

  1. Identify the choice made at each step (earliest finish, largest ratio, most profitable deadline).
  2. Structure the data so the choice is obvious — usually sort by the criterion.
  3. Take the best available candidate and commit irrevocably.
  4. Argue correctness with an exchange argument: swapping a non-greedy choice for the greedy one never hurts.
  5. Watch for the classic failures: greedy is correct for fractional knapsack but wrong for 0/1, and largest-first coins are only optimal for canonical coin systems.

Vocabulary you'll meet everywhere here

Greedy-choice property
an optimal solution exists that contains the current local best choice
Exchange argument
the proof technique: transform any optimal solution into the greedy one step by step
Irrevocable commitment
greedy never backtracks; if the choice can be wrong, greedy is the wrong tool
Canonical coin system
coin sets where largest-first is provably optimal (canonical) — not all sets are
Matroid structure
the formal framework (behind Kruskal) that guarantees greedy optimality on independent systems

Where this family shows up

  • Activity selection and interval scheduling (sort by finish time)
  • Fractional knapsack (by value/weight ratio), Huffman coding, job sequencing with deadlines
  • Minimum platforms (sweep), gas station circuit, jump-game reachability, and the greedy cores of Dijkstra, Prim and Kruskal

Common pitfalls

  • Applying greedy to 0/1 knapsack: the ratio rule is provably wrong there — use DP
  • Non-canonical coin systems: for coins {1, 3, 4}, amount 6 needs 3+3, but largest-first gives 4+1+1
  • Sorting by the wrong key (start time instead of finish time for activity selection)
  • No correctness proof: if you cannot sketch the exchange argument, the greedy is probably a heuristic
Python · Greedy Algorithms in Code
def activity_selection(start, finish):
    n = len(start)
    acts = sorted(range(n), key=lambda i: finish[i])
    out = [acts[0]]
    last = finish[acts[0]]
    for i in acts[1:]:
        if start[i] >= last:
            out.append(i)
            last = finish[i]
    return out

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.