Professional Documents
Culture Documents
Greedy Algo
Greedy Algo
Greedy Algo
Gordon Gecko
(Michael Douglas)
■ Refueling stations at certain points along the way. Sort breakpoints by increasing value:
0 = b0 < b1 < b2 < ... < bn.
■ Truck fuel capacity = C.
S ← {0} S = breakpoints selected.
Greedy algorithm. x = 0
while (x ≠ bn)
Go as far as you can before refueling.
let p be largest integer such that bp ≤ x + C
■
if (bp = x)
C C C C return "no solution"
x ← bp
1 2 3 4 5 6 7 S ← S ∪ {p}
return S
Princeton Palo Alto
C C C
3 4
Selecting Breakpoints Selecting Breakpoints
Theorem: greedy algorithm is optimal. Theorem: greedy algorithm is optimal.
g0 g1 g2 gr gp g0 g1 g2 gp
Greedy: 1 2 3 4 5 6 7 8 9 Greedy: 1 2 3 4 5 6 7 8 9
OPT: 1 2 3 4 5 6 7 OPT: 1 2 3 4 55 6 7
f0 f1 f2 fr fq f0 f1 f2 fq
r=4 r=4 r=5
5 6
gq D
g0 g1 g2 gp
Greedy: 1 2 3 4 5 6 7 E
F
OPT: 1 2 3 4 5
G
f0 f1 f2 fq
H Time
r=q=5
7
0 1 2 3 4 5 6 7 8 9 10 11 8
Activity Selection: Greedy Algorithm Activity Selection
Theorem: greedy algorithm is optimal.
Greedy Activity Selection Algorithm
Sort jobs by increasing finish times so that
Proof (by contradiction):
f1 ≤ f2 ≤ ... ≤ fn.
■ Let g1, g2, . . . gp denote set of jobs selected by greedy and assume
S = φ S = jobs selected. it is not optimal.
for j = 1 to n ■ Let f1, f2, . . . fq denote set of jobs selected by optimal solution with
if (job j compatible with A) f1 = g1, f2= g2, . . . , fr = gr for largest possible value of r.
S ← S ∪ {j}
return S ■ Note: r < q.
p=6
Greedy: 1 5 8 9 13 21
f1 = g 1 f2 = g 2 f3 = g 3
OPT: 1 5 8 11 15 17 21
r=3 q=7
9 10
p=6 p=6
Greedy: 1 5 8 9 13 21 Greedy: 1 5 8 9 13 21
f1 = g 1 f2 = g 2 f3 = g 3 f1 = g 1 f2 = g 2 f3 = g 3
OPT: 1 5 8 9 11 15 17 21 OPT: 1 5 8 9 15 17 21
13 14
15 16
Does Greedy Always Work? Characteristics of Greedy Algorithms
US postal denominations: 1, 10, 21, 34, 70, 100, 350, 1225, 1500. Greedy choice property.
■ Ex. 140¢. ■ Globally optimal solution can be arrived at by making locally
■ Greedy: 100, 34, 1, 1, 1, 1, 1, 1. optimal (greedy) choice.
■ Optimal: 70, 70. ■ At each step, choose most "promising" candidate, without
worrying whether it will prove to be a sound decision in long run.
17 18
d=9 d = 15 d = 11
Lateness = 3 max lateness = 2
d=8 d=6 d=9
19 20
Minimizing Lateness: No Idle Time Minimizing Lateness: Inversions
Fact 1: there exists an optimal schedule with no idle time. An inversion in schedule S is a pair of jobs i and j such that:
■ i<j
d=4 d=6 d = 12 ■ j scheduled before i inversion
0 1 2 3 4 5 6 7 8 9 10 11
d5 = 8 d2 = 4
d=4 d=6 d = 12
0 1 2 3 4 5 6 7 8 9 10 11
Fact 3: greedy schedule ⇔ no inversions.
Fact 2: the greedy schedule has no idle time. Fact 4: if a schedule (with no idle time) has an inversion, it has one
whose with a pair of inverted jobs scheduled consecutively.
21 22
d2 = 4 d5 = 8 i j
f’j
Fact 3: greedy schedule ⇔ no inversions.
Swapping two adjacent, inverted jobs does not increase max lateness.
Fact 4: if a schedule (with no idle time) has an inversion, it has one ■ l’k = lk for all k ≠ i, j
whose with a pair of inverted jobs scheduled consecutively. ■ l’i ≤ li
If job j is late:
Fact 5: swapping two adjacent, inverted jobs:
■
l′j = f j′ − d j (definitio n)
■ Reduces the number of inversions by one. = fi − d j ( j finishes at time f i )
■ Does not increase the maximum lateness. ≤ fi − di (i < j)
≤ li (definitio n)
Theorem: greedy schedule is optimal.
23 24