2. GREEDY ALGORITHM
Definition-:
A greedy algorithm is an algorithm that follows the problem
solving approach of making the locally optimal choice at each
stage with the hope of finding a global optimum.
3. GREEDY ALGORITHM
Simply……
A greedy algorithm always makes the choice that looks
best at the moment
Everyday examples:
Playing cards
Invest on stocks
Choose a university
The hope: a locally optimal choice will lead to a globally
optimal solution
4. MIN No. Of Coins Problem
Given a value V, we want to make change for V Rs. We have
infinite supply of each of the denominations in Indian
currency.
{1,2,5,10,20,50,100,200,500,2000}coins/notes
Example-
Input: value = 70
We need a 50 Rs note and a 20 Rs note.
Output:2
5. PROPERTIES OF OPTIMAL GREEDY
ALGORITHM
Two ingredients that are exhibited by most problems
that lend themselves to a greedy strategy
Greedy-choice property
Optimal substructure
6. Greedy-Choice Property : A global optimum can
be arrived at by selecting a local optimum.
Optimal Substructure : An optimal solution to
the problem contains an optimal solution to
subproblems
16. ACTIVITY SELECTION PROBLEM
Problem -: given n activities with their start and finish times.
Select the maximum number of activites that can be
performed by a single person, assuming that a person can
only work on a single activity at a time
17. ACTIVITY SELECTION PROBLEM
Solution
Greedy Approcah
Sort the activities according to their finishing time
Select the first activity from the sorted array and print it.
DO the following for remaining activities in the sorted array.
If the start time of this activity is greater than or equal to the finish
time of previously selected activity then select this activity and print it.
21. DRAWBACK
Very often they don’t provide a globally optimum
solution
Hard to verify: Showing a greedy algorithm is
correct often requires a nuanced argument.