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
0
votes
1 answer

How can I implement a job queue with a greedy-worker-pool in Java EE 6 in a correct way?

I'm looking for a correct way, to do the following in Java EE 6, if possible with vanilla Java EE 6 only. I want to put a job in a job queue and have a fixed pool of worker objects, which should pull a job from the queue, if they are idle. The…
user1631581
  • 201
  • 1
  • 2
  • 7
0
votes
0 answers

Algorithm for text justification (flush left and right)

I'm looking to implement a way to perform text justification (flush left and right). Each output line can have a maximum width of M characters. Breaking of words is not allowed. For example, please see "Justified (flush left and right)" in this…
0
votes
1 answer

Searching an algorithm similar to producer-consumer

I would like to ask if someone would have an idea on the best(fastest) algorithm for the following scenario: X processes generate a list of very large files. Each process generates one file at a time Y processes are being notified that a file is…
0
votes
1 answer

Maximizing profit in graph having positive weight cycles

I have a set of vertices with some profit defined between each pair of vertices such that profit(i,j) may not be equal to profit(j,i). Moreover there exist positive weight cycles and the profit may be negative. This is a NP-hard problem to find the…
Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66
0
votes
1 answer

Matching a rule in the middle of any number of other rules

I'm working on a data definition language that uses braces to identify hierarchy. typeA idS { paramX = value typeB idT { paramY = value } } There are a large number of different rules to validate what params and…
0
votes
1 answer

PHP regex preg_match_all greedy modifier

Here is my code: echo "
"; preg_match_all("|<[^>]+>.*]+>|U", "example:this is a test", $out, PREG_PATTERN_ORDER); print_r($out); echo "
"; echo "
"; preg_match_all("|<[^>]+>.*]+>|", …
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
0
votes
2 answers

multi-dimensional knapsack with multiple constraints

i am unsure if i have even identified the problem correctly, but reading up on Knapsack problem seems the closest to what i am trying to solve: A cook has several ingredients of varying quantities. for example: 8 eggs 3 sausages 500 mL milk 12…
bouncingHippo
  • 5,940
  • 21
  • 67
  • 107
0
votes
1 answer

Greedy algorithm and coins algorithm

First, yes it's my HW and I find it hard so I'll really appreciate some guidance. I need to prove that for denomination of 1,x,x2...xn when x>=1 the greedy algorithm for the coins problem always work . We will always get the amount of money we need…
Bobbbaa
  • 199
  • 2
  • 12
0
votes
1 answer

Using the Matrix Chain with Greedy method

I am reading CLRS by myself and I am finding but difficult to understand few concepts. Compared to Greedy, in Dynamic Programming we make choices globally and end up with optimal solution. I understood these concepts well with examples of Shortest…
avi
  • 9,292
  • 11
  • 47
  • 84
0
votes
1 answer

Index out of range error Python greedy method

Hello I am currently getting an index out of range error from the following code: (I will post the code first and then the error) Main File: import Knapsack_Test size = 10 W = 2*size knapsack = Knapsack_Test.Knapsack_Test() for i in range(1, 10): …
user1661211
  • 43
  • 1
  • 7
0
votes
1 answer

Algorithm - Minimizing Equation

If we are given an equation say 3x + 2y <= 10, we want to find the value of x and y such that x + y = maximum and 10 - 3x - 2y is minimized. How can this be done? I am thinking of it as a dynamic programming problem ! but not sure if I am right. In…
Varun Sharma
  • 2,591
  • 8
  • 45
  • 63
0
votes
1 answer

How can i solve this?

Given an undirected weighted graph G = (V,E). Each vertex represents a city and the weight of an edge connected a and b is the number of years that it will take to finish building a high speed route between city a and city b. Describe an algorithm…
Aden Dong
  • 153
  • 4
0
votes
1 answer

Activity Selection

My question is that we are given a list of activities with their start and end times,then we have to find out the maximum activities we can do. The question is pretty common and i have applied a Greedy approach but i'm getting a runtime segmentation…
user1409637
-1
votes
2 answers

Greedy Activity-Selector algorithm: n activities in smallest amout of lecture halls pseudo code

I have spent hours trying to understand the answer to this problem. I just don't get it. Can somebody please give me some pseudo code (preferably pseudo code similar to Java) of the algorithm that would solve this problem based on the answer? That…
user1028641
  • 287
  • 1
  • 5
  • 15
-1
votes
0 answers

Not getting proper output in a fractional-knapsack test case

What am I missing in this code? I want to print selected items with 0 or 1, but I am not getting proper output in this fractional-knapsack test case. fractional-knapsack First line contains number of total items n Next n lines contain values and…