Suppose one room can host only one event at a time. You want to fit in as many events as possible. A tempting rule is to take the shortest event first. That can fail: a short event placed in the middle might block two events on either side. A different rule works—take the available event that finishes earliest, then repeat with events that start after it ends.
Background for this page. A feasible answer obeys the problem’s constraints. An optimal answer is the best feasible answer. A greedy method commits to a local choice without searching all future choices. Related explanations: Dynamic programming: subproblems, tables, and optimal choices · Minimum spanning trees: Kruskal, Prim, and safe edges.
Why can we trust that choice? Consider an optimal schedule. Its first event cannot finish earlier than the earliest-finishing available event. Replace that first event with the earliest-finishing one. The replacement leaves at least as much time for every later event, so it does not reduce the number of events we can fit. We can then make the same argument for the remaining schedule. This is an exchange argument: it shows that a locally chosen step can belong to some optimal solution.
# Sort events by finishing time.
put events in order of increasing finish time
# Keep the finish time of the last chosen event.
last_finish = before every event begins
for each event in that order:
# Choose it only if it does not overlap.
if event.start is at least last_finish:
choose the event
last_finish = event.finish
Sorting takes O(n log n); the scan takes O(n). The exact boundary rule must be stated: if one event ends at 3 and another starts at 3, the example treats them as compatible.
A greedy algorithm makes a choice now without revisiting it later. That is fast and simple when the problem has the right structure. It is not a general shortcut for optimization. The value of the method comes from the proof that its choice is safe, not from the fact that it feels sensible.
Huffman coding gives another example. To make a prefix code for symbols with known frequencies, repeatedly combine the two least frequent remaining nodes. Those nodes become siblings in a code tree. A priority queue can find the two smallest efficiently, giving an O(n log n) construction for n symbols. The proof relies on the fact that an optimal code can place the two least frequent symbols as deepest siblings. The resulting code gives frequent symbols shorter encodings, but the exact bit strings depend on tie choices.
Offline caching makes a different kind of greedy choice. If all future requests are known and the cache is full, evict the item whose next use lies farthest in the future. An exchange argument shows this minimizes misses under that model. A real cache usually cannot see the future, so the exact rule is a benchmark, not an everyday implementation. We will return to decisions without future knowledge in the online-algorithms module.
See why a plausible greedy rule can fail
Consider activities from 0–3, 3–6, and 2–4. The shortest activity is 2–4, but choosing it blocks both other activities. Choosing 0–3 and then 3–6 fits two. Earliest finish chooses 0–3, leaving the greatest possible room for later activities.
The exchange argument establishes why this particular rule is safe. Take an optimal schedule and replace its first activity with an earliest-finishing one. The replacement ends no later, so every later selected activity still fits. We can therefore find an optimum beginning with the greedy choice. Repeating on the remaining compatible activities completes the reasoning.
The argument assumes all activities count equally. Maximizing revenue from weighted activities is a different problem, for which earliest finish alone can fail.
Practice. Three events run from 1–3, 2–4, and 3–5. Under the rule that an event may start when another ends, which events does earliest-finish selection take? Why is “choose the shortest event” a different rule?
Answer. It takes 1–3, then 3–5, for two events. Duration alone ignores where an event sits in the schedule; an event can be short yet block useful neighbors.
Try another case. If one long activity pays 100 and two short compatible activities pay 1 each, does maximizing their count maximize revenue?
Reasoning. No. Two activities maximize count but earn only two. The objective determines which proof and algorithm apply.