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

Greedy Maximum Flow

The Dining Problem: Several families go out to dinner together. In order to increase their social interaction, they would like to sit at tables so that no two members of the same family are at the same table. Assume that the dinner contingent has p…
Manuel
  • 247
  • 2
  • 3
  • 14
3
votes
3 answers

Positive Distinct Summands in Python

This Code is giving wrong output for n=2 and it is very slow. How Can i make this code for finding as many positive distinct summands more efficient? TASK- Represent a given positive integer n as a sum of as many pairwise distinct positive integers…
3
votes
2 answers

JavaScript - What's wrong with this coin change algorithm

I'm trying to use the greedy algorithm for calculating the minimum number of coins needed to reach an amount in JavaScript The return result will be an array consisting of the number of coins at each level I decided to make a function that would…
user5680735
  • 703
  • 2
  • 7
  • 21
3
votes
6 answers

Smallest Lexicographic Subsequence of size k in an Array

Given an Array of integers, Find the smallest Lexical subsequence with size k. EX: Array : [3,1,5,3,5,9,2] k =4 Expected Soultion : 1 3 5 2
curious_cat
  • 886
  • 2
  • 11
  • 14
3
votes
3 answers

Greedy algorithm or dynamic programming?

There is list l = [x_1, ..., x_n] given. Every element of the list is the length of some piece of wood. To glue two pieces of wood, of lengths a and b you need max(a,b) of glue. After glueing, you got one piece of wood of length a+b. Compute minimum…
piternet
  • 315
  • 1
  • 5
3
votes
3 answers

Most efficient seating arrangement

There are n (n < 1000) groups of friends, with the size of the group being characterized by an array A[] (2 <= A[i] < 1000). Tables are present such that they can accommodate r(r>2) people at a time. What is the minimum number of tables needed for…
SHB
  • 589
  • 1
  • 6
  • 18
3
votes
2 answers

linear solution of grid constraints

You have a grid n x n with n rows and n columns. For every column j you are given a number Cj and for every row i you are given a number Ri. You need to mark some points on the grid, in this way: the number of marked points in every row is at most…
3
votes
1 answer

Implementing Activity Selection Prob using Dynamic Programming

How to implement Activity Selection Problem using Dynamic Programming (CLRS Exercise 16.1-1). I've implemented it using Greedy Method, which runs in linear time (assuming array is already sorted with finish time). I know it poses Optimal…
user5328749
3
votes
1 answer

Dynamic vs Greedy Algorithms : The debate regarding Neil G's answer on the same topic

I was trying to understand the differences between Dynamic and Greedy algorithms, and This answer by Neil G was quite helpful, but, there was this one statement that he made which caused a debate in the comments section. The difference between…
Somjit
  • 2,503
  • 5
  • 33
  • 60
3
votes
1 answer

Find the maximum height they can make by standing on each other?

Weights of n men and their strengths (max weight they can carry) are given. Height of all are same and given. Find the maximum height they can make by standing on each other? That means, you have to place them by taking maximum number of men from…
nits
  • 31
  • 2
3
votes
2 answers

A way to prove that there's no greedy algorithm that obtains optimal solution?

The question is pretty simple. I need to prove that there's no greedy algorithm that can obtain the optimal solution for a given problem. It is unclear to me if there is any condition that a problem must meet so that exists a certain greedy…
D1X
  • 5,025
  • 5
  • 21
  • 36
3
votes
1 answer

How to prove that Greedy approaches will not work

For any given problem where greedy approaches will not give optimal value, we can find a counter example to disprove that approach. However, is it possible to prove that for a given problem, any greedy approach in general will not work.
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
3
votes
1 answer

Trying to gain intuition for work scheduling greedy algorithm

I have the following scenario: (since I don't know of a way to show LaTeX, here's a screenshot) I'm having some trouble conceptualizing what's going on here. If I were to program this, I would probably attempt to structure this as some kind of heap…
user3713712
  • 105
  • 1
  • 2
  • 6
3
votes
1 answer

Greedy Algorithm for activity selection with activity value (CLRS 16.1-5)

Is there a greedy algorithm possible for this problem. I have worked out a DP algorithm for it but am not sure of a Greedy Algorithm for it. Please Explain if a greedy algorithm exists for it. For those who are not familiar with the problem: There…
KayEs
  • 135
  • 8
3
votes
4 answers

Issue in making a String Algorithm

Given a string made up of 'a' and 'b' only,the operation that is allowed is to remove a substring of "abb" if present from the string. My question is after applying this operation any no of times can i make the string empty. I need a O(n)…