1 / 72

Greedy algorithms

Greedy algorithms. David Kauchak cs302 Spring 2012. Administrative. Should be all caught up on grading Assignment out today (back to the normal routine). Interval scheduling.

jaguar
Download Presentation

Greedy algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Greedy algorithms David Kauchak cs302 Spring 2012

  2. Administrative • Should be all caught up on grading • Assignment out today (back to the normal routine)

  3. Interval scheduling • Given n activities A = [a1,a2, .., an] where each activity has start time si and a finish time fi. Schedule as many as possible of these activities such that they don’t conflict.

  4. Interval scheduling • Given n activities A = [a1,a2, .., an] where each activity has start time si and a finish time fi. Schedule as many as possible of these activities such that they don’t conflict. Which activities conflict?

  5. Interval scheduling Given n activities A = [a1,a2, .., an] where each activity has start time si and a finish time fi. Schedule as many as possiblesuch that they don’t conflict. Which activities conflict?

  6. Simple recursive solution Enumerate all possible solutions and find which schedules the most activities

  7. Simple recursive solution • Is it correct? • max{all possible solutions} • Running time? • O(n!)

  8. Can we do better? • Dynamic programming (next class) • O(n2) • Greedy solution – Is there a way to repeatedly make local decisions? • Key: we’d still like to end up with the optimal solution

  9. Overview of a greedy approach • Greedily pick an activity to schedule • Add that activity to the answer • Remove that activity and all conflicting activities. Call this A’. • Repeat on A’ until A’ is empty

  10. Greedy options • Select the activity that starts the earliest, i.e. argmin{s1, s2, s3, …, sn}?

  11. Greedy options • Select the activity that starts the earliest? non-optimal

  12. Greedy options • Select the shortest activity, i.e. argmin{f1-s1, f2-s2, f3-s3, …, fn-sn}

  13. Greedy options • Select the shortest activity, i.e. argmin{f1-s1, f2-s2, f3-s3, …, fn-sn} non-optimal

  14. Greedy options • Select the activity with the smallest number of conflicts

  15. Greedy options • Select the activity with the smallest number of conflicts

  16. Greedy options • Select the activity with the smallest number of conflicts

  17. Greedy options • Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, …, fn}?

  18. Greedy options • Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, …, fn}?

  19. Greedy options • Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, …, fn}?

  20. Greedy options • Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, …, fn}?

  21. Greedy options • Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, …, fn}?

  22. Greedy options • Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, …, fn}?

  23. Greedy options • Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, …, fn}?

  24. Greedy options • Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, …, fn}?

  25. Greedy options • Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, …, fn}?

  26. Greedy options • Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, …, fn}? Multiple optimal solutions

  27. Greedy options • Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, …, fn}?

  28. Greedy options • Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, …, fn}?

  29. Efficient greedy algorithm • Once you’ve identified a reasonable greedy heuristic: • Prove that it always gives the correct answer • Develop an efficient solution

  30. Is our greedy approach correct? “Stays ahead” argument: show that no matter what other solution someone provides you, the solution provided by your algorithm always “stays ahead”, in that no other choice could do better

  31. r1 r3 rk r2 … o1 o3 ok o2 Is our greedy approach correct? • “Stays ahead” argument • Let r1, r2, r3, …, rk be the solution found by our approach • Let o1, o2, o3, …, ok of another optimal solution • Show our approach “stays ahead” of any other solution

  32. r1 r3 rk r2 … o1 o3 ok o2 Stays ahead Compare first activities of each solution

  33. r1 r3 rk r2 … o1 o3 ok o2 Stays ahead finish(r1) ≤ finish(o1)

  34. Stays ahead … r3 rk r2 … o3 ok o2 We have at least as much time as any other solution to schedule the remaining 2…k tasks

  35. An efficient solution

  36. Running time? Θ(n log n) Θ(n) Better than: O(n!)O(n2) Overall: Θ(n log n)

  37. Scheduling all intervals • Given n activities, we need to schedule all activities. Goal: minimize the number of resources required.

  38. Greedy approach? The best we could ever do is the maximum number of conflicts for any time period

  39. Calculating max conflicts efficiently 3

  40. Calculating max conflicts efficiently 1

  41. Calculating max conflicts efficiently 3

  42. Calculating max conflicts efficiently 1

  43. Calculating max conflicts efficiently

  44. Calculating max conflicts

  45. Correctness? We can do no better then the max number of conflicts. This exactly counts the max number of conflicts.

  46. Runtime? O(2n log 2n + n) = O(n log n)

  47. Horn formulas • Horn formulas are a particular form of boolean logic formulas • They are one approach to allow a program to do logical reasoning • Boolean variables: represent some event • x = the murder took place in the kitchen • y = the butler is innocent • z = the colonel was asleep at 8 pm

  48. Implications • Left-hand side is an AND of any number of positive literals • Right-hand side is a single literal If the colonel was asleep at 8 pm and the butler is innocent then the murder took place in the kitchen • x = the murder took place in the kitchen • y = the butler is innocent • z = the colonel was asleep at 8 pm

  49. Implications • Left-hand side is an AND of any number of positive literals • Right-hand side is a single literal the murder took place in the kitchen • x = the murder took place in the kitchen • y = the butler is innocent • z = the colonel was asleep at 8 pm

  50. Negative clauses An OR of any number of negative literals not every one is innocent • u = the constable is innocent • t = the colonel is innocent • y = the butler is innocent

More Related