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

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
44:38
Largest Permutation 250 51:12
Medium greedy
Problem Score Companies Time Status
Meeting rooms 200 58:39
Distribute Candy 300 65:48
Seats 300 74:07
Assign Mice to Holes 300 21:24
Majority Element 400 19:07
Gas Station 700 56:42
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
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
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 *