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

Greedy optimization in R

I am trying to replicate Caruana et al.'s method for Ensemble selection from libraries of models (pdf). At the core of the method is a greedy algorithm for adding models to the ensemble (models can be added more than once). I've written an…
Zach
  • 29,791
  • 35
  • 142
  • 201
4
votes
5 answers

A greedy or dynamic algorithm to subset selection

I have a simple algorithmic question. I would be grateful if you could help me. We have some 2 dimensional points. A positive weight is associated to them (a sample problem is attached). We want to select a subset of them which maximizes the weights…
remo
  • 880
  • 2
  • 14
  • 32
4
votes
1 answer

Finding an increasing sequence a[] which minimizes sigma(abs(a[i]+c[i]))

Problem statement c is a given array of n integers; the problem is to find an increasing array of n integers a (a[i] <= a[i+1]) such that this sum is minimized: abs(a[0]+c[0]) + abs(a[1]+c[1]) + ... + abs(a[n-1]+c[n-1]) // abs(x) = absolute value of…
a-z
  • 1,634
  • 4
  • 16
  • 35
4
votes
4 answers

Scheduling, Greedy algorithm

This is a variation of the popular El Goog problem. Consider the following scheduling problem: There are n jobs, i = 1..n. There is 1 super computer and unlimited PCs. Each job needs to be pre-processed by the super computer first and then…
user183037
  • 2,549
  • 4
  • 31
  • 42
4
votes
2 answers

Greedy algorithm to pair numbers that minimizes the maximum sum

The input is a sequence of real numbers x1, x2, ..., x2n. We want to pair these numbers into n pairs. For the ith pair, (i = 1, 2, ..., n), let Si denote the sum of numbers in that pair. (For example if you pair x(2i−1) and x2i as the ith pair, Si =…
user183037
  • 2,549
  • 4
  • 31
  • 42
3
votes
1 answer

The Bus Driver : uva 11389

The problem is here: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2384 I managed to solve this problem using a greedy approach. I sorted the morning routes in descending order, and the evening routes…
TimeToCodeTheRoad
  • 7,032
  • 16
  • 57
  • 70
3
votes
4 answers

Can't get Perl regex to be non-greedy

My regex matches the last set of alpha characters in the line, regardless of what I do. I want it to match only the first occurrence. I have tried using the non-greedy operator, but it stubbornly matches the right-most set of alpha characters, in…
3
votes
1 answer

Create minimum no of groups of subsequent elements with max diff between elements being less than k

Q: Create minimum no of groups of subsequent elements with max diff between elments being less than k Constraints: 0 < A[i] < MAX_INT Number of elements: 1 < N < 10^6 Input N = 11 A = [1,2,4,10,5,4,11,21,15,5,1] K = 11 Output [ [1,2,4,10,5,4,11],…
pratik
  • 178
  • 11
3
votes
1 answer

Finding minimum number of points to cover all segments

Hi I have a problem as below: Given a set of n segments {[a0, b0], [a1, b1], . . . , [an-1, bn-1]} with integer coordinates on a line, find the minimum number m of points such that each segment contains at least one point. That is, find a set of…
Hoàng
  • 73
  • 6
3
votes
2 answers

Matching integers to ranges

I have n integers and m ranges (think [a, b]) of numbers such that n <= m. An integer and a range may be matched if the integer is in the range, i.e., a <= integer <= b. A range may only be matched with one integer and vice versa. Here is an…
AndW
  • 726
  • 6
  • 31
3
votes
1 answer

How do I change this greedy algorithm to predict the most overall score?

Hello, everyone, this is my first post here. So today during my university class, our professor gave us a task to write an algorithm: Write a function that returns the count for the amount of steps you need to make in order to get the most score in…
3
votes
1 answer

Greedy algorithm to split a list of lists of numbers into two partitions with same amount of each number in Python

I have a list of sublists of random positive integers. This list is controlled by 3 parameters: max_num: the maximum integer allowed in each sublist, e.g. if max_num = 3, the list will look like [[1,3], [3], [1,2,3], [1], ...]; max_length: the…
Shaun Han
  • 2,676
  • 2
  • 9
  • 29
3
votes
1 answer

Distribute given number in a greedy manner

I am hitting a wall in coming up with an equation to this simple question. I need a different perspective coming up with an algorithm. I have a number x and I want to distribute it to n elements in a greedy manner. For x=9,…
Tyr1on
  • 1,999
  • 2
  • 13
  • 20
3
votes
1 answer

Maximize the sum of absolute difference between max and min elements of all non overlapping sub-arrays that can be formed from an array?

for example : A = [2, 3, 0, 1, 5] Now A can be divided into multiple sub-arrays One way to do so is : A ~ [2], [3, 0], [1, 5] Now the sum of absolute difference between max and min elements of all non overlapping sub-arrays that can be formed from…
3
votes
0 answers

How can I check if an algorithmic paradigm can solve a certain type of problem or can not?

Well, I know that algorithmic paradigms are the templates to create algorithms to solve certain type of computational problems. (correct me if I am wrong) But what I don't understand is, "How do I check if the algorithmic paradigm is suitable for…