Chapter 4. Greedy Algorithms
Interval scheduling Greedy overview Shortest paths Minimum spanning trees
Chapter 4. Greedy Algorithms Interval scheduling Greedy overview - - PowerPoint PPT Presentation
Chapter 4. Greedy Algorithms Interval scheduling Greedy overview Shortest paths Minimum spanning trees What is a greedy algorithm? Hard to describe, but I know it when I see it! Interval Scheduling Schedule n jobs: j th job has start time s
Interval scheduling Greedy overview Shortest paths Minimum spanning trees
Schedule n jobs: jth job has start time sj, finish time fj. Two jobs compatible if they don't overlap. Goal: find maximum size subset of mutually compatible jobs.
Time
1 2 3 4 5 6 7 8 9 10 11
f g h e a b c d
A ← {} while (there are jobs compatible with A) pick “best” compatible job j A = A ∪ {j} } return A Greedy: pick j and never look back What rule to use?
Idea 1: Earliest start time. Consider jobs in ascending
Time
1 2 3 4 5 6 7 8 9 10 11
f g h e a b c d
Time
1 2 3 4 5 6 7 8 9 10 11
f g h e a b c d
Idea 2: Shortest interval. Consider jobs in ascending
Idea 3: Fewest conflicts. For each job, count the number
conflicts cj.
Time
1 2 3 4 5 6 7 8 9 10 11
f g h e a b c d
Idea 3: Fewest conflicts. For each job, count the number
conflicts cj.
Time
1 2 3 4 5 6 7 8 9 10 11
f g h e a b c d
5 3 4 6 5 5 4 2
Idea 4: Earliest finish time. Consider jobs in ascending
Time
1 2 3 4 5 6 7 8 9 10 11
f g h e a b c d
Sort jobs by finish times so that f1 ≤ f2 ≤ ... ≤ fn. A ← {} for j = 1 to n { if (job j compatible with A) A = A ∪ {j} } return A Proof and running time on board
Build up solution by adding items one at a time Choose next item by simple heuristic, never remove items Prove that the result is optimal! Simple algorithm -> hard part is proving it correct Running time usually n log n or worse: need to sort items
Lecture j starts at sj and finishes at fj. Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room.
Time 9 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
The depth of a set of intervals is the maximum number that contain any point in time-line. Key observation. Number of classrooms needed ≥ depth.
Time 9 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
Example: Depth of schedule below = 3 Question: Does there always exist a schedule equal to depth
Time 9 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 Time 9 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
Number classrooms 1, 2, 3, ... Sort intervals in some order: for each interval, assign it to first available classroom What order?
Sort intervals by starting time so that s1 ≤ s2 ≤ ... ≤ sn. k ← 0 / / Number of classrooms for j = 1 to n { if (lecture j is compatible with some classroom i ≤ k) schedule lecture j in classroom i else allocate a new classroom k + 1 schedule lecture j in classroom k + 1 k ← k + 1 }
Single computer processes one job at a time. Jobs i = 1,2,...,n: Processing time ti Deadline di Start time si -> finish time fi = si + ti. Lateness: li = max { 0, fi - di }. Goal: schedule start times of all jobs to minimize maximum lateness L = max li.
1 ≤ i ≤ n
di 6 ti 3 1 8 2 2 9 1 3 9 4 4 14 3 5 15 2 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
d5 = 14 d2 = 8 d6 = 15 d1 = 6 d4 = 9 d3 = 9
lateness = 2 lateness = 6
Max lateness: 6
Job Processing time Deadline
di 6 ti 3 1 8 2 2 9 1 3 9 4 4 14 3 5 15 2 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
d3 = 9
lateness = 1
Max lateness: 1
slacki 3 6 8 5 11 13
d1 = 6 d4 = 9 d2 = 8 d5 = 14 d6 = 15
di 6 ti 3 1 8 2 2 9 1 3 9 4 4 14 3 5 15 2 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
d4 = 9 d3 = 9
lateness = 1
Max lateness: 1
d1 = 6 d2 = 8 d5 = 14 d6 = 15
Claim: scheduling jobs by their deadline is
Let’ s establish some basic facts for the proof...
with no idle time.
1 2 3 4 5 6
d = 4 d = 6
7 8 9 10 11
d = 12
1 2 3 4 5 6
d = 4 d = 6
7 8 9 10 11
d = 12
time.
Idea: start with an optimal solution with no idle time, and gradually transform it into the greedy solution (*), without increasing the maximum lateness Discuss and outline on board
An inversion in schedule S is a pair of jobs i and j such that i is scheduled before j but dj < di.
di 6 ti 3 1 8 2 2 9 1 3 9 4 4 14 3 5 15 2 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
d4 = 9 d3 = 9 d1 = 6 d2 = 8 d5 = 14 d6 = 15
Goal: modify optimal solution to eliminate inversions to match greedy solution. But: this might not give exactly the greedy solution. Lemma A: all solutions with no idle time and no inversions have same maximum lateness Proof on board
Theorem: the greedy solution is optimal Proof on board
Greedy algorithm stays ahead. Show that after each step of the greedy algorithm, its solution is at least as good as an optimal solution. Exchange argument. Gradually transform an
algorithm(*) without hurting its quality.
(*) Or one just like it