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
14
votes
6 answers

Given an array of integers, find the LARGEST number using the digits of the array such that it is divisible by 3

E.g.: Array: 4,3,0,1,5 {Assume all digits are >=0. Also each element in array correspond to a digit. i.e. each element on the array is between 0 and 9. } In the above array, the largest number is: 5430 {using digits 5, 4, 3 and 0 from the array} My…
user1599964
  • 860
  • 2
  • 13
  • 29
13
votes
7 answers

Minimize the maximum difference between the heights

Given heights of n towers and a value k. We need to either increase or decrease height of every tower by k (only once) where k > 0. The task is to minimize the difference between the heights of the longest and the shortest tower after modifications,…
Ajay Singh
  • 161
  • 1
  • 1
  • 8
13
votes
2 answers

Correctness of greedy algorithm

In non-decreasing sequence of (positive) integers two elements can be removed when . How many pairs can be removed at most from this sequence? So I have thought of the following solution: I take given sequence and divide into two parts (first and…
lemoid
  • 147
  • 1
  • 7
12
votes
2 answers

Polynomial time solution for Tetris Puzzle

This is a puzzle based on Tetris. In this puzzle we are given the sequences of next n pieces that are going to fall from top. Our job is to maximize the score before its GameOver. There is no polynomial time algorithm known for the general Tetris…
xashru
  • 3,400
  • 2
  • 17
  • 30
12
votes
3 answers

Regex is behaving lazy, should be greedy

I thought that by default my Regex would exhibit the greedy behavior that I want, but it is not in the following code: Regex keywords = new Regex(@"in|int|into|internal|interface"); var targets = keywords.ToString().Split('|'); foreach (string t…
Stomp
  • 890
  • 1
  • 6
  • 19
11
votes
5 answers

Is Kadane's Algorithm Greedy or Optimised DP?

I feel like Kadane's algorithm is a modified version of the true dynamic programming solution of maximum subarray problem.Why do I feel so? I feel because the way to calculate the maximum subarray can be taken by: for(i=0;i
Shubham Sharma
  • 799
  • 7
  • 31
11
votes
1 answer

Determine if the solution can be optimally given using greedy algorithm

Most of the times the confusing fact is whether to go for an exhaustive search (dynamic programming or back tracking or brute force) to solve the problem or to go for the greedy approach. I am not talking about using greedy to determine the best…
Mohan
  • 1,850
  • 1
  • 19
  • 42
10
votes
3 answers

Optimal solution for creating a pile of boxes

I have a problem with one algorithm. There are given n boxes, each one has fixed weight and strength (both given in kg). Box's strength tells us what is the maximum weight it can bear. We have to form the highest pile of given boxes (each of them…
RobertMarianski
  • 101
  • 1
  • 4
10
votes
3 answers

Algorithm to "transfer water from a set of bottles to another one" (metaphorically speaking)

Ok, I have a problem. I have a set "A" of bottles of various sizes, all full of water. Then I have another set "B" of bottles of various sizes, all empty. I want to transfer the water from A to B, knowing that the total capacity of each set is the…
luca
  • 12,311
  • 15
  • 70
  • 103
10
votes
1 answer

Unable to understand algorithm

Here is the link of problem https://www.hackerrank.com/challenges/equal I read its editorial and unable to understand it. And if you are not make any account on hackerrank then surely you will not see it's editorial so here is some lines of…
Vijay Sharma
  • 342
  • 4
  • 15
10
votes
6 answers

Optimal room count and sizes for N overlapping Meeting Schedules

I bumped into this question and I am not sure if my solution is optimal. Problem Given N weighted (Wi) and possibly overlapping intervals (representing meeting schedules) , find the minimum number "&" capacity of meeting rooms needed to conduct all…
10
votes
1 answer

Greedy algorithms and optimal substructure

On Wikipedia page it is said that greedy algorithms are ideal only for problems which have optimal substructure. Questions: What is optimal/non-optimal substructure? What is local and global optimum? How to prove that Greedy algorithm yields global…
Ivan Voroshilin
  • 5,233
  • 3
  • 32
  • 61
9
votes
4 answers

How to traverse through all possible paths to a solution and pick the optimum path

I am not good at programmatically implementing a heuristic search algorithm / Dijkstra's algorithm/ A* search algorithm mentioned. However, while solving a problem mentioned in one of my post (Matrix manipulation: logic not fetching correct answer…
yeppe
  • 679
  • 1
  • 11
  • 43
9
votes
3 answers

Watch all movies algorithm

I came across this problem which seems pretty interesting. There are a few movies that we want to watch all of them but they only show at the following times: movieA : 15 movieB : 14, 15, 17 movieC : 15, 17 movieD : 15, 20 We can watch A at 15, B…
Arch1tect
  • 4,128
  • 10
  • 48
  • 69
9
votes
3 answers

Minimizing weighted sum

I came across this problem quite recently. Suppose there are n points on x-axis, x[0],x[1] .. x[n-1]. Let the weight associated with each of these points be w[0],w[1] .. w[n-1]. Starting from any point between 0 to n-1, the objective is to cover…
n00bc0d3r
  • 275
  • 2
  • 10
1
2
3
68 69