Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Greedy Algorithm

Go to Problems

Level 1

Jump to Level 2

Level 2

Jump to Level 3

Level 3

Jump to Level 4

Level 4

Jump to Level 5

Level 5

Jump to Level 6

Level 6

Jump to Level 7

Level 7

Jump to Level 8

Level 8

Be a Code Ninja!

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
44:38
Largest Permutation 250 51:12
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test