Site icon Study Algorithms

Algorithmic Paradigms – Greedy Algorithms

Greedy algorithms is a paradigm that describes the approach on how you can go about solving a problem. You might have heard about a lot of algorithmic techniques while learning how to solve problems in computer science. The others being:

In this post we shall understand what a greedy algorithm is and how can it be used to solve problems which don’t seem very trivial at the first glance.

What is a Greedy Algorithm?

What is the first thing that comes to your mind when you think about greed? In the English language it usually means wanting the best for oneself. We use this technique everywhere around us. For instance, while shopping, we try to go at stores with the maximum discount, while choosing an internet plan, we want the maximum speed at our price range and so on.

But these are very small problems that require only one step of action. What you basically doing is “making the choice that seems best at that point of time”. If you are trying to rent a house, with a greedy approach you would try to get the house which offers you the best deal in the minimum price. After that, the house you selected may or may not be the best one in terms of commute or facilities, but with a greedy approach you hope that it would be the best one.

An example from day to day life:

One might encounter problems that require you to choose a series of steps. Let us take up a more relatable example. Suppose you are a workaholic student, who wants to take the maximum number of lessons.

Let’s assume that you after your daily chores you have 10 hours remaining in a day to take up these lessons. How do you go about choosing the tasks?

Fig: All the possible tasks to chose from and the time taken
Solution:

Be greedy. A natural thought comes to your mind, “If I want to do maximum lessons, I should take up lessons that take minimum amount of time.” This defines our condition for a greedy algorithm and we hope that it will let us choose the maximum number of tasks. Hence at every step, we will try to choose the task that takes up the minimum time. Going by this approach let’s choose:

So with a greedy approach we were able to take a total of 4 lessons in a total of 10 hours. This is indeed the maximum number of tasks that we could have completed. What would happen if we chose some other strategy. Like choosing the task with maximum time or a mean time?

We were only able to complete 3 tasks with this approach. Hence for the given problem statement, a greedy approach would certainly give you the most-optimal solution.

Steps involved in a Greedy Algorithm

Based on the above example we can define the steps in a greedy algorithm.

When does this algorithm fail?

Suppose you want to go on a hike. Ideally, to reach the topmost point, you would want to take the shortest path. This is where the greedy algorithm would fail. Choosing a shortest path during a hike would mean that we go straight towards the destination. This may not at all be feasible path and you may actually spend more time to overcome the hurdles.

Fig: Greedy path and optimal path shown on a mountain hike

An optimal path may be a little twisted and longer, but it may take up less time as it was easy to complete. That is why people go on hikes and then map the path they found 🤔.

Conclusion:

There are 3 major takeaways about greedy algorithms:

Some best examples where a greedy algorithm gives the global optimum solution are:

Video Explanation:
Exit mobile version