Questions tagged [greedy]

A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum.

From Wikipedia:

A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum. On some problems, a greedy strategy need not produce an optimal solution, but nonetheless a greedy heuristic may yield locally optimal solutions that approximate a global optimal solution.

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 find a best solution but terminates in a reasonable number of steps; finding an optimal solution typically requires unreasonably many steps. In mathematical optimization, greedy algorithms solve combinatorial problems having the properties of matroids.

Specifics

In general, greedy algorithms have five components:

  1. A candidate set, from which a solution is created
  2. A selection function, which chooses the best candidate to be added to the solution
  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 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 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 solution.

Optimal substructure

"A problem exhibits optimal substructure if an optimal solution to the problem contains optimal solutions to the sub-problems."

1034 questions
502
votes
27 answers

Non greedy (reluctant) regex matching in sed?

I'm trying to use sed to clean up lines of URLs to extract just the domain. So from: http://www.suepearson.co.uk/product/174/71/3816/ I want: http://www.suepearson.co.uk/ (either with or without the trailing slash, it doesn't matter) I have…
Joel
  • 29,538
  • 35
  • 110
  • 138
193
votes
13 answers

Why doesn't Dijkstra's algorithm work for negative weight edges?

Can somebody tell me why Dijkstra's algorithm for single source shortest path assumes that the edges must be non-negative. I am talking about only edges not the negative weight cycles.
Madu
  • 4,849
  • 9
  • 44
  • 78
102
votes
7 answers

Why does the greedy coin change algorithm not work for some coin sets?

I understand how the greedy algorithm for the coin change problem (pay a specific amount with the minimal possible number of coins) works - it always selects the coin with the largest denomination not exceeding the remaining sum - and that it always…
user1311286
66
votes
8 answers

How to find maximum spanning tree?

Does the opposite of Kruskal's algorithm for minimum spanning tree work for it? I mean, choosing the max weight (edge) every step? Any other idea to find maximum spanning tree?
user467871
57
votes
6 answers

What is the difference between dynamic programming and greedy approach?

What is the main difference between dynamic programming and greedy approach in terms of usage? As far as I understood, the greedy approach sometimes gives an optimal solution; in other cases, the dynamic programming approach gives an optimal…
Riding Cave
  • 1,029
  • 1
  • 15
  • 32
44
votes
7 answers

How is dynamic programming different from greedy algorithms?

In the book I am using Introduction to the Design & Analysis of Algorithms, dynamic programming is said to focus on the Principle of Optimality, "An optimal solution to any instance of an optimization problem is composed of optimal solutions to its…
Irwin
  • 12,551
  • 11
  • 67
  • 97
33
votes
8 answers

Hungarian Algorithm: finding minimum number of lines to cover zeroes?

I am trying to implement the Hungarian Algorithm but I am stuck on the step 5. Basically, given a n X n matrix of numbers, how can I find minimum number of vertical+horizontal lines such that the zeroes in the matrix are covered? Before someone…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
28
votes
5 answers

What is the difference between Greedy-Search and Uniform-Cost-Search?

When searching in a tree, my understanding of uniform cost search is that for a given node A, having child nodes B,C,D with associated costs of (10, 5, 7), my algorithm will choose C, as it has a lower cost. After expanding C, I see nodes E, F, G…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
26
votes
5 answers

What's the difference between greedy and heuristic algorithm?

What's the difference between greedy and heuristic algorithm? I have read some articles about the argument and it seems to me that they are more or less the same type of algorithm since their main characteristic is to choose the best (local) option…
Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
25
votes
3 answers

Greedy, Non-Greedy, All-Greedy Matching in C# Regex

How can I get all the matches in the following example: // Only "abcd" is matched MatchCollection greedyMatches = Regex.Matches("abcd", @"ab.*"); // Only "ab" is matched MatchCollection lazyMatches = Regex.Matches("abcd", @"ab.*?"); // How can I…
Peter Lee
  • 12,931
  • 11
  • 73
  • 100
24
votes
3 answers

How to spot a "greedy" algorithm?

I am reading a tutorial about "greedy" algorithms but I have a hard time spotting them solving real "Top Coder" problems. If I know that a given problem can be solved with a "greedy" algorithm it is pretty easy to code the solution. However if I am…
Michael
  • 41,026
  • 70
  • 193
  • 341
24
votes
2 answers

Dijkstra's algorithm a greedy or dynamic programming algorithm?

In this post it is described Dijkstras as a greedy algorithm, while here and here it is shown to have connections with dynamic programming algorithms. Which one is it then?
vkaul11
  • 4,098
  • 12
  • 47
  • 79
21
votes
9 answers

Optimal Algorithm for Winning Hangman

In the game Hangman, is it the case that a greedy letter-frequency algorithm is equivalent to a best-chance-of-winning algorithm? Is there ever a case where it's worth sacrificing preservation of your remaining lives, for the sake of a better chance…
Ronald
  • 325
  • 1
  • 2
  • 9
18
votes
3 answers

Matching text between delimiters: greedy or lazy regular expression?

For the common problem of matching text between delimiters (e.g. < and >), there's two common patterns: using the greedy * or + quantifier in the form START [^END]* END, e.g. <[^>]*>, or using the lazy *? or +? quantifier in the form START .*? END,…
Heinzi
  • 167,459
  • 57
  • 363
  • 519
15
votes
10 answers

Usage examples of greedy algorithms?

What is the use of greedy algorithms? An real example?
Antonio Barra
  • 249
  • 1
  • 3
  • 7
1
2 3
68 69