Greedy Algorithms Interval Scheduling
Greedy Algorithms Interval Scheduling
Data Structures
Greedy Algorithms: Interval Scheduling
The Greedy approach
• Let i1, i2, … , ik be the order in which the intervals were added to
A by the algorithm.
Arguing for optimality
• Some notation:
• Let i1, i2, … , ik be the order in which the intervals were added to
A by the algorithm.
• Let i1, i2, … , ik be the order in which the intervals were added to
A by the algorithm.
• Let i1, i2, … , ik be the order in which the intervals were added to
A by the algorithm.
• Let i1, i2, … , ik be the order in which the intervals were added to
A by the algorithm.
• This means that f(ir) ≤ f(jr), as otherwise the algorithm would have
selected jr instead.
With a picture
ir
ir-1
…
jr
jr-1
f(jr-1) ≤ s(jr)
f(ir-1) ≤ f(jr-1)
Completing the proof
Completing the proof
• If yes, we select it and continue with the same checks for this
new interval.
Running Time
• Sort intervals in terms of increasing f(i).
• If yes, we select it and continue with the same checks for this
new interval.
• If yes, we select it and continue with the same checks for this
new interval.