Skip to content
Related Articles

Related Articles

Greedy Algorithms (General Structure and Applications)
  • Last Updated : 29 Apr, 2020

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 :

Recommended Articles
Page :