Greedy Algorithm
Greedy Algorithm
Greedy algorithms determine the minimum number of coins to give while making change.
These are the steps most people would take to emulate a greedy algorithm to represent 36
cents using only coins with values {1, 5, 10, 20}. The coin of the highest value, less than the
remaining change owed, is the local optimum. (In general, the change-making problem
requires dynamic programming to find an optimal solution; however, most currency systems
are special cases where the greedy strategy does find an optimal solution.)
A greedy algorithm is any algorithm that follows the problem-solving heuristic of making
the locally optimal choice at each stage. [1] In many problems, a greedy strategy does not
produce an optimal solution, but a greedy heuristic can yield locally optimal solutions that
approximate a globally optimal solution in a reasonable amount of time.
For example, a greedy strategy for the travelling salesman problem (which is of high
computational complexity) is the following heuristic: "At each step of the journey, visit the
nearest unvisited city." This heuristic does not intend to find the best solution, but it
terminates in a reasonable number of steps; finding an optimal solution to such a complex
problem typically requires unreasonably many steps. In mathematical optimization, greedy
algorithms optimally solve combinatorial problems having the properties of matroids and
give constant-factor approximations to optimization problems with the submodular structure.
For example consider the Fractional Knapsack Problem. The local optimal strategy is to
choose the item that has maximum value vs weight ratio. This strategy also leads to a
globally optimal solution because we are allowed to take fractions of an item.
Greedy Algorithm
In this tutorial, you will learn what a Greedy Algorithm is. Also, you will find an example of
a greedy approach.
A greedy algorithm is an approach for solving a problem by selecting the best option
available at the moment. It doesn't worry whether the current best result will bring the overall
optimal result.
The algorithm never reverses the earlier decision even if the choice is wrong. It works in a
top-down approach.
This algorithm may not produce the best result for all the problems. It's because it always
goes for the local best choice to produce the global best result.
However, we can determine if the algorithm can be used with any problem if the problem has
the following properties:
If an optimal solution to the problem can be found by choosing the best choice at each step
without reconsidering the previous steps once chosen, the problem can be solved using a
greedy approach. This property is called greedy choice property.
2. Optimal Substructure
If the optimal overall solution to the problem corresponds to the optimal solution to its
subproblems, then the problem can be solved using a greedy approach. This property is called
optimal substructure.
For example, suppose we want to find the longest path in the graph below from root to leaf.
Let's use the greedy algorithm here.
Apply greedy
approach to this tree to find the longest route
Greedy Approach
1. Let's start with the root node 20. The weight of the right child is 3 and the weight of the left
child is 2.
2. Our problem is to find the largest path. And, the optimal solution at the moment is 3. So,
the greedy algorithm will choose 3.
3. Finally the weight of an only child of 3 is 1. This gives us our final result 20 + 3 + 1 =
24.
However, it is not the optimal solution. There is another path that carries more weight ( 20 +
2 + 10 = 32) as shown in the image below.
Longest path
A greedy algorithm is an algorithm used to find an optimal solution for the given problem.
greedy algorithm works by finding locally optimal solutions ( optimal solution for a part of
the problem) of each part so show the Global optimal solution could be found.
In this problem, we will use a greedy algorithm to find the minimum number of coins/ notes
that could makeup to the given sum. For this we will take under consideration all the valid
coins or notes i.e. denominations of { 1, 2, 5, 10, 20, 50 , 100, 200 , 500 ,2000 }. And we
need to return the number of these coins/notes we will need to make up to the sum.
Example 1 −
Input : 1231
Output : 7
Explanation − We will need two Rs 500 notes, two Rs 100 notes, one Rs 20 note, one Rs 10
note and one Re 1 coin. That sums to 2+2+1+1+1 = 7
Example 2 −
Input : 2150
Output : 3
Explanation − We will need one Rs 2000 note, one Rs 100 note, and one Rs 50 note.
To solve this problem using a greedy algorithm, we will find the which is the largest
denomination that can be used. then we will subtract the largest denomination from the sum
and again do the same process until the sum becomes zero.
Algorithm
Input: sum,
Initialise the coins = 0
Step 1: Find the largest denomination that can be used i.e. smaller than
sum.
Step 2: Add denomination two coins and subtract it from the Sum
Step 3: Repeat step 2 until the sum becomes 0.
Step 4: Print each value in coins.
Example
Live Demo
#include <bits/stdc++.h>
using namespace std;
int notes[] = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 };
int n = sizeof(notes) / sizeof(notes[0]);
void minchange(int sum){
vector<int> coins;
for (int i = n - 1; i >= 0; i--) {
while (sum >= notes[i]) {
sum -= notes[i];
coins.push_back(notes[i]);
}
}
for (int i = 0; i < coins.size(); i++)
cout << coins[i] << "\t";
}
int main(){
int n = 3253;
cout << "The minimum number of coins/notes that sum up " << n << " is \t
";
minchange(n);
return 0;
}
Output
The minimum number of coins/notes that sum up 3253 is
2000 500 500 200 50 2 1