Notes on Greedy Algorithms

March 23, 2004

At first. Idea: When we have a choice to make. it’s always safe to make the greedy choice. Therefore. 1. Develop a recursive greedy algorithm 6. Typically. In the Activity Selection problem a greedy algorithm approach does yield an optimal solution. it looks like dynamic programming. • leaving just one subproblem. Prove that at any stage of recursion. Greedy Strategy The choice that seems best at the moment is the one we go with. we streamline these steps. 4. 5. 3. Used for opitmization. Convert it to an iterative algorithm. Greedy algorithms don’t always yield an optimal solution. one of the optimal choices is the greedy choice. Show that all but one of the subproblems resulting from the greedy choice are empty. Develop a recursive solution. Develop the substructure with an eye toward • makeing the greedy choice. But sometimes they do. make the one that looks best right now.Notes on Greedy Algorithms Similar to dynamic programming. 2. Typical streamlined steps: 2 . Make a locally optimal choice in hope of getting a globally optimal solution. Determine the optimal substructure.

Dynamic programming: • Make a choice at each step. • Solve top-down. but two key ingredients are 1. Can get efficiency gians from greedy-choice propery. Cast the optimization problem as one in which we make a choice and are left with one subproblem to solve. • Or. use a priority queue. Prove that there’s always an optimal solution that makes the greedy choice. Show that greedy choice and optimals solution yield optimal solution to the problem. Greedy: • Make a choice at each step. • Else. Typically show the greeedy-choice property by: • Look at globally optimal solution. • Solve bottom-up. 3. modify it to include the greedy choice. Greedy-choice property A globally optimal solution can be arrived at by making a locally optimal (greedy) choice. • Choice depends on knowing optimal solution to subproblems. yielding another solution that’s just as good. Solve subprobelms first. • If it includes the greedy choice.1. No general way to tell if a greedy algorithm is optimal. if dynamic data. 2. greedy-choice property and 2. • Make the choice before solving the subproblems. so that the greedy choice is always safe. done. • Preprocess to put it into greedy order. optimal substructure. 3 .

• But the fractional knapsack problem has the greedy-choice property. but can take fraction of an item. Fractional knapsack problem: • Like the 0-1 knapsack problem. weighs wi pounds. • Both have optimal substructure. W ) load ← 0 i←1 while load < W and i ≤ n do if wi ≤ W − load then take all of item i else take (W − load)/wi of item i add what was taken to load i←i+1 4 . dynamic programming The knapsack problem is a good example of the difference.Optimal substructure Just show that optimal solution to subproblem and greedy choice yield optimal solution to problem. Let vi /wi ≥ vi+1 /wi+1 for all i. FRACTIONAL-KNAPSACK(v. 0-1 knapsack problem: • n items. Greedy vs. • Have to either take an item or not take it—can’t take part of it. rank items by value/weight: vi /wi . • Item i is worth $vi . To solve the fractional problem. • Find a most valuable subset of items with total weight ≤ W . and the 0-1 knapsack problem does not. w.

• value = 220.Time: O(nlgn) to sort. i vi wi vi /wi 1 60 10 6 2 100 20 5 3 120 30 4 W = 50. weight = 50. • value = 160. Have 20 pounds of capacity left over. which lowers the average value per pound of the items taken. Greedy doesn’t work for the 0-1 knapsack problem. No leftover capacity. Greedy solution: • Take items 1 and 2. Optimal solution: • Take items 2 and 3. What You Should Know about Greedy Algorithms for Test: • Be able to walk through the GREEDY-ACTIVITY-SELECTOR algorithm (page 378) • Be able to explaing how the GREEDY-ACTIVITY-SELECTOR algorithm is making a greedy choice • Be able to tell the similarities and differences between dynamic programming and greedy algorithms • Understand what the knapsack problems are and know why one of the can be sloved using a greedy approach and why one of them cannot 5 . Might get empty space. O (n) thereafter. weight = 30.

Sign up to vote on this title
UsefulNot useful