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

Finding maximum element in an array - is it that you will call greedy algorithm or brute force algorithm or both?

Suppose an array of integers is given: {1,3,2,4,6,5,2} - MAX: 6 Using Brute force, finding maximum element in this is to observe every element (each element is a candidate for the solution), thus searching entire search space. Considering Greedy…
copo
  • 41
  • 10
-3
votes
1 answer

could not prove dynamic programming requirements

I am not able to prove the optimal substructure and overlapping subproblem property for the problem in the link. The exact problem with me is that even after understanding standard problems of dp , i face problem in approaching the newer problems.…
suraj bora
  • 61
  • 1
  • 11
-3
votes
1 answer

Greedy algorithm for finding minimum numbers of stops

Mr X is traveling by car on an expressway. Suppose there are several gas (petrol) stations on the way: at distances 0 = d0 < d1 < d2 < ... < dn from the starting point d0. Mr X’scar, when full, can travel a distance D >= max{di+1 - di} . Mr X…
Jun Hao
  • 209
  • 4
  • 9
-3
votes
1 answer

greedy algorithm pseudo code

Suppose you have to drive from Islamabad to Lahore. At the start, your gas tank is full. Your gas tank, when full, holds enough gas to travel m miles, and you have a map that gives distances between gas stations along the route. Let d1 < d2 < … < dn…
user2617096
  • 11
  • 1
  • 3
-3
votes
2 answers

perl pattern matching non-greedy match for string

I have a string which is like ascv/zxc/zxc-asd/zx.java now I wish to cut the string at second / and get the String value as ascv/zxc. Similarly I will input the special character type and its level. Based on the input it should cut the string. E.g.…
usercm
  • 45
  • 1
  • 1
  • 4
-4
votes
1 answer

Minimum cost to repair road

Let us assume there is highway There are N potholes at points [p1, p2, p3...., pn] There are equal number of service crew at points [s1, s2, s3...., sn] One service crew can repair one pothole only The cost to send a crew at point pX to repair a…
Hacky Dev
  • 3
  • 1
  • 4
-4
votes
1 answer

How to solve this question asked in premium coding contest?

Could someone guide me on how to solve this programming question? It seems something like DP problem which I couldn't find answer for. Question: There are ‘n’ ads. Each ad has an effectiveness value associated with it which is given in an array…
-4
votes
1 answer

programming developer hiring challenge problem

There are N teams in a software company. The ith team has Bi employees in it and a total budget of Ai units of money. Each team has to divide their budget within employees equally. But for some teams it's not possible. Therefore the company has to…
-4
votes
2 answers

Greedy algorithm for scheduling?

what is Greedy solution for scheduling problem when running time (like 5 minutes, 10 minutes, not starting and ending time ) of problem is given and you have m process to run them then what is minimum of maximum time ? Assume i have nine jobs (…
shark_s
  • 31
  • 5
-4
votes
1 answer

Design a Greedy algorithm for this preblem

In the interval covering problem, we are given n intervals [s1,t1), [s2,t2), ···, [sn,tn) such that S i∈[n][si,ti) = [0,T). The goal of the problem is to return a smallest-size set S ⊆ [n] such that S i∈S[si,ti) = [0,T). Design a greedy…
-4
votes
2 answers

Dynamic Programming vs Greedy Approach?

I see people biased towards DP approach over the greedy approach because it can solve optimization problems. What you guys think which one of them is preferable? I need to collect arguments in favor of preferable technique to argue with my mates.…
John Tota
  • 61
  • 1
  • 1
  • 3
-5
votes
1 answer

I was trying to write a Greedy Algorithm in C

Does this make any sense? I got stuck in here with 4 errors and it is because I didn't declared the ints q,d,n,p. But if I do so it'll keep sending me more errors. There might be something about having mixed ints and floats. #include…
msk1
  • 1
  • 1
1 2 3
68
69