Jump to content

Greedy algorithm

From Simple English Wikipedia, the free encyclopedia
Revision as of 14:16, 10 December 2012 by Eptalon (talk | changes)
Greedy algorithms determine the minimum number of coins to give while making change. These are the steps a human would take to emulate a greedy algorithm to represent 36 cents using only coins with values {1, 5, 10, 20}. The coin of the highest value, less than the remaining change owed, is the local optimum. Note that in general, dynamic programming or linear programming is required to find the optimal solution. However, most currency systems, including the Euro (pictured) and US Dollar, are special cases where the greedy strategy also finds an optimal solution.

A greedy algorithm is an algorithm that uses many iterations to compute the result. Such algorithms assume that this result will be obtained by selecting the best result at the current iteration. In other words: the global optimum is obtained by selecting the local optimum at the current time. Examples of such algorithms:

There are some problems where greedy algorithms do not produce the best possible solution. In such cases, they often produce the worst possible one. Again look at the coin-changing example above, and imagine that there are coins for 25 cent, 10 cent and 4 cent. Now imagine that the sum of 41 cent needs to be changed. A greedy algorithm would pick 25 cent, 10 cent, and 4 cent - there are 2 cents left, which cannot be changed. A human might see that it is possible to use 25 cent, and four times 4 cent, to make 41 cent.