Greedy Algorithm
A greedy algorithm constructs a solution step by step by selecting the locally optimal choice at each stage, achieving global optimality whenever the underlying problem satisfies the greedy-choice property, optimal substructure, or matroid axioms.
Greedy algorithm is an algorithmic optimization paradigm within computation and algorithms that builds a solution to a combinatorial problem incrementally through a sequence of myopic, irrevocable decisionsโselecting at each step the feasible option that offers the best local improvement in the objective function without backtracking over past choices. Because a greedy algorithm commits to a single decision at each stage and reduces the problem to a single smaller subproblem (typically after an O(n log n) sorting pass or inside a priority-queue loop), it avoids the multi-dimensional state tables of dynamic programming and the branching recursion trees of a divide-and-conquer algorithm.
For general combinatorial optimization problems within computational complexity theory, locally optimal choices do not always yield a globally optimal solution. However, a foundational structural theoryโinitiated by Hassler Whitney's introduction of matroids in 1935 and completed by Richard Rado (1957) and Jack Edmonds (1971)โproves that greedy optimization finds the global optimum across all nonnegative weight assignments if and only if the feasible set system forms a matroid (or greedoid). Even on NP-hard problems such as Set Cover or monotone submodular maximization, greedy algorithms achieve tight polynomial-time approximation ratios bounded by harmonic numbers H(n) โ ln n or Euler's constant (1 โ eโ1 โ 0.6321).
Structural Requirements: Greedy-Choice Property and Exchange Arguments
Greedy-Choice Property and Optimal Substructure
For a greedy algorithm to return a globally optimal solution, the problem must satisfy two structural properties:
- Optimal Substructure: An optimal solution to the problem instance contains within it optimal solutions to the residual subproblem left behind after a decision is made.
- Greedy-Choice Property: There exists at least one globally optimal solution S* that includes the first locally optimal ("greedy") choice g1. Whereas dynamic programming must solve candidate subproblems before choosing the initial transition, a greedy algorithm selects g1 first using local information alone and then solves the single remaining subproblem.
Exchange Arguments and Fractional vs. 0โ1 Knapsack
Correctness proofs for greedy algorithms typically use an exchange argument: given an arbitrary optimal solution O* that differs from the greedy solution G, identify the first greedy element gr โ O* and swap gr into O* in place of a conflicting element or โ O*. Using the greedy selection rule, one proves that the modified solution Oโฒ = (O* \ {or}) โช {gr} remains feasible and has objective value no worse than O*; repeating this swap inductively converts O* into G.
The boundary between greedy and dynamic-programming problems is illustrated by the Knapsack Problem over n items with weights wi, values vi, and capacity W:
- Fractional Knapsack (Greedy Optimal in O(n log n)): When continuous fractions xi โ [0, 1] are permitted, sorting items by descending value-to-weight density vi / wi and taking maximum shares of the highest-density items is globally optimal.
- 0โ1 Knapsack (Greedy Fails): When items are indivisible (xi โ {0, 1}), greedy density choice leaves unfillable capacity gaps (for W = 50 and items (10, 60), (20, 100), (30, 120), greedy picks 10 + 20 for value 160, whereas optimal 0โ1 picks 20 + 30 for value 220).
Matroid Theory and the RadoโEdmonds Characterization
Hassler Whitney's Matroid Axioms (1935) and the RadoโEdmonds Theorem (1957โ1971)
In 1935, Hassler Whitney introduced matroids to unify linear independence in vector spaces and cycle-freeness in graphs. A finite matroid is a pair M = (E, โ) consisting of a ground set E and a family โ โ 2E of independent subsets satisfying three axioms:
- Non-Emptiness: โ โ โ.
- Hereditary Property: If B โ โ and A โ B, then A โ โ.
- Exchange (Augmentation) Property: If A, B โ โ with |A| < |B|, there exists some element x โ B \ A such that A โช {x} โ โ.
By the Exchange Property, all maximal independent sets (bases) of a matroid have identical cardinality rank(M). In a graphic matroid, E is the edge set of an undirected graph G and โ is the family of acyclic edge subsets (forests).
Given a hereditary system (E, โ) and nonnegative weights w : E โ โโฅ0, the matroid greedy algorithm sorts E in non-increasing weight order w(e1) โฅ ยทยทยท โฅ w(em), sets S โ โ , and adds ei to S whenever S โช {ei} โ โ. The RadoโEdmonds Theorem proves that this greedy procedure finds a maximum-weight independent set for every nonnegative weight function w if and only if (E, โ) is a matroid (yielding Kruskal's minimum spanning tree algorithm on graphic matroids).
| Greedy Algorithm | Problem Solved | Greedy Selection Rule at Each Step | Time Complexity | Optimality Bound |
|---|---|---|---|---|
| Interval Scheduling | Max cardinality disjoint intervals | Select compatible interval with earliest finish time fi | O(n log n) | Global optimum |
| Huffman Coding (1952) | Minimum expected prefix-free code length | Merge two symbols/subtrees with lowest probabilities p1, p2 | O(n log n) | Global optimum (H(X) โค Lฬ < H(X) + 1) |
| Kruskal / Prim MST | Minimum-weight spanning tree of G | Lightest edge crossing cut / avoiding cycles | O(|E| log |V|) | Global optimum (Graphic Matroid) |
| Dijkstra's Algorithm (1959) | Single-source shortest paths (w โฅ 0) | Extract unvisited vertex u with smallest tentative d[u] | O(|E| + |V| log |V|) | Global optimum for w โฅ 0 |
| Submodular Maximization (1978) | Max monotone submodular f(S), |S| โค k | Pick element x maximizing marginal gain ฮ(x | S) | O(k ยท n) | (1 โ eโ1) โ 0.6321 approximation |
Classical Greedy Algorithms and Approximation Bounds
Interval Scheduling, Huffman Entropy Coding, and Submodular Optimization
- Earliest-Finish-Time Interval Scheduling: Given n activities [si, fi), sorting by earliest finish time (f1 โค ยทยทยท โค fn) and iteratively selecting the first compatible activity (si โฅ flast) leaves the maximum remaining horizon [fi, โ) and achieves maximum cardinality in O(n log n) time.
- Huffman Prefix-Free Coding (1952): Given symbol probabilities p1, ..., pn, David Huffman's algorithm repeatedly merges the two lowest-probability nodes in a min-heap into a parent of probability p(x) + p(y), minimizing expected codeword length Lฬ = โ pi li within 1 bit of Shannon entropy (H(X) โค Lฬ < H(X) + 1).
- Submodular Maximization and Bioinformatics: For any monotone submodular function f : 2E โ โโฅ0 (satisfying diminishing marginal returns), George Nemhauser, Laurence Wolsey, and Marshall Fisher (1978) proved that greedily picking k elements by largest marginal gain achieves f(Sk) โฅ (1 โ (1 โ 1/k)k) f(OPT) โฅ (1 โ eโ1) f(OPT) โ 0.6321 f(OPT). In bioinformatics, greedy agglomeration drives progressive multiple sequence alignment guide trees and neighbor-joining phylogenetic tree reconstruction.
Frequently Asked Questions
How can you tell whether a greedy heuristic might fail on a new problem?
Test whether a locally attractive choice can block two slightly smaller choices whose combined value exceeds the single greedy pick. In unweighted interval scheduling, earliest finish time is always optimal; when intervals carry arbitrary positive weights, a short early interval of weight 1 can block a longer interval of weight 100, requiring weighted interval dynamic programming.
Why does greedy coin change work for U.S. coins {1, 5, 10, 25} but fail for arbitrary coin systems?
In a canonical coin system like {1, 5, 10, 25}, repeatedly picking the largest coin โค the remaining amount always minimizes coin count. For a non-canonical system like {1, 3, 4} with target 6, greedy picks 4 + 1 + 1 (3 coins), whereas the optimum is 3 + 3 (2 coins).
References
- Whitney, H. (1935). "On the abstract properties of linear dependence." American Journal of Mathematics, 57(3), 509โ533. https://doi.org/10.2307/2371182
- Huffman, D. A. (1952). "A method for the construction of minimum-redundancy codes." Proceedings of the IRE, 40(9), 1098โ1101. https://doi.org/10.1109/JRPROC.1952.273898
- Dijkstra, E. W. (1959). "A note on two problems in connexion with graphs." Numerische Mathematik, 1(1), 269โ271. https://doi.org/10.1007/BF01386390
- Edmonds, J. (1971). "Matroids and the greedy algorithm." Mathematical Programming, 1(1), 127โ136. https://doi.org/10.1007/BF01584082
- Nemhauser, G. L., Wolsey, L. A., & Fisher, M. L. (1978). "An analysis of approximations for maximizing submodular set functionsโI." Mathematical Programming, 14(1), 265โ294. https://doi.org/10.1007/BF01588971