Greedy Algorithm - Introduction
What is a Greedy Algorithm?
- A Greedy Algorithm is an algorithm or rather a technique, that always makes a choice that seems to be the best at the moment. This means that it makes a locally optimal choice assuming that this choice will result in a globally optimal solution of the given problem.
- As a Greedy Algorithm assumes that the choice it made at the local level is the best choice available at that moment, hence the Greedy Algorithm has only one shot to compute the optimal solution of the given problem. The Greedy Solution never goes back and reverses its decision.
Where Should we use a Greedy Algorithm?
- When we see that the given problem has Optimal Substructure then we can use the Greedy Algorithm to solve that problem. Optimal Substructure means that optimal solution to the subproblems (optimal local solution) can lead to the optimal solution of the whole problem (optimal global solution).
Common Greedy Algoritms
- Selection Sort
- Insertion Sort
- Topological Sort
- Kruskal's Algorithm
- Prim's Algorithm
- Dijkstra's Algorithm
- Activity Selection Problem
- Coin Change Problem
- Fractional Knapsack
Good Resources for practicing Greedy Algorithms
- I highly recommend checking out this article on Greedy Algorithms by wh0ami on Leetcode: https://leetcode.com/discuss/general-discussion/669996/greedy-for-beginners-problems-sample-solutions
- In case, the author removes the article, I am also attaching the PDF file of this article: https://drive.google.com/file/d/1t7jIWshgSa8ZvoNVpfQQI728qtXwkEM9/view?usp=sharing
Comments
Post a Comment