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

How can I distribute databases across servers as evenly as possible while minimizing the movements required?

The Problem: I'm attempting to create a program to redistribute ~1200 databases of various sizes across a finite number of servers to create as even a distribution as possible. The databases are currently on those servers, but the distribution is…
3
votes
2 answers

missionaries and cannibals heuristic

I want to implement missionaries and cannibals with python with different search algorithms. One of the algorithms is Greedy or A* that needs a heuristic function to work. I cant think of any correct heuristic to work. Can you suggest a heuristic ?…
Amirition
  • 328
  • 5
  • 22
3
votes
1 answer

Prove or give a counter example for this greedy algorithm

I'm having trouble with this homework question. I think the main confusion comes from not identifying a basis for a counter example. Let P1, . . . , Pn be programs stored on a disk. Program Pi requires Si megabytes of storage, and the capacity of…
3
votes
2 answers

highest product of 3 implementation in haskell

I'd like the algorithm for highest product of 3 problem implemented in haskell. Here's the problem statement: Given an array of integers, find the highest product you can get from three of the integers. For example given [1, 2, 3, 4], the…
Aria Pahlavan
  • 1,338
  • 2
  • 11
  • 22
3
votes
2 answers

How do I parse this correctly with spirit?

My situation: I'm new to Spirit, I have to use VC6 and am thus using Spirit 1.6.4. I have a line that looks like this: //The Description;DESCRIPTION;; I want to put the text DESCRIPTION in a string if the line starts with //The Description;. I have…
foraidt
  • 5,519
  • 5
  • 52
  • 80
3
votes
1 answer

Calculate the largest best combination of denominations for a number

Problem Statement: I need to get the best combination of denomination for a given number. Example: I have three denominations {50,25,10} and given number is 30 then list should return <10,10,10>. for number 80 it should return <50,10,10,10> as…
3
votes
1 answer

Huffman Code writing bits to a file for compression

I was asked to use huffman code to compress an input file and write it to an output file. I have finished implementing the huffman tree structure and generating the huffman codes. But I dont know how to write those codes into a file so that the file…
adonayresom
  • 280
  • 3
  • 15
3
votes
2 answers

Max coverage disjoint intervals

Assume you have k<=10^5 intervals [a_i, b_i] \in [1,10^18] (some of them may overlap), and you need to choose a set of intervals mutually disjoint such that their union is maximal. Not maximum number of disjoint intervals, but the union must cover…
Moro Silverio
  • 87
  • 2
  • 7
3
votes
3 answers

What is the bad case in greedy algorithm for coin changing algorithm?

The Coin changing problem is making change for n cents using the fewest number of coins. Can you give a set of coin denominations for which the greedy algorithm does not yield an optimal solution. The set should include a penny so that there is a…
Anand
  • 43
  • 1
  • 8
3
votes
1 answer

Solving knapsack with fractional knapsack approach

There are two well-known knapsack problems: 1) Given n items, each has its weight and cost. We need to select items, that will fit in our knapsack and have maximal cost in sum. It can be easily solved using dynamic programming. 2) Fractional…
Aleksandr Tukallo
  • 1,299
  • 17
  • 22
3
votes
2 answers

Greedy Recursive Search

My professor gave us an assignment where I need to search around a given point in a grid for all other spots that makes up a group (in this example I need to find the number of spots that in a "L" shape within the problem). So the grid is 10x10, and…
Kendrick Lamar
  • 781
  • 2
  • 8
  • 18
3
votes
2 answers

Arrange n items in k nonempty groups such that the difference between the minimum element and the maximum element of each group is minimized

Given N items with values x[1], ..., x[n] and an integer K find a linear time algorithm to arrange these N items in K non empty groups such that in each group the range (difference between minimum and maximum element values/keys in each group) is…
bolzano
  • 816
  • 2
  • 13
  • 30
3
votes
1 answer

What's the meaning of first-cut Techniques?

The 3rd part of "Algorithms Design Techniques and Analysis" wrote by M H Alsuwaiyel is named "First-Cut Techniques" including the greedy Approach and Graph Traversal. And I want to know the meaning of the "first-cut Techniques" .I couldn't find it…
ViggoC
  • 33
  • 3
3
votes
2 answers

greedy algorithm (with coins) in C

In my code I usede the greedy algorithm in order to use the minimum amaount of coins. For example: I must return $0.41, the minimum amount of coins I can use is 4: 1 - 0,25; 1 - 0.10; 1 - 0.05; 1 - 0.01; There are 4 types of coins:…
Daniel Yefimov
  • 860
  • 1
  • 10
  • 24
3
votes
1 answer

Counter example for Job Scheduling Algorithm "Earliest End time First"

Well, we have the greedy algorithm for the job scheduling (scheduling the maximum number of jobs). we can use different techniques Shortest job first Earliest start time first Job with minimal conflicts first Earliest end time first I have the…
Muaaz Khalid
  • 2,199
  • 2
  • 28
  • 51