Compsci 100, Fall 2009, Recitation November 13

Greedy Algorithms

Each of the APTs below uses some kind of greedy approach in solving it. A greedy algorithm is one in which a locally optimal or greedy solution leads to a globally optimal result. One canonical example is change-making in the U.S. To give change with the minimal number of coins choose the largest denomination/coin that can be used and repeat.

For example, to give change for $0.72, choose a quarter, that leaves $0.47. Choose a quarter again, that leaves $0.22. Choose a dime twice, then two pennies. This is a total of 5 coins, and that's the fewest coins possible to give change for $0.72.

The greedy approach doesn't work if you're missing coins: for example, to give change for $0.32 if you're out of nickels. A greedy approach would choose a quarter (maximal/largest coin) and seven pennies for eight total coins. But three dimes and two pennies makes change with five coins.