Greedy Algorithms (General Structure and Applications)

Greedy Algorithms works step-by-step, and always chooses the steps which provide immediate profit/benefit. It chooses the “locally optimal solution”, without thinking about future consequences. Greedy algorithms may not always lead to the optimal global solution, because it does not consider the entire data. The choice made by the greedy approach does not consider the future data and choices.

All greedy algorithms follow a basic structure:

getOptimal(Item, arr[], int n)
  1) Initialize empty result : result = {}  
  2) While (All items are not considered)

      // We make a greedy choice to select
      // an item.
      i = SelectAnItem() 

      // If i is feasible, add i to the 
      // result
      if (feasible(i))
        result = result U i 
  3) return result

Characteristics of Greedy approach
1. There is an ordered list of resources(profit, cost, value, etc.)
2. Maximum of all the resources(max profit, max value, etc.) are taken.
3. For example, in fractional knapsack problem, the maximum value/weight is taken first according to available capacity.

Applications of Greedy Algorithms
1. Finding an optimal solution (Activity selection, Fractional Knapsack, Job Sequencing, Huffman Coding).
2. Finding close to the optimal solution for NP-Hard problems like TSP.

Advantages and Disadvantages of Greedy Approach
Advantages



  • Greedy approach is easy to implement.
  • Typically have less time complexities.
  • Greedy algorithms can be used for optimization purposes or finding close to optimization in case of NP Hard problems.

Disadvantages

  • The local optimal solution may not always be global optimal.

Standard Greedy Algorithms :

  1. Activity Selection Problem
  2. Egyptian Fraction
  3. Job Sequencing Problem
  4. Job Sequencing Problem (Using Disjoint Set)
  5. Huffman Coding
  6. Water Connection Problem
  7. Policemen catch thieves
  8. Minimum Swaps for Bracket Balancing
  9. Fitting Shelves Problem
  10. Kruskal’s Minimum Spanning Tree
  11. Prim’s Minimum Spanning Tree
  12. Boruvka’s Minimum Spanning Tree
  13. Reverse delete algorithm for MST
  14. Dijkastra’s Shortest Path Algorithm

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.