Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Greedy Algorithm

Last Updated: Nov 17, 2023
Go to Problems
locked
Greedy Algorithm
info
Complete all the problems in this Topic to unlock a badge
Completed
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!
Contents

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.

 

 

Video Courses
By

View All Courses
Excel at your interview with Masterclasses Know More
Certificate included
What will you Learn?
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+91 *
+91
Change Number
Graduation Year *
Graduation Year *
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
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
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