csc373 week 2 greedy algorithms nisarg shah
play

CSC373 Week 2: Greedy Algorithms Nisarg Shah 373F19 - Nisarg Shah - PowerPoint PPT Presentation

CSC373 Week 2: Greedy Algorithms Nisarg Shah 373F19 - Nisarg Shah 1 Recap Divide & Conquer Master theorem Counting inversions in ( log ) Finding closest pair of points in 2 in log Fast


  1. CSC373 Week 2: Greedy Algorithms Nisarg Shah 373F19 - Nisarg Shah 1

  2. Recap • Divide & Conquer ➢ Master theorem ➢ Counting inversions in 𝑃(𝑜 log 𝑜) ➢ Finding closest pair of points in ℝ 2 in 𝑃 𝑜 log 𝑜 ➢ Fast integer multiplication in 𝑃 𝑜 log 2 3 ➢ Fast matrix multiplication in 𝑃 𝑜 log 2 7 ➢ Finding 𝑙 𝑢ℎ smallest element (in particular, median) in 𝑃(𝑜) 373F19 - Nisarg Shah 2

  3. Greedy Algorithms • Greedy (also known as myopic) algorithm outline ➢ We want to find a solution 𝑦 that maximizes some objective function 𝑔 ➢ But the space of possible solutions 𝑦 is too large ➢ The solution 𝑦 is typically composed of several parts (e.g. 𝑦 may be a set, composed of its elements) ➢ Instead of directly computing 𝑦 … o Compute it one part at a time o Select the next part “greedily” to get maximum immediate benefit (this needs to be defined carefully for each problem) o May not be optimal because there is no foresight o But sometimes this can be optimal too! 373F19 - Nisarg Shah 3

  4. Interval Scheduling • Problem ➢ Job 𝑘 starts at time 𝑡 𝑘 and finishes at time 𝑔 𝑘 ➢ Two jobs are compatible if they don’t overlap ➢ Goal: find maximum-size subset of mutually compatible jobs 373F19 - Nisarg Shah 4

  5. Interval Scheduling • Greedy template ➢ Consider jobs in some “natural” order ➢ Take each job if it’s compatible with the ones already chosen • What order? ➢ Earliest start time: ascending order of 𝑡 𝑘 ➢ Earliest finish time: ascending order of 𝑔 𝑘 ➢ Shortest interval: ascending order of 𝑔 𝑘 − 𝑡 𝑘 ➢ Fewest conflicts: ascending order of 𝑑 𝑘 , where 𝑑 𝑘 is the number of remaining jobs that conflict with 𝑘 373F19 - Nisarg Shah 5

  6. Example • Earliest start time: ascending order of 𝑡 𝑘 • Earliest finish time: ascending order of 𝑔 𝑘 • Shortest interval: ascending order of 𝑔 𝑘 − 𝑡 𝑘 • Fewest conflicts: ascending order of 𝑑 𝑘 , where 𝑑 𝑘 is the number of remaining jobs that conflict with 𝑘 373F19 - Nisarg Shah 6

  7. Interval Scheduling • Does it work? Counterexamples for earliest start time shortest interval fewest conflicts 373F19 - Nisarg Shah 7

  8. Interval Scheduling • Implementing greedy with earliest finish time (EFT) ➢ Sort jobs by finish time. Say 𝑔 1 ≤ 𝑔 2 ≤ ⋯ ≤ 𝑔 𝑜 ➢ When deciding whether job 𝑘 should be included, we need to check whether it’s compatible with all previously added jobs 𝑗 ∗ , where 𝑗 ∗ is the last added job o We only need to check if 𝑡 𝑘 ≥ 𝑔 o This is because for any jobs 𝑗 added before 𝑗 ∗ , 𝑔 𝑗 ≤ 𝑔 𝑗 ∗ o So we can simply store and maintain the finish time of the last added job ➢ Running time: 𝑃 𝑜 log 𝑜 373F19 - Nisarg Shah 8

  9. Interval Scheduling • Optimality of greedy with EFT ➢ Suppose for contradiction that greedy is not optimal ➢ Say greedy selects jobs 𝑗 1 , 𝑗 2 , … , 𝑗 𝑙 sorted by finish time ➢ Consider the optimal solution 𝑘 1 , 𝑘 2 , … , 𝑘 𝑛 (also sorted by finish time) which matches greedy for as long as possible o That is, we want 𝑘 1 = 𝑗 1 , … , 𝑘 𝑠 = 𝑗 𝑠 for greatest possible 𝑠 373F19 - Nisarg Shah 9

  10. Interval Scheduling Another standard method is induction • Optimality of greedy with EFT ➢ Both 𝑗 𝑠+1 and 𝑘 𝑠+1 were compatible with the previous selection ( 𝑗 1 = 𝑘 1 , … , 𝑗 𝑠 = 𝑘 𝑠 ) ➢ Consider the solution 𝑗 1 , 𝑗 2 , … , 𝑗 𝑠 , 𝑗 𝑠+1 , 𝑘 𝑠+2 , … , 𝑘 𝑛 o It should still be feasible (since 𝑔 𝑗 𝑠+1 ≤ 𝑔 𝑘 𝑠+1 ) o It is still optimal o And it matches with greedy for one more step (contradiction!) 373F19 - Nisarg Shah 10

  11. Interval Partitioning • Problem ➢ Job 𝑘 starts at time 𝑡 𝑘 and finishes at time 𝑔 𝑘 ➢ Two jobs are compatible if they don’t overlap ➢ Goal: group jobs into fewest partitions such that jobs in the same partition are compatible • One idea ➢ Find the maximum compatible set using the previous greedy EFT algorithm, call it one partition, recurse on the remaining jobs. ➢ Doesn’t work (check by yourselves) 373F19 - Nisarg Shah 11

  12. Interval Partitioning • Think of scheduling lectures for various courses into as few classrooms as possible • This schedule uses 4 classrooms for scheduling 10 lectures 373F19 - Nisarg Shah 12

  13. Interval Partitioning • Think of scheduling lectures for various courses into as few classrooms as possible • This schedule uses 3 classrooms for scheduling 10 lectures 373F19 - Nisarg Shah 13

  14. Interval Partitioning • Let’s go back to the greedy template! ➢ Go through lectures in some “natural” order ➢ Assign each lecture to a compatible classroom (which?), and create a new classroom if the lecture conflicts with every existing classroom • Order of lectures? ➢ Earliest start time: ascending order of 𝑡 𝑘 ➢ Earliest finish time: ascending order of 𝑔 𝑘 ➢ Shortest interval: ascending order of 𝑔 𝑘 − 𝑡 𝑘 ➢ Fewest conflicts: ascending order of 𝑑 𝑘 , where 𝑑 𝑘 is the number of remaining jobs that conflict with 𝑘 373F19 - Nisarg Shah 14

  15. Interval Partitioning • At least when you assign each lecture to an arbitrary feasible classroom, three of these heuristics do not work. • The fourth one works! (next slide) 373F19 - Nisarg Shah 15

  16. Interval Partitioning 373F19 - Nisarg Shah 16

  17. Interval Partitioning • Running time ➢ Key step: check if the next lecture can be scheduled at some classroom ➢ Store classrooms in a priority queue o key = finish time of its last lecture ➢ Is lecture 𝑘 compatible with some classroom? o Same as “Is 𝑡 𝑘 at least as large as the minimum key?” o If yes: add lecture 𝑘 to classroom 𝑙 with minimum key, and increase its key to 𝑔 𝑘 o Otherwise: create a new classroom, add lecture 𝑘 , set key to 𝑔 𝑘 ➢ 𝑃(𝑜) priority queue operations, 𝑃(𝑜 log 𝑜) time 373F19 - Nisarg Shah 17

  18. Interval Partitioning • Proof of optimality (lower bound) ➢ # classrooms needed ≥ maximum “depth” at any point o depth = number of lectures running at that time ➢ We now show that our greedy algorithm uses only these many classrooms! 373F19 - Nisarg Shah 18

  19. Interval Partitioning • Proof of optimality (upper bound) ➢ Let 𝑒 = # classrooms used by greedy ➢ Classroom 𝑒 was opened because there was a schedule 𝑘 which was incompatible with some lectures already scheduled in each of 𝑒 − 1 other classrooms ➢ All these 𝑒 lectures end after 𝑡 𝑘 ➢ Since we sorted by start time , they all start at/before 𝑡 𝑘 ➢ So at time 𝑡 𝑘 , we have 𝑒 overlapping lectures ➢ Hence, depth ≥ 𝑒 ➢ So all schedules use ≥ 𝑒 classrooms. ➢ QED! 373F19 - Nisarg Shah 19

  20. Interval Graphs • Interval scheduling and interval partitioning can be seen as graph problems • Input ➢ Graph 𝐻 = (𝑊, 𝐹) ➢ Vertices 𝑊 = jobs/lectures ➢ Edge 𝑗, 𝑘 ∈ 𝐹 if jobs 𝑗 and 𝑘 are incompatible • Interval scheduling = maximum independent set (MIS) • Interval partitioning = graph colouring 373F19 - Nisarg Shah 20

  21. Interval Graphs • MIS and graph colouring are NP-hard for general graphs • But they’re efficiently solvable for interval graphs ➢ Interval graphs = graphs which can be obtained from incompatibility of intervals ➢ In fact, this holds even when we are not given an interval representation of the graph • Can we extend this result further? ➢ Yes! Chordal graphs o Every cycle with 4 or more vertices has a chord 373F19 - Nisarg Shah 21

  22. Minimizing Lateness • Problem ➢ We have a single machine ➢ Each job 𝑘 requires 𝑢 𝑘 units of time and is due by time 𝑒 𝑘 ➢ If it’s scheduled to start at 𝑡 𝑘 , it will finish at 𝑔 𝑘 = 𝑡 𝑘 + 𝑢 𝑘 ➢ Lateness: ℓ 𝑘 = max 0, 𝑔 𝑘 − 𝑒 𝑘 ➢ Goal: minimize the maximum lateness, 𝑀 = max ℓ 𝑘 𝑘 • Contrast with interval scheduling ➢ We can decide the start time ➢ All jobs must be scheduled on a single machine 373F19 - Nisarg Shah 22

  23. Minimizing Lateness • Example Input An example schedule 373F19 - Nisarg Shah 23

  24. Minimizing Lateness • Let’s go back to greedy template ➢ Consider jobs one-by- one in some “natural” order ➢ Schedule jobs in this order (nothing special to do here, since we have to schedule all jobs and there is only one machine available) • Natural orders? ➢ Shortest processing time first: ascending order of processing time 𝑢 𝑘 ➢ Earliest deadline first: ascending order of due time 𝑒 𝑘 ➢ Smallest slack first: ascending order of 𝑒 𝑘 − 𝑢 𝑘 373F19 - Nisarg Shah 24

  25. Minimizing Lateness • Counterexamples ➢ Shortest processing time first o Ascending order of processing time 𝑢 𝑘 ➢ Smallest slack first o Ascending order of 𝑒 𝑘 − 𝑢 𝑘 373F19 - Nisarg Shah 25

  26. Minimizing Lateness • By now, you should know what’s coming… • We’ll prove that earliest deadline first works! 373F19 - Nisarg Shah 26

  27. Minimizing Lateness • Observation 1 ➢ There is an optimal schedule with no idle time 373F19 - Nisarg Shah 27

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend