SLIDE 3 Event Scheduling, 4
function greedySch(L: a list of events) Sort L so that f1 ≤ · · · ≤ fk S ← ∅; last ← 0 for i ← 1 to k do if last ≤ si then S ← S ∪ { (si, fi) }; last ← fi return S // Runtime: ??
Lemma
greedySch returns a schedule with as many events as possible.
◮ S′ = { (s′
1, f ′ 1), . . . , (s′ m, f ′ m) } is the
schedule greedySch returns. ◮ S′′ = { (s′′
1 , f ′′ 1 ), . . . , (s′′ n, f ′′ n ) } is
an optimal schedule. (So m ≤ n.) Goal: Show m = n. Strategy: Turn S′′ into S′. Suppose f ′
1 ≤ · · · ≤ f ′ m and f ′′ 1 ≤ · · · ≤ f ′′ n .
For ℓ = 1, . . . , m, let S′′
ℓ =
{ (s′
1, f ′ 1), . . . , (s′ ℓ, f ′ ℓ), (s′′ ℓ+1, f ′′ ℓ+1), . . . , (s′′ m, f ′′ m) }.
We want to show each S′′
ℓ is feasible.
Base Case Claim 1: f ′
1 ≤ f ′′ 1 .
Proof: By greedySch’s choice, f ′
1 = the
smallest finishing time. Hence, f ′
1 ≤ f ′′ 1 .
Claim 2: S′′
1 is feasible.
Proof: Since f ′
1 ≤ f ′′ 1 (Claim 1) and f ′′ 1 ≤ s′′ 2
(S′′ has no overlaps), f ′
1 ≤ s′′ 2 . Hence, S′′ 1 has
no overlaps.
Royer Greedy Algorithms
Event Scheduling, 5
function greedySch(L: a list of events) Sort L so that f1 ≤ · · · ≤ fk S ← ∅; last ← 0 for i ← 1 to k do if last ≤ si then S ← S ∪ { (si, fi) }; last ← fi return S // Runtime: ??
Lemma
greedySch returns a schedule with as many events as possible.
◮ S′ = { (s′
1, f ′ 1), . . . , (s′ m, f ′ m) } is the
schedule greedySch returns. ◮ S′′ = { (s′′
1 , f ′′ 1 ), . . . , (s′′ n, f ′′ n ) } is
an optimal schedule. (So m ≤ n.) Goal: Show m = n. Strategy: Turn S′′ into S′. Suppose f ′
1 ≤ · · · ≤ f ′ m and f ′′ 1 ≤ · · · ≤ f ′′ n .
For ℓ = 1, . . . , m, let S′′
ℓ =
{ (s′
1, f ′ 1), . . . , (s′ ℓ, f ′ ℓ), (s′′ ℓ+1, f ′′ ℓ+1), . . . , (s′′ m, f ′′ m) }.
We want to show each S′′
ℓ is feasible.
Induction step Case IH: S′′
ℓ−1 is feasible, (1<ℓ≤ m).
Convention: s′′
n+1 = ∞.
Claim 3: f ′
ℓ ≤ f ′′ ℓ .
Proof: Since S′′
ℓ−1 is consistent, s′′ ℓ ≥ f ′ ℓ−1. By
greeySch’s greedy choice, f ′
ℓ is the smallest
finishing time with a corresponding starting time ≥ f ′
ℓ−1. Hence, f ′ ℓ ≤f ′′ ℓ .
Claim 4: S′′
ℓ is feasible.
Proof: Since f ′
ℓ ≤ f ′′ ℓ (Claim 3) & f ′′ ℓ ≤ s′′ ℓ+1
(S′′
ℓ has no overlaps), f ′ ℓ ≤ s′′ ℓ+1.
Hence, S′′
ℓ+1 has no overlaps.
Royer Greedy Algorithms
Event Scheduling, 6
function greedySch(L: a list of events) Sort L so that f1 ≤ · · · ≤ fk S ← ∅; last ← 0 for i ← 1 to k do if last ≤ si then S ← S ∪ { (si, fi) }; last ← fi return S // Runtime: ??
Lemma
greedySch returns a schedule with as many events as possible.
◮ S′ = { (s′
1, f ′ 1), . . . , (s′ m, f ′ m) } is the
schedule greedySch returns. ◮ S′′ = { (s′′
1 , f ′′ 1 ), . . . , (s′′ n, f ′′ n ) } is
an optimal schedule. (So m ≤ n.) Goal: Show m = n. Strategy: Turn S′′ into S′. Suppose f ′
1 ≤ · · · ≤ f ′ m and f ′′ 1 ≤ · · · ≤ f ′′ n .
For ℓ = 1, . . . , m, let S′′
ℓ =
{ (s′
1, f ′ 1), . . . , (s′ ℓ, f ′ ℓ), (s′′ ℓ+1, f ′′ ℓ+1), . . . , (s′′ m, f ′′ m) }.
We proved: S′′
m is feasible.
Final step Suppose by way of contradiction that m < n. Since S′′
m is feasible, (s′′ m+1, f ′′ m+1) does not
1, f ′ 1), . . . , (s′ m, f ′ m).
But then by greedySch’s greedy choice, the algorithm would have chosen at least one more event to add to S, a contradiction. Therefore, m = n and S′ (= S′′
m) is optimal.
✷
Royer Greedy Algorithms
Knapsack, 1
The Knapsack Problem (KP)
Given: ◮ A knapsack with capacity W kgs. ◮ Items 1, . . . , n ◮ Item i has weight wi and value vi. Find: S ⊆ { 1, . . . , n } so that ◮ ∑i∈S wi ≤ W and ◮ ∑i∈S vi is maximized.
Greedy Heuristic
Order items so that v1/wi ≥ · · · ≥ vn/wn cap ← W; S ← ∅ for i ← 1 to n do if wi ≤ cap then S ← S ∪ { i } cap ← cap − wi return S
Image from: http://commons.wikimedia.org/wiki/File:Knapsack.svg Royer Greedy Algorithms