Chapter 4. Greedy Algorithms Interval scheduling Greedy overview - - PowerPoint PPT Presentation

chapter 4 greedy algorithms
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Chapter 4. Greedy Algorithms

Interval scheduling Greedy overview Shortest paths Minimum spanning trees

slide-2
SLIDE 2

What is a greedy algorithm?

Hard to describe, but I know it when I see it!

slide-3
SLIDE 3

Interval Scheduling

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

slide-4
SLIDE 4

Greedy Template

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?

slide-5
SLIDE 5

Interval Scheduling: Greedy Solution

Idea 1: Earliest start time. Consider jobs in ascending

  • rder of start time sj.

Time

1 2 3 4 5 6 7 8 9 10 11

f g h e a b c d

a, g

slide-6
SLIDE 6

Interval Scheduling: Greedy Solution

Time

1 2 3 4 5 6 7 8 9 10 11

f g h e a b c d

c, h

Idea 2: Shortest interval. Consider jobs in ascending

  • rder of interval length fj - sj.
slide-7
SLIDE 7

Interval Scheduling: Greedy Solution

Idea 3: Fewest conflicts. For each job, count the number

  • f conflicting jobs cj. Schedule in ascending order of

conflicts cj.

Time

1 2 3 4 5 6 7 8 9 10 11

f g h e a b c d

slide-8
SLIDE 8

Interval Scheduling: Greedy Solution

Idea 3: Fewest conflicts. For each job, count the number

  • f conflicting jobs cj. Schedule in ascending order of

conflicts cj.

Time

1 2 3 4 5 6 7 8 9 10 11

f g h e a b c d

h, b, e

5 3 4 6 5 5 4 2

slide-9
SLIDE 9

Interval Scheduling: Greedy Solution

Idea 4: Earliest finish time. Consider jobs in ascending

  • rder of finish time fj.

Time

1 2 3 4 5 6 7 8 9 10 11

f g h e a b c d

b, e, h

slide-10
SLIDE 10

Earliest Finish Time - Optimal Solution

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

slide-11
SLIDE 11

Greedy Overview

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

slide-12
SLIDE 12

Interval Partitioning

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

slide-13
SLIDE 13

Interval Partitioning Lower Bound

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

slide-14
SLIDE 14

Interval Partitioning Lower Bound

Example: Depth of schedule below = 3 Question: Does there always exist a schedule equal to depth

  • f intervals?

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

slide-15
SLIDE 15

Idea

Number classrooms 1, 2, 3, ... Sort intervals in some order: for each interval, assign it to first available classroom What order?

slide-16
SLIDE 16

Interval Partitioning: Greedy Solution

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 }

Complexity?

slide-17
SLIDE 17

Scheduling to Minimize Lateness

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

slide-18
SLIDE 18

Scheduling Example

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

Attempt 1: Sort by t

Job Processing time Deadline

slide-19
SLIDE 19

Scheduling Example: Smallest Slack time first

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

slide-20
SLIDE 20

Scheduling Example: Earliest Deadline First

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

slide-21
SLIDE 21

Minimizing Lateness: Analysis

Claim: scheduling jobs by their deadline is

  • ptimal

Let’ s establish some basic facts for the proof...

slide-22
SLIDE 22

Minimizing Lateness: No Idle Time

  • Observation. There exists an optimal schedule

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

  • Observation. The greedy schedule has no idle

time.

slide-23
SLIDE 23

Minimizing Lateness: Proof Approach

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

slide-24
SLIDE 24

Minimizing Lateness: Inversions

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

slide-25
SLIDE 25

Minimizing Lateness: Inversions

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

slide-26
SLIDE 26

Minimizing Lateness: Proof!

Theorem: the greedy solution is optimal Proof on board

slide-27
SLIDE 27

Proof Strategies for Greedy Algorithms

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

  • ptimal solution to the one found by the greedy

algorithm(*) without hurting its quality.

(*) Or one just like it