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

Huffman coding prove on a 8 bit sequence

A data file contains a sequence of 8-bit characters such that all 256 characters are about as common: the maximum character frequency is less than twice the minimum character frequency. Prove that Huffman coding in this case is not more efficient…
daniel pns
  • 19
  • 1
  • 3
-2
votes
2 answers

A greedy + binary search programming problem

Problem Description: Lian raises a pig and wants to sell it to the market. The pigs on the market need to have quality assurance, at least m kg. He has n bags of feed to feed the pigs, and the weight of each bag is a kg. If pig eats more than one…
Kaiden
  • 15
  • 4
-2
votes
1 answer

MaxValueSelect() function has some errors . any fixes?

My code is: def maxValueSelection(items,V): maxval = 0 val = 0 sorted_dict={} for i in items.items(): sorted_dict[i[1][1]] = [i[1][0],i[0]] sorted_dict_list = (sorted(sorted_dict))[::-1] while sorted_dict_list!=[]: item = sorted_dict_list[0] …
-2
votes
1 answer

Getting some error with car refuling problem. It works for most of the test cases but not for all. Can you pls tell me why this code is wrong

This is a greedy algorithm problem named as car refueling problem. At first, I'm checking if it is possible to complete the track or not. After this, I am counting the least no of steps by using the greedy method. My approach is: First I am going to…
-2
votes
1 answer

How to cover a range using a set of ranges with minimal overlap?

Assume that there are n tasks and a group of m people which can each do a range of tasks (Ti to Tj). The cost of completing each task is k* no. of people who have completed that task. What will be minimum cost to complete all the tasks atleast once,…
-2
votes
1 answer

How to find the item number of maximum profit and how to assign array[100000]???? help me

Questions – A file is given as “input.txt” name. In this file, you have 100000(N) items. Every item has a fixed weight wi and price pi. You can take maximum 2036 weights. Find out the way to take items, so that you can get maximum profit. You must…
-2
votes
1 answer

cs50 cash not returning desired output

cs50 greedy algorithm description This is my code for the cs50 greedy algorithm. However, it doesn't output any answer. When I change float dollars to int dollars it works, but only for integers greater or equal to 1. For numbers like 0.32, it…
-2
votes
2 answers

Distirbute Candy - Finding minimum reproducible example of the problem

The question is to distribute candies to N children.Each child has a rating. Distribution should be such that each child have at least one candy and children with higher rating gets more candies than their neighbors. Find minimum number of candies…
Arun R Nambiar
  • 127
  • 2
  • 12
-2
votes
1 answer

Why is my CS50 code not working and why my code keeps overflowing or giving a value of 1?

I am working on the "greedy" algo in CS50. I am not sure what's wrong but it keeps giving me a value of 1 or overflowing when I input a value. Please see below: #include #include #include int main(void) { //define…
echooca
  • 1
  • 3
-2
votes
1 answer

Problem creating String using concatenated Integers from Arrays

This is a weight loading program using a greedy approach to fill wagons in a caravan. I am trying to concatenate Integer elements from an array into a string output where it must follow this pattern. ``` (wagon capacity) -> (weights of packages in…
-2
votes
1 answer

Find a greedy algorithm for beach and umbrella question

A day at the beach. A group of n people are lying on the beach. The beach is represented by the real line R and the location of the i-th person is some integer xi ∈ Z. Your task is to prevent people from getting sunburned by covering them with…
mayank raj
  • 11
  • 1
-2
votes
1 answer

Proof of a greedy algorithm concerning "Buy and Resell Problem"

"Buy and Resell Problem" is a classical optimization problem. It can be described in the following way: There are $n$ cities. For each city, the price of products in this city is given (a positive number). Now a person will travel from city 1 to…
zbh2047
  • 393
  • 1
  • 9
-2
votes
4 answers

Increasing number sequence in a string java

Problem: Check if the numbers in the string are in increasing order. Return: True -> If numbers are in increasing order. False -> If numbers are not in increasing order. The String sequence are : CASE 1 :1234 (Easy) 1 <2<3<4 …
Aastha Jain
  • 180
  • 1
  • 1
  • 13
-2
votes
1 answer

Greedy algorithm set cover

In the below instance of set cover. How many sets the greedy algorithm will pick?. All sets have cost 1. Can anyone explain me. What is the solution for this question. So how will be greedy algorithm works for the 2nd instance. how many sets it…
-2
votes
1 answer

Binary search algorithm to maximize values

I was assigned this problem I have to solve: There are plenty of guided activities in a certain swimming pool. Therefore, the usage rules are very strict: The free time slots are only one minute long. After using a free slot, we must wait for at…
Marc Ortiz
  • 2,242
  • 5
  • 27
  • 46