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

Finding Smallest Number of Elements to make a Sum

I have a simple algorithmic question: If I have certain elements that integer values like: 1 1 1 1 1 1 1 1 1 1 1 1 10 12 2 and I have to make the sum 12, the minimum number of elements needed would 1, I would just use 12. Thus, my question is how…
user3188300
  • 341
  • 2
  • 10
3
votes
1 answer

greedy algorithm matrix of 0\1

I'm studying for an algorithm exam and I cant find a way to solve the next problem: INPUT: Positive integers r1,r2....rn and c1,c2....cn OUTPUT: An n by n matrix A with 0/1 entries such that for all i the sum of the i-th row in A is ri and the…
user51929
  • 185
  • 1
  • 15
3
votes
3 answers

Python Greedy Algorithm

I am writing a greedy algorithm (Python 3.x.x) for a 'jewel heist'. Given a series of jewels and values, the program grabs the most valuable jewel that it can fit in it's bag without going over the bag weight limit. I've got three test cases here,…
idalsin
  • 546
  • 2
  • 9
  • 31
3
votes
1 answer

Proof of Longest Increasing Subsequence using greedy patience sort

I came across the solution that uses Patience sort to obtain the length of the Longest Increasing Subsequence (LIS). http://www-stat.stanford.edu/~cgates/PERSI/papers/Longest.pdf, and here - http://en.wikipedia.org/wiki/Patience_sorting. The proof…
sachuraju
  • 123
  • 8
3
votes
2 answers

Why doesn't greedy algorithm work for some currencies that are unlike US currency?

To make $6.39, you can choose: $5 bill $1 bill to make $6. 25¢ coin, to make $6.25 10¢ coin, to make $6.35 Four 1¢ coins, to make $6.39. However it doesn't work if the currency has coins with weights of 1,7, and 10. My question is, why does…
sarath
  • 57
  • 7
3
votes
1 answer

greedy block ()* contains wildcard

I am building a grammar in ANTLR4, and I am getting this warning TL4.g4:224:12: greedy block ()* contains wildcard; the non-greedy syntax ()*? may be preferred Here is the line of code it is referring to block : ( statement | functionDecl )*…
Gautam
  • 7,868
  • 12
  • 64
  • 105
3
votes
1 answer

Algorithm to fill large rectangle with smaller rectangles starting from center

I have various sizes of rectangles, and I am trying to fit them into a larger rectangle starting from the center. Below you will find an animation I created to visually describe what needs to take place. I have been struggling to come up with a way…
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
3
votes
1 answer

Greedy Set Coverage algorithm built by *removing* sets

I am trying to implement a solution for a set coverage problem using a greedy algorithm. The classic greedy approximation algorithm for it is input: collection C of sets over universe U , costs: C→R ≥0 output: set cover S 1. Let S←∅. 2.…
Dale M
  • 2,453
  • 1
  • 13
  • 21
3
votes
2 answers

min change greedy algorithm in java

Ok so i need to make a program to ask me for an amount of money, then I need it to tell me the least amount of coins to make it. The coins I can use are: dollars, quarters, dimes, nickels, and pennies. For example, When I run the program it's…
Emily Jordan
  • 29
  • 2
  • 3
3
votes
2 answers

How can I keep concatenated tokens separate during lexing when a more general token is availible

The language I'm working on allows certain tokens to be stuck together (eg "intfloat") and I'm looking for a way to have the lexer not turn them into an ID so they're available separately at parse time. The simplest grammar I can come up with that…
3
votes
2 answers

Ignoring an optional suffix with a greedy regex

I'm performing regex matching in .NET against strings that look like this: 1;#Lists/General Discussion/Waffles Win 2;#Lists/General Discussion/Waffles Win/2_.000 3;#Lists/General Discussion/Waffles Win/3_.000 I need to match the URL portion…
Alex Angas
  • 59,219
  • 41
  • 137
  • 210
3
votes
1 answer

How do i solve this in a faster and more accurate way?

Here is the question: Ramu was a lazy farmer. He had inherited a fairly large farm and a nice house from his father. Ramu leased out the farm land to others and earned a rather handsome income. His father used to keep a buffalo at home and sell…
2147483647
  • 1,177
  • 3
  • 13
  • 33
3
votes
1 answer

How to trim the returned nodes of Greedy Best First Search and A* Algorithms?

I am trying to implement GBFS and A* with a grid (2D array). I think before we go any further...do both of the algorithms remember previous locations and if so, does this therefore mean that it will jump to that location if the heuristics are better…
Johnathan Au
  • 5,244
  • 18
  • 70
  • 128
2
votes
2 answers

How can I, in R, use a greedy site selection algorithm that maximises unrepresented species richness?

I would like to use a 'greedy' algorithm that would select sites such as to maximize unrepresented species richness. The first site selected would be the one with the highest richness,while the second would be the one with highest unrepresented…
2
votes
1 answer

Is there a way to cover n elements and make sure they are connected?

I'm trying to create a game but I am having some difficulties in coming up with a suitable algorithm for my problem. I have elements from 1 to n and I am trying to cover all of the elements using the minimum amount of elements as possible. Each…