Jump to content

Greedy algorithm

From Wikipedia, the free encyclopedia
Greedy algorithms determine the minimum number of coins to give while making change. These are the steps most people 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. (In general, thechange-making problemrequiresdynamic programmingto find an optimal solution; however, most currency systems are special cases where the greedy strategy does find an optimal solution.)

Agreedy algorithmis anyalgorithmthat follows the problem-solvingheuristicof making the locally optimal choice at each stage.[1]In many problems, a greedy strategy does not produce an optimal solution, but a greedy heuristic can yield locally optimal solutions that approximate a globally optimal solution in a reasonable amount of time.

For example, a greedy strategy for thetravelling salesman problem(which is of highcomputational complexity) is the following heuristic: "At each step of the journey, visit the nearest unvisited city." This heuristic does not intend to find the best solution, but it terminates in a reasonable number of steps; finding an optimal solution to such a complex problem typically requires unreasonably many steps. In mathematical optimization, greedy algorithms optimally solve combinatorial problems having the properties ofmatroidsand give constant-factor approximations to optimization problems with the submodular structure.

Specifics

[edit]

Greedy algorithms produce good solutions on somemathematical problems,but not on others. Most problems for which they work will have two properties:

Greedy choice property
Whichever choice seems best at a given moment can be made and then (recursively) solve the remaining sub-problems. 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 fromdynamic programming,which is exhaustive and is guaranteed to find the solution. After every stage, dynamic programming makes decisions based on all the decisions made in the previous stage and may reconsider the previous stage's algorithmic path to the solution.
Optimal substructure
"A problem exhibitsoptimal substructureif an optimal solution to the problem contains optimal solutions to the sub-problems. "[2]

Cases of failure

[edit]
Examples on how a greedy algorithm may fail to achieve the optimal solution.
Starting from A, a greedy algorithm that tries to find the maximum by following the greatest slope will find the local maximum at "m", oblivious to the global maximum at "M".
To reach 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.

Greedy algorithms fail to produce the optimal solution for many other problems and may even produce theunique worst possiblesolution. One example is thetravelling salesman problemmentioned above: for each number of cities, there is an assignment of distances between the cities for which the nearest-neighbour heuristic produces the unique worst possible tour.[3] For other possible examples, seehorizon effect.

Types

[edit]

Greedy algorithms can be characterized as being 'short sighted', and also as 'non-recoverable'. They are ideal only for problems that have an 'optimal substructure'. Despite this, for many simple problems, the best-suited algorithms are greedy. 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 variations to the greedy algorithm:[4]

  • Pure greedy algorithms
  • Orthogonal greedy algorithms
  • Relaxed greedy algorithms

Theory

[edit]

Greedy algorithms have a long history of study incombinatorial optimizationandtheoretical computer science.Greedy heuristics are known to produce suboptimal results on many problems,[5]and so natural questions are:

  • For which problems do greedy algorithms perform optimally?
  • For which problems do greedy algorithms guarantee an approximately optimal solution?
  • For which problems are the greedy algorithm guaranteednotto produce an optimal solution?

A large body of literature exists answering these questions for general classes of problems, such asmatroids,as well as for specific problems, such asset cover.

Matroids

[edit]

Amatroidis a mathematical structure that generalizes the notion oflinear independencefromvector spacesto arbitrary sets. If an optimization problem has the structure of a matroid, then the appropriate greedy algorithm will solve it optimally.[6]

Submodular functions

[edit]

A functiondefined on subsets of a setis calledsubmodularif for everywe have that.

Suppose one wants to find a setwhich maximizes.The greedy algorithm, which builds up a setby incrementally adding the element which increasesthe most at each step, produces as output a set that is at least.[7]That is, greedy performs within a constant factor ofas good as the optimal solution.

Similar guarantees are provable when additional constraints, such as cardinality constraints,[8]are imposed on the output, though often slight variations on the greedy algorithm are required. See[9]for an overview.

Other problems with guarantees

[edit]

Other problems for which the greedy algorithm gives a strong guarantee, but not an optimal solution, include

Many of these problems have matching lower bounds; i.e., the greedy algorithm does not perform better than the guarantee in the worst case.

Applications

[edit]

Greedy algorithms typically (but not always) fail to find the globally optimal solution because they usually do not operate exhaustively on all the data. They can make commitments to certain choices too early, preventing them from finding the best overall solution later. For example, all knowngreedy coloringalgorithms for thegraph coloring problemand all otherNP-completeproblems do not consistently find optimum solutions. Nevertheless, they are useful because they are quick to think up and often give good approximations to the optimum.

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 likedynamic programming.Examples of such greedy algorithms areKruskal's algorithmandPrim's algorithmfor findingminimum spanning treesand the algorithm for finding optimumHuffman trees.

Greedy algorithms appear in the networkroutingas well. Using greedy routing, a message is forwarded to the neighbouring node which is "closest" to the destination. The notion of a node's location (and hence "closeness" ) may be determined by its physical location, as ingeographic routingused byad hoc networks.Location may also be an entirely artificial construct as insmall world routinganddistributed hash table.

Examples

[edit]

See also

[edit]

References

[edit]
  1. ^Black, Paul E. (2 February 2005)."greedy algorithm".Dictionary of Algorithms and Data Structures.U.S. National Institute of Standards and Technology(NIST).Retrieved17 August2012.
  2. ^Cormen et al. 2001,Ch. 16
  3. ^Gutin, Gregory; Yeo, Anders; Zverovich, Alexey (2002)."Traveling salesman should not be greedy: Domination analysis of greedy-type heuristics for the TSP".Discrete Applied Mathematics.117(1–3): 81–86.doi:10.1016/S0166-218X(01)00195-0.
  4. ^DeVore, R. A.; Temlyakov, V. N. (1996-12-01)."Some remarks on greedy algorithms".Advances in Computational Mathematics.5(1): 173–187.doi:10.1007/BF02124742.ISSN1572-9044.
  5. ^Feige 1998
  6. ^Papadimitriou & Steiglitz 1998
  7. ^Nemhauser, Wolsey & Fisher 1978
  8. ^Buchbinder et al. 2014
  9. ^Krause & Golovin 2014
  10. ^"Lecture 5: Introduction to Approximation Algorithms"(PDF).Advanced Algorithms (2IL45) — Course Notes.TU Eindhoven.Archived(PDF)from the original on 2022-10-09.

Sources

[edit]
[edit]