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 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 1
2/8/19 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 s j and finishes at f j • Two jobs are compatible if they don't overlap • Goal : find maximum subset of mutually compatible jobs a • Every job is worth equal b money. • To earn the most money à c schedule the most jobs d e f g h Time 0 1 2 3 4 5 6 7 8 9 10 11 Feb 8, 2019 CSCI211 - Sprenkle 4 2
2/8/19 Interval Scheduling • Earliest start time. Consider jobs in ascending order of start time s j Ø Utilize CPU as soon as possible • Earliest finish time. Consider jobs in ascending order of finish time f j Ø Resource becomes free ASAP Ø Maximize time left for other requests • Shortest interval. Consider jobs in ascending order of interval length f j – s j • Fewest conflicts. For each job, count the number of conflicting jobs c j . Schedule in ascending order of conflicts c j Can we “break” any of these? i.e., prove they’re not optimal? Feb 8, 2019 CSCI211 - Sprenkle 5 Counterexamples to Optimality of Various Job Orders Not optimal when … breaks earliest start time breaks shortest length breaks fewest conflicts Feb 8, 2019 CSCI211 - Sprenkle 6 3
2/8/19 Interval Scheduling: Greedy Algorithm • Consider jobs in increasing order of finish time • Take each job provided it’s compatible with the ones already taken Sort jobs by finish times finish times so that f 1 £ f 2 £ ... £ f n jobs selected G = {} for for j = 1 to to n if if job j compatible with G G = G È {j} return return G Feb 8, 2019 CSCI211 - Sprenkle 7 Interval Scheduling B C A E D F G H Time 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 Feb 8, 2019 CSCI211 - Sprenkle 8 4
2/8/19 Interval Scheduling B C A E D F G H Time 0 1 2 3 4 5 6 7 8 9 10 11 B 0 1 2 3 4 5 6 7 8 9 10 11 Feb 8, 2019 CSCI211 - Sprenkle 9 Interval Scheduling B C A E D F G H Time 0 1 2 3 4 5 6 7 8 9 10 11 B C 0 1 2 3 4 5 6 7 8 9 10 11 Feb 8, 2019 CSCI211 - Sprenkle 10 5
2/8/19 Interval Scheduling B C A E D F G H Time 0 1 2 3 4 5 6 7 8 9 10 11 B A 0 1 2 3 4 5 6 7 8 9 10 11 Feb 8, 2019 CSCI211 - Sprenkle 11 Interval Scheduling B C A E D F G H Time 0 1 2 3 4 5 6 7 8 9 10 11 B E 0 1 2 3 4 5 6 7 8 9 10 11 Feb 8, 2019 CSCI211 - Sprenkle 12 6
2/8/19 Interval Scheduling B C A E D F G H Time 0 1 2 3 4 5 6 7 8 9 10 11 B D E 0 1 2 3 4 5 6 7 8 9 10 11 Feb 8, 2019 CSCI211 - Sprenkle 13 Interval Scheduling B C A E D F G H Time 0 1 2 3 4 5 6 7 8 9 10 11 B E F 0 1 2 3 4 5 6 7 8 9 10 11 Feb 8, 2019 CSCI211 - Sprenkle 14 7
2/8/19 Interval Scheduling B C A E D F G H Time 0 1 2 3 4 5 6 7 8 9 10 11 B E G 0 1 2 3 4 5 6 7 8 9 10 11 Feb 8, 2019 CSCI211 - Sprenkle 15 Interval Scheduling B C A E D F G H Time 0 1 2 3 4 5 6 7 8 9 10 11 B E H 0 1 2 3 4 5 6 7 8 9 10 11 Feb 8, 2019 CSCI211 - Sprenkle 16 8
2/8/19 Interval Scheduling: Greedy Algorithm • Consider jobs in increasing order of finish time • Take each job provided it’s compatible with the ones already taken Sort jobs by finish times so that f 1 £ f 2 £ ... £ f n jobs selected G = {} for j = 1 to for to n if if job j compatible with G G = G È {j} return return G Runtime of algorithm? • Where/what are the costs? Feb 8, 2019 CSCI211 - Sprenkle 17 Interval Scheduling: Greedy Algorithm • Consider jobs in increasing order of finish time. • Take each job provided it’s compatible with the ones already taken. O(n logn) Sort jobs by finish times so that f 1 £ f 2 £ ... £ f n jobs selected G = {} for j = 1 to to n for O(n) if job j compatible with G O( 1 ) if G = G È {j} return G return • Implementation. O(n log n) Ø Remember job j* that was added last to G Ø Job j is compatible with G if s j ³ f j * Feb 8, 2019 CSCI211 - Sprenkle 18 9
2/8/19 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 CSCI211 - Sprenkle 19 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 a 1 , . . . , a k be the first k measures of greedy algorithm and o 1 , . . . , o 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 a r ≥ o r or a r ≤ o r Ø 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 à Make sure maps back to measure of optimality Feb 8, 2019 CSCI211 - Sprenkle 20 10
2/8/19 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 a 1 , a 2 , ..., a k denote set of jobs selected by greedy ( k jobs) Ø Let o 1 , o 2 , ..., o m denote set of jobs in optimal solution ( m jobs) Ø Both sets ordered by finish time for comparison ordering ➨ Want to show that k = m Greedy: a 1 a 2 a r o 1 o 2 o r OPT: Feb 8, 2019 CSCI211 - Sprenkle 21 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 a 1 , a 2 , ..., a k denote set of jobs selected by greedy ( k jobs) Ø Let o 1 , o 2 , ..., o m denote set of jobs in optimal solution ( m jobs) Ø Both sets ordered by finish time for comparison ordering ➨ Want to show that k = m Greedy: a 1 a 2 a r OPT: o 1 o 2 o r What can we say about a 1 and o 1 ? f(a 1 ) ≤ f(o 1 ) Feb 8, 2019 CSCI211 - Sprenkle 22 11
2/8/19 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(a 1 ) <= f(o 1 ) Ø 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(a r ) <= f(o r ) Prove for r+1 Greedy: a 1 a 2 a r OPT: o 1 o 2 o r Feb 8, 2019 CSCI211 - Sprenkle 23 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(a 1 ) <= f(o 1 ) Ø 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(a r ) <= f(o r ) Job a r+1 finishes after o r+1 Greedy: a 1 a 2 a r a r+1 o r+1 . . . OPT: o 1 o 2 o r How Greedy stays ahead why not replace job a r+1 with job o r+1 ? Feb 8, 2019 CSCI211 - Sprenkle 24 12
2/8/19 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(a r ) ≤ f(o r ) Ø Since m > k, there is a request o k+1 in Optimal Why wouldn't Greedy have o k+1 ? Greedy: a 1 a 2 a r a k o k o k+1 OPT: o 1 o 2 o r 25 25 Feb 8, 2019 CSCI211 - Sprenkle 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(a r ) ≤ f(o r ) Ø Since m > k, there is a request o k+1 in Optimal • Starts after o k ends à after a k ends Ø So, Greedy could also add o k+1 • Contradiction because now Greedy has another job Why wouldn't Greedy have o k+1 ? Greedy: a 1 a 2 a r a k o k o k+1 OPT: o 1 o 2 o r 26 26 Feb 8, 2019 CSCI211 - Sprenkle 13
Recommend
More recommend