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

Two greedy quantifiers in the same regex

If I have an unknown string of the structure: "stuff I don't care about THING different stuff I don't care about THING ... THING even more stuff I don't care about THING stuff I care about" I want to capture the "stuff I care about" which will…
noah
  • 2,616
  • 13
  • 27
4
votes
1 answer

Weighted disjunction in Perl Regular Expressions?

I am fairly experienced with regular expressions, but I am having some difficulty with a current application involving disjunction. My situation is this: I need to separate an address into its component parts based on a regular expression match on…
NatHillard
  • 306
  • 2
  • 10
4
votes
2 answers

Non-greedy (lazy) matching using regex?

How do you implement non-greedy matching in Stata using regex? Or does Stata even have this capability? I want to extract all text that occurs between a hashtag "#" and a period ".". Example code: clear set obs 3 generate…
user812783765
  • 165
  • 1
  • 1
  • 7
4
votes
4 answers

Solutions to problems using dynamic programming or greedy methods?

What properties should the problem have so that I can decide which method to use dynamic programming or greedy method?
Eric
  • 111
  • 7
4
votes
1 answer

Algorithm to generate a binary matrix

Given two input arrays [R1, ..., Rn] and [C1, ..., Cn]. We want to create a binary matrix A (of size-nxn) such that the sum of elements in column i of A be Ci and the sum of elements in row j of A be Rj. I tried to fill using greedy algorithm : fill…
Mike
  • 61
  • 9
4
votes
1 answer

Dynamic programming for primitive calculator

I'm dealing with the problem, that is pretty similar to change coins problem. I need to implement a simple calculator, that can perform the following three operations with the current number x: multiply x by 2, multiply x by 3, or add 1 to x. Goal…
Daniel Chepenko
  • 2,229
  • 7
  • 30
  • 56
4
votes
2 answers

Find minimum cost to visit all nodes of a tree

Given the root of a tree where each edge has an associated cost. Find the minimum cost to visit every node of the tree. A recursive solution that came to my mind is: base case when node is a leaf return 0. for every child c of the node recursively…
Prashant Bhanarkar
  • 930
  • 3
  • 14
  • 32
4
votes
1 answer

What is the name of this greedy algorithm to solve NP-Hard Vertex Cover

I found this pseudocode in a textbook but I do not properly understand it, and it was poorly explained. Algorithm 8: Greedy Vertex Cover Algorithm Example(G=(V,E)) 1) C := ;. 2) while (E 6= ;) • Select a node v of maximal degree in G. • C := C…
Barney Chambers
  • 2,720
  • 6
  • 42
  • 78
4
votes
2 answers

How to make a preprocessor macro greedy?

We have the following preprocessor macro. Its used to help with Doxygen documentation because Doxygen has troubles with C++ and some template typedefs: #if defined(DOXYGEN_PROCESSING) # define DOCUMENTED_TYPEDEF(x, y) class y : public x {}; #else #…
jww
  • 97,681
  • 90
  • 411
  • 885
4
votes
2 answers

What's the fastest heuristic algorithm to split students into groups?

I have X number of students, where X is a multiple of 6. I now want to split up the students into groups of 6. I have a function that measures how "good" a group of 6 is (lets say it's a black box that runs in constant time for now). By splitting up…
4
votes
2 answers

greedy algorithm, scheduling

I am trying to understand how Greedy Algorithm scheduling problem works. So I've been reading and googling for a while since I could not understand Greedy algorithm scheduling problem. We have n jobs to schedule on a single resource. The job (i) has…
Andy Min
  • 91
  • 1
  • 6
4
votes
3 answers

Maximum no of trains that k platform can hold

Given arrival and departure times of N trains that reach a railway station, for a given k platforms, return the maximum number of trains that we can house on the k platforms. k <<< N Arrival and departure time Array Input: arr[] = {9:00, 9:40,…
aibotnet
  • 1,326
  • 1
  • 13
  • 27
4
votes
6 answers

How can I fix my regex to not match too much with a greedy quantifier?

I have the following line: "14:48 say;0ed673079715c343281355c2a1fde843;2;laka;hello ;)" I parse this by using a simple regexp: if($line =~ /(\d+:\d+)\ssay;(.*);(.*);(.*);(.*)/) { my($ts, $hash, $pid, $handle, $quote) = ($1, $2, $3, $4,…
Lasse A Karlsen
  • 801
  • 5
  • 14
  • 23
4
votes
1 answer

Minimal number of Increasing Subsequences

I have a sequence of integers {a1,a2...an} and I want to divide it into minimum number of "increasing subsequences". For example: let the sequence be {10,30,20,40}, then the answer would be 2. In this case, the first increasing sequence would be…
divanshu
  • 335
  • 1
  • 5
  • 12
4
votes
1 answer

How can you be sure that a problem exhibits "Greedy choice property"?

I am afraid that there might be a situation for which the "greedy choice property" might not hold. For any problem, I can only check for small data-sets. What if, for large data-sets, the property fails? Can we ever be sure?
Lazer
  • 90,700
  • 113
  • 281
  • 364