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

Greedy Algorithm Implementation

You know who knows who among n people that you would like to have come to a party. Assume "knows" is symmetric: If I know you, you know me. You make further requirements that you want each person to have at least 5 new people to meet at the party,…
Peter Weglarz
  • 81
  • 1
  • 1
  • 2
8
votes
4 answers

When locally optimal solutions equal global optimal? Thinking about greedy algorithm

Recently I've been looking at some greedy algorithm problems. I am confused about locally optimal. As you know, greedy algorithms are composed of locally optimal choices. But combining of locally optimal decisions doesn't necessarily mean globally…
Ivan
  • 766
  • 6
  • 17
8
votes
3 answers

Dynamic programming get maximum diamond

I was trying to solve one Interview problem that is: Given a matrix of n*n. Each cell contain 0, 1, -1. 0 denotes there is no diamond but there is a path. 1 denotes there is diamond at that location with a path -1 denotes that the path is…
Khatri
  • 616
  • 6
  • 19
8
votes
3 answers

How is Greedy Technique different from Exhaustive Search?

I have some sample problems that I'm writing pseudo code for and I'm noticing alarming patterns between the greedy technique and exhaustive search. Job 1, Job 2, Job 3, Job 4, Job 5 Person: 1 9 2 7 8 Person: 2 6 4 …
Stubudd
  • 141
  • 2
  • 8
8
votes
2 answers

Why is the greedy algorithm optimal?

Codility, lesson 14, task TieRopes (https://codility.com/demo/take-sample-test/tie_ropes). Stated briefly, the problem is to partition a list A of positive integers into the maximum number of (contiguous) sublists having sum at least K. I've only…
NPS
  • 6,003
  • 11
  • 53
  • 90
8
votes
2 answers

What is the term for the opposite of eager/greedy search?

So an eager search is where you take an initial solution even if a better solution is just down the road... what is the opposite term for eager search? All my google results get me a reference to Paul Revere's ride. In these times of chaos and…
bharal
  • 15,461
  • 36
  • 117
  • 195
8
votes
4 answers

What is the correct approach to solve SPOJ DIEHARD?

I was trying to solve a practice question on SPOJ https://www.spoj.pl/problems/DIEHARD/ . However both my greedy approach resulted in Wrong Answer and recursion was too slow for the worst case.Can anyone tell how to approach this problem? I am…
user1724072
  • 331
  • 1
  • 3
  • 8
7
votes
4 answers

Is there an edge we can delete without disconnecting the graph?

Before I start, yes this is a homework. I would not have posted here if I haven't been trying as hard as I could to solve this one for the last 14 hours and got nowhere. The problem is as follows: I want to check whether I can delete an edge from a…
Fingolfin
  • 5,363
  • 6
  • 45
  • 66
7
votes
4 answers

Dynamic Programming - making change

I'm having trouble figuring out my last section of code for a Dynamic Coin Changing Problem. I have included the code below. I can't figure out the last else. Should I just use the greedy algorithm at that point or can I calculate the answer from…
T. Thomas
  • 660
  • 3
  • 10
  • 25
7
votes
1 answer

Longest K Sequential Increasing Subsequences

Why I created a duplicate thread I created this thread after reading Longest increasing subsequence with K exceptions allowed. I realised that the person who was asking the question hadn't really understood the problem, because he was referring to a…
Ermolai
  • 303
  • 4
  • 15
7
votes
1 answer

Greedy algorithm for finding a negafibonacci representation of a number?

According to Zeckendorf's theorem, every positive integer can be written in a unique way as the sum of non-consecutive distinct Fibonacci numbers. Such a decomposition can be found easily with a greedy algorithm consisting essentially in subtracting…
7
votes
4 answers

Why does Kruskal's algorithm find the minimum spanning tree if it's greedy?

Why does Kruskal's algorithm find the minimum spanning tree if it's greedy? Isn't a minimum spanning tree a global optimization problem? Isn't the point of being greedy is that there is a chance you won't find the most optimal solution? So how can…
DiderDrogba344
  • 534
  • 8
  • 17
7
votes
3 answers

Two player grid traversal game

Given a M * N grid and location of two players p1 and p2on grid. There are n balls placed on different positions on the grid. Let the location of these balls be B(1), B(2), B(3) ..., B(n). We need to calculate the minumum manhattan distance…
Nikhil Pathania
  • 812
  • 6
  • 19
7
votes
3 answers

Greedy algorithm: Interval coloring

In interval scheduling, the algorithm is to pick the earliest finish time. But in interval colouring the former does not work. Is there an example or explanation on why picking earliest finish time won't work for interval colouring? The interval…
AlwaysNull
  • 348
  • 2
  • 4
  • 15
7
votes
2 answers

Point covering problem

I recently had this problem on a test: given a set of points m (all on the x-axis) and a set n of lines with endpoints [l, r] (again on the x-axis), find the minimum subset of n such that all points are covered by a line. Prove that your solution…
Sean
  • 3,002
  • 1
  • 26
  • 32
1 2
3
68 69