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

Finding the Maximum and Minimum Number of Operations to Delete all Element in a Circular List

In a circular list of length n, where a1 is adjacent to a2, a2 is adjacent to a3, and an is adjacent to a1. Each operation can delete one number, after each deletion, if there exists adjacent equal numbers, one of them will be automaticlly deleted…
Yu Xing
  • 19
  • 3
-1
votes
1 answer

error in arraylist sorting using collections.sort()

I am writing a code on fractional knapsack. But, the sorting algorithm is not working well. And hence my code is not working correctly. This is my code: import java.util.*; class Item { int profit, weight; float ppw; //ppw is profit per…
-1
votes
2 answers

Sum of subsequences in cpp, I have to print all the sums of subsequences. I'm storing it in vector g here. result shows g is empty

class Solution { public: void f(vector arr, int N, int i , int sum, vector g){ if (i>= N){// when index reaches the end of the array g.push_back(sum);//add the final sum to vector g return; …
-1
votes
1 answer

In which cases Dijkstra works for Negative Weights?

Dijkstra isn't assured to work on graphs with negative weights: Why doesn't Dijkstra's algorithm work for negative weight edges?, but can I assume it works for one of the following cases (even with negative weights) Directed Graph with no Directed…
user20217739
-1
votes
1 answer

Python "Interval Scheduling algorithm" went wrong

use python to implement this algorithm the input should be a nested lsit. e.g.: job = [[0,1],[1,5],[2,3],[2,5],[5,7]] expected result is: [[[0, 1], [1, 5], [5, 7]], [[2, 3], [3, 4]], [[2, 5]]] def interval_partitioning(job): #sort the…
jwan218
  • 1
  • 2
-1
votes
1 answer

Mailman uses greedy algorithm to deliver as many letters as possible

So my professor asked this on the test the other day and I am not sure I got it right. A mailman has only one hour to deliver as many letters as possible(one by one). In order to deliver a letter in distance n it takes 5 + 0 .5logn minutes (log with…
Katerina
  • 7
  • 1
-1
votes
1 answer

Minimum jumps required to make a group of persons sit together- Proof of Greedy Approach?

I found one greedy algorithm problem, where we have to find min no. of jumps required to make persons sit together. Here is a link to the article for reference. Question Description from article: Title: Minimum jumps required to make a group of…
Aayush
  • 45
  • 2
  • 7
-1
votes
2 answers

Expected primary-expression before ‘long’

What is the problem with my code ? prog.cpp: In function ‘void helper()’: prog.cpp:15:25: error: expected primary-expression before ‘long’ ans = ans + max(long long(0),vec[i]-i); #include using namespace std; #define ll long…
-1
votes
2 answers

sorting array of arrays in descending order using arrays.sort function is resulting in errors in java

problem:https://leetcode.com/problems/maximum-units-on-a-truck/ I am supposed to sort array of arrays of size 2(eg. [[1,3],[2,2],[3,1]]) in descending order according to 2nd value of the inner element. i.e for 1st element[1,3]according to value 3. ,…
smit
  • 59
  • 1
  • 6
-1
votes
2 answers

Determine Maximum Profit Algorithm C++

Consider the following problem: The Searcy Wood Shop has a backlog of orders for its world famous rocking chair (1 chair per order). The total time required to make a chair is 1 week. However, since the chairs are sold in different regions and…
-1
votes
1 answer

RegEx is Too Greedy

I have requirement to validate a range. The input is in the following format: string example1 = "anydate between 20100101 ~~ 20100101"; string example2 = "anydate between 20100101 and 20100101"; string example3 = "docid between 1 ~~ 2"; I'm using…
Naga
  • 9
  • 2
-1
votes
2 answers

Not able to understand one Top Coder entry on Greedy Algorithms

I am actually practicing Greedy Algorithms and There is a problem on topcoder.. So need some help.... The problem is about Robust Computer System.. http://www.topcoder.com/stat?c=problem_statement&pm=2235&rd=5070 I don't understand what is MFC.…
AlgoGeek
  • 91
  • 2
  • 8
-1
votes
1 answer

Is a polynomial time algorithm possible for this question or is it a NP-hard problem? If yes, can someone help me with it?

We have been given tree lets say T with vertices set V, now try giving an algorithm to find the minimum cardinality subset of vertices W. Given every vertex in the set V has an edge with at least one vertex in set W.
-1
votes
1 answer

Make the string lexicographically smallest and non palindromic by replacing exactly one character

Given a palindromic string palindrome, replace exactly one character by any lowercase English letter so that the string becomes the lexicographically smallest possible string that isn't a palindrome. After doing so, return the final string. If…
curiousD
  • 27
  • 6
-1
votes
1 answer

Should this be solved by DP instead?

I am trying to solve a problem: Given an NxM matrix, each element represents the cost associated to buy an item. Given that we plan to buy M-1 items from each of the N rows, what is the minimum amount that we would have to spend, if we plan to buy…
P.K.
  • 379
  • 1
  • 4
  • 16