Greedy Algorithm

Go to Problems

Greedy Algorithm Examples

Let us see with the help of below examples about how greedy algorithm can be used to find optimal solutions.

Path Finder Problem

Case Study:

Consider you want to visit a restaurant (point B) in your car from your home (Point A) and there are 20 possible ‘paths’ or ‘routes’. The paths are composed of many roads - each adjacent to the other. Imagine that upon leaving your home, you would encounter many junctions since we have many roads splitting at many points.

Let us assume that these junctions don’t have markers which shows the remaining distance to restaurant. Instead of that, we know the length of all the individual roads from here. To make things simpler, assume that you know these lengths before you are leaving from your home. Naturally you would want to reach the restaurant using minimum fuel and as early as possible. Right?

So, if you choose to minimize cost of the fuel, it would be same as taking paths of minimum length. Now, how should we proceed?

Analysis:

  • The simplest and the most obvious method is to compute all path lengths at home, and choose the one with minimum value. This is called brute force.
    • The problem with such solutions is you have to waste a lot of your time and rough-sheets calculating what path has minimum lengths and you would need a calculator too.
    • It may not be a mighty task with 20 roads, but if you’d 20000 it won’t be simple anymore. Can you imagine yourself calculating the distance when you are starving? We surely can’t think when hungry! :D

  • Now let’s try to solve things in easier manner. Leave home without performing any calculations. When you encounter a junction with several roads, what would be your natural instinct?
    • Say you see 5 roads of length 1,4,20,100 and 200- which one would you choose?
    • Unless you apply algorithms at every point of your life, you would instinctively choose the road of length 1.
    • If you had been able to calculate using the minimum distance remaining to restaurant along this path (which unfortunately you don’t know yet), you would have chosen the optimal path.
    • However, by choosing the path which at the moment seemed best you chose a suboptimal path hoping that this would help u minimize fuel in the overall run.
    • If you apply the same intuition and logic at every junction, computer scientists would declare that you have used Greedy Algorithm successfully to reach the restaurant.

Simple, isn’t it? You don’t need to calculate exhaustively all road lengths in advance (especially when you are hungry!). You just have to follow minimum path length road at a given junction. This reduces a lots of wasteful computations.

 

 

Serious about Learning Programming ?

Learn this and a lot more with Scaler Academy's industry vetted curriculum which covers Data Structures & Algorithms in depth.

Greedy Algorithm Problems

Easy greedy
Problem Score Companies Time Status
Highest Product 200 29:25
Bulbs 200
23:18
Disjoint Intervals 200
47:18
Largest Permutation 250 53:54
lock
Topic Bonus
Bonus will be unlocked after solving min. 1 problem from each bucket