1 / 110

Greedy algorithms

Greedy algorithms. David Kauchak cs161 Summer 2009. Administrative. Thank your TAs . Kruskal’s and Prim’s. Make greedy decision about the next edge to add. Greedy algorithm structure.

hsonya
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 cs161 Summer 2009

  2. Administrative • Thank your TAs 

  3. Kruskal’s and Prim’s • Make greedy decision about the next edge to add

  4. Greedy algorithm structure • A locally optimal decision can be made which results in a subproblem that does not rely on the local decision

  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 possible of these activities such that they don’t conflict.

  6. 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?

  7. 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?

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

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

  10. Can we do better? • Dynamic programming • O(n2) • Greedy solution – Is there a way to make a local decision?

  11. 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

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

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

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

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

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

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

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

  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}?

  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}? Multiple optimal solutions

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

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

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

  32. 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

  33. 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

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

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

  36. 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

  37. An efficient solution

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

  39. Scheduling all intervals • Given n activities, we need to schedule all activities. Minimize the number of resources required.

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

  41. Calculating max conflicts efficiently 3

  42. Calculating max conflicts efficiently 1

  43. Calculating max conflicts efficiently 3

  44. Calculating max conflicts efficiently 1

  45. Calculating max conflicts efficiently

  46. Calculating max conflicts

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

  48. Runtime? • O(2n log 2n + n) = O(n log n)

  49. 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

  50. 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

More Related