ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

Introduction to Greedy Technique with example

prodevelopertutorial August 18, 2019

Greedy method is a simple technique that is easy to understand.

Definition:

 

In greedy approach, we make decision on the current information available at the present time without worrying about the affect on the future result.

 

Let’s understand the above statement with help of an example.

 

Consider the graph below, you need to go from A to D with minimum cost.

 

Introduction to Greedy Technique with example

From node “a”, we can either go to node “b” or node “c”.

With greedy method approach, we choose “a” to “b”. Because it is having less cost than “a” to “c”.

 

Then we move from “b” to “d”. Hence the total cost for “a” to “d” is “a to b” + “b to d” i.e 2 + 1 = 3.

 

Here we got the path with minimum cost.

 

But greedy approach will not always give the optimal solution.

 

Consider other graph as given below:

 

Introduction to Greedy Technique with example

 

Now we need to reach from node “a” to “d”. As always we become greedy and choose “a” to “b” as the cost is “1”. Then we move from “b” to “d”. Hence the total cost will be 7.

 

But if you observe, “a to c” and “c to d” will cost only 3.

 

Hence in the definition we can say that, we make a decision based on current data available. Once the choice is made you cannot change it.

 

Ok, we stump upon an important question. When to choose greedy method to solve a problem?

 

For a problem to be solved by greedy approach, the problem should satisfy below 2 Properties:

 

  1. Greedy Choice Property:

This property states that a global optimal solution can be achieved by selecting locally optimal solution.

 

  1. Optimal Sub Problem Property:

It means, the sub problem you choose should be the optimal of all the sub problems present.

 

Let us understand above 2 properties with help of an example. Consider the graph below and you need to reach from node “a” to “b” at less cost.

 

Introduction to Greedy Technique with example

 

For the 1stproperty: We choose the locally optimal sub solution and choose to go from “a” to “b”. Then we go to “b” to “d”. hence we have locally chosen the optimal solution and has

Resulted in globally optimal solution.

 

For the 2ndproperty: The sub problem which we choose should be optimal of all the sub problem.

 

In this example, from “a” we can go to “b” or “c”. We have chosen to go “a to b”. And again we have “c to d” or “b to d”. Again we chosen to go “b to d”, which is optimal of the sub problem. Hence we can solve this problem with help of greedy approach.

 

Below are the generic steps to be followed for a greedy algorithm.

  1. Initialize the set of items
  2. At each step, take a single item.
  3. Check if the selected item is feasible or not. If feasible add to the current set.
  4. If not feasible, discard the item and move to the next item.

We can see this kind of implementation in MST algorithms, that are discussed in coming chapters.

 

Below are some of the examples/ problems that can be solved using greedy approach.

 

  1. Finding shortest path
  2. Finding Minimum Spanning Tree [MST]
  3. Job sequencing problem
  4. Fractional Knapsack problem

We shall see all the above problems in coming chapters.

Further Reading:

AJ’s definitive guide for DS and Algorithms. Click here to study the complete list of algorithm and data structure tutorial. 85+ chapters to study from.

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT