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

Maximum N digit number we can make with certain number of match sticks

The picture shows how many sticks you need to draw each of the digits. given a number that consists of N digits. I want to move some sticks to maximize the number. I am not allowed to change the number of digits. It had to have N digits always.I…
-1
votes
1 answer

undirected graph what is the possible minimum spanning tree?

I try to solve this question but I am not sure of my solution the quetion This is my solution: I use Kruskal algorithm and I choose the edges that will make (a) leaf solution thank you.
Manal
  • 43
  • 4
-1
votes
2 answers

Greedy.c outputting the wrong number and I have no idea why? (CS50 Code)

I am working on a program to work out the least number of coins for an amount of change given by the user. It is meant to check for values of 0.25, 0.10, 0.05 and 0.01. If the user's value is greater than, or equal to one of these (in a set of…
-1
votes
2 answers

USACO Training: Mixing Milk fails on the last Case

Here is the Mixing Milk Problem The Merry Milk Makers company buys milk from farmers, packages it into attractive 1- and 2-Unit bottles, and then sells that milk to grocery stores so we can each start our day with delicious cereal and milk. Since…
Henry Zhu
  • 2,488
  • 9
  • 43
  • 87
-1
votes
1 answer

Calculating the set of computers infected by malware, in a network of communicating computers?

A little backstory: It has been a really long time since I've coded in C (a year. Jumping back into this is really overwhelming. I need some advice on implementation. Could someone could explain how you would go about designing this? I understand…
-1
votes
1 answer

Maximum sum obtained after picking numbers in magical order from an array

There are N integers in an array.If you select element at index "i",then you get array[i] value in your packet, and array[i],array[i-1] and array[i+1] becomes zero, after selecting array[i] (i.e You can't take these elements any more in next…
-1
votes
1 answer

OpenGL greedy meshing using VBO

I am trying to reduce the number of faces that are rendered in my voxel engine by implementing a greedy meshing algorithm similar to Mikola's (http://0fps.net/2012/06/30/meshing-in-a-minecraft-game/) Since I am using a VBO to draw each triangle for…
user4743748
-1
votes
1 answer

how to prove this is a matroid in greedy solving?

Suppose we have a finite set called P and we have partitioned it into separate subsets p1, p2, ... pj we define q as all subsets S which at most have one member of each pi. so q = { s:|intersect(s,pi)| <= 1, for i = 1...j } prove that (P,q) is a…
user3070752
  • 694
  • 4
  • 23
-1
votes
2 answers

Greedy Algorithm Nodes

I was asked this question on an interview. Can someone give me some insight on how to do this? I was stumped Often, greedy algorithms they are used as heuristics. An independent set in an undirected graph G is a set of nodes I so that no edge has…
user3602193
  • 11
  • 1
  • 1
  • 3
-1
votes
2 answers

finding the min number of moves by greedy algorithm

There is a 2-d grid which contains chocolates in random cells.In one move ,i can take all the chocolates contained in one row or in one column.What will be the minimum number of moves required to take all the chocolates? Example:cells containing…
RKTSP
  • 99
  • 1
  • 8
-1
votes
1 answer

Disjoint Set Forest to schedule jobs

How can I use the disjoint set- forests to schedule jobs with penalties, such that the penalties are minimized? We could first arrange the jobs in decreasing order on the basis of their penalties. Each node x of the forest will represent the job…
tanvi
  • 927
  • 5
  • 17
  • 32
-1
votes
1 answer

Matlab: cumsum and exhaustive unique element's indexing with greedy perspective

Trying to understand how to manipulate two matrices having the following elements (i ll try to simplify my problem so to be easier to answer) by expanding my previous question: First matrix: 8 2 5 Mat1 = [ 3 7 8 ] 6 5 0…
professor
  • 247
  • 3
  • 12
-1
votes
3 answers

How to spot an "A*" (A STAR) algorithm?

My intuition and assumption is whenever we can't use greedy, then A* will be the way to go, but I am not 100% sure. I need some more examples and patterns on how to recognize and spot A* algorithm. Can someone give some special extreme cases that…
peter
  • 8,333
  • 17
  • 71
  • 94
-1
votes
1 answer

RegEx Nearest word match greedy

I have this text "(Objectid < 200 OR ObjectID > 600) and (test or best) W/5 AND (apple OR 10a) AND (Objectid < 100 OR ObjectID > 500)" I want to get the string(s) containing the substring And or Or wrapped in ( ) nearest to W/digit (from the left…
-1
votes
2 answers

Greedy vs Non-Greedy Pattern Matching in Java

What is the regular expression to capture the following groups in the given string using JAVA: hey,soul,345 with the constraint that the first word may include a comma as well. I have the following regex: (.*),(.*),(.*) but I essentially want to…
syker
  • 10,912
  • 16
  • 56
  • 68