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
7
votes
4 answers

Proof of optimality of a greedy solution to job sequencing

In this Job Sequencing Problem, how do we prove that the solution which greedy approach will provide is a optimal one? Moreover, I am also not able to figure out the O(n) solution as the author later is claiming It can be optimized to almost O(n)…
Walt
  • 1,426
  • 2
  • 17
  • 30
7
votes
2 answers

Gas Station-like Algorithm with minimum cost? Greedy or DP?

I have an array of n service stations D[] on a highway such that D[i] is the distance of the station i from the start of the highway. I also have an array of costs C[] such that C[i] is the cost of getting my vehicle serviced at station i. I have to…
Anagha
  • 71
  • 1
  • 2
6
votes
1 answer

Permuting rows in an array to eliminate increasing subsequences

The following problem is taken from Problems on Algorithms (Problem 653): You are given a n x 2 matrix of numbers. Find an O(n log n) algorithm that permutes the rows in the array such that that neither column of the array contains an increasing…
GEP
  • 311
  • 3
  • 12
6
votes
2 answers

What is "Greedy Token Parsing"?

What is Greedy Token Parsing in PHP? I was reading a PHP coding guide which said the following... "Always use single quoted strings unless you need variables parsed, and in cases where you do need variables parsed, use braces to prevent greedy…
Jacob Haug
  • 509
  • 1
  • 9
  • 18
6
votes
2 answers

Algorithm for 2 player game subtracting perfect squares from a given number

I need some help with a problem. I recently got this in an interview and I was trying to solve it but having no luck. Here's the question: Code an algorithm for a game consisting of two players. The input is a positive integer x. Each round, a…
Thomas99
  • 71
  • 1
  • 5
6
votes
3 answers

Minimum number of AND operations to make all array elements zero

I came across this question in a programming contest: We are given an array consisting of n elements. At each iteration, you can select any two elements ai and aj and replace ai with ai & aj. & is the bitwise AND operator. Find the minimum number…
Mbt925
  • 1,317
  • 1
  • 16
  • 31
6
votes
5 answers

regex greedy problem (C#)

I've a input string like "===text=== and ===text===" and I want to replace wiki syntax with the corresponding html tag. input: ===text=== and ===text=== desirable output:

text

and

text

but with the following code I get this…
dannyyy
  • 1,784
  • 2
  • 19
  • 43
6
votes
1 answer

Is he being greedy?

I am solving a question from LeetCode.com: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you…
user8305079
6
votes
4 answers

Covering segments by points

I did search and looked at these below links but it didn't help . Point covering problem Segments poked (covered) with points - any tricky test cases? Need effective greedy for covering a line segment Problem Description: You are given a set of…
James92
  • 81
  • 2
  • 8
6
votes
1 answer

TSP: Worst case ratio grows

As part of my high school thesis I am describing the heuristics for the Traveling Salesman Problem. I was reading this sort of case study (Page 8) but I cannot unserstand what these sentences mean: The running time for NN as described is Θ(N^2 ).…
6
votes
1 answer

Sort list based on pair-wise affiliation of its elements

Given a list of elements, say [1,2,3,4], and their pair-wise affiliation, say [[0, 0.5, 1, 0.1] [0.5, 0, 1, 0.9] [ 1, 1, 0, 0.2] [0.1, 0.9, 0.2, 0]] For those familiar with graph-theory, this is basically an adjacency matrix. What is the…
j-i-l
  • 10,281
  • 3
  • 53
  • 70
6
votes
1 answer

Coin Change : Greedy Approach

The Problem is making n cents change with quarters, dimes, nickels, and pennies, and using the least total number of coins. In the particular case where the four denominations are quarters,dimes, nickels, and pennies, we have c1 = 25, c2 = 10, c3 =…
bhavya
  • 169
  • 1
  • 2
  • 7
6
votes
1 answer

Why doesn't greedy approach work in this case?

I am trying to solve the following SPOJ problem. The input is: 1. Total weight of a certain amount of money in coins, 2. values and corresponding weights of the coins of used currency. The goal is to find the minimum possible monetary value of the…
mirgee
  • 390
  • 2
  • 13
6
votes
0 answers

Algorithm to minimize the cost for mechanic

There is one straight road. And on some distance some job is present. And every job has some importance. Now, mathematically, an array of locations is given to us, location[]. and there importance array is given, importance[]. If you move from one…
devsda
  • 4,112
  • 9
  • 50
  • 87
6
votes
2 answers

Make one or zero regex operator greedy

I have two sentences as input. Let's say for example: I love my red car. I love my car. Now I want to match every textpart inside the span-tags AND if available the color. If I use the following…
netblognet
  • 1,951
  • 2
  • 20
  • 46