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
5
votes
4 answers

The greedy algorithm and implementation

Hello I've just started learning greedy algorithm and I've first looked at the classic coin changing problem. I could understand the greediness (i.e., choosing locally optimal solution towards a global optimum.) in the algorithm as I am choosing the…
Mail
  • 314
  • 1
  • 2
  • 9
5
votes
2 answers

Maximum sum of the range non-overlapping intervals in a list of Intervals

Someone asked me this question: You are given a list of intervals. You have to design an algorithm to find the sequence of non-overlapping intervals so that the sum of the range of intervals is maximum. For Example: If given intervals…
Black_Hat
  • 943
  • 1
  • 9
  • 12
5
votes
2 answers

C# RegEx preventing greedy match with same pattern repeated

In implementing a small script parser I've come across a problem with this sample of code - in wanting to select only the part between and including the "if { }" statements, it's being greedy and selecting all including the last line. I guess what I…
Jamie Barron
  • 121
  • 1
  • 6
5
votes
2 answers

php regex: lookbehind and lookahead and greediness problem

This should be simple but I'm a noob and I can't for the life of me figure it out. I'm trying to use regex to match text inside of special open/close tags: [p2][/p2] So in this text: apple [p2]banana[/p2] grape [p2]lemon[/p2] it should match…
Jared Henderson
  • 1,299
  • 3
  • 13
  • 21
5
votes
4 answers

Improvement of the Greedy Algorithm

I've been working on an abstract chess algorithm using Haskell (trying to expand my understanding of different paradigms), and I've hit a challenge that I've been pondering about for weeks. Here's the problem: Given a board (represented by a list…
user2043598
5
votes
5 answers

Find the index in the circle such that a traveler can completes one round

I appeared in an interview. I stuck in one question. I am asking the same. Question: There is circular road is given. That road contains number of petrol pumps. Each petrol pump have given amount of petrol. Distance between each two…
devsda
  • 4,112
  • 9
  • 50
  • 87
4
votes
2 answers

Algorithm to recover a set given the sums of all its subsets

There is a set A of positive/negative integers. We are given N numbers which are the sum of all its subsets. The task is to find the set A itself. Following is an example. Input: 0 -2 4 5 2 3 9 7 Output: A = {-2 4 5} For the case of positive…
fmatt
  • 464
  • 1
  • 5
  • 15
4
votes
3 answers

Jumping Frog with Energy - Difficult Dynamic Programming/Greedy Problem

Given a set of n stones, arranged in a row at equal distances from each other. The stones are numbered from 0 to n-1. A frog that is on stone number 0 has to get to stone number n-1. A jump is a move from stone i to stone j ( i
PK96
  • 49
  • 1
  • 6
4
votes
2 answers

Minimum steps to reach target, given (1, 0) and operations A = 2A - B or B = 2B - A

I had an interview and I was not able to give a best approach for the problem. A=1, B=0 - Operation L: A=2A-B - Operation R: B=2B-A For each step, only one operation(L or R) is taken place. For a given number N, what is the minimum number of…
Ming Soon
  • 998
  • 2
  • 11
  • 32
4
votes
4 answers

Why does my Boost.Regex search report only one match iteration?

I am trying to find out how many regex matches are in a string. I'm using an iterator to iterate the matches, and and integer to record how many there were. long int before = GetTickCount(); string text; boost::regex…
scottm
  • 27,829
  • 22
  • 107
  • 159
4
votes
2 answers

Minimum number of platforms required for a railway station

The problem is as follows: "Given arrival and departure times of all trains that reach a railway station, the task is to find the minimum number of platforms required for the railway station so that no train waits. Trains can arrive before midnight…
Coder333
  • 63
  • 5
4
votes
3 answers

How do I pick elements from two arrays such that their sum is minimum?

I have two arrays of equal length filled with integers(could be positive or negative but never 0). At each index I can either choose the element of array1 or from array2, and the absoulute value of the sum of such elements should be minimum. Eg: a1…
4
votes
0 answers

What is the difference between greedy and best-first search algorithms?

Best-first search - a search that has an evaluation function f(n) that determines the cost of expanding node n and chooses the lowest cost available node Uninformed search - has no knowledge of h(n) Informed search - has knowledge of h(n) Greedy…
user10939349
  • 41
  • 1
  • 3
4
votes
2 answers

Greedy Coin Change Time Complexity

I'm trying to figure out the time complexity of a greedy coin changing algorithm. (I understand Dynamic Programming approach is better for this problem but I did that already). I'm not sure how to go about doing the while loop, but I do get the for…
RiseWithMoon
  • 104
  • 2
  • 8
4
votes
1 answer

Divide to n bins with minimum cost

Consider 2*k tuples (a0, b0), (a1, b1), ... and 2 bins A and B. placing the i-th tuple in bin A will cost you ai dollar, in bin B will cost you bi dollar. What the minimum cost to place k elements in bin A and k elements in bin B. I came up with…