Lecture 2
Lecture 2
Lecture 2
and E. Tardos.
1
CMPT405/705 Greedy Algorithms Qianping Gu
• Given a set S = {a1 , .., an } of proposed jobs that wish to use a resource
which can serve one job at a time. Each ai has a start time si and a finish time
fi with 0 ≤ si < fi < ∞. If selected, ai takes place in time interval [si , fi ).
• Jobs ai and aj are compatible if [si , fi ) ∩ [sj , fj ) = ∅.
• Goal, Find a maximum subset of mutually compatible jobs from S .
2
CMPT405/705 Greedy Algorithms Qianping Gu
• Find an optimal solution in steps, each step optimizes a partial solution locally.
• Consider jobs in some order, select each job compatible with the selected ones.
• Possible selections:
Earliest-start-time-first, consider jobs in ascending order of si .
3
CMPT405/705 Greedy Algorithms Qianping Gu
Earliest-finish-time-first algorithm
• Intuition, select ak which leaves resource available for as many other jobs as
possible.
• Algorithm
Assume f1 ≤ .. ≤ fn . Let Sk = {ai ∈ S|si ≥ fk }.
Choose a1 ; then choose an ai with the smallest fi from S1 ;
repeat this selection, assume ak was chosen in the previous round of selection,
choose an ai from Sk , until no activity can be selected.
• Sort jobs in ascending order of finish time takes O(n log n) time, the rest steps
take O(n) time, total running time O(n log n).
4
CMPT405/705 Greedy Algorithms Qianping Gu
i 1 2 3 4 5 6 7 8 9 10 11
si 1 3 0 5 3 5 6 8 8 2 12
fi 4 5 6 7 9 9 10 11 12 14 16
• Since S8 = {a11 }, S ∗ = {a1 , a4 , a8 } ∪ {a11 } and then select a job from S11 .
5
CMPT405/705 Greedy Algorithms Qianping Gu
aj
am
6
CMPT405/705 Greedy Algorithms Qianping Gu
7
CMPT405/705 Greedy Algorithms Qianping Gu
– Prove that a greedy choice will give an optimal solution to each subproblem.
8
CMPT405/705 Greedy Algorithms Qianping Gu
• Let S = {a1 , .., an } be a set of proposed jobs that wish to use a resource
which can serve one job at a time. Each ai has deadline di and wishes a
contiguous time interval ti , before di to use the resource. For each ai , a
schedule S decides the time si at which ai starts to use the resource. The
finish time of ai is fi = si + ti .
9
CMPT405/705 Greedy Algorithms Qianping Gu
A greedy algorithm
• Order the jobs in the order a1 , .., an s.t. d1 ≤ d2 ≤ ... ≤ dn . Let t be the
earliest available time of the resource (e.g., t = 0).
= t and f1 = s1 + t1 ;
For a1 , let s1
For ai with 2 ≤ i ≤ n, si = fi−1 and fi = si + ti ;
Assign each ai to resource at time interval [si , fi );
• No idle time property: The resource serves each job at any time point from the
start of the 1st served job until the finish time of the last served one.
• No inversion property: A schedule has an inversion if there are two jobs ai and
aj s.t. dj < di and sj > si . A schedule has no inversion if si < sj for
di < dj .
10
CMPT405/705 Greedy Algorithms Qianping Gu
• Claim 2: All schedules with no idle time and no inversions have the same maximum
lateness.
Let S and S ′ be schedules with no idle time and no inversions. Then two jobs ai and aj
are in different served orders in S and S ′ only if di
= dj = d. All jobs with deadline d
are served consecutively in S and S ′ (after jobs with deadline< d and before jobs with
deadline> d. The maximum lateness of all jobs with deadline d is independent of the
orders they served.
S lateness=d
deadline<d ai aj deadline>d
lateness
d
S’ lateness=d
deadline<d aj ai deadline>d
lateness
d
11
CMPT405/705 Greedy Algorithms Qianping Gu
• Claim 3: There is an optimal schedule that has no idle time and no inversions.
Let S : aj1 , .., ajn be an optimal schedule with no idle time (by Claim 1). If S has no
inversion then the claim holds. Assume S has an inversion. Then there is pair aji and
aji+1 s.t. dji > dji+1 . Let S ′ be the schedule obtained by exchanging the ordr of aji
and aji+1 . Then S ′ has one less inversion than S . We prove l(S ′ ) ≤ l(S).
Let ljr be the lateness and fjr be the finish time of ajr in S . Let lj′ be the lateness of
r
ajr in S ′ . Then
ljr = lj′ r for every r 6= i, i + 1,
lj′ i+1 < fji+1 − dji+1 = lji+1 and
lj′ i = fji+1 − dji < fji+1 − dji+1 = lji+1 .
We get max{lj′ , lj′ i+1 } ≤ max{lji , lji+1 }. Thus, l(S ′ ) ≤ l(S).
i
Repeat the exchange process above, we get the claim.
S aj aj
i i+1
lj
i lj
dj dj fj i+1 fj
i+1 i i i+1
S’ aj aj
i+1 i
l’j
l’j i
dj dj i+1 fj fj
i+1 i i i+1
12
CMPT405/705 Greedy Algorithms Qianping Gu
• The greedy algorithm finds an optimal schedule in t(n) + O(n) time, t(n) is the time to
sort n numbers.
Proof. By Claim 3, there is an optimal schedule with no idle time and no inversion. By Claim 2,
all schedules with no idle time and on inversion have the same maximum lateness. Hence, a
schedule with no idle time and no inversion is optimal. The algorithm finds a schedule with no idle
time and no inversion, an optimal one.
It takes t(n) time to sort the jobs in ascending order of their deadlines and O(n) time compute
si and fi . The running time of the algorithm is t(n) + O(n).
13
CMPT405/705 Greedy Algorithms Qianping Gu
50
14
CMPT405/705 Greedy Algorithms Qianping Gu
• Single source shortest paths (SSSP) problem, find the shortest paths
(distances) from a source node s to all other nodes.
• All pairs shortest paths (APSP) problem, for every pair of nodes u, v , find the
shortest path (distance) from u to v .
15
CMPT405/705 Greedy Algorithms Qianping Gu
• Greedy approach: Let s be a source node, maintain a set S of nodes, for each u ∈ S ,
d(s, u) =length of a shortest path s u has been found.
– Initialize S = {s}, d(s, s) = 0;
– In each step, choose a node v 6∈ S which minimizes
˜ v) = mine=(u,v):u∈S d(s, u) + d(e);
d(s,
˜ v);
S = S ∪ {v}; d(s, v) = d(s,
– Repeat above until S = V (G).
16
CMPT405/705 Greedy Algorithms Qianping Gu
17
CMPT405/705 Greedy Algorithms Qianping Gu
18
CMPT405/705 Greedy Algorithms Qianping Gu
Theorem. [Dijkstra 1956] Dijkstra’s Algorithm finds the distance d(s, u) and the shortest path
from s to every node in G.
Proof. Let Pu be the path s, .., P [P [u]], P [u], u. We prove the loop invariant: For each node
u ∈ S , d(s, u) =length of a shortest path s u and Pu is a shortest path s u. For |S| = 1,
S = {s}, d(s, s) = 0 and Ps = s, the invariant is true. Assume the invariant holds for |S| ≥ 1.
Let v be the next node added to S and e ˜ v) = d(s, u) + d(e). Let
= (u, v) be the edge s.t. d(s,
P be any other path s v , e1 = (x, y) be the 1st edge in P with x ∈ S and y 6∈ S , and P ′ be
the subpath s x of P . Then
˜ y) ≥ d(s,
d(P ) ≥ d(P ′ ) + d(e1 ) ≥ d(s, x) + d(e1 ) ≥ d(s, ˜ v).
u e v
s
P’ x y
e1
19
CMPT405/705 Greedy Algorithms Qianping Gu
• Straightforward implementation
˜ v) = mine=(u,v),u∈S {d(s, u) + d(e)} minimal,
O(n) time to find v with d(s,
2
total O(n ) time.
• Improvements
˜ v) = mine=(u,v),u∈S {d(s, u) + d(e)} in a priority queue.
Keep d(s,
By a binary heap, O(1) time to find v and O(log n) time to adjust the heap after
˜ v
an update d(s,
′ ˜ v ′ ), d(s, v) + d(v, v ′ )}, total O(m log n) time.
) = min{d(s,
20
CMPT405/705 Greedy Algorithms Qianping Gu
Clustering
• Clustering instance: a set V of n points p1 , .., pn and a distance function
d(pi , pj ) for pi , pj ∈ V with d(pi , pi ) = 0, d(pi , pj ) ≥ 0 for i 6= j and
d(pi , pj ) = d(pj , pi ).
For subsets V1 and V2 of V , distance between V1 and V2 is defined as
d(V1 , V2 ) = minpi ∈V1 ,pj ∈V2 {d(pi , pj )}.
• k -Clustering problem
Given a clustering instance and integer k , partition V into k subsets V1 , .., Vk
s.t. mini6=j d(Vi , Vj ) is maximized. The problem can be solved by an algorithm
for the minimum spanning tree problem.
21
CMPT405/705 Greedy Algorithms Qianping Gu
22
CMPT405/705 Greedy Algorithms Qianping Gu
8 7 8 7
b c d b c d
4 2 9 4 2 9
4 4
a 11 i 14 e a 11 i 14 e
7 6 7 6
8 10 8 10
(a) g (b) g
h 1 2 f h 1 2 f
23
CMPT405/705 Greedy Algorithms Qianping Gu
8 7 8 7
b c d b c d
4 2 9 4 2 9
11 4 11 4
a i 14 e a i 14 e
7 6 7 6
8 10 8 10
(a) g (b) g
h 1 2 f h 1 2 f
8 7 8 7
b c d b c d
4 2 9 4 2 9
11 4 11 4
a i 14 e a i 14 e
7 6 7 6
8 10 8 10
(c) g (d) g
h 1 2 f h 1 2 f
8 7 8 7
b c d b c d
4 2 9 4 2 9
11 4 11 4
a i 14 e a i 14 e
7 6 7 6
8 10 8 10
(e) g (f) g
h 1 2 f h 1 2 f
8 7 8 7
b c d b c d
4 2 9 4 2 9
11 4 11 4
a i 14 e a i 14 e
7 6 7 6
8 10 8 10
(g) g (h) g
h 1 2 f h 1 2 f
24
CMPT405/705 Greedy Algorithms Qianping Gu
8 7 8 7
b c d b c d
4 2 9 4 2 9
4 4
a 11 i 14 e a 11 i 14 e
7 6 7 6
8 10 8 10
(i) (j)
h 1 g 2 f h 1 g 2 f
8 7 8 7
b c d b c d
4 2 9 4 2 9
4 4
a 11 i 14 e a 11 i 14 e
7 6 7 6
8 10 8 10
(k) (l)
h 1 g 2 f h 1 g 2 f
8 7 8 7
b c d b c d
4 2 9 4 2 9
4 4
a 11 i 14 e a 11 i 14 e
7 6 7 6
8 10 8 10
(m) (n)
h 1 g 2 f h 1 g 2 f
25
CMPT405/705 Greedy Algorithms Qianping Gu
8 7 8 7 8 7
b c d b c d b c d
4 2 9 4 2 9 4 2 9
4 4 4
a 11 i 14 e a 11 i 14 e a 11 i 14 e
7 6 7 6 7 6
8 10 8 10 8 10
(a) g (b) g (c) g
h 1 2 f h 1 2 f h 1 2 f
26
CMPT405/705 Greedy Algorithms Qianping Gu
8 7 8 7 8 7
b c d b c d b c d
4 2 9 4 2 9 4 2 9
11 4 11 4 11 4
a i 14 e a i 14 e a i 14 e
7 6 7 6 7 6
8 10 8 10 8 10
(a) g (b) g (c) g
h 1 2 f h 1 2 f h 1 2 f
8 7 8 7 8 7
b c d b c d b c d
4 2 9 4 2 9 4 2 9
4 4 4
a 11 i 14 e a 11 i 14 e a 11 i 14 e
7 6 7 6 7 6
8 10 8 10 8 10
(d) (e) (f)
h 1 g 2 f h 1 g 2 f h 1 g 2 f
8 7 8 7 8 7
b c d b c d b c d
4 2 9 4 2 9 4 2 9
4 4 4
a 11 i 14 e a 11 i 14 e a 11 i 14 e
7 6 7 6 7 6
8 10 8 10 8 10
(g) g (h) g (i) g
h 1 2 f h 1 2 f h 1 2 f
27
CMPT405/705 Greedy Algorithms Qianping Gu
28