Skip to content
Related Articles

Related Articles

Greedy Algorithms (General Structure and Applications)

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 22 Aug, 2022
Improve Article
Save Article

Greedy Algorithms work step-by-step, and always choose 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 future data and choices. In some cases making a decision that looks right at that moment gives the best solution (Greedy), but in other cases, it doesn’t. The greedy technique is used for optimization problems (where we have to find the maximum or minimum of something). The Greedy technique is best suited for looking at the immediate situation.

All greedy algorithms follow a basic structure: 

  1. declare an empty result = 0.
  2. We make a greedy choice to select, If the choice is feasible add it to the final result.
  3. return the result.

Why choose Greedy Approach:

The greedy approach has a few tradeoffs, which may make it suitable for optimization. One prominent reason is to achieve the most feasible solution immediately. In the activity selection problem (Explained below), if more activities can be done before finishing the current activity, these activities can be performed within the same time.  Another reason is to divide a problem recursively based on a condition, with no need to combine all the solutions. In the activity selection problem, the “recursive division” step is achieved by scanning a list of items only once and considering certain activities.

Greedy choice property: 

This property says that the globally optimal solution can be obtained by making a locally optimal solution (Greedy). The choice made by a Greedy algorithm may depend on earlier choices but not on the future. It iteratively makes one Greedy choice after another and reduces the given problem to a smaller one.

Optimal substructure:

A problem exhibits optimal substructure if an optimal solution to the problem contains optimal solutions to the subproblems. That means we can solve subproblems and build up the solutions to solve larger problems.

Note: Making locally optimal choices does not always work. Hence, Greedy algorithms will not always give the best solutions.

Characteristics of Greedy approach:

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

Applications of Greedy Algorithms:

Advantages of the Greedy Approach: 

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

Disadvantages of the Greedy Approach:

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

Standard Greedy Algorithms :

  • Prim’s Algorithm
  • Kruskal’s Algorithm
  • Dijkstra’s Algorithm


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!