0% found this document useful (0 votes)
11 views

Greedy Algorithm

Uploaded by

Asish Patra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Greedy Algorithm

Uploaded by

Asish Patra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Greedy

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:

1. Greedy Choice Property 2. Optimal Substructure


If an optimal solution to the problem can be If the optimal overall solution to the problem
found by choosing the best choice at each step corresponds to the optimal solution to its
without reconsidering the previous steps once subproblems, then the problem can be solved
chosen, the problem can be solved using a greedy using a greedy approach. This property is called
approach. This property is called greedy choice optimal substructure.
property.
Greedy Algorithm Examples

Fractional Knapsack: Dijkstra’s algorithm: Kruskal’s algorithm: Huffman coding:


Optimizes the value of items Finds the shortest path Finds the minimum Compresses data by
that can be fractionally from a source vertex to all spanning tree of a assigning shorter codes to
included in a knapsack with other vertices in a weighted graph. more frequent symbols.
limited capacity. weighted graph.
Solving a Problem using Greedy Algorithm

Problem:
You have to make a change of an amount using the smallest possible number of coins.
o Amount: $18

Available coins are


• $5 coin
• $2 coin
• $1 coin

There is no limit to the number of each coin you can use.


Solution
• Create an empty solution-set = { }. Available coins are {5, 2, 1}.

• 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 first iteration, solution-set = {5} and sum = 5.

• In the second iteration, solution-set = {5, 5} and sum = 10.

• In the third iteration, solution-set = {5, 5, 5} and sum = 15.

• 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

The minimum number of coins/notes that sum up 3253 is


2000 500 500 200 50 2 1
Input: sum,

Initialise the coins = 0

Step 1: Find the largest denomination that can be used

Algorithm
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.


Advantages of Greedy Approach

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.

You might also like