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
1 answer

Expedition Problem from SPOJ. Using heap data structure

Problem Expedition A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of…
Son Nguyen
  • 31
  • 1
3
votes
1 answer

Determine the minimum score in each fight

There are T1 and T2 Teams playing a game.Both Team have N players.The power of T1 team player are represented in an array and Similarly T2 team player. The rule of Game are follows: 1.There are only N fights. 2.No player of any team can play…
go_k
  • 105
  • 10
3
votes
2 answers

What is a greedy algorithm for this problem that is minimally optimal + proof?

The details are a bit cringe, fair warning lol: I want to set up meters on the floor of my building to catch someone; assume my floor is a number line from 0 to length L. The specific type of meter I am designing has a radius of detection that is…
John Adams
  • 109
  • 10
3
votes
2 answers

Select M items from N items such that completing these M item's tasks take the minimum time

I'm trying to solve the following problem: You are given N items. Each item contains three tasks A, B and C. The time required to complete task A is TA, task B is TB, task C is TC. Now, we must select M items such that completing these M item's…
3
votes
3 answers

What kind of algorithm paradigm/algorithm design paradigm is A* (A star) pathfinding algorithm?

I'm not sure about what kind of design paradigm is A* (A star) pathfinding algorithm. According of the topics of the book "Introduction to the Design & Analysis of Algorithms" by Anany Levitin, I think the design paradigm is a greedy technique,…
3
votes
3 answers

Car Fueling Problem (Greedy Algorithm), Nested while loop with O(n) complexity

Input (1) the maximum distance that a car can travel with a full tank: L km; (2) an integer array, [0, x1, x2, …, xn, xn+1], each integer represents the distance between a location and a source point A. The first integer is 0, which is the distance…
hack3r-0m
  • 700
  • 6
  • 20
3
votes
1 answer

What happens if we are inconsistent while creating Huffman Tree?

Why can’t we be inconsistent while creating Huffman Tree i.e sometime make the higher frequency node left child and sometimes right I know per convention, we have to decided beforehand if we would assign the larger node to the left or right child…
Surbhi Jain
  • 369
  • 1
  • 11
3
votes
4 answers

How to solve this matrix problem to collect all the seeds?

Problem description: Hammy the hamster found a building with seeds. Help him collect them all. The building is arranged in a matrix of rooms, with seeds in some rooms. Once in the building, Hammy will only run in a straight line, all the way through…
abhimanyue
  • 196
  • 1
  • 12
3
votes
2 answers

greedy algorithm for turned based game

I need to know how to implement a greedy algorithm in a card game using C#. The game is a turn based game. When the AI should issue some cards, it must be based on the latest state of other cards that already are on the table. Does anyone have a…
3
votes
1 answer

Algorithm for minimizing maximium product (candy and ballons)

Hello I need help with the following problem: we have m balloons and unlimited amount of candy. Some kid wants us give him ni balloons in every i day (array n). He also has a tax array b - which is tax bi on day i. If we give the kid ni balloons on…
Teodor Dyakov
  • 344
  • 2
  • 13
3
votes
2 answers

Given a sorted histogram of n bars, pick k bars while minimising the area enclosed with right wall

Given an integer k and a sorted histogram with n bars with heights a1, a2, a3,..., an(this sequence is non-decreasing since histogram is sorted). We have to pick k bars(1 <= k <= n) out of these n bars such that the area enclosed between chosen bars…
3
votes
2 answers

Dividing players into "winners" and "losers": how to prove that greedy solution gives optimal result?

I have a problem that states the following: n players (where n is even) are to a play games against each other. Everyone will not necessarily play but a player can only play against someone else once. If two people do decide to play against each…
Ayumu Kasugano
  • 440
  • 4
  • 13
3
votes
1 answer

Java merge adjacent array elements to yield maximum min value

excuse me for the confusing title, I need to implement an algorithm which can be simplified as the following: given an array of integers, and the number of merges needed (denoted as k), return the maximum min value of the merged array, a merge can…
Chen Xie
  • 3,849
  • 8
  • 27
  • 46
3
votes
2 answers

Algorithmic Strategy for selection of minimum number of baskets

Example: You have 4 baskets named P,Q,R,S. You have 4 items in those baskets named A,B,C,D. The composition of baskets are as follows PIC --A B C D P 6 4 0 7 Q 6 4 1 1 R 4 6 3 6 S 4 6 2 3 Basket P has 6A, 4B, No C's and…
3
votes
2 answers

Greedy algorithm to find minimum number of lines that can intersect all circles in the plane

I have a problem from homework where I need to find an O(n.log(n)) greedy algorithm for the minimum number of lines required to intersect all circles in the plane, as shown with the example below. The starting point of all the lines is (0,0) which…
BolbazarMarme
  • 1,221
  • 2
  • 13
  • 25