GREEDY APROACH TOWARDS
PROBLEM SOLUTION
RASHID ANSARI
170847980002
MTECH (ACDS)
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.
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
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
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
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
Applications
Activity Selection Problem
Minimum spanning tree
Knapsack Problem
Optimal Merging
Traveling Salesman Problem
Huffman Coding
Kruskal’s Alogorithm For Minimum
Spanning Tree
This is minimum spanning tree
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
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.
ACTIVITY SELECTION PROBLEM
ACTIVITY A1 A2 A3 A4 A5 A6
START 0 3 1 5 5 8
FINISH 6 4 2 9 7 9
ACTIVITY A3 A2 A1 A5 A6 A4
START 1 3 0 5 8 5
FINISH 2 4 6 7 9 9
SORTED
Step 1:
ACTIVITY SELECTION PROBLEM
 Step 2
A3
Step 3
A3->A2
A3->A2->A5
A3->A2->A5->A6
TOTAL = 4 ACTIVITES
ACTIVITY A3 A2 A1 A5 A6 A4
START 1 3 0 5 8 5
FINISH 2 4 6 7 9 9
ADVANTAGE
SIMPLE
EASY TO IMPLEMENT
RUN FAST
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.
THANKYOU!

Greedy aproach towards problem solution

  • 1.
    GREEDY APROACH TOWARDS PROBLEMSOLUTION RASHID ANSARI 170847980002 MTECH (ACDS)
  • 2.
    GREEDY ALGORITHM Definition-: A greedyalgorithm 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 greedyalgorithm 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. OfCoins 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 OPTIMALGREEDY 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
  • 7.
    Applications Activity Selection Problem Minimumspanning tree Knapsack Problem Optimal Merging Traveling Salesman Problem Huffman Coding
  • 8.
    Kruskal’s Alogorithm ForMinimum Spanning Tree
  • 15.
    This is minimumspanning tree
  • 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 GreedyApprocah  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.
  • 18.
    ACTIVITY SELECTION PROBLEM ACTIVITYA1 A2 A3 A4 A5 A6 START 0 3 1 5 5 8 FINISH 6 4 2 9 7 9 ACTIVITY A3 A2 A1 A5 A6 A4 START 1 3 0 5 8 5 FINISH 2 4 6 7 9 9 SORTED Step 1:
  • 19.
    ACTIVITY SELECTION PROBLEM Step 2 A3 Step 3 A3->A2 A3->A2->A5 A3->A2->A5->A6 TOTAL = 4 ACTIVITES ACTIVITY A3 A2 A1 A5 A6 A4 START 1 3 0 5 8 5 FINISH 2 4 6 7 9 9
  • 20.
  • 21.
    DRAWBACK Very often theydon’t provide a globally optimum solution Hard to verify: Showing a greedy algorithm is correct often requires a nuanced argument.
  • 22.