Greedy algorithm Created 2026-10-13 Updated 2026-10-18
A greedy algorithm builds a feasible solution by repeatedly committing to a locally preferred next choice. Each commitment is final: the algorithm does not revisit it when later choices become available. In mathematical optimization, the important question is whether those commitments can still attain the best objective function value.
For example, a shared telescope can accept several observation requests with fixed time slots. In interval scheduling, every completed request counts equally, and overlapping requests cannot both be accepted. Choosing the request that finishes earliest leaves the resource available sooner. The example below turns that intuition into a precise rule, then shows why choosing the earliest start or shortest duration can lose jobs.
Greedy algorithms are attractive because a single pass can replace a search through many candidate subsets. However, "locally best" is only a design proposal. A problem-specific greedy-choice property, often established by an exchange argument, must connect the local decision to the actual objective function. The largest-individual-value rule in the 0-1 knapsack problem fails; Dijkstra's algorithm needs its nonnegative-edge assumptions. A rule's success in one optimization task does not justify it in another.
Greedy-choice property 2026-10-18
A greedy-choice property says that some optimal feasible solution contains the locally selected choice. Together with optimal substructure in the remaining instance, it allows repeated commitments. An exchange argument often establishes this property.[ref]
Greedy Algorithms. Algorithms by Jeff Erickson; §4.3 General Pattern, printed pp. 164–165: exchange without worsening an optimum
For interval scheduling, a safe first choice need not belong to every optimal feasible solution. When several optima exist, a commitment may exclude one while retaining another. The useful loop invariant is that the accepted prefix can still be extended to an optimum. Merely keeping the accepted intervals compatible proves feasibility, which is a weaker claim.
In this separate three-job example, labels denote new jobs:Their durations are five, five and two. Consider the rule "accept a shortest remaining job, discard all jobs conflicting with it, and repeat". It selects first. But overlaps on and on , so both are discarded. The rule returns one job, while the compatible subset contains two. The common endpoint five is allowed, and no feasible solution contains all three jobs, so two is the maximum.
A two-unit job blocks two compatible five-unit jobs
. A separate three-job counterexample to choosing shortest duration. The left panel accepts only ; the right panel accepts and . Shaded bands mark the two positive-length overlaps with . Details.Duration alone ignores where a job sits. Here the shortest job straddles the useful handover time. Earliest finish instead chooses , then , and attains two. A counterexample can refute a proposed greedy-choice property even when the rule succeeds on many other inputs.
