Lecture 9: Greedy AlgorithmsLecture 9: Greedy Algorithms version of September 28b, 2016 A greedy...
Transcript of Lecture 9: Greedy AlgorithmsLecture 9: Greedy Algorithms version of September 28b, 2016 A greedy...
Lecture 9: Greedy Algorithms
version of September 28b, 2016
A greedy algorithm always makes the choice that looks best at
the moment and adds it to the current partial solution.
Greedy algorithms donβt always yield optimal solutions, but when
they do, theyβre usually the simplest and most efficient
algorithms available.
Interval Scheduling
Interval scheduling.
Job π starts at π π and finishes at ππ.
Two jobs are compatible if they don't overlap.
Goal: find maximum size subset of mutually compatible jobs.
2
Time0 1 2 3 4 5 6 7 8 9 10 11
f
g
h
e
a
b
c
d
Interval Scheduling
Interval scheduling.
Job π starts at π π and finishes at ππ.
Two jobs are compatible if they don't overlap.
Goal: find maximum size subset of mutually compatible jobs.
3
Time0 1 2 3 4 5 6 7 8 9 10 11
f
g
h
e
a
b
c
d
{a,g} is NOT a maximum-size subset.
Interval Scheduling
Interval scheduling.
Job π starts at π π and finishes at ππ.
Two jobs are compatible if they don't overlap.
Goal: find maximum size subset of mutually compatible jobs.
4
Time0 1 2 3 4 5 6 7 8 9 10 11
f
g
h
e
a
b
c
d
{b,e,h} is a maximum-size subset.
Interval Scheduling: Greedy Algorithms
Greedy template. Consider jobs in some order. Take a job provided it's
compatible with the ones already taken.
[Earliest start time] Consider jobs in increasing order of start time
π π.
[Earliest finish time] Consider jobs in increasing order of finish
time ππ.
[Shortest interval] Consider jobs in increasing order of interval
length ππ β π π.
[Fewest conflicts] For each job, count the number of conflicting
jobs ππ. Schedule in ascending order of conflicts ππ.
5
Interval Scheduling: Greedy Algorithms
Greedy template. Consider jobs in some order. Take a job provided it's
compatible with the ones already taken.
6
cba de breaks earliest start time
a bc
breaks shortest interval
cba dehj
gik
fbreaks fewest conflicts
Chooses {e} instead of {a,b,c,d}
Chooses {c} instead of {a,b}
Chooses {f} which forces choosing {a,f,d} instead of {a,b,c,d}
Interval Scheduling
Greedy algorithm. Consider jobs in increasing order of finish time.
Take each job provided it's compatible with the ones already taken
7
Time0 1 2 3 4 5 6 7 8 9 10 11
6
7
8
4
3
1
2
5
1
4
8
Interval Scheduling: Greedy Algorithm
Greedy algorithm. Consider jobs in increasing order of finish time.
Take each job provided it's compatible with the ones already taken.
Running time: Ξ(π logπ).
Remember the finish time of the last job added to π΄.
Job π is compatible with π΄ if π π πππ π‘.
Remember: Correctness (optimality) of greedy algorithms is usually not
obvious. Need to prove!
8
Sort jobs by finish times so that π1 π2 β¦ πππ΄ β β , πππ π‘ β 0for π β 1 to π
if π π β₯ πππ π‘ then π΄ β π΄ βͺ {π}, πππ π‘ β ππreturn π΄
Interval Scheduling: Correctness
Theorem. Greedy algorithm is optimal.
Proof.
Assume greedy is different from OPT. Let's see whatβs different.
Let π1, π2, β¦ ππ denote the set of jobs selected by greedy.
Let π1, π2, β¦ ππ denote set of jobs in the optimal solution.
Find largest possible value of π such that π1 = π1, π2 = π2, β¦ , ππ = ππ
9
π1 π2 ππ
π1 π1 ππ ππ+1
. . .
Greedy:
OPT: ππ+1
In OPT, we replace job ππ+1with job ππ+1 , keeping the remainder of OPT the same. => OPT still has the same number of jobs, so it remains optimal.
This has created a new optimal solution that shares its first r+1 jobs with Greedy.
By definition of Greedy, job ππ+1 finishes before ππ+1
ππ+1
Interval Scheduling: Correctness
Theorem. Greedy algorithm is optimal.
Proof.
Assume greedy is different from OPT. Let's see whatβs different.
Let π1, π2, β¦ ππ denote the set of jobs selected by greedy.
Let π1, π2, β¦ ππ denote set of jobs in the optimal solution with
π1 = π1, π2 = π2, β¦ , ππ = ππ for the largest possible value of π.
Do this replacement repeatedly until OPT is the same as greedy.
β Important: Since cost remains the same, final solution weβve
created, which is Greedy, is optimal! 10
π1 π2 ππ
π1 π1 ππ ππ+1
. . .
Greedy:
OPT:
must still be compatible
job ππ+1 finishes before ππ+1
The Fractional Knapsack Problem
Input: A set of π items, where item π has weight π€π and value π£π, and a
knapsack with capacity π.
Goal: Find 0 β€ π₯1, β¦ , π₯π β€ 1 such that π=1π π₯ππ€π β€ π and π=1
π π₯ππ£π is
maximized.
There are two different versions of this problem:
The π₯π βs must be 0 or 1: The 0/1 knapsack problem.
The π₯π βs can take fractional values: The fractional knapsack problem11
Optimal 0/1 Optimal Fractional
The Greedy Algorithm for Fractional Knapsack
Idea:
Sort all items by value-per-pound
For each item, take as much as possible
Running time: Ξ(π log π)
Note: This algorithm cannot solve the 0/1 version optimally (why).
12
Sort items so that π£1
π€1>
π£2
π€2> β― >
π£π
π€ππ€ β πfor π β 1 to π
if π€π β€ π€ then
π₯π β 1π€ β π€ βπ€π
else
π₯π β π€/π€πreturn
return
Greedy Algorithm: Correctness
Theorem: The greedy algorithm is optimal.
Proof: We will assume that π=1π π€π β₯ π. Otherwise the algorithm is
trivially optimal.
Let the greedy solution be πΊ = π₯1, π₯2, β¦ , π₯π , 0, β¦ , 0
Note: All π₯π βs must be equal to 1, except possibly for π = π.
Consider any optimal solution π = π¦1, π¦2, β¦ , π¦π Note: Both πΊ and π must fully pack the knapsack.
Look at the first item π where the two solutions differ.
That is
πΊ = π₯1, π₯2, β¦ , π₯πβ1, π₯π , β¦ , π₯π , 0, β¦ , 0
π = π₯1, π₯2, β¦ , π₯πβ1, π¦π , β¦β¦β¦β¦β¦ , π¦π By definition of greedy , π₯π > π¦π Let π₯ = π₯π β π¦π
13
Greedy Algorithm: Correctness (continued)
Recall π₯ = π₯π β π¦ππΊ = π₯1, π₯2, β¦ , π₯πβ1, π₯π , β¦ , π₯π , 0, β¦ , 0 π = π₯1, π₯2, β¦ , π₯πβ1, π¦π , β¦β¦β¦β¦β¦ , π¦π
We will modify π as follows:
Set π¦π β π₯π and
remove π₯π€π units of total weight from items π + 1 to item π
This is always doable because, in π, the total weight of items π to
π is the same as that in πΊ
After the modification:
The total value cannot decrease, since all the subsequent items
have lesser or equal value-per-pound.
Since π is already an optimal solution, its value cannot increase.
So πβs value must stay the (optimal) same.
By repeating this process, we will eventually convert π into πΊ, without
changing the total value of the selection. Therefore πΊ is also optimal.
14
Interval Partitioning
Interval partitioning.
Lecture π starts at π π and finishes at ππ.
Goal: find the minimum number of classrooms to schedule all
lectures so that no two occur at the same time in the same room.
Ex: This schedule uses 4 classrooms to schedule 10 lectures.
15
Time9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30
h
c
b
a
e
d g
f i
j
3 3:30 4 4:30
Interval Partitioning
Interval partitioning.
Lecture π starts at π π and finishes at ππ.
Goal: find the minimum number of classrooms to schedule all
lectures so that no two occur at the same time in the same room.
Ex: This schedule uses only 3.
16
Time9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30
h
c
a e
f
g i
j
3 3:30 4 4:30
d
b
Interval Partitioning: Greedy Algorithm
Greedy algorithm. Consider lectures in increasing order of start time:
assign lecture to any compatible classroom.
17
Sort intervals by starting time so that π 1 π 2 β¦ π π.π β 0 // # classrooms used so far
for π β 1 to πif lecture π is compatible with some classroom π then
schedule lecture π in classroom πelse
allocate a new classroom π + 1schedule lecture π in classroom π + 1π β π + 1
Interval Partitioning
Interval partitioning.
Lecture π starts at π π and finishes at ππ.
Goal: find the minimum number of classrooms to schedule all lectures so
that no two occur at the same time in the same room.
ALG: Sort by start time. Insert in order, opening new classroom when needed
18
Time9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30
8
1
3
4
7
6
9
10
3 3:30 4 4:30
5
2
1
2
3
4
5
6
7
8
9
10
Interval Partitioning: Lower Bound on Optimal Solution
Def. The depth of a set of open intervals is the maximum number that
contain any time instance.
Key observation. Number of classrooms needed depth.
Ex: Depth of schedule below = 3 this schedule is optimal.
We will show: The # classrooms used by the greedy algorithm = depth.
19
Time9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30
h
c
a e
f
g i
j
3 3:30 4 4:30
d
b
Interval Partitioning: Correctness
Theorem. Greedy algorithm is optimal.
Pf.
Let π = number of classrooms opened by greedy algorithm .
Classroom π is opened because we needed to schedule a lecture, say π,
that is incompatible with all π β 1 other classrooms.
Since we sorted by start time, all these incompatibilities are caused by
lectures that all start no later than π π and finish later than than π π.
Thus, we have π lectures all overlapping at time π π + for some > 0;
the d-1 incompatible ones and lecture π.
depth β₯ π.
Thus, Since every algorithm uses at least depth classrooms
Greedy is optimal.20
Interval Partitioning: Running Time
Running time: Ξ(π log π)
To implement line (*) the algorithm maintains, for each classroom, the
finishing time of the last item placed in the classroom. It then
compares π π to those finishing times. If π π β₯ one of those finishing
times, it places lecture j in the associated classroom
A Brute-force implementation of line (*) takes π(π) time
β π(π2) in total
Observation: If π is not compatible with the classroom with the earliest
finish time, then π is not compatible with any other classroom 21
Sort intervals by starting time so that π 1 π 2 β¦ π π.π β 0 // # classrooms used so far
for π β 1 to πif lecture π is compatible with some classroom π then (*)
schedule lecture π in classroom π (**)
else
allocate a new classroom π + 1schedule lecture π in classroom π + 1 (***)
π β π + 1
Interval Partitioning: Running Time
Running time: Ξ(π log π)
To implement line (*) we can keep the classrooms in a min priority queue using
the finishing times of the last class in the room as key
To check whether there is a compatible classroom we do an extract-min to find
the minimum finishing time in the priority queue.
If (**) is implemented then just add the new finishing time π π to p. queue
If (***) is implemented then re-insert that minimum finishing time back into the
p. queue AND insert the new finishing time π π into the p. queue
22
Sort intervals by starting time so that π 1 π 2 β¦ π π.π β 0 // # classrooms used so far
for π β 1 to πif lecture π is compatible with some classroom π then (*)
schedule lecture π in classroom π (**)
else
allocate a new classroom π + 1schedule lecture π in classroom π + 1 (***)
π β π + 1
π(logπ)
π(logπ)
π(logπ)