Presentation is loading. Please wait.

Presentation is loading. Please wait.

Greedy Algorithms.

Similar presentations


Presentation on theme: "Greedy Algorithms."— Presentation transcript:

1 Greedy Algorithms

2 The two key components Optimal Sub-structure Greedy Property
You solve the problem by solving a sub-problem optimally Greedy Property Using the choice that seems best at the moment leads to the optimal result This is tougher to show! Greedy problems are the “easy” ones for finding optimal results They are less common than the “tough” ones, unfortunately…

3 Classical problem: Making change
Given Coins: 1 cent, 5, 10, 25, 50, and 100 cents, how can you get a certain total in the fewest coins? Straightforward: choose most of the largest coin, then next largest, etc. For these denominations, this is optimal, but this is NOT true for general coin values. Example: 1, 10, 15, 20 cent denominations 25 cent goal Greedy strategy would give Optimal result would be 10+15 The point: you need to ensure that you have a situation where greedy applies!

4 Classical problem: Scheduling jobs
Every job has a start and a finish time Can work on only one job at a time Want to schedule as many jobs as possible

5 Possible greedy choices
Take jobs in order of earliest start time Take jobs in order of earliest finish time Take jobs in order of length of job (shortest job first) Take jobs in order of fewest conflicts with other jobs

6 Earliest Start Shortest Job Fewest Conflicts

7 Possible greedy choices
Take jobs in order of earliest start time Take jobs in order of earliest finish time Take jobs in order of length of job (shortest job first) Take jobs in order of fewest conflicts with other jobs Only the earliest finish time leads to an optimal solution! If there were any other optimal solution not including that, you could replace one of those choices with the one that ends earlier, and still have an optimal solution. Need to think about what would happen if there WERE a better solution from the non-greedy choice – show there’s a conflict or that the greedy choice is just as good

8 Room Scheduling Assume classes with start and end times (like tasks, above) How many rooms are needed to teach all of them? Same as the depth – the number of tasks overlapping at once Can use a greedy approach: Just assign to first available room. Sort start and end times together. Then linearly process Each time a start is passed, increase by 1, each time an end is passed, decrease by 1 Keep track of how many rooms max were needed This is also the basis for the sweep-and-prune approach for collision detection in graphics!

9 Minimizing maximum lateness
Tasks of particular length, and deadline time. Want to minimize the maximum lateness Can always do task with earliest deadline first Ensures that there is no “idle” time – the best solution must also have no idle time. Reversing any two from that order will lead to a worse result So, that order can’t change.

10 More Greedy solutions Will see several examples later on – e.g. in some graph applications Balancing Stations (see book) Grouping most w/least See book for some more specific examples


Download ppt "Greedy Algorithms."

Similar presentations


Ads by Google