## Introduction to Greedy Algorithm

Greedy Algorithm is a special type of algorithm that is used to solve optimization problems by deriving the maximum or minimum values for the particular instance. This algorithm selects the optimum result feasible for the present scenario independent of subsequent results. The greedy algorithm is often implemented for condition-specific scenarios. This Algorithm is used to solve optimization problems, maximization problems, and minimization problems. And It provides feasible or optimized solutions. Some of the problem scenarios where it can be the best fit such as Huffman coding, Minimal spanning tree graph using Prim’s or Kruskal’s algorithm and finding the shortest path between two vertices of a graph.

### What is Greedy Algorithm?

It is an algorithmic strategy used to make the best optional choice at a very small stage while eventually outputting a globally optimum solution. This algorithm picks the best solution feasible at that moment without regard to any consequences. Greedy method says that problem should be solved in stages wherein each one input is considered given that this input is feasible. As this approach only focuses on an immediate result with no regard for the bigger picture, is considered greedy.

### Defining the Core Concept

Till now, we know what it is and why is it named so. The below pointers will make you understand the greedy algorithm in a better way. By now, it has been very clear that the greedy algorithm only works when there is a problem; nevertheless, this approach is only applicable if we have a condition or constraint to that problem.

#### Types of Problems

**Minimization Problem:**Getting a solution to a problem is easy given that all the conditions are met. However, when this problem demands a minimum result, it is then called a Minimization Problem.**Maximization Problem:**A problem that demands the maximum result is known as the maximization problem.**Optimization Problem:**A problem is called optimization problem when it requires either minimum or maximum results.

#### Types of Solutions

**Feasible Solution:**Now when a problem arises, we have many plausible solutions to this problem. Yet, taking into consideration the condition set on that problem, we choose solutions that satisfy the given condition. Such solutions that help us get results meeting the given condition is called a Feasible Solution**.****Optimal Solution:**A solution is called optimal when it is already feasible and achieves the objective of the problem; the best result. This objective could either be the minimum or maximum result. The point here to be noticed is that any problem will only have one optimal solution.

The following example will make you understand the greedy method easily. Say, one wants to buy the best car available in the market. One of the methods to choose this car is by analyzing all the cars in the market. Now, this being a time-consuming, to make it easy one selects car from those specific brands which they are interested to invest in. Categorizing this further, one would again choose the desired models looking at its features. Therefore, the approach used here is greedy as this solution was the optimal solution for you while considering all the factors were favorable to you.

### Core Components of Greedy Algorithm

Now when we have a better understanding of this mechanism, let’s explore the core components of a greedy algorithm that sets it apart from other processes:

**Candidate set:**An answer is created from this set.**Selection function:**It selects the best candidate to be included in the solution.**Feasibility function:**This section calculates if a candidate can be used to contribute to the solution.**An objective function:**It assigns a value to a complete or a partial solution.**A solution function:**This is used to indicate if a proper solution has been met.

### Where does the Greedy Algorithm work the best?

It can be applied to the below-mentioned problems.

- The Greedy approach can be used to find the minimal spanning tree graph using Prim’s or Kruskal’s algorithm
- Finding the shortest path between two vertices is yet another problem that can be solved using a greedy algorithm. Applying the Dijkstra’s algorithm along with the greedy algorithm will give you an optimal solution.
- Huffman Coding

### Advantages

The biggest advantage that the Greedy algorithm has over others is that it is easy to implement and very efficient in most of cases.

4.7 (8,463 ratings)

View Course

### Disadvantages

It basically builds a solution part by part and choosing the next part in such a way that it yields the best solution to the present problem at hand immediately. As a result, there is no regard or worry about the consequences of the current decision taken. Never reconsidering the choices taken previously, it fails to produce an optimal solution, though it gives a near optimal solution**. **Knapsack Problem and Travelling Salesman Problem are examples of problems where it fails to produce an optimal solution.

**Knapsack Problem:**Most commonly known by the name rucksack problem, is an everyday problem faced by many people. Say, we have set of items and each has different weigh and value (profit) to filled into a container or should be collected in such a way that the total weight is less than or equal to that of the container while the total profit is maximized.

### Conclusion

It is best applicable when one needs a solution in real-time and approximate answers is “good enough”. Clearly, it minimizes time while making sure that an optimal solution is produced, hence it is more applicable to use in a situation where less time is required. Post-reading this article, one might have a fair idea about greedy algorithms. In addition, this post explains why it is regarded as the best frameworks that answer nearly all programming challenges along with helping you to decide the most optimal solution at a given point of time.

However, on the rough side, for applying the theory of greedy algorithm one must work harder to know the correct issues. Although it’s a scientific concept that has logic, it also has an essence of creativity.

### Recommended Articles

This has been a guide to What is a Greedy Algorithm. Here we discussed the Core Concept, components, Advantage, and Disadvantage of Greedy Algorithm. You can also go through our other suggested articles to learn more –