Sub topic 2
The Greedy Choice Property
This chapter assumes you can write Python functions, read Big-O notation, and follow a proof by contradiction. It also builds on the previous chapter's definition of a greedy algorithm: one that makes the best-looking choice at each step and never revisits it.
What the property says
A problem has the greedy choice property when some optimal solution contains the choice your greedy rule makes first. If you commit to that choice, you have not ruled out reaching an optimal answer.
The claim is deliberately weak. The greedy choice does not have to appear in every optimal solution. It only has to appear in at least one. Many failed proofs try to show the stronger claim and get stuck, because several different optimal solutions often exist.
Choosing the right rule
Most problems offer several "obvious" greedy rules. Take interval scheduling, where you want the largest set of non-overlapping intervals:
| Rule | First pick | Safe? |
|---|---|---|
| Earliest start time | Interval that begins first | No |
| Shortest duration | Interval with the smallest length | No |
| Earliest finish time | Interval that ends first | Yes |
A rule's first pick is safe only if some optimal solution contains it. On small inputs you can test this by brute force before you try to prove anything:
from itertools import combinations
def compatible(subset):
s = sorted(subset)
return all(a[1] <= b[0] for a, b in zip(s, s[1:]))
def optimal_sets(intervals):
for k in range(len(intervals), 0, -1):
found = [set(c) for c in combinations(intervals, k) if compatible(c)]
if found:
return found
return [set()]
def choice_is_safe(intervals, rule):
first = min(intervals, key=rule)
return any(first in opt for opt in optimal_sets(intervals))
a = [(0, 10), (1, 3), (4, 6), (7, 9)]
print(choice_is_safe(a, lambda iv: iv[1])) # True: picks (1, 3)
print(choice_is_safe(a, lambda iv: iv[0])) # False: picks (0, 10)
b = [(0, 5), (4, 7), (6, 11)]
print(choice_is_safe(b, lambda iv: iv[1] - iv[0])) # False: picks (4, 7)
print(choice_is_safe(b, lambda iv: iv[1])) # True: picks (0, 5)A False result is a counterexample, and it settles the question: that rule is wrong. A True result is only evidence. The property has to hold for every input, so a test passing on one input proves nothing.
The shape of the proof
The usual argument starts from an arbitrary optimal solution and modifies it until it contains the greedy choice.
For earliest finish time, the argument goes like this. Let g be the interval that ends first and let x be the first interval in an optimal solution O. Since g ends no later than x, swapping x for g creates no overlap with the rest of O, and the solution keeps the same size. Later chapters cover this swap technique in full as the exchange argument.
Common errors
- Trusting intuition. "Shortest first" sounds reasonable, but input
babove breaks it. Look for a counterexample before you write a proof. - Proving too much. You only need some optimal solution to contain the greedy choice, not every one.
- Stopping after one step. The property justifies the first choice only. To apply the rule again you also need the remaining subproblem to look like the original, which is optimal substructure, the topic of the next chapter.
Exercise
For interval scheduling, consider the rule "pick the interval that overlaps the fewest other intervals." Use choiceissafe on [(0, 2), (1, 3), (1, 3), (1, 3), (2, 4), (3, 5), (4, 6), (5, 7), (5, 7), (5, 7), (6, 8)] to test it. What does the result tell you?
Show answer
(3, 5) overlaps only two others, (2, 4) and (4, 6), so it is the first pick. The only optimal solution is {(0, 2), (2, 4), (4, 6), (6, 8)}, with size 4, and it does not contain (3, 5). The checker prints False, which shows the fewest-overlaps rule lacks the greedy choice property. To run it, pass rule=lambda iv: sum(1 for o in data if o != iv and o[0] < iv[1] and iv[0] < o[1]).
Exercise
Why is it enough to show that the greedy choice is in some optimal solution, rather than in all of them?
Show answer
The algorithm only needs one optimal solution to stay reachable after it commits to the choice. If any optimal solution contains the choice, committing to it loses nothing, even when other optimal solutions exclude it.