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

Number of minimum vertex covers in a tree

There are [at least] three algorithms which find minimum vertex cover in a tree in linear (O(n)) time. What I'm interested in is a modification of all of these algorithms so that I'll also get number of these minimum vertex covers. For example for…
user1
  • 945
  • 2
  • 13
  • 37
6
votes
3 answers

Find Maximum Distance between two points

Yesterday, I appeared in an interview. I was stuck in one of the question, and I am asking here the same. There is an array given that shows points on x-axis, there are N points. and M coins also given. Remember N >= M You have to maximize the…
devsda
  • 4,112
  • 9
  • 50
  • 87
5
votes
5 answers

Why does my non-greedy Perl regex match nothing?

I thought I understood Perl RE to a reasonable extent, but this is puzzling me: #!/usr/bin/perl use strict; use warnings; my $test = "'some random string'"; if($test =~ /\'?(.*?)\'?/) { print "Captured $1\n"; print "Matched…
Sundar R
  • 13,776
  • 6
  • 49
  • 76
5
votes
9 answers

Car Fueling Problem by Greedy Algorithm (getting list index out of range)

I have a small problem solving the Car fueling problem using the Greedy Algorithm. Problem Introduction You are going to travel to another city that is located miles away from your home city. Your car can travel at most miles on a full tank and…
theWellHopeErr
  • 1,856
  • 7
  • 22
5
votes
2 answers

An × chessboard is to be cut into its · unit squares

An × chessboard is to be cut into its · unit squares. At each step, you can make either one horizontal cut or one vertical cut. The first cut will split the board into two sub-boards; after that each cut splits one remaining sub-board into two.…
5
votes
1 answer

Maximum Coin Partition

Since standing at the point of sale in the supermarket yesterday, once more trying to heuristically find an optimal partition of my coins while trying to ignore the impatient and nervous queue behind me, I've been pondering about the underlying…
Treecj
  • 427
  • 2
  • 19
5
votes
1 answer

Sequence increasing and decreasing by turns

Let's assume we've got a sequence of integers of given length n. We want to delete some elements (maybe none), so that the sequence is increasing and decreasing by turns in result. It means, that every element should have neighbouring elements…
piternet
  • 315
  • 1
  • 5
5
votes
2 answers

Can Binary Search be / Is Binary Search a greedy algorithm?

I'm reading different materials about Binary search, and it's not clear to me is it a greedy binary (which looks to me like it's not) or, CAN it be a greedy algorithm with some specific implementation? And if it can be greedy, how it make sense? If…
Johnny
  • 14,397
  • 15
  • 77
  • 118
5
votes
2 answers

The two pointer algorithm

I'm trying to understand the two pointer algorithm approach, so I've been reading this article So here is the question. Suppose we have an array of N elements. And we want to find the largest contiguous sequence of elements in that array where the…
Rockstar5645
  • 4,376
  • 8
  • 37
  • 62
5
votes
1 answer

C++ regex for overlapping matches

I have a string 'CCCC' and I want to match 'CCC' in it, with overlap. My code: ... std::string input_seq = "CCCC"; std::regex re("CCC"); std::sregex_iterator next(input_seq.begin(), input_seq.end(), re); std::sregex_iterator end; while (next != end)…
Gábor Erdős
  • 3,599
  • 4
  • 24
  • 56
5
votes
1 answer

C round function is throwing error: "undefined reference to `round'..."

I hope someone can help me. I am working through CS50x and am working on Pset1 - greedy. I am getting the following error whenever I compile my code: /tmp/greedy-46be96.o: In function `main': greedy.c:(.text+0x95): undefined reference to…
Phrike
  • 91
  • 1
  • 2
  • 7
5
votes
1 answer

Where is the flaw in my algorithm to find whether there exists a permutation of two arrays A,B such that they have (A[i]+B[i]) >= k

For example, with k=10 A=[2,1,3] B=[7,8,9] the answer is yes because you can re-arrage the elements to be A=[1,2,3] B=[9,8,7] which then it's true that A[i]+B[i] >= 10 = k for i=0,1,2. My algorithm is greedy, like int k = parameters[1]; int[] A =…
user6048670
  • 2,861
  • 4
  • 16
  • 20
5
votes
2 answers

knapsack algorithm with two weights

I have questions which is the following: Solve the knapsack 0-1 problem(not fractional) Assuming that every object have weight w1 or w2 (there only two weights). Capacity=W, the algorithm must run on O(nlogn). I tried to solve, the greedy…
5
votes
1 answer

Greedy Algorithm Java / firstFit method

I am taking a data structures and algorithms with Java class at my local community college, and I am completely stuck on my current homework assignment. The problem is as follows... Write a program that packs the objects of various weights into…
P. Fernandez
  • 187
  • 1
  • 6
5
votes
1 answer

How to do conditional greedy match in Perl?

I want Perl to parse a code text and identify certain stuffs, example code: use strict; use warnings; $/ = undef; while () { s/(\w+)(\s*<=.*?;)/$1_yes$2/gs; print; } __DATA__ always @(posedge clk or negedge rst_n) if(!rst_n)begin …
katyusza
  • 325
  • 2
  • 12