Download presentation
Presentation is loading. Please wait.
Published byWidyawati Tanuwidjaja Modified over 5 years ago
1
Greedy Algorithms Kevin Kauffman CS 309s
2
No decision we make now can come back to hurt us later!
What is Greedy? For each decision you make, the locally optimal choice will lead to the globally optimal solution There is some hard and fast rule that you can apply every time No decision we make now can come back to hurt us later!
3
Making Change If I ask you to make 47¢ in change, how do you go about doing it? Is there a rule we can apply for making change for arbitrary amounts of money? Does this rule still apply if we change the values and coins available (say, removing the nickel and asking for 30¢)?
4
Proving that Greedy is Correct
Greedy proofs are often tricky Often must rely on intuition during contests Think: is there any non-greedy selection that I could make that would yield a more optimal solution? Can I show that any non-greedy selection must necessarily lead to a equally or less optimal solution?
5
Task Completion What method do you use?
Suppose we have a set of tasks (perhaps class projects), and each task must be completed by a fixed time. Determine whether you can complete all tasks on time What method do you use?
6
Task Completion Rule: solve the outstanding task with the earliest deadline first How do we know this is correct?
7
Proof Say d(A) and d(B) are the first two deadlines If we solve them in the order of the deadlines, A < d(A) and A+B < d(B) Suppose we flip the problems Now A+B < d(A) and d(A) < d(B), so A+B < d(B) The constraints on d(A) are now tighter, while the constraint on d(B) remained the same!
8
Proof The best ordering of any pair is to complete the one with the earliest deadline first The only ordering which satisfies this for all consecutive pairs of tasks is completing the tasks in deadline order Didn’t NEED to prove this (proof is trivial), but its important to understand the method, since sometimes you need to see intuitively how any non greedy selection can’t make the solution worse
9
Problem Walkthrough Dennis Matveyev Programming Contest 2010: Coins (Problem A)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.