Objectives Greedy Algorithms Interval scheduling Interval - - PDF document

objectives
SMART_READER_LITE
LIVE PREVIEW

Objectives Greedy Algorithms Interval scheduling Interval - - PDF document

2/8/19 Objectives Greedy Algorithms Interval scheduling Interval partitioning Feb 8, 2019 CSCI211 - Sprenkle 1 Review What is a greedy algorithm? What is the template for a greedy algorithm? What problem were we trying to


slide-1
SLIDE 1

2/8/19 1

Objectives

  • Greedy Algorithms

Ø Interval scheduling Ø Interval partitioning

Feb 8, 2019 1 CSCI211 - Sprenkle

Review

  • What is a greedy algorithm?
  • What is the template for a greedy algorithm?
  • What problem were we trying to solve?
  • What orders did we come up with?

Ø What approaches didn’t work?

  • How did they prove they didn’t work?

Ø Can you “break” any of the other orders?

  • Find a counterexample to finding the optimal (not

necessarily based on our example)

Feb 8, 2019 CSCI211 - Sprenkle 2

slide-2
SLIDE 2

2/8/19 2

Review: Greedy Algorithms

  • Template
  • 1. Consider candidates in some order
  • Decision: What order is best?
  • 2. Take each candidate provided it’s compatible with

the ones already taken

  • At each step, take as much as you can get

Ø Feasible – satisfy problem’s constraints Ø Locally optimal – best local choice among available feasible choices Ø Irrevocable – after decided, no going back

Feb 8, 2019 CSCI211 - Sprenkle 3

Review: Interval Scheduling

  • Job j starts at sj and finishes at fj
  • Two jobs are compatible if they don't overlap
  • Goal: find maximum subset of mutually compatible

jobs

Feb 8, 2019 CSCI211 - Sprenkle 4

Time

1 2 3 4 5 6 7 8 9 10 11

f g h e a b c d

  • Every job is worth equal

money.

  • To earn the most money à

schedule the most jobs

slide-3
SLIDE 3

2/8/19 3

Interval Scheduling

  • Earliest start time. Consider jobs in ascending order of

start time sj

Ø Utilize CPU as soon as possible

  • Earliest finish time. Consider jobs in ascending order of

finish time fj

Ø Resource becomes free ASAP Ø Maximize time left for other requests

  • Shortest interval. Consider jobs in ascending order of

interval length fj – sj

  • Fewest conflicts. For each job, count the number of

conflicting jobs cj. Schedule in ascending order of conflicts cj

Feb 8, 2019 CSCI211 - Sprenkle 5

Can we “break” any of these? i.e., prove they’re not optimal?

Counterexamples to Optimality of Various Job Orders

Feb 8, 2019 CSCI211 - Sprenkle 6

breaks earliest start time breaks shortest length breaks fewest conflicts

Not optimal when …

slide-4
SLIDE 4

2/8/19 4

Interval Scheduling: Greedy Algorithm

  • Consider jobs in increasing order of finish time
  • Take each job provided it’s compatible with the ones

already taken

Feb 8, 2019 CSCI211 - Sprenkle 7

Sort jobs by finish times finish times so that f1 £ f2 £ ... £ fn G = {} for for j = 1 to to n if if job j compatible with G G = G È {j} return return G jobs selected

Interval Scheduling

Time A C F B D G E 1 2 3 4 5 6 7 8 9 10 11 H 1 2 3 4 5 6 7 8 9 10 11

Feb 8, 2019 8 CSCI211 - Sprenkle

slide-5
SLIDE 5

2/8/19 5

Interval Scheduling

1 2 3 4 5 6 7 8 9 10 11 B Time A C F B D G E 1 2 3 4 5 6 7 8 9 10 11 H

Feb 8, 2019 9 CSCI211 - Sprenkle

Interval Scheduling

1 2 3 4 5 6 7 8 9 10 11 B C Time A C F B D G E 1 2 3 4 5 6 7 8 9 10 11 H

Feb 8, 2019 10 CSCI211 - Sprenkle

slide-6
SLIDE 6

2/8/19 6

Interval Scheduling

1 2 3 4 5 6 7 8 9 10 11 B A Time A C F B D G E 1 2 3 4 5 6 7 8 9 10 11 H

Feb 8, 2019 11 CSCI211 - Sprenkle

Interval Scheduling

1 2 3 4 5 6 7 8 9 10 11 B E Time A C F B D G E 1 2 3 4 5 6 7 8 9 10 11 H

Feb 8, 2019 12 CSCI211 - Sprenkle

slide-7
SLIDE 7

2/8/19 7

Interval Scheduling

1 2 3 4 5 6 7 8 9 10 11 B E D Time A C F B D G E 1 2 3 4 5 6 7 8 9 10 11 H

Feb 8, 2019 13 CSCI211 - Sprenkle

Interval Scheduling

1 2 3 4 5 6 7 8 9 10 11 B E F Time A C F B D G E 1 2 3 4 5 6 7 8 9 10 11 H

Feb 8, 2019 14 CSCI211 - Sprenkle

slide-8
SLIDE 8

2/8/19 8

Interval Scheduling

1 2 3 4 5 6 7 8 9 10 11 B E G Time A C F B D G E 1 2 3 4 5 6 7 8 9 10 11 H

Feb 8, 2019 15 CSCI211 - Sprenkle

Interval Scheduling

1 2 3 4 5 6 7 8 9 10 11 B E H Time A C F B D G E 1 2 3 4 5 6 7 8 9 10 11 H

Feb 8, 2019 16 CSCI211 - Sprenkle

slide-9
SLIDE 9

2/8/19 9

Interval Scheduling: Greedy Algorithm

  • Consider jobs in increasing order of finish time
  • Take each job provided it’s compatible with the
  • nes already taken

Feb 8, 2019 CSCI211 - Sprenkle 17

Sort jobs by finish times so that f1 £ f2 £ ... £ fn G = {} for for j = 1 to to n if if job j compatible with G G = G È {j} return return G jobs selected

Runtime of algorithm?

  • Where/what are the costs?

Interval Scheduling: Greedy Algorithm

  • Consider jobs in increasing order of finish time.
  • Take each job provided it’s compatible with the
  • nes already taken.
  • Implementation. O(n log n)

Ø Remember job j* that was added last to G Ø Job j is compatible with G if sj ³ fj*

Feb 8, 2019 CSCI211 - Sprenkle 18

Sort jobs by finish times so that f1 £ f2 £ ... £ fn G = {} for for j = 1 to to n if if job j compatible with G G = G È {j} return return G jobs selected

O(1)

O(n) O(n logn)

slide-10
SLIDE 10

2/8/19 10

Analyzing Interval Scheduling

  • Correctness: Know that the intervals are

compatible

Ø Handled by the if statement

  • But is it optimal?

Ø What does it mean to be optimal? Ø Recall our goal for maximization

Feb 8, 2019 19 CSCI211 - Sprenkle

Greedy Stays Ahead Proofs

  • 1. Define your solutions

Ø Describe the form of your greedy solution (A) and of some other solution (possibly the optimal solution, O)

  • 2. Find a measure

Ø Find a measure by which greedy stays ahead of the optimal solution

  • Ex: Let a1, . . . , ak be the first k measures of greedy algorithm and o1 , . . . ,
  • m be the first m measures of optimal solution (sometimes m = k )
  • 3. Prove greedy stays ahead

Ø Show that greedy’s partial solutions constructed are always just as good as the optimal solution’s initial segments based on the measure

  • Ex: for all indices r ≤ min(k,m), prove by induction that ar ≥ or or ar ≤ or

Ø Use the greedy algorithm to help you argue the inductive step

  • 4. Prove optimality

Ø Prove that since greedy stays ahead of the other solution with respect to the measure, then the greedy solution is optimal

Feb 8, 2019 CSCI211 - Sprenkle 20

à Make sure maps back to measure of optimality

slide-11
SLIDE 11

2/8/19 11

Interval Scheduling: Optimality Analysis

  • Theorem. Greedy algorithm is optimal, i.e., schedules the most

jobs possible

  • Pf. (by contradiction)

Ø Assume greedy is not optimal Ø Let a1, a2, ..., ak denote set of jobs selected by greedy (k jobs) Ø Let o1, o2, ..., om denote set of jobs in optimal solution (m jobs) Ø Both sets ordered by finish time for comparison ordering ➨ Want to show that k = m

Feb 8, 2019 CSCI211 - Sprenkle 21

  • 1
  • 2
  • r

a1 a2 ar

Greedy: OPT:

Interval Scheduling: Optimality Analysis

  • Theorem. Greedy algorithm is optimal, i.e., schedules the most

jobs possible

  • Pf. (by contradiction)

Ø Assume greedy is not optimal Ø Let a1, a2, ..., ak denote set of jobs selected by greedy (k jobs) Ø Let o1, o2, ..., om denote set of jobs in optimal solution (m jobs) Ø Both sets ordered by finish time for comparison ordering ➨ Want to show that k = m

Feb 8, 2019 CSCI211 - Sprenkle 22

  • 1
  • 2
  • r

a1 a2 ar

Greedy: OPT:

What can we say about a1 and o1? f(a1) ≤ f(o1)

slide-12
SLIDE 12

2/8/19 12

Interval Scheduling: Optimality Analysis

  • Theorem. Greedy algorithm is optimal

Ø i.e., schedules the most jobs possible

  • Pf. (by contradiction)

Ø Since we picked the first job to have the first finishing time, we know that f(a1) <= f(o1)

Ø Want to show that Greedy “stays ahead”

  • Each interval finishes at least as soon as Optimal’s

Ø Induction hypothesis: for all indices r <= k, f(ar) <= f(or)

Feb 8, 2019 CSCI211 - Sprenkle 23

  • 1
  • 2
  • r

a1 a2 ar

Greedy: OPT:

Prove for r+1

Interval Scheduling: Analysis

  • Theorem. Greedy algorithm is optimal

Ø i.e., schedules the most jobs possible

  • Pf. (by contradiction)

Ø Since we picked the first job to have the first finishing time, we know that f(a1) <= f(o1)

Ø Want to show that Greedy “stays ahead”

  • Each interval finishes at least as soon as Optimal’s

Ø Induction hypothesis: for all indices r <= k, f(ar) <= f(or)

Feb 8, 2019 CSCI211 - Sprenkle 24

  • 1
  • 2
  • r

a1 a2 ar ar+1

. . . Greedy: OPT:

  • r+1

why not replace job ar+1 with job or+1?

Job ar+1 finishes after or+1

How Greedy stays ahead

slide-13
SLIDE 13

2/8/19 13

25

Interval Scheduling: Analysis

  • Theorem. Greedy algorithm is optimal.

Ø i.e., schedules the most jobs possible

  • Pf. (by contradiction)

Ø Assume Greedy is not optimal (i.e., m > k)

  • Optimal solution has more jobs than Greedy

Ø We already showed that for all indices r ≤ k, f(ar) ≤ f(or) Ø Since m > k, there is a request ok+1 in Optimal

Feb 8, 2019 CSCI211 - Sprenkle 25

  • k+1
  • k

Why wouldn't Greedy have ok+1?

  • 1
  • 2
  • r

a1 a2 ar ak

Greedy: OPT:

26

Interval Scheduling: Analysis

  • Theorem. Greedy algorithm is optimal.

Ø i.e., schedules the most jobs possible

  • Pf. (by contradiction)

Ø Assume Greedy is not optimal (i.e., m > k) Ø We already showed that for all indices r ≤ k, f(ar) ≤ f(or) Ø Since m > k, there is a request ok+1 in Optimal

  • Starts after ok ends à after akends

Ø So, Greedy could also add ok+1

  • Contradiction because now Greedy has another job

Feb 8, 2019 CSCI211 - Sprenkle 26

  • k+1
  • k

Why wouldn't Greedy have ok+1?

  • 1
  • 2
  • r

a1 a2 ar ak

Greedy: OPT:

slide-14
SLIDE 14

2/8/19 14

Problem Assumptions

  • All requests were known to scheduling algorithm

Ø Online algorithms: make decisions without knowledge of future input

  • Each job was worth the same amount

Ø What if jobs had different values?

  • E.g., scaled with size
  • Single resource requested

Ø Rejected requests that didn’t fit

Feb 8, 2019 CSCI211 - Sprenkle 27

INTERVAL PARTITIONING

Feb 8, 2019 CSCI211 - Sprenkle 28

slide-15
SLIDE 15

2/8/19 15

Interval Partitioning

  • Lecture j starts at sjand 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.

  • Ex: 10 lectures in 4 classrooms

Feb 8, 2019 CSCI211 - Sprenkle 29

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

What are our constraints? Can we use fewer rooms?

Interval Partitioning

  • Lecture j starts at sjand 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.

  • Alternative schedule uses only 3 classrooms

Feb 8, 2019 CSCI211 - Sprenkle 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-16
SLIDE 16

2/8/19 16

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

a, b, c all contain 9:30

Interval Partitioning: Lower Bound on Optimal Solution

  • Def. The depth of a set of open intervals is the

maximum number that contain any given time.

  • Key observation. # of classrooms needed ³ depth.
  • Ex: Depth of schedule below = 3 Þ schedule

below is optimal.

Feb 8, 2019 31 CSCI211 - Sprenkle

Does there always exist a schedule equal to depth of intervals?

Interval Partitioning Discussion

  • Does there always exist a schedule equal to depth of

intervals?

  • Can we make decisions locally to get a global
  • ptimum?

Ø Or are there long-range obstacles that require more resources?

Feb 8, 2019 CSCI211 - Sprenkle 32

slide-17
SLIDE 17

2/8/19 17

Interval Partitioning: Greedy Algorithm

  • Consider lectures in increasing order of start

time: assign lecture to any compatible classroom

Feb 8, 2019 CSCI211 - Sprenkle 33

Sort intervals by starting time so that s Sort intervals by starting time so that s1 £ s2 £ ... ... £ sn d = 0 for for j = 1 = 1 to to n if if lecture j is compatible with some classroom k lecture j is compatible with some classroom k schedule lecture schedule lecture j in classroom in classroom k else else allocate a new classroom allocate a new classroom d + 1 + 1 schedule lecture schedule lecture j in classroom in classroom d + 1 + 1 d = = d + 1 + 1 number of allocated classrooms

Analyze algorithm

Sort intervals by starting time so that s Sort intervals by starting time so that s1 £ s2 £ ... ... £ sn d = 0 for for j = 1 = 1 to to n if if (lecture (lecture j is compatible with some classroom is compatible with some classroom k) schedule lecture schedule lecture j in classroom in classroom k else else allocate a new classroom allocate a new classroom d + 1 + 1 schedule lecture schedule lecture j in classroom in classroom d + 1 + 1 d = = d + 1 + 1

Interval Partitioning: Greedy Algorithm

  • Consider lectures in increasing order of start time: assign

lecture to any compatible classroom

  • Implementation: O(n log n)

Ø For each classroom k, maintain the finish time of the last job added. Ø Keep the classrooms in a priority queue by last job finish time.

Feb 8, 2019 CSCI211 - Sprenkle 34

number of allocated classrooms

slide-18
SLIDE 18

2/8/19 18

Exam 1 – due next Friday at 5 p.m.

  • Open

Ø Your brain Ø Your notes, wiki Ø Handouts Ø My posted slides, course web site Ø Sakai forum for our class (posted solutions) Ø Me (more limited than with problem sets)

  • Closed – everything else
  • Start early! No extensions

Feb 8, 2019 CSCI211 - Sprenkle 35

Next Week

  • No class on Wednesday

Ø Work on exam

  • No wiki
  • Office Hours

Ø M: 2:30-5 p.m. Ø W: 9:45 – 11:30 a.m. (class time), 2:30-5 p.m. Ø R: 2:30-5 p.m. Ø And by appointment

  • Try to rotate – limit of 10 minutes

Feb 8, 2019 CSCI211 - Sprenkle 36