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 Can extend this to figuring out how many rooms are needed to schedule events.

8 Example (last programming lab): Dragon of Loowater
N heads of dragons, M knights Each dragon head and each knight has a value, only knights with greater value than the head can defeat it Knights can defeat one head only Match knights with dragon heads to minimize total knight height Greedy solution: Choose the shortest knight needed Sort the heads and the knights For smallest head, pick smallest knight possible, then repeat until done

9 More Greedy solutions Will see several examples later on – e.g. in some graph applications Balancing Stations (see book) Grouping most w/least Watering Grass (see book) Minimizing Lateness Optimal Caching


Download ppt "Greedy Algorithms."

Similar presentations


Ads by Google