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
0 answers

Makefile target too greedy

deps/%/: git clone something into deps/$*/ deps/%/ebin/: deps/%/ compile things and put them into newly-created deps/$*/ebin/ I have two targets that share a common pattern (% is the * of Makefiles) but unfortunately the pattern is…
fenollp
  • 2,406
  • 1
  • 15
  • 14
0
votes
1 answer

Find if it's possible to make up a integer with some given numbers

Question: Find if its possible to make up a integer (say N) with 21,19 and 37? a. N will be provided as input b. You can use only these three numbers: 27,19,37 c. Only multiplication, addition, repetition and replacement are allowed For…
Arnab Dutta
  • 168
  • 11
0
votes
1 answer

Worst case of nearest neighbor heuristic for symmetric TSP

I have implemented the nearest neighbor heuristic for solving symmetric TSP problems. I was wondering if there is any relation between the solution found by the heuristic and the optimal solution? Can we state theoretically how much higher the route…
user19553
  • 11
  • 1
0
votes
1 answer

Is there a name for the algorithm solutions for "booking rooms with the least room switches"?

I was discussing with a co-worker a problem we were having with a piece of software we deploy, and he mentioned how it was similar to the conceptual problem of booking rooms over a course of time and the algorithm should output the room bookings…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
0
votes
1 answer

Greedy Algorithm- Knapsack Puzzle

I am attempting to solve the Knapsack problem with a greedy algorithm in Python 3.x. Below is my code, and the sample cases I'm using to test it. Each sample case is in the form line[0] = max weight, line[1:] in form (weight, value.) Sample case 1…
idalsin
  • 546
  • 2
  • 9
  • 31
0
votes
0 answers

why is draggable not working on parent when greedy?

I added the option greedy:true on my droppable but after dragging an element into a child, the parent is not responding. I think is a problem with jqueryui. To reproduce drop an element into the child and then try to drop another element directly…
shuji
  • 7,369
  • 7
  • 34
  • 49
0
votes
2 answers

Greedy algorithm odd behavior

I've implemented the Greedy algorithm to solve Egyptian fractions, however I'm getting some unexpected results. Here's my code from math import ceil from fractions import Fraction def go(frac): ret = [] while frac > 0: if…
eulr
  • 171
  • 2
  • 19
0
votes
1 answer

Minimum number of char substitutions to get a palindrome

I would like to solve this problem from TopCoder, in which a String is given and in each step you have to replace all occurrences of an character (of your choice) with another character (of your choice), so that at the end after all steps you get a…
V G
  • 18,822
  • 6
  • 51
  • 89
0
votes
2 answers

Greedy Algorithm - Depreciating Cost

This is a problem I found as an extra note in Algorithm Design by Kleinberg and Tardos. Suppose we are trying to sell off equipment whose cost is depreciating at a factor of ri < 1 per month, starting from $100, so if you sell it t months from now…
user2761431
  • 925
  • 2
  • 11
  • 26
0
votes
5 answers

Regex greedyness REasking

I have this text $line = "config.txt.1", and I want to match it with regex and extract the number part of it. I am using two versions: $line = "config.txt.1"; (my $result) = $line =~ /(\d*).*/; #ver 1, matched, but returns nothing (my $result)…
Juto
  • 1,246
  • 1
  • 13
  • 24
0
votes
1 answer

Java regex for parsing Qt localization functions params

Qt has special functions for localization call. Functions like qsTr(), tr(), QT_TR_NOOP(), qsTranslate(), etc. I need to parse the content of above functions` calls params. For example, qsTr(): qsTr(string source, string disambiguation, int…
Alex Gorbunov
  • 239
  • 3
  • 8
0
votes
2 answers

Perl regex becoming greedy when used (.*?) with anchors

I have a perl regex to add youtube video link to video tag. The YouTube videos link can be within anchors sometimes and sometimes without anchors. I have checked anchor with any value using (.*?) but it behaving as greedy. below is the regex that I…
Laeeq
  • 403
  • 1
  • 3
  • 14
0
votes
1 answer

Should I expect to have exactly same match everytime I run the match with greedy algorithm?

I'm using greedy algorithm or local algorithm to match propensity score in two groups. I will end up with same number of matches every time but sometimes it doesn't match exactly same with last time. Is this normal?
monkey
  • 7
  • 2
0
votes
0 answers

greedy algorithms and arrays

The problem of scheduling n jobs of known durations t1,t2,...tn for execution on a single processor. The jobs can be executed in any order, one job at a time. we want to find a schedule that minimizes the total time spend by all the jobs in this…
0
votes
1 answer

Greedy algorithm for a given unsorted input with time complexity "nlogn"

Multi-Constrained Knapsack Problem i have such a given example ,i m just trying to understand, whats the difference between greedy algorithm with O(n*logn) and greedy algorithm for O(n2)? I really do not know how to start please help! Should i sort…
user2333464
  • 3
  • 1
  • 4