Greedy Algorithms What is Greedy Algorithm l Optimization

  • Slides: 19
Download presentation
Greedy Algorithms

Greedy Algorithms

What is “Greedy Algorithm” l Optimization problem usually goes through a sequence of steps

What is “Greedy Algorithm” l Optimization problem usually goes through a sequence of steps l A greedy algorithm makes the choice looks best at that moment ( hoping to find the global optimal through local optimal choices )

Example 1: Coins l There are 7 kinds of coins: – l $0. 1,

Example 1: Coins l There are 7 kinds of coins: – l $0. 1, $0. 2, $0. 5, $1, $2, $5, $10 What is the minimum number of coins needed to pay $18 exactly?

Example 2: Stamps l There are 16 kinds of stamps – l $0. 10,

Example 2: Stamps l There are 16 kinds of stamps – l $0. 10, $0. 20, $0. 50, $1. 00, $1. 40, $1. 80, $1. 90, $2. 00, $2. 40, $2. 50, $3. 00, $5. 00, $10. 00, $13. 00, $20. 00, $50. 00 What is the minimum number stamps needed to pay $26 exactly ?

Greedy Algorithms l Advantages – – l Easy to code Efficient Disadvantages – –

Greedy Algorithms l Advantages – – l Easy to code Efficient Disadvantages – – Do not always yield optimal solution Not easy to prove or disprove

Greedy Algorithms l Examples – – Dijkstra’s shortest path algorithm Prim/Kruskal’s MST algorithms

Greedy Algorithms l Examples – – Dijkstra’s shortest path algorithm Prim/Kruskal’s MST algorithms

Example 3: Fractional Knapsack l There are N objects, each with weight wi and

Example 3: Fractional Knapsack l There are N objects, each with weight wi and value vi. Any amount ( including fractions ) of each item can be taken provided that the total weight does not exceed W. l How much of each item should we take in order to maximize the total value?

Optimal substructure l An optimal solution to the original problem contains optimal solutions to

Optimal substructure l An optimal solution to the original problem contains optimal solutions to the subproblems

Greedy-choice Property l We can arrive the globally optimal solution by making a locally

Greedy-choice Property l We can arrive the globally optimal solution by making a locally optimal choice, and then solving the sub-problems

Example 4: 0 -1 Knapsack problem l The situation is the same, exactly that

Example 4: 0 -1 Knapsack problem l The situation is the same, exactly that each item can only be taken or left behind l Greedy solvable? Why?

Steps l 1. Show there is always an optimal solution which makes greedy choice

Steps l 1. Show there is always an optimal solution which makes greedy choice l 2. Show that if we combine an optimal solution to the sub-problem with the greedy choice, we can get a optimal solution to the original problem

Example 5: Activity l There are n activities, starting at time si and finishing

Example 5: Activity l There are n activities, starting at time si and finishing at fi. l Choose the maximum number of activities so that they do not overlap each other.

Example 5: Activity l Greedy Solvable? l If so, how? – – – By

Example 5: Activity l Greedy Solvable? l If so, how? – – – By first start time? By shortest duration? By fewest overlap? By first end time? By. . .

Example 5: Activity l Must there be an optimal solution contain the greedy choice?

Example 5: Activity l Must there be an optimal solution contain the greedy choice? l Does optimal substructure exist?

Example 6: Advertisement l There are n intervals, [ai, bi] l Choosing the minimum

Example 6: Advertisement l There are n intervals, [ai, bi] l Choosing the minimum number of points {pj} so that each interval contains at least one of the points

Example 6: Advertisement l Must there be an optimal solution contain the greedy choice?

Example 6: Advertisement l Must there be an optimal solution contain the greedy choice? l Does optimal substructure exist?

Example 7: Bridge l http: //www. plastelina. net/games/game 3. html l Greedy Solvable?

Example 7: Bridge l http: //www. plastelina. net/games/game 3. html l Greedy Solvable?

Example 7: Bridge l Spilt the cases into: – – – 3 or less

Example 7: Bridge l Spilt the cases into: – – – 3 or less people 4 people More than 4 people

Example 8: Egyptian fraction l An Egyptian fraction is a sum of distinct unit

Example 8: Egyptian fraction l An Egyptian fraction is a sum of distinct unit fraction l Given a fraction and express it as an Egyptian fraction