Vous êtes sur la page 1sur 4

Greedy algorithm

2. A selection function, which chooses the best candidate to be added to the solution

3620=16

20

1610=6

20 10

65=1

20 10

11=0

20 10

3. A feasibility function, that is used to determine if a


candidate can be used to contribute to a solution

4. An objective function, which assigns a value to a solution, or a partial solution, and

5. A solution function, which will indicate when we


have discovered a complete solution

Greedy algorithms determine 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 the change-making problem requires dynamic programming or integer programming to nd an optimal solution; However, most currency systems, including the Euro and US Dollar,
are special cases where the greedy strategy does nd an optimal
solution.)

Greedy algorithms produce good solutions on some


mathematical problems, but not on others. Most problems for which they work, will have two properties:

Greedy choice property We can make whatever choice


seems best at the moment and then solve the subproblems that arise later. The choice made by a
greedy algorithm may depend on choices made so
far but not on future choices or all the solutions to the
subproblem. It iteratively makes one greedy choice
after another, reducing each given problem into a
smaller one. In other words, a greedy algorithm
never reconsiders its choices. This is the main difference from dynamic programming, which is exhaustive and is guaranteed to nd the solution. After
every stage, dynamic programming makes decisions
based on all the decisions made in the previous stage,
and may reconsider the previous stages algorithmic
path to solution.

A greedy algorithm is an algorithm that follows the


problem solving heuristic of making the locally optimal
choice at each stage[1] with the hope of nding a global
optimum. In many problems, a greedy strategy does not
in general produce an optimal solution, but nonetheless a
greedy heuristic may yield locally optimal solutions that
approximate a global optimal solution in a reasonable
time.

For example, a greedy strategy for the traveling salesman


problem (which is of a high computational complexity) is
the following heuristic: At each stage visit an unvisited
city nearest to the current city. This heuristic need not
nd a best solution but terminates in a reasonable number of steps; nding an optimal solution typically requires
unreasonably many steps. In mathematical optimization,
greedy algorithms solve combinatorial problems having Optimal substructure A problem exhibits optimal
substructure if an optimal solution to the problem
the properties of matroids.
contains optimal solutions to the sub-problems.[2]

Specics
1.1 Cases of failure

In general, greedy algorithms have ve components:

Examples on how a greedy algorithm may fail to achieve


the optimal solution.

1. A candidate set, from which a solution is created


1

4 EXAMPLES

2 Types
M

Greedy algorithms can be characterized as being 'short


sighted', and as 'non-recoverable'. They are ideal only for
problems which have 'optimal substructure'. Despite this,
for many simple problems (e.g. giving change), the best
suited algorithms are greedy algorithms. It is important,
however, to note that the greedy algorithm can be used as
a selection algorithm to prioritize options within a search,
or branch and bound algorithm. There are a few variam
tions to the greedy algorithm:
Pure greedy algorithms
Orthogonal greedy algorithms
Relaxed greedy algorithms

A
Starting at A, a greedy algorithm will nd the local maximum at m, oblivious of the global maximum at M.

3 Applications
Greedy algorithms mostly (but not always) fail to nd the
globally optimal solution, because they usually do not operate exhaustively on all the data. They can make commitments to certain choices too early which prevent them
from nding the best overall solution later. For example,
all known greedy coloring algorithms for the graph coloring problem and all other NP-complete problems do not
consistently nd optimum solutions. Nevertheless, they
are useful because they are quick to think up and often
give good approximations to the optimum.

With a goal of reaching the largest-sum, at each step,


the greedy algorithm will choose what appears to be the
optimal immediate choice, so it will choose 12 instead of
3 at the second step, and will not reach the best solution,
which contains 99.
For many other problems, greedy algorithms fail to produce the optimal solution, and may even produce the
unique worst possible solution. One example is the
traveling salesman problem mentioned above: for each
number of cities, there is an assignment of distances between the cities for which the nearest neighbor heuristic
produces the unique worst possible tour.[3]
Imagine the coin example with only 25-cent, 10-cent, and
4-cent coins. The greedy algorithm would not be able
to make change for 41 cents, since after committing to
use one 25-cent coin and one 10-cent coin it would be
impossible to use 4-cent coins for the balance of 6 cents,
whereas a person or a more sophisticated algorithm could
make change for 41 cents with one 25-cent coin and four
4-cent coins.

If a greedy algorithm can be proven to yield the global


optimum for a given problem class, it typically becomes
the method of choice because it is faster than other optimization methods like dynamic programming. Examples
of such greedy algorithms are Kruskals algorithm and
Prims algorithm for nding minimum spanning trees,
and the algorithm for nding optimum Human trees.
The theory of matroids, and the more general theory of
greedoids, provide whole classes of such algorithms.
Greedy algorithms appear in network routing as well. Using greedy routing, a message is forwarded to the neighboring node which is closest to the destination. The
notion of a nodes location (and hence closeness) may
be determined by its physical location, as in geographic
routing used by ad hoc networks. Location may also be
an entirely articial construct as in small world routing
and distributed hash table.

4 Examples
The activity selection problem is characteristic to
this class of problems, where the goal is to pick the
maximum number of activities that do not clash with
each other.

3
In the Macintosh computer game Crystal Quest the
objective is to collect crystals, in a fashion similar
to the travelling salesman problem. The game has
a demo mode, where the game uses a greedy algorithm to go to every crystal. The articial intelligence does not account for obstacles, so the demo
mode often ends quickly.
The matching pursuit is an example of greedy algorithm applied on signal approximation.
A greedy algorithm nds the optimal solution to
Malfattis problem of nding three disjoint circles
within a given triangle that maximize the total area
of the circles; it is conjectured that the same greedy
algorithm is optimal for any number of circles.
A greedy algorithm is used to construct a Human
tree during Human coding where it nds an optimal solution.
In decision tree learning greedy algorithms are commonly used however they are not guaranteed to nd
the optimal solution.

See also
Epsilon-greedy strategy
Greedy algorithm for Egyptian fractions
Greedy source
Matroid

Notes

[1] Black, Paul E. (2 February 2005). greedy algorithm.


Dictionary of Algorithms and Data Structures. U.S. National Institute of Standards and Technology (NIST). Retrieved 17 August 2012.
[2] Introduction to Algorithms (Cormen, Leiserson, Rivest,
and Stein) 2001, Chapter 16 Greedy Algorithms.
[3] (G. Gutin, A. Yeo and A. Zverovich, 2002)

References
Introduction to Algorithms (Cormen, Leiserson, and
Rivest) 1990, Chapter 17 Greedy Algorithms p.
329.
Introduction to Algorithms (Cormen, Leiserson,
Rivest, and Stein) 2001, Chapter 16 Greedy Algorithms.

G. Gutin, A. Yeo and A. Zverovich, Traveling salesman should not be greedy: domination analysis of
greedy-type heuristics for the TSP. Discrete Applied Mathematics 117 (2002), 8186.
J. Bang-Jensen, G. Gutin and A. Yeo, When the
greedy algorithm fails. Discrete Optimization 1
(2004), 121127.
G. Bendall and F. Margot, Greedy Type Resistance
of Combinatorial Problems, Discrete Optimization
3 (2006), 288298.

8 External links
Hazewinkel, Michiel, ed. (2001), Greedy algorithm, Encyclopedia of Mathematics, Springer,
ISBN 978-1-55608-010-4
Greedy algorithm visualization A visualization of a
greedy solution to the N-Queens puzzle by Yuval
Baror.
Python greedy coin example by Noah Gift.

9 TEXT AND IMAGE SOURCES, CONTRIBUTORS, AND LICENSES

Text and image sources, contributors, and licenses

9.1

Text

Greedy algorithm Source: http://en.wikipedia.org/wiki/Greedy_algorithm?oldid=630041357 Contributors: AxelBoldt, Hfastedge,


CatherineMunro, Notheruser, PeterBrooks, Charles Matthews, Dcoetzee, Malcohol, Jaredwf, Meduz, Sverdrup, Wlievens, Enochlau,
Giftlite, Smjg, Kim Bruning, Jason Quinn, Pgan002, Andycjp, Andreas Kaufmann, TomRitchford, Discospinster, ZeroOne, Nabla, Diomidis Spinellis, Nandhp, Obradovic Goran, Haham hanuka, CKlunck, Swapspace, Ralphy, Ryanmcdaniel, Hammertime, Mechonbarsa,
CloudNine, Mindmatrix, LOL, Cruccone, Ruud Koot, Que, Sango123, FlaBot, New Thought, Kri, Pavel Kotrc, YurikBot, Wavelength,
Hairy Dude, TheMandarin, Nethgirb, Bota47, Marcosw, Darrel francis, SmackBot, Brianyoumans, Unyoyega, KocjoBot, NickShaforosto,
Trezatium, SynergyBlades, DHN-bot, Omgoleus, MichaelBillington, Mlpkr, Wleizero, Cjohnzen, Mcstrother, Suanshsinghal, Cydebot,
Jibbist, Thijs!bot, Wikid77, Nkarthiks, Escarbot, Uselesswarrior, Clan-destine, Salgueiro, Chamale, Jddriessen, Albany NY, Magioladitis,
Eleschinski2000, Avicennasis, David Eppstein, Mange01, Zangkannt, Policron, BernardZ, Maghnus, TXiKiBoT, ArzelaAscoli, Monty845,
Hobartimus, Denisarona, HairyFotr, Meekywiki, Enmc, Addbot, Legobot, Fraggle81, Materialscientist, Shrishaster, Hhulzk, Rickproser,
, Eirik the Viking, X7q, Kiefer.Wolfowitz, A8UDI, JumpDiscont, Skakkle, Polariseke, John of Reading, Bernard Teo, Optimering, ZroBot, Ziradkar, Chire, AManWithNoPlan, EdoBot, Swfung8, Petrb, ClueBot NG, ElphiBot, Makecat-bot, Lesser Cartographies,
Scarlettail, Kuchayrameez, Srijanrshetty, Amaniitk and Anonymous: 103

9.2

Images

File:Greedy-search-path-example.gif Source: http://upload.wikimedia.org/wikipedia/commons/8/8c/Greedy-search-path-example.gif


License: CC-BY-SA-3.0 Contributors: Own work Original artist: Swfung8
File:Greedy_Glouton.svg Source: http://upload.wikimedia.org/wikipedia/commons/0/06/Greedy_Glouton.svg License: CC-BY-SA-3.0
Contributors: Own work Original artist: Tos
File:Greedy_algorithm_36_cents.svg Source: http://upload.wikimedia.org/wikipedia/commons/d/da/Greedy_algorithm_36_cents.svg
License: Public domain Contributors: Own work Original artist: Nandhp
File:Internet_map_1024.jpg Source: http://upload.wikimedia.org/wikipedia/commons/d/d2/Internet_map_1024.jpg License: CC-BY2.5 Contributors: Originally from the English Wikipedia; description page is/was here. Original artist: The Opte Project
File:MaximumParaboloid.png Source: http://upload.wikimedia.org/wikipedia/commons/6/62/MaximumParaboloid.png License: CCBY-SA-3.0 Contributors: Transferred from en.wikipedia; transferred to Commons by User:Enen using CommonsHelper. Original artist:
Original uploader was Sam Derbyshire at en.wikipedia
File:Nuvola_apps_edu_mathematics_blue-p.svg Source: http://upload.wikimedia.org/wikipedia/commons/3/3e/Nuvola_apps_edu_
mathematics_blue-p.svg License: GPL Contributors: Derivative work from Image:Nuvola apps edu mathematics.png and Image:Nuvola
apps edu mathematics-p.svg Original artist: David Vignoni (original icon); Flamurai (SVG convertion); bayo (color)

9.3

Content license

Creative Commons Attribution-Share Alike 3.0

Vous aimerez peut-être aussi