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
0 answers

Algorithm to minimize number of operations in multiplication of a binary matrix by a vector

Let D be a type of object which defines the plus and minus operators (to give more context, D is a large matrix of real numbers with fixed dimensions). The plus operator is associative and commutative and returns another object of type D. The minus…
Fabio
  • 2,105
  • 16
  • 26
2
votes
1 answer

Efficient Algorithm for Amount allocation problem

I am wondering if there is an efficient way to solve the following question. We have 2 groups of buckets, which is represented by number arrays. The number is the bucket size. The bucket size and the number of buckets in each group is not limited.…
2
votes
1 answer

Minimum number of jumps II

I am trying to solve the GeeksForGeeks problem Minimum number of jumps: Given an array of N integers arr[] where each element represents the max length of the jump that can be made forward from that element. Find the minimum number of jumps to…
2
votes
1 answer

Proof of optimality of greedy algorithm for scheduling

Unable to come up with a formal proof of optimality for algorithm A for the given problem. Have convinced myself that it is possible to execute some optimal schedule O in increasing order of the events' deadline. But don't know how to formally prove…
Ajax
  • 123
  • 1
  • 7
2
votes
1 answer

Complexity of greedy algorithm for TSP

I have a complete graph G = (V, E), with n vertices. I want to create a path that starts from a vertices, passes through all other vertices exactly once and returns to the start (a.k.a. a Hamiltonian path) of minimum length. Edit: This is the…
2
votes
1 answer

Maximum Sum Elements in Range

Given an array 'A' of size 'N' containing integers. You need to answer 'Q' queries of type [ L R X Y ]. In each of the query you need to select at least 'X' elements and at most 'Y' elements from range 'L' to 'R' of the array 'A' such that their…
raxona3dd
  • 23
  • 4
2
votes
1 answer

Identifying when greedy method gives optimum solution

I was looking at this problem on leetcode https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/description/ The hints tell us to follow a greedy approach where we convert the time difference to minutes and pick the largest…
2
votes
1 answer

Showing that Greedy algorithm exhibits optimal substructure and greedy choice

I am in need of help proving that an algorithm has greedy choice property and optimal substructure. Context of the problem: Consider a problem where a company owns n gas stations that are connected by a highway. Each gas station has a limited…
2
votes
1 answer

Can Someone Explain what is happening on line 4. I mean how the value of 'now' is getting updated?

this is the code a question which uses greedy approach, can someone help what is happening at line 5 i.e., how the value of 'now' getting updated? def calculate(): arr=[30, 15, 60, 75, 45, 15, 15, 45] last,now=0,0 for i in arr: …
user18292009
2
votes
1 answer

0-1 knapsack dynamic programming with two constraints

For a 0-1 knapsack problem with given weights and values of n items, and a maximum weight capacity W, I know it can be solved with a double nested loop using dynamic approach. My question is if we say that each of the n items have an additional…
2
votes
1 answer

Ferry loading problem

I have a difficulty with an undermentioned algorithmic problem: There is a three-lane ferry in a port and in front of it there is a queue of N vehicles. Each of them has got specified lenght in cm. We also know the lenght (L) of the ferry. We…
Teodore
  • 21
  • 1
  • 3
2
votes
1 answer

Open the lock - LeetCode, why counter keeps incrementing in every recursive call?

I am working on the Open the lock challenge on LeetCode: You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we…
2
votes
1 answer

Python: non greedy before or after

I made a few tests to help myself to understand non-greedy in Python, but it made me much more confused than before. Thank you for the help! lan='From 000@hhhaaa@stephen.marquard@uct.ac.za@bbb@ccc fff@ddd eee' print(re.findall('\S+@\S+?',lan)) …
floatShark
  • 23
  • 4
2
votes
1 answer

How to add a edges between component of a graph in igraph R

I have a graph containing 4 components. Now, I want to add an edge among all components based on the size of the membership. For example, the following graph contains 4 components. First, I will connect all components with only one edge and take…
0Knowledge
  • 747
  • 3
  • 14
2
votes
1 answer

smallest match in regular expression

I have this kind of expression: var string = [a][1] [b][2] [c][3] [d .-][] [e][4] I woud like to match the fourth element [d .-][] which may contain any character (letters, numbers, punctuation, etc) within the first pair of bracket but the second…
r_31415
  • 8,752
  • 17
  • 74
  • 121