Greedy Algorithms: 4.1 Interval Scheduling
Greedy Algorithms: 4.1 Interval Scheduling
1 Interval Scheduling
Chapter 4
Greedy
Algorithms
c
[Fewest conflicts] For each job j, count the number of
d conflicting jobs cj. Schedule in ascending order of cj.
e
g
h
Time
0 1 2 3 4 5 6 7 8 9 10 11
3 4
Interval Scheduling: Greedy Algorithms Interval Scheduling: Greedy Algorithm
Greedy template. Consider jobs in some natural order. Greedy algorithm. Consider jobs in increasing order of finish time.
Take each job provided it's compatible with the ones already taken. Take each job provided it's compatible with the ones already taken.
A ← φ
for j = 1 to n {
counterexample for shortest interval
if (job j compatible with A)
A ← A ∪ {j}
}
return A
counterexample for fewest conflicts
5 6
job ir+1 finishes before jr+1 job ir+1 finishes before jr+1
why not replace job jr+1 solution still feasible and optimal,
with job ir+1? but contradicts maximality of r.
7 8
Interval Partitioning
4 e j
3 c d g
2 b h
1 a f i
10
Interval partitioning. Def. The depth of a set of open intervals is the maximum number that
Lecture j starts at sj and finishes at fj. contain any given time.
Goal: find minimum number of classrooms to schedule all lectures
so that no two occur at the same time in the same room. Key observation. Number of classrooms needed ≥ depth.
Ex: This schedule uses only 3. Ex: Depth of schedule below = 3 ⇒ schedule below is optimal.
a, b, c all contain 9:30
3 c d f j 3 c d f j
2 b g i 2 b g i
1 a e h 1 a e h
9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30
Time Time
11 12
Interval Partitioning: Greedy Algorithm Interval Partitioning: Greedy Analysis
Greedy algorithm. Consider lectures in increasing order of start time: Observation. Greedy algorithm never schedules two incompatible
assign lecture to any compatible classroom. lectures in the same classroom.
13 14
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 Minimizing Lateness: Greedy Algorithms
Greedy template. Consider jobs in some order. Greedy template. Consider jobs in some order.
[Shortest processing time first] Consider jobs in ascending order [Shortest processing time first] Consider jobs in ascending order
of processing time tj. of processing time tj.
1 2
tj 1 10 counterexample
[Earliest deadline first] Consider jobs in ascending order of dj 100 10
deadline dj.
[Smallest slack] Consider jobs in ascending order of slack dj - tj. [Smallest slack] Consider jobs in ascending order of slack dj - tj.
1 2
tj 1 10
counterexample
dj 2 10
17 18
Greedy algorithm. Earliest deadline first. Observation. There exists an optimal schedule with no idle time.
d=4 d=6 d = 12
0 1 2 3 4 5 6 7 8 9 10 11
Sort n jobs by deadline so that d1 ≤ d2 ≤ … ≤ dn
t ← 0
d=4 d=6 d = 12
for j = 1 to n
0 1 2 3 4 5 6 7 8 9 10 11
Assign job j to interval [t, t + tj]
sj ← t, fj ← t + tj
t ← t + tj
output intervals [sj, fj]
Observation. The greedy schedule has no idle time.
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 20
Minimizing Lateness: Inversions Minimizing Lateness: Inversions
Def. Given a schedule S, an inversion is a pair of jobs i and j such that: Def. Given a schedule S, an inversion is a pair of jobs i and j such that:
i < j but j scheduled before i. i < j but j scheduled before i.
inversion inversion
fi fi
Observation. Greedy schedule has no inversions. Claim. Swapping two consecutive, inverted jobs reduces the number of
inversions by one and does not increase the max lateness.
Observation. If a schedule (with no idle time) has an inversion, it has
one with a pair of inverted jobs scheduled consecutively. 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)
21 22
Theorem. Greedy schedule S is optimal. Greedy algorithm stays ahead. Show that after each step of the greedy
Pf. Define S* to be an optimal schedule that has the fewest number of algorithm, its solution is at least as good as any other algorithm's.
inversions, and let's see what happens.
Can assume S* has no idle time. Structural. Discover a simple "structural" bound asserting that every
If S* has no inversions, then S = S*. possible solution must have a certain value. Then show that your
If S* has an inversion, let i-j be an adjacent inversion. algorithm always achieves this bound.
– swapping i and j does not increase the maximum lateness and
strictly decreases the number of inversions Exchange argument. Gradually transform any solution to the one found
– this contradicts definition of S* ▪ by the greedy algorithm without hurting its quality.
23 24
Optimal Offline Caching
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
Farthest-in-future. Evict item in the cache that is not requested until Def. A reduced schedule is a schedule that only inserts an item into
farthest in the future. the cache in a step in which that item is requested.
current cache: a b c d e f
Intuition. Can transform an unreduced schedule into a reduced one
with no more cache misses.
future queries: g a b c e d a b b a c d e a f a d e f g h ...
27 28
Reduced Eviction Schedules Farthest-In-Future: Analysis
Claim. Given any unreduced schedule S, can transform it into a reduced Theorem. FF is optimal eviction algorithm.
schedule S' with no more cache misses. doesn't enter cache at requested Pf. (by induction on number or requests j)
Pf. (by induction on number of unreduced items) time
Suppose S brings d into the cache at time t, without a request. Invariant: There exists an optimal reduced schedule S that makes
Let c be the item S evicts when it brings d into the cache. the same eviction schedule as SFF through the first j+1 requests.
Case 1: d evicted at time t', before next request for d.
Case 2: d requested at time t' before d is evicted. ▪ Let S be reduced schedule that satisfies invariant through j requests.
We produce S' that satisfies invariant after j+1 requests.
S S' S S' Consider (j+1)st request d = dj+1.
c c c c
Since S and SFF have agreed up until now, they have the same cache
t t t t contents before request j+1.
d d 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).
t' t' t' t'
S' = S satisfies invariant.
e d evicted at time t',
e d requested at time t'
d
before next request
Case 1 Case 2
29 30
Pf. (continued) Let j' be the first time after j+1 that S and S' take a different action,
Case 3: (d is not in the cache; SFF evicts e; S evicts f ≠ e). and let g be item requested at time j'.
must involve e or f (or both)
– begin construction of S' from S by evicting e instead of f
31 32
Farthest-In-Future: Analysis Caching Perspective
Let j' be the first time after j+1 that S and S' take a different action, Online vs. offline algorithms.
and let g be item requested at time j'. Offline: full sequence of requests is known a priori.
must involve e or f (or both)
Online (reality): requests are not known in advance.
j' same e same f
Caching is among most fundamental online problems in CS.
S S'
33 34
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
shortest path from Princeton CS department to Einstein's house
7 t
44
36
Dijkstra's Algorithm Dijkstra's Algorithm
add v to S, and set d(v) = π(v). shortest path to some u in explored add v to S, and set d(v) = π(v). shortest path to some u in explored
part, followed by a single edge (u, v) part, followed by a single edge (u, v)
le v
le v
d(u) d(u)
u u
S S
s s
37 38
Invariant. For each node u ∈ S, d(u) is the length of the shortest s-u path. For each unexplored node, explicitly maintain " (v) = min d (u) + l e .
e = (u,v) : u # S
Pf. (by induction on |S|)
Base case: |S| = 1 is trivial. Next node to explore = node with minimum π(v).
Inductive hypothesis: Assume true for |S| = k ≥ 1. When exploring v, for each incident edge e = (v, w), update
!
Let v be next node added to S, and let u-v be the chosen edge. " (w) = min { " (w), " (v) + l e }.
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). Efficient implementation. Maintain a priority queue of unexplored
Let x-y be the first edge in P that leaves S, ! nodes, prioritized by π(v).
P
39 40
Edsger W. Dijkstra
The question of whether computers can think is like the Extra Slides
question of whether submarines can swim.
41
Coin Changing
Coin Changing Goal. Given currency denominations: 1, 5, 10, 25, 100, devise a method
to pay amount to customer using fewest number of coins.
Ex: 34¢.
Ex: $2.89.
44
Coin-Changing: Greedy Algorithm Coin-Changing: Analysis of Greedy Algorithm
Cashier's algorithm. At each iteration, add coin of the largest value Theorem. Greedy algorithm is optimal for U.S. coinage: 1, 5, 10, 25, 100.
that does not take us past the amount to be paid. 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.
Sort coins denominations by value: c1 < c2 < … < cn.
– if not, it needs enough coins of type c1, …, ck-1 to add up to x
coins selected – table below indicates no optimal solution can do this
S ← φ Problem reduces to coin-changing x - ck cents, which, by induction, is
while (x ≠ 0) {
let k be largest integer such that ck ≤ x optimally solved by greedy algorithm. ▪
if (k = 0)
return "no solution found" All optimal solutions Max value of coins
x ← x - ck k ck
must satisfy 1, 2, …, k-1 in any OPT
S ← S ∪ {k}
} 1 1 P≤4 -
return S
2 5 N≤1 4
3 10 N+D≤2 4+5=9
4 25 Q≤3 20 + 4 = 24
Q. Is cashier's algorithm optimal?
5 100 no limit 75 + 24 = 99
45 46
Counterexample. 140¢.
Greedy: 100, 34, 1, 1, 1, 1, 1, 1.
Optimal: 70, 70.
47
Selecting Breakpoints Selecting Breakpoints: Greedy Algorithm
49 50
g0 g1 g2 gr gr+1 g0 g1 g2 gr gr+1
Greedy: Greedy:
51 52