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.
Given finitely many nonempty closed intervals , choose as few points as possible so that each interval contains a chosen point. An optimal greedy algorithm repeatedly chooses the smallest end of an uncovered interval. After choosing , any still uncovered interval starts strictly after , since its end cannot be smaller than . Thus the intervals whose ends trigger successive choices are pairwise disjoint. Every feasible point set needs a separate point in each triggering interval, proving that the greedy point set is optimal. Sorting by end and scanning takes time. The inclusion of endpoints means an interval starting at the last chosen point is already covered.
Repeatedly removing a longest strictly increasing subsequence need not minimise the number of increasing subsequences in a partition. In , removing leaves the decreasing pair , for three subsequences overall. The partition and uses only two and is optimal because the original sequence is not increasing.
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]
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.
An exchange argument modifies an optimal feasible solution to include a desired choice while preserving feasibility and not worsening its objective function value. The modification need not strictly improve the optimum; equality suffices.[ref]
For interval scheduling, the exchange replaces the first selected job with an earliest-finishing job. Its safety comes from the finish-time inequality below. Replacing a job with a shorter one has no such guarantee: it might move the occupied interval into the middle of two useful jobs, as the shortest-duration interval scheduling counterexample shows.
Given a finite set of jobs with fixed half-open intervals , where , choose a subset of maximum cardinality. Each job occupies one shared resource throughout its interval. A feasible solution has pairwise disjoint intervals: for distinct selected jobs , either or . Thus a finish and the next start may coincide. Every job has value one; occupied time is not the objective function.[ref]
Consider these six original jobs, with arbitrary time units:
Job can be followed immediately by and . Job conflicts with all three. Starting the resource earlier is therefore not automatically beneficial: one long commitment can displace several shorter requests.
The earliest-finish greedy algorithm uses a sorting algorithm to put jobs in nondecreasing finish time. Scan that order, accepting the first job and then each job whose start is at least the last accepted finish. Break finish-time ties arbitrarily; an empty input returns the empty set. For these jobs, the order and decisions are
After , job starts before the current finish five and is rejected. Accept ; then reject because six is before seven. Finally accept . The result is , with cardinality four. Leaving idle makes room for more jobs.
By contrast, the earliest-start rule scans . It accepts , skips , accepts , and skips . Its feasible solution contains only two jobs. Here and meet at time six and are compatible; and overlap on .
Figure 1.
Earliest finish accepts four jobs; earliest start accepts two
. The same six original intervals appear in both panels. Colored jobs are accepted and gray jobs rejected. A filled start and hollow finish depict a half-open interval; jobs meeting at an endpoint are compatible. Details.
There is also an elementary optimality certificate for this example. The four subsets , , and form a partition of a set of the jobs. A feasible solution takes at most one job from each group: overlap, as do , and each other group is a singleton. Hence four is an upper bound, attained by . Checking all subsets gives a unique optimum of size four: .
The earliest-finish theorem states that this greedy algorithm maximizes job count for every finite instance of interval scheduling. For the exchange argument, let be the first job in an optimal feasible solution and an earliest-finishing job. Every later selected job satisfies
Replace by ; compatibility and cardinality survive. The remainder must be optimal among jobs starting at or after , or improving it would improve the whole selection. Mathematical induction on the smaller remaining instance, starting with the empty set, completes the outline.[ref]
Why is the algorithm fast? Once sorted, each job is examined once, using only the last accepted finish to decide compatibility. The scan takes time; comparison sorting algorithms give total time complexity for , in Big O notation, assuming constant-cost endpoint comparisons. Already sorted input needs only the scan.[ref]
The reason the shortcut is correct is stronger than "finishing sooner looks good": the exchange argument preserves every later job of a suitable optimum. This is the problem-specific fact that permits final decisions without exploring all subsets.
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.
Figure 1.
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.
Weighted interval scheduling assigns each job a reward and maximizes their sum over compatible jobs. Earliest finish can fail for this different objective function; dynamic programming can compare alternatives.[ref]
For a separate two-job example, let have reward one and reward ten. Earliest finish accepts and rejects , earning one; the optimum accepts , earning ten. Replacing by preserves job count but loses reward. Thus the interval scheduling exchange argument proves the unit-value theorem only: its preserved quantity must match the intended objective function.

Ancestors (3)

  1. Algorithm
  2. Computer science
  3. Home

Synonyms (1)

Discussion (0) New discussion

There are no discussions about this article yet.

Articles by others on the same topic (2)

A greedy algorithm is a computational method that makes the most optimal choice at each step with the hope of finding the global optimum. The fundamental principle behind greedy algorithms is to build up a solution piece by piece, always choosing the next piece that offers the most immediate benefit (i.e., the most "greedy" choice), without considering the long-term consequences.