Greedy PDF
Greedy PDF
Greedy PDF
Greedy Algorithms
Paul Beame
Greedy Algorithms
Greedy algorithms
Interval Scheduling
Easy to produce
Fast running times
Work only on certain classes of problems
Interval Scheduling
Single resource
Reservation requests
Of form Can I reserve it from start time
s to finish time f?
s < f
Find: maximum number of requests that
can be scheduled so that no two
reservations have the resource at the
same time
Interval scheduling
Interval Scheduling
Formally
Requests 1,2,,n
request i has start time s and finish time f > s
i
i
i
Requests i and j are compatible iff either
request i is for a time entirely before request j
fi s j
or, request j is for a time entirely before
request i
fj s i
Set A of requests is compatible iff every pair of
j is compatible
requests i,j A, i
Goal: Find maximum size subset A of compatible
requests
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.
a
b
c
d
e
f
g
h
0
5
10
11
Time
6
10
By definition of compatibility
st request r then the start time of that
If O contains a k+1
request must be after ok and thus after ak
Thus r is compatible with the first k requests in A
Therefore
Greedy:
a1
a1
ar
OPT:
o1
o2
or
ak+1
ok+1
...
11
12
O(nlog n) time
Sort jobs by finish times so that 0 f1 f2 ... fn.
O(n log n)
A
last 0
for j = 1 to n {
if (last sj)
A A {j}
last fj
}
return A
O(n)
14
Interval partitioning.
Interval Partitioning
10:30
11
11:30
12
12:30
1:30
2:30
3:30
4:30
Time
9:30
j
i
i
2
d
b
h
f
10
a
9:30
d
b
Interval partitioning.
e
10
10:30
11
11:30
12
12:30
h
1
1:30
2:30
3:30
4:30
Time
15
16
d
b
a
9
9:30
17
e
10
10:30
11
11:30
12
12:30
h
1
1:30
2:30
3:30
4:30
Time
18
For i=1 to n
j1
While (request i not scheduled)
lastj finish time of the last request
currently scheduled on resource j
if silastj then schedule request i on
resource j
jj+1
End While
End For
19
20
May be slow
O(nd)
which may be (n2)
d1
Schedule request 1 on resource 1
last1f1
Insert 1 into priority queue Q with key = last1
For i=2 to n
j findmin(Q)
if silastj then
schedule request i on resource j
lastj fi
Increasekey(j,Q) to lastj
else
d d+1
schedule request i on resource d
lastdfi
Insert d into priority queue Q with key = lastd
End For
O(n log n)
O(n log d)
22
23
Example:
tj
dj
14
15
lateness = 2
d3 = 9
d2 = 8
d6 = 15
3
d1 = 6
5
lateness = 0
10
d4 = 9
11
max lateness = 6
d5 = 14
8
12
13
14
15
25
26
Greedy Algorithm:
Earliest Deadline First
tj
10
dj
100
10
counterexample
tj
10
dj
10
counterexample
27
28
max lateness = 1
d1 = 6
0
d2 = 8
3
d3 = 9
5
d4 = 9
7
d5 = 14
9
10
11
12
d6 = 15
13
14
15
29
30
d=4
1
d=6
2
d=4
0
before swap
d = 12
d=6
10
11
10
11
d = 12
7
31
32
fi
i
f'j
Lateness Li
Li since i is scheduled earlier in O
than in O
Requests i and j together occupy the same total
time slot in both schedules
All other requests k
i,j have Lk=Lk
f =f so L = f -d =f -d < f -d =L
j
i
j
j j
i j
j j
j
Maximum lateness has not increased!
33
34
QED
35
36
We know that
Therefore
37
38
Optimal Caching/Paging
Memory systems
Optimal Caching/Paging
39
40
Caching Example
This is optimal
41
Other Algorithms
Let NextAccessi(d)=min{ j
i : dj=d} be the
next point in D that item d will be requested
Evict d such that NextAccessi(d) is largest
k=3, C={a,b,c}
D=
abcdadeadbc
SFIF=
c
b
ed
S =
b
c
de
Exchange Argument
Least-Frequenty-Used-In-Future?
We can swap choices to convert other schedules
to Farthest-In-Future without losing quality
43
44
Caching.
a
b
requests
future queries:
g a b c e d a b b a c d e af a d e f g h ...
eject this one
cache miss
cache
45
46
an unreduced schedule
S'
t'
t'
t
d
d
t'
e
S'
t'
d requested at time t'
a reduced schedule
Case 1
47
Case 2
48
Farthest-In-Future: Analysis
Farthest-In-Future: Analysis
Proof. (continued)
same
same
same
S'
e
same
S'
now S' agrees with SFIF on first j+1 requests; we show that
having element f in cache is no worse than having element e
49
50
Farthest-In-Future: Analysis
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)
j'
same
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'.
same
j'
S'
same
S'
same
j'
same
same
S'
51
52
Caching Perspective
53
54
A greedy algorithm
Dijsktras Algorithm
Dijkstra(G,w,s)
S{s}
d[s]0
while S
V do
of all edges e=(u,v) s.t. v
S and u
S select* one
with the minimum value of d[u]+w(e)
SS {v}
d[v]d[u]+w(e)
pred[v]u
Dijkstras Algorithm:
Maintain a set S of vertices whose shortest paths
are known
initially S={s}
Maintaining current best lengths of paths that only
go through S to each of the vertices in G
path-lengths to elements of S will be right, to
V-S they might not be right
Repeatedly add vertex v to S that has the shortest
path-length of any vertex in V-S
update path lengths based on new paths
through v
Dijkstras Algorithm
0
57
Dijkstras Algorithm
Update distances
4
10
Dijkstras Algorithm
Add to S
2
59
10
5
1
2
7
58
10
1
3
10
Add to S
4
4
1
Dijkstras Algorithm
56
2
60
10
Dijkstras Algorithm
Update distances
61
Dijkstras Algorithm
Update distances
63
Dijkstras Algorithm
Update distances
4
10
Dijkstras Algorithm
Add to S
2
65
10
13
5
1
2
7
64
8
13
10
13
5
1
2
7
4
10
Add to S
Dijkstras Algorithm
6
1
62
10
5
1
10
2
7
Add to S
1
Dijkstras Algorithm
8
13
2
66
11
Dijkstras Algorithm
Update distances
67
Dijkstras Algorithm
Update distances
15
69
Dijkstras Algorithm
Update distances
15
4 14
10
10 20
Dijkstras Algorithm
Add to S
15
4 14
16
10
2
71
10 20
13
5
1
8
13
70
6
1
16
10
13
10
15
16
5
1
4
10
Add to S
9
10
Dijkstras Algorithm
6
1
68
8
13
16
10
16
13
5
1
10
2
7
Add to S
1
Dijkstras Algorithm
8
13
16
2
72
12
Dijkstras Algorithm
Update distances
15
4
8
10 20
73
Dijkstras Algorithm
Update distances
15
Add to S
4 14
75
Dijkstras Algorithm
Update distances
15
4 14
10 19
Dijkstras Algorithm
Add to S
15
4 14
16
10
2
77
10 19
13
5
1
8
13
76
10
16
10 19
4 14
13
10
15
16
10 19
3
6
9
5
9
10
Dijkstras Algorithm
6
1
74
8
13
10 20
16
4 14
10
16
15
13
4 14
5
1
10
Add to S
1
Dijkstras Algorithm
8
13
16
2
78
13
Dijkstras Algorithm
Update distances
15
4 14
10 18
10 18
79
4 14
10
16
1
3
15
13
10
Add to S
1
Dijkstras Algorithm
8
13
16
2
80
Dijkstras Algorithm
d(v) d(x)
x-v path length 0
82
Need to
keep current distance values for nodes in
V-S
find minimum current distance value
reduce distances when vertex moved to S
Priority Queue:
Implementations
83
Return the element with the smallest key and delete it from the data
structure
Decrease-key
Delete-min
84
14
85
86
-1
8
11
10
13
88
Dijsktras Algorithm
Dijkstra(G,w,s)
S{s}
d[s]0
while S
V do
of all edges e=(u,v) s.t. v
S and u
S select* one
with the minimum value of d[u]+w(e)
SS {v}
d[v]d[u]+w(e)
pred[v]u
Prims Algorithm:
4 12
Greedy Algorithm
87
3
6
7
start at a vertex s
add the cheapest edge adjacent to s
repeatedly add the cheapest edge that
joins the vertices explored so far to the rest
of the graph
Exactly like Dijsktras Algorithm but with a
different metric
90
15
Prims Algorithm
Prim(G,w,s)
S{s}
Kruskals Algorithm
Start with the vertices and no edges
Repeatedly add the cheapest edge that
joins two different components. i.e. that
doesnt create a cycle
while S
V do
of all edges e=(u,v) s.t. v
S and u
S select* one
with the minimum value of w(e)
SS {v}
pred[v]u
*For each vS maintain small[v]=minimum value of w(e)
over all vertices uS s.t. e=(u,v) is in of G
91
92
1
3
8
11
8
10
13
94
Prims Algorithm
Prims Algorithm
4 12
8
93
6
-1
7
4
6
9
-1
4 12
95
5
13
9
8
8
11
10
7
96
16
Kruskals Algorithm
Kruskals Algorithm
Always chooses cheapest edge connecting
two pieces of the graph that arent yet
connected
This is the cheapest edge across any cut
which has those two pieces on different
sides and doesnt split any current pieces.
97
98
11
4 12
5
9
8
13
10
-1
8
11
Proof of Lemma:
An Exchange Argument
Kruskals Algorithm
1
4 12
8
6
-1
10
13
99
Proof of Lemma
100
Proof of Lemma
u
e
101
102
17
Proof of Lemma
Proof of Lemma
w(e)w(h)
w(e)w(h)
u
e
103
Kruskals Algorithm
Implementation & Analysis
Maintaining components
105
106
104
107
Array
insert O(1), delete-min O(n), decrease-key O(1)
2
2
total O(n+n +m)=O(n )
Heap
insert, delete-min, decrease-key all O(log n)
total O(m log n)
d-Heap (d=m/n)
insert, decrease-key O(log
m/n n)
delete-min O((m/n) log
m/n n)
total O(m log
m/n n)
108
18
Chazelle
109
110
Given
a collection U of n objects {p ,,p }
1
n
Distance measure d(p ,p ) satisfying
i j
d(pi,pi)=0
d(pi,pj)>0 for ij
d(pi,pj)=d(pj,pi)
Positive integer kn
Find a k-clustering, i.e. partition of U into k clusters
C1,,Ck, such that the spacing between the
clusters is as large possible where
spacing = min{d(pi,pj): pi and pj in different
clusters}
111
112
Greedy Algorithm
113
Since they are different and cover the same set of points
there is some pair of points pi,pj such that pi,pj are in some
cluster Cr but pi, pj are in different clusters Cs and Ct
Since pi,pj Cr, pi and pj have a path between them
all of whose edges have distance at most d*
This path must cross between clusters in the C
clustering so the spacing in C is at most d*
114
19