Using an exchange argument to justify a greedy choice

← Back to Blog & Articles

Greedy solutions are easy to propose and dangerously easy to get wrong, because “this seems like the obviously best choice” is a feeling, not a proof.

The standard tool for turning that feeling into an actual guarantee is the exchange argument, and it’s worth being able to produce one on demand in an interview, because stating “I believe this greedy choice is optimal, and here’s why” is exactly the kind of reasoning that separates a strong candidate from one who got lucky on the examples.

The exchange argument has a consistent three-step shape. First, assume for contradiction that some optimal solution exists that does not make the greedy choice at the first point of disagreement — call this optimal solution OPT. Second, show that you can modify OPT by swapping out whatever choice it made at that point for the greedy choice instead, without making the solution any worse — this is the “exchange” step, and it’s where the actual structural insight of the problem lives. Third, conclude that since this exchanged solution is at least as good as OPT and now agrees with the greedy choice, the greedy choice must be part of some optimal solution — repeating this argument at every subsequent point of disagreement shows the fully greedy solution is optimal.

Apply that shape to session scheduling. Sort all sessions by end time, and let g be the session the greedy strategy picks first — the one with the earliest end time. Suppose, for contradiction, that some optimal solution OPT doesn’t include g, and let o be whichever session OPT picks first instead. Because g has the earliest end time of any session in the whole input, and o is some session in OPT, it must be true that g ends no later than o does. That means you can remove o from OPT and insert g in its place: g doesn’t overlap anything else OPT chose after o, precisely because g frees up the schedule at least as early as o did, so nothing that was compatible with o’s end time stops being compatible with g’s (earlier or equal) end time. This exchange produces a new solution with exactly the same number of sessions as OPT — so it’s still optimal — and now includes g. Since this holds no matter which optimal solution you started with, some optimal solution always includes the greedy choice, and inducting this same argument on the remaining sessions (everything starting after g ends, which is exactly the smaller version of the same problem) shows the entire greedy strategy — always take the compatible session with the earliest end time — produces an optimal solution overall.

Notice the pattern for your own use: identify the greedy choice, assume an optimal solution disagrees with it, and show the disagreement can be repaired by swapping in the greedy choice without any loss. If you ever try this and find you can’t complete the swap without a loss — the greedy choice can genuinely make things worse somewhere down the line — that failure is itself valuable information: it’s telling you the problem doesn’t have the safe greedy structure, and you should reach for dynamic programming instead.

Continue with the book

Explore the full treatment, worked examples, and exercises in Algorithmic Edge.


Companion article for Algorithmic Edge, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top