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

Codeforces question (unable to detect problem in my code)

Over the past 2 days i was practising on codeforces . I am new to programming and currently doing implementation problems. This question is 1337B ( KANA AND DRAGON QUEST) https://codeforces.com/problemset/problem/1337/B I cannot figure out my…
Space 007
  • 3
  • 2
-1
votes
2 answers

Having problem in compiling this Greedy Algorithm's python question

I was trying this question which is a greedy algorithm question. The celebration party problem. When i run it , as you can see below, it says list indices must be integer.. Can you help me with this, I am new to algorithm coding. I am also open to…
-1
votes
1 answer

How to approach the following greedy/dynamic problem?

I have to check if it is possible to get sum from given numbers >to be strictly greater than given sum for example: if given sum is 9 and the given numbers are [3,5,7] then the correct answer is 1*3 +1*7=10>9 so the out put should be [1,0,1]…
gaurav
  • 15
  • 2
-1
votes
1 answer

Create a new list in a for loop every time necessary

So I am trying to create a new SEPARATE list every time a statement is true in python. Essentially: for i in range(z): if X is true: create a new list There is no way to know how many lists I need, I can try to estimate as I am doing a…
-1
votes
1 answer

Algorithm to find maximum number of meetings possible with some constraints

Let's say I want to meet with some friends and they send me their availabilities on what days they can meet in an interval format. For example [1,4] means that a friend can meet me on days 1,2,3, and 4. I have a list of all the intervals for my…
genescuba
  • 41
  • 5
-1
votes
3 answers

Determine the minimum number of coins required for giving a change using greedy approach

As an assignment for my course on Design and Analysis of Algorithms, I was asked to determine the minimum number of coins required for giving a change, using a greedy approach. I came up with this intuitive…
Divyanshu Varma
  • 122
  • 3
  • 17
-1
votes
1 answer

Programming_question on dynamic programming

A company produces a product. The production is done in a very mysterious way. The firm either produces one extra product in a day or they have a power to double the production of products they had on the previous day. Initially they have only 1…
K. Bakshi
  • 41
  • 5
-1
votes
1 answer

Activity Selection using python

I'm trying to solve the Activity Selection problem using the greedy algorithm. My code fails for some inputs. I'm sorting the activity list based on the finish_time_list. The code works fails for the following input: activity_list=[1, 2, 3, 4, 5,…
HackersInside
  • 307
  • 2
  • 6
  • 17
-1
votes
1 answer

the error in python greedy matching

import re greedyHaRegex = re.compile(r'(Ha){3, 5}') mo1 = greedyHaRegex.search('HaHaHaHaHa') mo1.group() Traceback (most recent call last): File "", line 1, in mo1.group() AttributeError: 'NoneType' object has no attribute…
-1
votes
1 answer

reason for the failure of greedy algorithm in following case

I tried solving one problem which was claimed to be solvable using Dynamic programming only. Following is the problem. A man has total energy as H and he needs to cover distance D. He wants to cover this distance using maximum energy upto H in…
rainyday
  • 353
  • 3
  • 17
-1
votes
1 answer

Evenly color graph in ruby

Please let me know the problem in the code that I implemented. Problem Statement: There are n nodes and m colors to be filled in a Graph g. Assign colors to the nodes such that no two adjacent nodes have same colors and try to evenly distribute the…
krnbatta
  • 497
  • 5
  • 17
-1
votes
1 answer

Reverse Huffman's algorithm?

I have a problem simlar to Huffman's encoding, I'm not sure exactly how it can be solved or if it is a reverse Huffman's encoding. But it definitely can be solved using a greedy approach. Consider a set of length, each associated with a…
bli00
  • 2,215
  • 2
  • 19
  • 46
-1
votes
3 answers

a slight confusion with floating point values in C. (cs50 pset1 greedy.c)

I can't understand why the code below doesn't work properly with the value 4.2. I learnt using a debugger that 4.2 isn't actually the number four point two; rather as a floating point value 4.2 becomes 4.19999981 To make up for this, I just added…
withwavy
  • 33
  • 5
-1
votes
2 answers

Balanced Partition greedy approach

I was looking at the balanced partitioning problem here and here (problem 7). The problem basically asks to partition a given array of numbers into 2 subsets (S1 and S2) such that absolute difference between the sums of numbers is S1 ans S2 |sum(S1)…
kmad1729
  • 1,484
  • 1
  • 16
  • 20
-1
votes
1 answer

check50 not working properly

check50 gives weird output please help #include #include #include int main(void) { int amount,n25,n10,n5,n1,total; int rem; float gamount; printf("O hai!"); do{ printf("How much change is owed?\n"); …
ydj
  • 1