Greedy Algorithms: Slides by Kevin Wayne. All Rights Reserved
Greedy Algorithms: Slides by Kevin Wayne. All Rights Reserved
Greedy
Algorithms
1
4.1 Interval Scheduling
Interval Scheduling
Interval scheduling.
Job j starts at sj and finishes at fj.
Two jobs compatible if they don't overlap.
Goal: find maximum subset of mutually compatible jobs.
g
h
Time
0 1 2 3 4 5 6 7 8 9 10 11
3
Interval Scheduling: Greedy Algorithms
4
Interval Scheduling: Greedy Algorithms
5
Interval Scheduling: Greedy Algorithm
A ← φ
for j = 1 to n {
if (job j compatible with A)
A ← A ∪ {j}
}
return A
6
Interval Scheduling: Analysis
Greedy: i1 i2 ir ir+1
7
Interval Scheduling: Analysis
Greedy: i1 i2 ir ir+1
8
4.1 Interval Partitioning
Interval Partitioning
Interval partitioning.
Lecture j starts at sj and finishes at fj.
Goal: find minimum number of classrooms to schedule all lectures
so that no two occur at the same time in the same room.
4 e j
3 c d g
2 b h
1 a f i
10
Interval Partitioning
Interval partitioning.
Lecture j starts at sj and finishes at fj.
Goal: find minimum number of classrooms to schedule all lectures
so that no two occur at the same time in the same room.
3 c d f j
2 b g i
1 a e h
11
Interval Partitioning: Lower Bound on Optimal Solution
Def. The depth of a set of open intervals is the maximum number that
contain any given time.
3 c d f j
2 b g i
1 a e h
12
Interval Partitioning: Greedy Algorithm
for j = 1 to n {
if (lecture j is compatible with some classroom k)
schedule lecture j in classroom k
else
allocate a new classroom d + 1
schedule lecture j in classroom d + 1
d ← d + 1
}
13
Interval Partitioning: Greedy Analysis
14
4.2 Scheduling to Minimize Lateness
Scheduling to Minimizing Lateness
1 2 3 4 5 6
Ex:
tj 3 2 1 4 3 2
dj 6 8 9 9 14 15
d3 = 9 d2 = 8 d6 = 15 d1 = 6 d5 = 14 d4 = 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16
Minimizing Lateness: Greedy Algorithms
17
Minimizing Lateness: Greedy Algorithms
tj 1 10 counterexample
dj 100 10
1 2
tj 1 10
counterexample
dj 2 10
18
Minimizing Lateness: Greedy Algorithm
t ← 0
for j = 1 to n
Assign job j to interval [t, t + tj]
sj ← t, fj ← t + tj
t ← t + tj
output intervals [sj, fj]
max lateness = 1
d1 = 6 d2 = 8 d3 = 9 d4 = 9 d5 = 14 d6 = 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
19
Minimizing Lateness: No Idle Time
d=4 d=6 d = 12
0 1 2 3 4 5 6 7 8 9 10 11
d=4 d=6 d = 12
0 1 2 3 4 5 6 7 8 9 10 11
20
Minimizing Lateness: Inversions
before swap j i
21
Minimizing Lateness: Inversions
before swap j i
after swap i j
f'j
Pf. Let l be the lateness before the swap, and let l ' be it afterwards.
l 'k = lk for all k ≠ i, j
l 'i ≤ li l"j = f j" # d j (definition)
= fi # d j ( j finishes at time fi )
If job j is late:
$ fi # d i (i < j)
$ li (definition)
22
!
Minimizing Lateness: Analysis of Greedy Algorithm
23
Greedy Analysis Strategies
Greedy algorithm stays ahead. Show that after each step of the greedy
algorithm, its solution is at least as good as any other algorithm's.
24
4.3 Optimal Caching
Optimal Offline Caching
Caching.
Cache with capacity to store k items.
Sequence of m item requests d1, d2, …, dm.
Cache hit: item already in cache when requested.
Cache miss: item not already in cache when requested: must bring
requested item into cache, and evict some existing item, if full.
a a b
Ex: k = 2, initial cache = ab, red = cache miss
b a b
requests: a, b, c, b, c, a, a, b.
c c b
Optimal eviction schedule: 2 cache misses.
b c b
c c b
a a b
a a b
b a b
requests cache
26
Optimal Offline Caching: Farthest-In-Future
current cache: a b c d e f
27
Reduced Eviction Schedules
a a b c a a b c
a a x c a a b c
c a d c c a b c
d a d b d a d c
a a c b a a d c
b a x b b a d b
c a c b c a c b
a a b c a a c b
a a b c a a c b
28
Reduced Eviction Schedules
S S' S S'
c c c c
t t t t
d d
Case 1 Case 2
29
Farthest-In-Future: Analysis
30
Farthest-In-Future: Analysis
Pf. (continued)
Case 3: (d is not in the cache; SFF evicts e; S evicts f ≠ e).
– begin construction of S' from S by evicting e instead of f
j same e f same e f
S S'
j+1
j same e d same d f
S S'
– now S' agrees with SFF on first j+1 requests; we show that having
element f in cache is no worse than having element e
31
Farthest-In-Future: Analysis
Let j' be the first time after j+1 that S and S' take a different action,
and let g be item requested at time j'.
must involve e or f (or both)
32
Farthest-In-Future: Analysis
Let j' be the first time after j+1 that S and S' take a different action,
and let g be item requested at time j'.
must involve e or f (or both)
33
Caching Perspective
34
4.4 Shortest Paths in a Graph
2 23 3
9
s
14
18 Cost of path s-2-3-5-t
= 9 + 23 + 2 + 16
2 6
6
30 4 19 = 50.
11
15 5
5
6
20 16
7 t
44
36
Dijkstra's Algorithm
Dijkstra's algorithm.
Maintain a set of explored nodes S for which we have determined
the shortest path distance d(u) from s to u.
Initialize S = { s }, d(s) = 0.
Repeatedly choose unexplored node v which minimizes
" (v ) = min d (u ) + l e ,
e = (u , v ) : u ! S
le v
d(u)
u
S
s
37
Dijkstra's Algorithm
Dijkstra's algorithm.
Maintain a set of explored nodes S for which we have determined
the shortest path distance d(u) from s to u.
Initialize S = { s }, d(s) = 0.
Repeatedly choose unexplored node v which minimizes
" (v ) = min d (u ) + l e ,
e = (u , v ) : u ! S
le v
d(u)
u
S
s
38
Dijkstra's Algorithm: Proof of Correctness
Invariant. For each node u ∈ S, d(u) is the length of the shortest s-u path.
Pf. (by induction on |S|)
Base case: |S| = 1 is trivial.
Inductive hypothesis: Assume true for |S| = k ≥ 1.
Let v be next node added to S, and let u-v be the chosen edge.
The shortest s-u path plus (u, v) is an s-v path of length π(v).
Consider any s-v path P. We'll see that it's no shorter than π(v).
Let x-y be the first edge in P that leaves S,
P
S u
v
l (P) ≥ l (P') + l (x,y) ≥ d(x) + l (x, y) ≥ π(y) ≥ π(v)
39
Dijkstra's Algorithm: Implementation
For each unexplored node, explicitly maintain " (v) = min d (u) + l e .
e = (u,v) : u # S
Priority Queue
PQ Operation Dijkstra Array Binary heap d-way Heap Fib heap †
40
Edsger W. Dijkstra
41
Extra Slides
Coin Changing
Ex: 34¢.
Ex: $2.89.
44
Coin-Changing: Greedy Algorithm
S ← φ
while (x ≠ 0) {
let k be largest integer such that ck ≤ x
if (k = 0)
return "no solution found"
x ← x - ck
S ← S ∪ {k}
}
return S
45
Coin-Changing: Analysis of Greedy Algorithm
Theorem. Greedy algorithm is optimal for U.S. coinage: 1, 5, 10, 25, 100.
Pf. (by induction on x)
Consider optimal way to change ck ≤ x < ck+1 : greedy takes coin k.
We claim that any optimal solution must also take coin k.
– if not, it needs enough coins of type c1, …, ck-1 to add up to x
– table below indicates no optimal solution can do this
Problem reduces to coin-changing x - ck cents, which, by induction, is
optimally solved by greedy algorithm. ▪
46
Coin-Changing: Analysis of Greedy Algorithm
Counterexample. 140¢.
Greedy: 100, 34, 1, 1, 1, 1, 1, 1.
Optimal: 70, 70.
47
Selecting Breakpoints
Selecting Breakpoints
Selecting breakpoints.
Road trip from Princeton to Palo Alto along fixed route.
Refueling stations at certain points along the way.
Fuel capacity = C.
Goal: makes as few refueling stops as possible.
C C C C
1 2 3 4 5 6 7
49
Selecting Breakpoints: Greedy Algorithm
while (x ≠ bn)
let p be largest integer such that bp ≤ x + C
if (bp = x)
return "no solution"
x ← bp
S ← S ∪ {p}
return S
50
Selecting Breakpoints: Correctness
g0 g1 g2 gr gr+1
Greedy:
OPT: ...
f0 f1 f2 fr fr+1 fq
why doesn't optimal solution
drive a little further?
51
Selecting Breakpoints: Correctness
g0 g1 g2 gr gr+1
Greedy:
OPT: ...
f0 f1 f2 fr fq
another optimal solution has
one more breakpoint in common
⇒ contradiction
52