# greedy - Greedy Algorithms Many real-world problems are...

This preview shows pages 1–6. Sign up to view the full content.

Greedy Algorithms: Many real-world problems are optimization problems in that they attempt to find an optimal solution among many possible candidate solutions. A familiar scenario is the change-making problem that we often encounter at a cash register: receiving the fewest numbers of coins to make change after paying the bill for a purchase. For example, the purchase is worth \$5.27, how many coins and what coins does a cash register return after paying a \$6 bill? The Make-Change algorithm: For a given amount (e.g. \$0.73), use as many quarters (\$0.25) as possible without exceeding the amount. Use as many dimes (\$.10) for the remainder, then use as many nickels (\$.05) as possible. Finally, use the pennies (\$.01) for the rest.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Example: To make change for the amount x = 67 (cents). Use q = x /25 = 2 quarters. The remainder = x – 25 q = 17, which we use d = 17/10 = 1 dime. Then the remainder = 17 – 10 d = 7, so we use n = 7/5 = 1 nickel. Finally, the remainder = 7 – 5 n = 2, which requires p = 2/1 = 2 pennies. The total number of coins used = q + d + n + p = 6. Note: The above algorithm is optimal in that it uses the fewest number of coins among all possible ways to make change for a given amount. (This fact can be proven formally.) However, this is dependent on the denominations of the US currency system. For example, try a system that uses denominations of 1-cent, 6-cent, and 7-cent coins, and try to make change for x = 18 cents. The greedy strategy uses 2 7-cents and 4 1-cents, for a total of 6 coins. However, the optimal solution is to use 3 6-cent coins.
A Generic Greedy Algorithm: (1) Initialize C to be the set of candidate solutions (2) Initialize a set S = the empty set (the set is to be the optimal solution we are constructing). (3) While C and S is (still) not a solution do (3.1) select x from set C using a greedy strategy (3.2) delete x from C (3.3) if { x } S is a feasible solution, then S = S { x } (i.e., add x to set S ) (4) if S is a solution then return S (5) else return failure In general, a greedy algorithm is efficient because it makes a sequence of (local) decisions and never backtracks. The solution is not always optimal, however.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The Knapsack Problem: Given n objects each have a weight w i and a value v i , and given a knapsack of total capacity W . The problem is to pack the knapsack with these objects in order to maximize the total value of those objects packed without exceeding the knapsack’s capacity. More formally, let x i denote the fraction of the object i to be included in the knapsack, 0 x i 1, for 1 i n . The problem is to find values for the x i such that Note that we may assume because otherwise, we would choose x i = 1 for each i which would be an obvious optimal solution. = = n i i i n i i i v x W w x 1 1 maximized. is and = n i i W w 1
There seem to be 3 obvious greedy strategies: (Max value) Sort the objects from the highest value to the lowest, then pick them in that order.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 29

greedy - Greedy Algorithms Many real-world problems are...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online