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

Greedy Algorithm - Roman Numerals

I'm trying to implement a C program which converts an integer n (between 0 and 1024) into a Roman numeral by way of a greedy algorithm. I have tried to do this in the following manner: #include #include void convert(int); int…
Luke Collins
  • 1,433
  • 3
  • 18
  • 36
-2
votes
6 answers

CS50 PSET 1 Greedy

I am really new to programming and I just started trying out some problem sets on harvard's CS50. Would appreciate it if anyone can point out to me why my code is wrong. After I compiled and run the code, there isn't any output. On another note,…
cornstar94
  • 57
  • 2
  • 11
-2
votes
1 answer

Algorithm for maximizing happiness when distributing objects to 2 groups

You have an array of n fruits you must give to 2 people, X and Y. X will gain x_i happiness when eating the ith fruit, Y will gain y_i happiness when eating the ith fruit. We want to maximize total happiness of X and Y. The distribution of fruits…
-2
votes
2 answers

Python greedy thief

Why is it giving me an error " 'int' object is not subscriptable " when i run the program? I looked if i was doing anything wrong, i understand it has to be an integer on line 24, but when I'm changing capacity[1] to capacity(int[1]) , it gives me…
-2
votes
1 answer

Greedy Algorithm Optimization

Consider a DVR recorder that has the duty to record television programs. Each program has a starting time and ending time. The DVR has the following restrictions: It may only record up to two items at once. If it chooses to record an item, it…
user3188300
  • 341
  • 2
  • 10
-2
votes
3 answers

Minimization of the maximum element in an array

You have an array of size n of positive integers on which you can do the following operation by at most N times: Choose a sub-array and decrease its elements value by k (k must be smaller than the sub-array's minimum value). Such an operation…
GEP
  • 311
  • 3
  • 12
-3
votes
2 answers

Fastest way to find out the least nr of m given sets reunited include a separate m+1 set

The following gives a practical example: Let's say for m = 4: // the sets for reuniting Set1 = { 5 , 1 , 2 } Set2 = { 2 , 6 , 3 } Set3 = { 7 , 8 , 4 } Set4 = { 4 , 9 , 10} // the set I need to form Set m+1: Set5 = { 1 , 2 , 3 , 4 } I have to find…
-3
votes
1 answer

Getting a signed integer overflow on a Leetcode problem

I am trying the problem : 1833. Maximum Ice Cream Bars from Leetcode: It is a sweltering summer day, and a boy wants to buy some ice cream bars. At the store, there are n ice cream bars. You are given an array costs of length n, where costs[i] is…
-3
votes
1 answer

applying non-zero offset 4 to null pointer

There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart…
-3
votes
1 answer

How to get solution path from greedy algorithm?

I have greedy algoithm with job scheduling problem, but I want to return which one projects were chosen to get this max value, how can I do that? from dataclasses import dataclass from datetime import date @dataclass class InvestmentProject: …
Reboot
  • 7
  • 4
-3
votes
1 answer

How to solve 0/1 knapsack by greedy algorithm and only focus on benefit?

I need to write C++ to solve 0/1 knapsack problem by greedy algorithm. Because it is a NP complete problem so we can only find the nearest solution.I have already finished it by ratio(value/weight), but dont know how to turn Benefit focus…
ash dong
  • 25
  • 2
-3
votes
1 answer

I am getting segmentation fault while running this hackerrank Gridland metro's solution

#include #include #include using namespace std; int main() { //start of input long n,m,k; cin>>n>>m>>k; bool a[n][m]; for(long i=0;i
-3
votes
1 answer

return unique items that can be bought with a certain amount

we have few items sorted in a particular order... all items have associated prices p(p1, p2...pn) say item1 - p1 item2 - p2 . . . itemn - pn we have x amount to buy items. and initially 0 < x <= p after each iteration x will become x-p1,…
Curi0usM3
  • 33
  • 5
-3
votes
3 answers

cs50 pset1 greedy unusual bug

My code for the greedy programme works fine for all numbers so far except for 4.2 . Would really appreciate it if anyone can point out the error :) greedy.c exists :) greedy.c compiles :) input of 0.41 yields output of 4 :) input of 0.01 yields…
-3
votes
2 answers

Solving Kaeru Jump (ACM ICPC Japan)

This is from ACM ICPC Japan. The question along with diagram is explained in link here. Can anyone help me with the solution logic? I know only the naive brute force approach which is not passing me here.
Prem Anand
  • 97
  • 8
1 2 3
68
69