04 Greedy Algorithms I
04 Greedy Algorithms I
G REEDY A LGORITHMS I
‣ coin changing
‣ interval scheduling
‣ scheduling to minimize lateness
‣ optimal caching
‣ coin changing
‣ interval scheduling
‣ scheduling to minimize lateness
‣ optimal caching
Coin changing
Ex. 34¢.
Cashier's algorithm. At each iteration, add coin of the largest value that
does not take us past the amount to be paid.
Ex. $2.89.
3
Cashier's algorithm
At each iteration, add coin of the largest value that does not take us past
the amount to be paid.
WHILE x > 0
k ← largest coin denomination ck such that ck ≤ x
IF no such k, RETURN "no solution"
ELSE
x ← x – ck
S ←S∪{k}
RETURN S
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
5
Analysis of cashier's algorithm
Theorem. Cashier's algorithm is optimal for U.S. coins: 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 cashier's algorithm. ▪
1 1 P ≤ 4 –
2 5 N ≤ 1 4
3 10 N+D ≤ 2 4+5=9
4 25 Q ≤ 3 20 + 4 = 24
5 100 no limit 75 + 24 = 99
6
Cashier's algorithm for other denominations
A. No. Consider U.S. postage: 1, 10, 21, 34, 70, 100, 350, 1225, 1500.
・Cashier's algorithm: 140¢ = 100 + 34 + 1 + 1 + 1 + 1 + 1 + 1.
・Optimal: 140¢ = 70 + 70.
‣ coin changing
‣ interval scheduling
‣ scheduling to minimize lateness
‣ optimal caching
SECTION 4.1
Interval scheduling
d
jobs d and g
are incompatible
e
h
time
0 1 2 3 4 5 6 7 8 9 10 11
9
Interval scheduling: greedy algorithms
10
Interval scheduling: greedy algorithms
11
Interval scheduling: earliest-finish-time-first algorithm
FOR j = 1 TO n
IF job j is compatible with A
A ←A∪{j}
RETURN A
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
13
Interval scheduling: analysis of earliest-finish-time-first algorithm
14
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 lectures occur at the same time in the same room.
4 e j
3 g
c d
2 b h
1 a f i
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 lectures occur at the same time in the same room.
3 f
c d j
2 b g i
1 a e h
17
Interval partitioning: greedy algorithms
3
2
1
3
2
1
3
2
1
18
Interval partitioning: earliest-start-time-first algorithm
FOR j = 1 TO n
IF lecture j is compatible with some classroom
Schedule lecture j in any such classroom k.
ELSE
Allocate a new classroom d + 1.
Schedule lecture j in classroom d + 1.
d←d +1
RETURN schedule.
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
19
Interval partitioning: earliest-start-time-first algorithm
Pf. Store classrooms in a priority queue (key = finish time of its last lecture).
・To determine whether lecture j is compatible with some classroom,
compare sj to key of min classroom k in priority queue.
・To add lecture j to classroom k, increase key of classroom k to fj.
・Total number of priority queue operations is O(n).
・Sorting by start time takes O(n log n) time. ▪
20
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.
depth = 3
3 c d f j
2 b g i
1 a e h
22
4. G REEDY A LGORITHMS I
‣ coin changing
‣ interval scheduling
‣ scheduling to minimize lateness
‣ optimal caching
SECTION 4.2
Scheduling to minimizing lateness
1 2 3 4 5 6
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
24
Minimizing lateness: greedy algorithms
25
Minimizing lateness: greedy algorithms
1 2
counterexample
tj 1 10
dj 100 10
1 2
tj 1 10 counterexample
dj 2 10
26
Minimizing lateness: earliest deadline first
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
27
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
28
Minimizing lateness: inversions
j i
29
Minimizing lateness: inversions
before swap j i
after swap i j
f 'j
31
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.
32
4. G REEDY A LGORITHMS I
‣ coin changing
‣ interval scheduling
‣ scheduling to minimize lateness
‣ optimal caching
SECTION 4.3
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.
cache miss
(eviction)
a a b
Ex. k = 2, initial cache = ab, requests: a, b, c, b, c, a, a. b a b
Optimal eviction schedule. 2 evictions. c c b
b c b
c c b
a a b
b a b
requests cache 34
Optimal offline caching: greedy algorithms
previous queries
⋮
a a w x y z FIFO: eject a
d a w x d z LRU: eject d
a a w x d z
b a b x d z
c a b c d z
cache miss g
(which item to eject?)
b
e
d
⋮
future queries
35
Optimal offline caching: farthest-in-future (clairvoyant algorithm)
current cache a a b c d e
cache miss f
(which item to eject?)
a
b
c
e
g
b
e
d FF: eject d
future
queries
Def. A reduced schedule is a schedule that only inserts an item into the
cache in a step in which that item is requested.
item inserted
when not requested
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 b c
a a b c a a b c
37
Reduced eviction schedules
. . c . . c
. . c . . c
. . c . . c
time t ¬d . . d d enters cache ¬d . . c
without a request
might as well
. . d . . c
leave c in cache
. . d . . c
d evicted before
time t' e . . e next request e . . e
. . e . . e
38
Reduced eviction schedules
. . c . . c
. . c . . c
. . c . . c
time t ¬d . . d d enters cache ¬d . . c
without a request
might as well
. . d . . c
leave c in cache
. . d . . c until d is requested
d requested
time t' d . . d before d evicted d . . d
. . d . . d
39
Farthest-in-future: analysis
Invariant. There exists an optimal reduced schedule S that makes the same
eviction schedule as SFF through the first j requests.
Pf. [by induction on j]
Let S be reduced schedule that satisfies invariant through j requests.
We produce S' that satisfies invariant after j + 1 requests.
・Consider (j + 1)st request d = dj+1.
・Since S and SFF have agreed up until now, they have the same cache
contents before request j + 1.
・Case 1: (d is already in the cache). S' = S satisfies invariant.
・Case 2: (d is not in the cache and S and SFF evict the same element).
S' = S satisfies invariant.
40
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
same e f j same e 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
- let S' behave the same as S until S' is forced to take a different action
(because either S evicts e; or because either e or f is requested)
41
Farthest-in-future: analysis
Let j' be the first time after j + 1 that S' must take a different action from S,
and let g be item requested at time j'.
involves e or f (or both)
S S'
・Case 3a: g = e.
Can't happen with FF since there must be a request for f before e.
・Case 3b: g = f.
Element f can't be in cache of S, so let e' be the element that S evicts.
- if e' = e, S' accesses f from cache; now S and S' have same cache
- if e' ≠ e, we make S' evict e' and brings e into the cache;
now S and S' have the same cache
We let S' behave exactly like S for remaining requests.
Let j' be the first time after j + 1 that S' must take a different action from S,
and let g be item requested at time j'.
involves e or f (or both)
S S'
S S'
44