1 / 18

Lecture 7 Greedy Algorithms

Lecture 7 Greedy Algorithms. Basic Algorithm Design Techniques. Analyzing running time. Divide and conquer Dynamic Programming Greedy Common Theme: To solve a large, complicated problem, break it into many smaller sub-problems. Design algorithm. Proof of correctness. Greedy Algorithm.

tarabowman
Download Presentation

Lecture 7 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. Lecture 7 Greedy Algorithms

  2. Basic Algorithm Design Techniques Analyzing running time • Divide and conquer • Dynamic Programming • Greedy • Common Theme: To solve a large, complicated problem, break it into many smaller sub-problems. Design algorithm Proof of correctness

  3. Greedy Algorithm • If a problem requires to make a sequence of decisions, for the first decision, make the “best” choice given the current situation. • (This automatically reduces the problem to a smaller sub-problem which requires making one fewer decisions)

  4. Warm-up: Walking in Manhattan “Walk in a direction that reduces distance to the destination.”

  5. Greedy does not always work Driving in New York: one-way streets, traffic…

  6. Design and Analysis • Designing a Greedy Algorithm: • 1. Break the problem into a sequence of decisions. • 2. Identify a rule for the “best” option. • Analyzing a Greedy Algorithm: • Important! Often fails if you cannot find a proof. • Technique: Proof by contradiction.Assume there is a better solution, show that it is actually not better than what the algorithm did.

  7. Example 1: Interval Scheduling • There are n meeting requests, meeting i takes time(si, ti) • Cannot schedule two meeting together if their intervals overlap. • Goal: Schedule as many meetings as possible. • Example: Meetings (1,3), (2, 4), (4, 5), (4, 6), (6, 8) • Solution: 3 meetings ((1, 3), (4, 5), (6, 8))

  8. Designing the Algorithm • Think of the problem as making a sequence of decisions (similar to Dynamic Programming)- schedule one meeting at each iteration • Think of all the options for the first decision, use a simple criteria to select the “best” • Question: Which meeting should we schedule first?A. The one that started earliestB. The one that ends earliestC. The one that takes least amount of time

  9. Algorithm • Always try to schedule the meeting that ends earliest • Why is this algorithm correct? • Intuition: We will never regret our first choice because it is “better” than all other choices. • But how do we prove this formally? Schedule Sort the meetings according to end time For i = 1 to n If meeting i does not conflict with previous meetings Schedule meeting i

  10. Proof of correctness in pictures I have scheduled the most number of possible meetings! ……

  11. I have scheduled the most number of possible meetings! …… …… No you are wrong! There is a way to schedule more meetings.

  12. Can you show me how to do that? Here are the first two meetings I scheduled. …… …… Yes, those are fine. I made the same decisions.

  13. and this is my third meeting i3. …… i3 …… j3 Wait, you can’t do that. You need to schedule meeting j3

  14. My meeting i3 ends before your meeting j3, so if you swap j3 to i3, you are still OK right? …… i3 …… i3 j3 I guess you are right. Fine I will do that.

  15. But isn’t this the same thing? You could have swapped to my 5th meeting. …… …… OK you got me on the 3rd meeting. But you made another mistake on the 5th meeting!

  16. May rounds later……

  17. …… …… OK, OK, you can keep all of your meetings, but I could still schedule a meeting jk+1!

  18. That doesn’t make any sense. If there is such a meeting, I must consider it after ik, and I would have scheduled that meeting! …… …… OK, you win.

More Related