Algorithm Design Paradigms

Greedy Algorithms

At each step, take the locally best choice and never reconsider it. Greedy is fast and simple, but only correct when the problem has the right structure — it is the algorithm design paradigm most often applied incorrectly.
  • A greedy algorithm builds a solution incrementally, committing to the best-looking choice at each step and never backtracking
  • Correctness requires proof — usually via the "exchange argument": any optimal solution can be transformed into the greedy one without getting worse
  • Two structural properties that make greedy work: the greedy-choice property (a locally optimal choice is part of some global optimum) and optimal substructure
  • Classic correct greedy algorithms: Dijkstra's Shortest Paths, Prim's and Kruskal's Minimum Spanning Trees, Huffman coding, activity/interval scheduling
  • When greedy is wrong, it fails silently — it produces a valid answer, just not the optimal one; always verify with a counterexample or a proof, never by intuition alone

Greedy is the cheapest algorithm design paradigm to implement and the easiest to get wrong. The code is almost always short: sort by some criterion, walk through, take what fits. The hard part is entirely in the proof that the locally best choice can never be worse than deferring — which is why greedy algorithms in textbooks are always paired with a correctness argument, not just a description.

Activity selection — the canonical correct greedy
static List<Activity> selectActivities(List<Activity> activities) {
    activities.sort(Comparator.comparingInt(a -> a.finishTime));  // greedy criterion: earliest finish

    List<Activity> chosen = new ArrayList<>();
    int lastFinish = Integer.MIN_VALUE;
    for (Activity a : activities) {
        if (a.startTime >= lastFinish) {   // never conflicts with what we already chose
            chosen.add(a);
            lastFinish = a.finishTime;
        }
    }
    return chosen;
}
Sorting by finish time (not duration, not start time) is what makes this greedy choice provably optimal
Greedy criterion matters — same problem shape, different rule
ProblemGreedy ruleCorrect?
Activity selectionearliest finish time firstYes — provably optimal
Activity selectionshortest duration firstNo — classic counterexample exists
Fractional knapsackhighest value/weight ratio firstYes
0/1 knapsackhighest value/weight ratio firstNo — needs Dynamic Programming
Coin change (US coins)largest denomination firstYes, for this coin system
Coin change (arbitrary denominations)largest denomination firstNo — needs DP
Sources
  • Algorithms (4th ed.)Ch. 4.3 — Minimum Spanning Trees
  • Data Structures and Algorithms in Java (6th ed.)Ch. 13.4 — Greedy Method
  • Algorithms Notes for ProfessionalsCh. Greedy Algorithms