# Greedy Algorithms (General Structure and Applications)

• Difficulty Level : Easy
• Last Updated : 02 May, 2021

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. 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 best suited for looking at the immediate situation.

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```

Why to 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
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.

• 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.

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

Standard Greedy Algorithms :

My Personal Notes arrow_drop_up