Talk:Greedy algorithm

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Untitled[edit]

The page says that Kruskal's Algorithm is also a Greedy Algorithm. Tho actually this does not work locally, instead Kruskal always takes the smallest weight of all to add an edge to the set. It knows the whole set of edges and in opposite to Prim it takes the smallest weight - shouldnt we remove Kruskal from this page?

--- User:WhiteCrow 7/24/2005

I would find Kruskal's algorithm a more natural example of what I understand to be a greedy algorithm than Prim's. In fact, the phrasing seems somewhat unlucky: I'd describe a greedy algorithm as "an algorithm that repeatedly extends a partial solution in the way that increases the objective function most" (in the case of a maximization problem). In Prim's algorithm we maintain the invariant that the partial solution is a tree; in Kruskal's algorithm we're already happy with a forest. —Preceding unsigned comment added by 131.155.68.93 (talkcontribs) 4 October 2005
My PhD computer scientist wife read this over and says the article is basically OK. We looked at the algorithms book that I reference (a standard book on the subject) and it agrees with the article's content. About Kruskal's: the book also specifically characterizes Kruskal's as a greedy algorithm (p. 504). --Brianyoumans 09:42, 6 August 2006 (UTC)[reply]
Kruskal's algorithm is definitely greedy. When it adds an edge to the forest, it will never have to remove it from there. --ZeroOne (talk | @) 12:02, 30 November 2006 (UTC)[reply]

Example?[edit]

A little example (even pseudocode) would be convincing. Can someone add a typical greedy problem & solution to the article? —Preceding unsigned comment added by 86.34.42.206 (talkcontribs) 19 March 2006

See the image on the page for a simple example. Also see the linked articles Kruskal's algorithm and Dijkstra's algorithm. The first one has a complete example, the second one some pseudocode. --ZeroOne (talk | @) 17:42, 10 November 2006 (UTC)[reply]

Making Change[edit]

I feel that the Making Change problem in the picture should be changed to another problem because it is not a true greedy algorithm, or at least moved into a subsection with an explanation.

The making change problem is not a real greedy algorithm, because given different denominations of coins such as {1,4,6} then the greedy algorithm will fail to find an optimal solution for example 9 will be given 6+1+1+1 when the optimal is 4+4+1.

So it fails the greedy-choice property.

81.103.164.48 20:09, 19 January 2007 (UTC)[reply]

But it is not a necessary condition for a greedy algorithm to produce an optimal solution. BarrBarrBarr 06:44, 24 March 2007 (UTC)[reply]

Agree with Barr^3. But the text should make it clear that it may not produce an optimal solution, and can reference dynamic programming as a technique used for an algorithm to find an optimal solution. --Nethgirb 05:18, 10 August 2007 (UTC)[reply]
Is there a formula or reference explaining for what coin sets the greedy algorithm does or doesn't always yield the optimal way to make change with the minimum number of coins? Newyorkbrad 03:14, 10 August 2007 (UTC)[reply]
Yes there is. If a coin value is more than two times bigger than the one smaller than it, you cannot use greedy. So if coins is a sorted array of coin values, this works:
   def canUseGreedy(coins)
       n = coins.length
       i = 1
       while(i < n) do
         if (2*coins[i]>coins[i-1]) #compare each coin with the previous one
           return false
         end
         i = i+1
       end
       return true
   end

Lars'); DROP TABLE Students;-- (talk) 00:05, 6 December 2007 (UTC)[reply]

I've added a note saying the greedy strategy is not optimal in general, that the general solution needs dynamic programming, and added the appropriate link. Hairy Dude (talk) 17:39, 18 February 2008 (UTC)[reply]
This program suggests that US coins do not have the greedy property (2*10 < 25), but my simple program indicates that to 100 they do:
 coins = [1,5,10,25,100]
 
 sols = [[]]
 for i in range(1,101):
   t = i
   best = [] # perform greedy
   for c in reversed(coins):
       while t >= c:
           t -= c
           best.append(c)
   
   for c in coins: # DP knapsack
       if c > len(sols): break
  	trial = sols[i-c]+[c]
       if len(trial) < len(best):
           print "failure", best, trial # highlight solutions in which greedy is not optimal
           best = trial
   
   sols.append(best)
Try it with coins = [1,4,6] to see that that is indeed a non-greedyable knapsack —Preceding unsigned comment added by 192.150.10.200 (talk) 23:16, 25 March 2008 (UTC)[reply]
Ah, ok. When you said sorted I assumed in increasing order, when in fact you meant decreasing order. That makes more sense. —Preceding unsigned comment added by 192.150.10.200 (talk) 23:24, 25 March 2008 (UTC)[reply]

Is the comment about US coins being special (in that the greedy algorithms works only for them) correct? The majority of the world's countries have coin systems like this [1 2 5 10 20 50 100 200]. I believe this is a "greedable" coin system, and have run the code above to verify this. In fact, isn't the previously stated rule for "greedable" coin systems simply very wrong? —Preceding unsigned comment added by 91.125.119.202 (talk) 08:23, 20 May 2008 (UTC)[reply]

I believe the previous statement about greedability should read less than twice the amount of the previous coin, not more than. -Kade304 (talk) 16:27, 30 May 2008 (UTC)[reply]

Is there a different example that could be used, one that is simpler (because people, like me, may not understand what quarters, dimes, pennies and nickels are and how they work). Sorry, I can't think of a simple example just yet though.AndrewHarvey4 (talk) 07:39, 8 November 2008 (UTC)[reply]

Relevancy of the Example Picture[edit]

Am I the only one bothered by the fact that the example image attempts to describe a real world situation by specifying the currency, but provides inaccurate coin values (Except for Australia specifically). If it is a real world example, then specificity, accuracy, and relevance are pretty important. Not to offend the folks from Down Under, but if this were specified then providing the example to a primarily American website made using the American English dialect in regards to Australian Currency when that same currency verbiage is in use in both places... is rather irrelevant.

NOTE: Yes I looked up the history of the 20 remaining 20 cent silver coins minted in the 19th century which are not currently in circulation.

This should be specified and the exception of American vs. Australian Currency should be clarified. If the currency itself is irrelevant, leaving the value of the objects as the only important element, then the real world example of money may be a poor example and could be replaced. I'm certain that weight, length, or area make more generic real-world examples. Cabbruzz (talk) 17:32, 29 October 2010 (UTC)[reply]


I agree with this, when I came to this article I was confused for a while and had to investigate further to clarify that the example was using coins of those values rather than American values. I was not sure that the 20 represented a single coin, rather than two 10 cent coins. I think it would be sufficient for the example to state what coin values were being used so there is no ambiguity. Switching to American values would also work, but would take a lot more effort. I will go ahead and make the former edit. Omgoleus (talk) 21:43, 29 December 2010 (UTC)[reply]

Ordered list[edit]

Can anyone shed some light on the purpose of the ordered (numbered) list at the beginning of the article? --Thinboy00 talk/contribs @983, i.e. 22:35, 10 November 2007 (UTC)[reply]

It looks like an anatomy list. --Thinboy00 talk/contribs @985, i.e. 22:38, 10 November 2007 (UTC)[reply]
Actually, an anonymous user messed it up here and no one spotted it... I've fixed it now. —ZeroOne (talk / @) 12:25, 11 November 2007 (UTC)[reply]

Opposite[edit]

Can someone mention what the name is for the "opposite" or "antonym" of the greedy algorithm, if there is one?

Well the most obvious "opposite" if there could be said to be such a thing would be an algorithm that selects a globally optimal solution, rather than a locally optimal one. In practice, though, non-greedy algorithms include a lot more than just globally optimal algorithms. Dcoetzee 18:02, 30 May 2008 (UTC)[reply]
"ungreedy" is sometimes used for algorigtms that take the most conservative choice at each step rather than the one that increases the solution value the most, such as in Regular expressions BCoates (talk) 05:13, 7 September 2008 (UTC)[reply]

regex[edit]

Does greedy regex follow these rules, matter for this sort of topic? If it matters, I'm thinking about pcre regex. —Preceding unsigned comment added by 81.155.84.169 (talk) 21:06, 18 July 2008 (UTC)[reply]

Greedy regexps are only sort of related to greedy algorithms - the match is always expanded as much as possible before applying the next term of the regexp, but greedy and nongreedy regexps describe different languages and solve different problems; it's not a local heuristic for estimating a solution to a problem. Dcoetzee 03:57, 9 November 2008 (UTC)[reply]

Greedy vs. Hill Climbing[edit]

There is also an article on Hill climbing algorithms. I can't tell the difference - is one more general than the other? If so, it seems like the relevant page should explain that. This comment is also posted on the discussion page there. Thanks. --Jdvelasc (talk) 19:06, 10 September 2009 (UTC)[reply]

If there is a distinction, I've mostly heard "hill climbing" in the context of traversing the solution space of a function (typically with gradient descent, but not always), while the "greedy approach" usually relates to problems that boil down to graph traversal. The latter can be formulated as a function, but it's hard to describe continuous functions in terms of a graph, so hill climbing is probably the more general term. If there is more to it, I am also curious. Maghnus (talk) 17:44, 26 September 2009 (UTC)[reply]
The difference is that a hill climbing algorithm starts with a complete (but random) solution whereas a greedy algorithm starts without a solution, adding partial solutions until it reaches a complete solution. Compare an example about the TSP: a hill climbing algorithm would start with a randomized visitation order and then swap the order of nodes until it cannot optimize the solution anymore. A greedy algorithm, however, would start from a single node and add new nodes into the solution one by one until all nodes have been visited, at which point it terminates.
I don't think I can be as bold as to say you are comparing apples and oranges but there is a difference. And if anything, in my opinion 'greedy algorithm' is the more general term. If you read the hill climbing article you'll see a few variants listed. The 'simple hill climbing' version would be an example of a greedy algorithm whereas the 'Stochastic hill climbing' wouldn't.
ZeroOne (talk / @) 21:42, 2 September 2010 (UTC)[reply]

"forwardly greedy selection algorithm"[edit]

I saw this phrase in an article. Could someone document what it means? —Preceding unsigned comment added by Skysong263 (talkcontribs) 19:53, 23 April 2010 (UTC)[reply]

Computational mind: a complex dynamics perspective[edit]

I just reverted the addition of a citation to "Computational mind: a complex dynamics perspective" (Vladimir G. Ivancevic, Tijana T. Ivancevic, Springer, 2007). At first glance, the page in question of the book [1] makes it look like our article is a copyvio — it has essentially the same text as one of our paragraphs. On second glance, though, judging from the date of publication of the book, the dates at which the text in question was added to our article, and the history of our article showing the text growing organically rather than being added all at once, I think the more likely hypothesis is that Ivancevic and Ivancevic copied from us. Therefore, per WP:CIRCULAR, we can't use it as a source. —David Eppstein (talk) 19:41, 13 November 2011 (UTC)[reply]

Apparent contradiction?[edit]

New to editing Wikipedia, so please go gently...

I noticed that in the "Cases of failure" section, an example of coins is given where a greedy algorithm will fail to find the example quantity of 41 cents. However, immediately following that, the "Types" section says "greedy algorithms are best suited for simple problems (e.g. giving change)." This seems to be a contradiction - with one section saying the algorithm is suited to finding optimal change and the preceding section illustrating a failure case with giving change.

A possible edit would be "(e.g. giving change in most currencies)" or something similar. I'm not sure where the example is based as I've never come across a 4-cent coin (I'm writing from the UK). Another idea would be to change the example of failure to a different problem. Any thoughts? — Preceding unsigned comment added by Snapey1979 (talkcontribs) 10:07, 17 April 2012 (UTC)[reply]

Clean up applications[edit]

There are many many more applications of the greedy algorithm than the applications section lets on. I added a template asking to expand and improve that section. For example, the introduction to this paper contains a number of applications of greedy just to submodular function maximization.

No such thing as logical or not. — Preceding unsigned comment added by Lyhendq (talkcontribs) 07:38, 23 September 2018 (UTC)[reply]

underlinking[edit]

An IP user with no other history (but evident knowledge of syntax) stripped out many links from the top section. Now maybe there were too many, but now I think there are too few. Opinions? —Tamfang (talk) 05:47, 3 October 2019 (UTC)[reply]