Greedy Algorithms
What is “Greedy Algorithm” Optimization problem usually goes through a sequence of steps A greedy algorithm makes the choice looks best at that moment ( hoping to find the global optimal through local optimal choices )
Example 1: Coins There are 7 kinds of coins: – $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 There are 16 kinds of stamps – $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 Advantages – Easy to code – Efficient Disadvantages – Do not always yield optimal solution – Not easy to prove or disprove
Greedy Algorithms Examples – Dijkstra’s shortest path algorithm – Prim/Kruskal’s MST algorithms
Example 3: Fractional Knapsack There are N objects, each with weight w i and value v i. Any amount ( including fractions ) of each item can be taken provided that the total weight does not exceed W. How much of each item should we take in order to maximize the total value?
Optimal substructure An optimal solution to the original problem contains optimal solutions to the sub- problems
Greedy-choice Property 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 The situation is the same, exactly that each item can only be taken or left behind Greedy solvable? Why?
Steps 1. Show the there is always an optimal solution which makes greedy choice 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 There are n activities, starting at time s i and finishing at f i. Choose the maximum number of activities so that they do not overlap each other.
Example 5: Activity Greedy Solvable? If so, how? – By first start time? – By shortest duration? – By fewest overlap? – By first end time? – By...
Example 5: Activity Must there be an optimal solution contain the greedy choice? Does optimal substructure exist?
Example 6: Advertisement There are n intervals, [a i, b i ] Choosing the minimum number of points {p j } so that each interval contains at least one of the points
Example 6: Advertisement Must there be an optimal solution contain the greedy choice? Does optimal substructure exist?
Example 7: Bridge Greedy Solvable?
Example 7: Bridge Spilt the cases into: – 3 or less people – 4 people – More than 4 people
Example 8: Egyptian fraction An Egyptian fraction is a sum of distinct unit fraction Given a fraction and express it as an Egyptian fraction