Greedy Algorithm
Greedy Algorithm
Algorithm
M AT H E M AT I C A L P R O B L E M
S O LV I N G
Introduction
• 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:
Problem:
You have to make a change of an amount using the smallest possible number of coins.
o Amount: $18
• We are supposed to find the sum = 18. Let's start with sum = 0.
• Always select the coin with the largest value (i.e. 5) until the sum > 18. (When we select the largest value at
each step, we hope to reach the destination faster. This concept is called greedy choice property.)
• In the fourth iteration, solution-set = {5, 5, 5, 2} and sum = 17. (We cannot select 5 here because if we do
so, sum = 20 which is greater than 18. So, we select the 2nd largest item which is 2.)
• Similarly, in the fifth iteration, select 1. Now sum = 18 and solution-set = {5, 5, 5, 2, 1}.
C/C++ Program for Greedy Algorithm to find Minimum number
of Coins
• 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 −
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
#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
Algorithm
i.e. smaller than sum.
The algorithm is easier to describe. This algorithm can perform better than other
algorithms (but, not in all cases).
Disadvantages/Limitations of
Using a Greedy Algorithm:
Greedy algorithms may not always find the best possible solution.
The order in which the elements are considered can significantly impact the
outcome.
Greedy algorithms focus on local optimizations and may miss better solutions that
require considering a broader context.
Greedy algorithms are not applicable to problems where the greedy choice does
not lead to an optimal solution.