Sub topic 2
Why a Greedy Choice Is Safe: The Exchange Argument
Assume you know asymptotic notation, basic recursion, arrays, and sorting. You should also understand intervals and why an algorithm that sorts n items usually takes O(n log n) time.
The greedy-choice property
A greedy algorithm builds a solution one choice at a time. At each step, it selects the option that looks best immediately. It does not normally reconsider earlier choices.
This strategy is correct only when the problem has the greedy-choice property. This property means that at least one optimal solution begins with the choice made by the greedy algorithm.
The greedy-choice property is different from optimal substructure. Optimal substructure means that the remaining part of an optimal solution is itself optimal for the smaller remaining problem. Dynamic programming relies on this property, but a problem can have optimal substructure without allowing greedy choices.
Proving a greedy choice with an exchange argument
An exchange argument shows that a greedy choice is safe by transforming an optimal solution.
Use this pattern:
- Start with an arbitrary optimal solution.
- Identify its first choice.
- If it already uses the greedy choice, the proof continues.
- Otherwise, replace its first choice with the greedy choice.
- Show that the replacement does not make the solution worse.
- Conclude that some optimal solution contains the greedy choice.
The replacement is called an exchange. The proof does not need to show that every optimal solution uses the greedy choice. It only needs to show that at least one optimal solution can be changed to use it.
Activity selection by earliest finish time
In the activity-selection problem, each activity has a start time and a finish time. We want the largest possible set of activities that do not overlap. An activity can start exactly when the previous activity finishes.
The greedy rule is:
> Always choose, among the compatible activities, the one with the earliest finish time.
Suppose the greedy algorithm chooses activity g first. Consider an optimal solution whose first activity is a. Since g finishes no later than a, replace a with g.
Every later activity in the optimal solution starts after a finishes. Because g finishes no later than a, those later activities also start after g finishes. Therefore, the replacement remains valid and keeps the same number of activities.
Now the remaining activities form a smaller activity-selection problem starting after g finishes. Repeating the same argument proves the algorithm optimal.
def max_activities(intervals):
count = 0
last_finish = float("-inf")
for start, finish in sorted(intervals, key=lambda item: item[1]):
if start >= last_finish:
count += 1
last_finish = finish
return count
activities = [
(1, 4), (3, 5), (0, 6), (5, 7),
(3, 9), (5, 9), (6, 10), (8, 11),
(8, 12), (2, 14), (12, 16)
]
print(max_activities(activities)) # 4The sorting step costs O(n log n). The scan costs O(n), so the total time is O(n log n). The algorithm selects activities such as (1, 4), (5, 7), (8, 11), and (12, 16).
The rule “choose the activity with the earliest start time” is not generally correct. An activity may start early but occupy most of the schedule. The finish time is the quantity that leaves the most room for future choices.
Exercise
Why does replacing the first activity in an optimal schedule with the earliest-finishing compatible activity preserve the number of activities?
Show answer
The greedy activity finishes no later than the original first activity, so every later activity remains compatible. The schedule therefore has the same number of activities.