Notes are here

advertisement
Greedy Algorithms
Kevin Kauffman
CS 309s
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!
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¢)?
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?
Task Completion
• 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?
Task Completion
• Rule: solve the outstanding task with the
earliest deadline first
• How do we know this is correct?
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!
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
Problem Walkthrough
• Dennis Matveyev Programming Contest 2010:
Coins (Problem A)
Download