The Wayback Machine - https://web.archive.org/web/20241008065454/https://www.geeksforgeeks.org/introduction-to-greedy-algorithm-data-structures-and-algorithm-tutorials/
Open In App

Greedy Algorithm Tutorial – Examples, Application and Practice Problem

Last Updated : 07 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Greedy Algorithm is defined as a method for solving optimization problems by taking decisions that result in the most evident and immediate benefit irrespective of the final outcome. It works for cases where minimization or maximization leads to the required solution.

Image

Greedy Algorithm

What is Greedy Algorithm?

A greedy algorithm is a problem-solving technique that makes the best local choice at each step in the hope of finding the global optimum solution. It prioritizes immediate benefits over long-term consequences, making decisions based on the current situation without considering future implications. While this approach can be efficient and straightforward, it doesn’t guarantee the best overall outcome for all problems.

However, it’s important to note that not all problems are suitable for greedy algorithms. They work best when the problem exhibits the following properties:

  • Greedy Choice Property: The optimal solution can be constructed by making the best local choice at each step.
  • Optimal Substructure: The optimal solution to the problem contains the optimal solutions to its subproblems.

Characteristics of Greedy Algorithm

Here are the characteristics of a greedy algorithm:

  • Greedy algorithms are simple and easy to implement.
  • They are efficient in terms of time complexity, often providing quick solutions.
  • Greedy algorithms are used for optimization problems where a locally optimal choice leads to a globally optimal solution.
  • These algorithms do not reconsider previous choices, as they make decisions based on current information without looking ahead.
  • Greedy algorithms are suitable for problems for optimal substructure.

These characteristics help to define the nature and usage of greedy algorithms in problem-solving.

Examples of Greedy Algorithm

Several well-known algorithms fall under the category of greedy algorithms. Here are a few examples:

  • Dijkstra’s Algorithm: This algorithm finds the shortest path between two nodes in a graph. It works by repeatedly choosing the shortest edge available from the current node.
  • Kruskal’s Algorithm: This algorithm finds the minimum spanning tree of a graph. It works by repeatedly choosing the edge with the minimum weight that does not create a cycle.
  • Fractional Knapsack Problem: This problem involves selecting items with the highest value-to-weight ratio to fill a knapsack with a limited capacity. The greedy algorithm selects items in decreasing order of their value-to-weight ratio until the knapsack is full.
  • Scheduling and Resource Allocation : The greedy algorithm can be used to schedule jobs or allocate resources in an efficient manner.
  • Coin Change Problem : The greedy algorithm can be used to make change for a given amount with the minimum number of coins, by always choosing the coin with the highest value that is less than the remaining amount to be changed.
  • Huffman Coding : The greedy algorithm can be used to generate a prefix-free code for data compression, by constructing a binary tree in a way that the frequency of each character is taken into consideration.

Want to master Greedy algorithm and more? Check out our DSA Self-Paced Course for a comprehensive guide to Data Structures and Algorithms at your own pace. This course will help you build a strong foundation and advance your problem-solving skills.

Why to use Greedy Approach?

Here are some reasons why you might use the Greedy Approach:

  • Simple and easy to understand: The Greedy Approach is straightforward and easy to implement, making it a good choice for beginners.
  • Fast and efficient: It usually finds a solution quickly, making it suitable for problems where time is a constraint.
  • Provides a good enough solution: While not always optimal, the Greedy Approach often finds a solution that is close to the best possible solution.
  • Can be used as a building block for other algorithms: The Greedy Approach can be used as a starting point for developing more complex algorithms.
  • Useful for a variety of problems: The Greedy Approach can be applied to a wide range of optimization problems, including knapsack problems, scheduling problems, and routing problems.

However, it’s important to remember that the Greedy Approach doesn’t always find the optimal solution . There are cases where it can lead to suboptimal solutions. Therefore, it is necessary to carefully consider the problem and the potential drawbacks before using the Greedy Approach.

How does the Greedy Algorithm works?

Greedy Algorithm solve optimization problems by making the best local choice at each step in the hope of finding the global optimum. It’s like taking the best option available at each moment, hoping it will lead to the best overall outcome.

Here’s how it works:

  1. Start with the initial state of the problem. This is the starting point from where you begin making choices.
  2. Evaluate all possible choices you can make from the current state. Consider all the options available at that specific moment.
  3. Choose the option that seems best at that moment, regardless of future consequences. This is the “greedy” part – you take the best option available now, even if it might not be the best in the long run.
  4. Move to the new state based on your chosen option. This becomes your new starting point for the next iteration.
  5. Repeat steps 2-4 until you reach the goal state or no further progress is possible. Keep making the best local choices until you reach the end of the problem or get stuck..

Example:

Let’s say you have a set of coins with values {1, 2, 5, 10, 20, 50, 100} and you need to give minimum number of coin to someone change for 36 .

The greedy algorithm for making change would work as follows:

  1. Start with the largest coin value that is less than or equal to the amount to be changed. In this case, the largest coin less than 36 is 20 .
  2. Subtract the largest coin value from the amount to be changed, and add the coin to the solution. In this case, subtracting 20 from 36 gives 16 , and we add a 20 coin to the solution.
  3. Repeat steps 1 and 2 until the amount to be changed becomes 0.

So, using the greedy algorithm, the solution for making change for 36 would be one 20 coins, one 10 coin, one 5 coins and one 1 coin needed.

Note: This is just one example, and other greedy choices could have been made at each step. However, in this case, the greedy approach leads to the optimal solution.

The greedy algorithm is not always the optimal solution for every optimization problem, as shown in the example below.

Graph with weighted vertices

Graph with weighted vertices

  • In the above graph starting from the root node 10 if we greedily select the next node to obtain the most weighted path the next selected node will be 5 that will take the total sum to 15 and the path will end as there is no child of 5 but the path 10 -> 5 is not the maximum weight path.
Image

Greedy Approach fails

  • In order to find the most weighted path all possible path sum must be computed and their path sum must be compared to get the desired result, it is visible that the most weighted path in the above graph is 10 -> 1 -> 30 that gives the path sum 41 .
Image

Correct Approach

  • In such cases Greedy approach wouldn’t work instead complete paths from root to leaf node has to be considered to get the correct answer i.e. the most weighted path, This can be achieved by recursively checking all the paths and calculating their weight.

Greedy Algorithm Vs Dynamic Programming

Below are the comparison of Greedy Algorithm and Dynamic Programming based on various criteria:

Criteria Greedy Algorithm Dynamic Programming
Basic Idea Makes the locally optimal choice at each stage Solves subproblems and builds up to the optimal solution
Optimal Solution Not always guaranteed to provide the globally optimal solution Guarantees the globally optimal solution
Time Complexity Typically faster; often linear or polynomial time Usually slower due to solving overlapping subproblems
Space Complexity Requires less memory; often constant or linear space Requires more memory due to storing intermediate results
Subproblems Overlapping Does not handle overlapping subproblems Handles overlapping subproblems efficiently
Examples Finding minimum spanning tree, Huffman coding Matrix chain multiplication, shortest path problems
Applications Used when a greedy choice at each step leads to the globally optimal solution Applied when the problem can be broken down into overlapping subproblems

Applications of Greedy Algorithms:

  • Dijkstra’s shortest path algorithm: Finds the shortest path between two nodes in a graph.
  • Kruskal’s minimum spanning tree algorithm: Finds the minimum spanning tree for a weighted graph.
  • Huffman coding: Creates an optimal prefix code for a set of symbols based on their frequencies.
  • Fractional knapsack problem: Determines the most valuable items to carry in a knapsack with a limited weight capacity.
  • Activity selection problem: Chooses the maximum number of non-overlapping activities from a set of activities.

Advantages of Greedy Algorithms:

  • Simple and easy to understand: Greedy algorithms are often straightforward to implement and reason about.
  • Efficient for certain problems: They can provide optimal solutions for specific problems, like finding the shortest path in a graph with non-negative edge weights.
  • Fast execution time: Greedy algorithms generally have lower time complexity compared to other algorithms for certain problems.
  • Intuitive and easy to explain : The decision-making process in a greedy algorithm is often easy to understand and justify.
  • Can be used as building blocks for more complex algorithms: Greedy algorithms can be combined with other techniques to design more sophisticated algorithms for challenging problems.

Disadvantages of the Greedy Approach:

  • Not always optimal: Greedy algorithms prioritize local optima over global optima, leading to suboptimal solutions in some cases.
  • Difficult to prove optimality: Proving the optimality of a greedy algorithm can be challenging, requiring careful analysis.
  • Sensitive to input order: The order of input data can affect the solution generated by a greedy algorithm.
  • Limited applicability: Greedy algorithms are not suitable for all problems and may not be applicable to problems with complex constraints.

Greedy Algorithm Most Asked Interview Problems :

Some of the popular problems on the Greedy Approach that are widely asked in interviews are:

Problems

Activity Selection Problem

Kruskal’s Minimum Spanning Tree Algorithm

Huffman Coding

Efficient Huffman Coding for Sorted Input

Prim’s Minimum Spanning Tree Algorithm

Prim’s MST for Adjacency List Representation

Dijkstra’s Shortest Path Algorithm

Job Sequencing Problem

Greedy Algorithm to find Minimum number of Coins

K Centers Problem

Connect n ropes with minimum cost

Graph coloring

Fractional Knapsack Problem

Minimize Cash Flow among a given set of friends who have borrowed money from each other

Find minimum time to finish all jobs with given constraints

Find maximum sum possible equal to sum of three stacks

Minimum Number of Platforms Required for a Railway/Bus Station

Greedy Algorithm Tutorial – FAQs

What is a greedy algorithm?

A greedy algorithm is an algorithm that makes the best local choice at each step in the hope of finding the global optimum solution. It is a heuristic approach, meaning it does not guarantee finding the optimal solution but often provides a good approximation.

When should I use a greedy algorithm?

Greedy algorithms are best suited for problems where optimal substructure exists. This means that the optimal solution to the problem can be constructed from the optimal solutions to its subproblems. Examples of problems where greedy algorithms are effective include Dijkstra’s shortest path algorithm, Kruskal’s minimum spanning tree algorithm, and Huffman coding .

What are the advantages and disadvantages of greedy algorithms?

Advantages of greedy algorithms:

  • Easy to understand and implement.
  • Often computationally efficient.
  • Can provide good approximate solutions even when the optimal solution is difficult to find.

Disadvantages of greedy algorithms:

  • Do not always guarantee finding the optimal solution.
  • May not be applicable to all problems.

What are some examples of greedy algorithms?

Below are some example of greedy algorithms:

  • Dijkstra’s shortest path algorithm
  • Kruskal’s minimum spanning tree algorithm
  • Huffman coding
  • Fractional knapsack problem
  • Activity selection problem

Where can I learn more about greedy algorithms?

There are many resources available online and in textbooks to learn more about greedy algorithms. Some good resource to start learning Greedy algorithms are:

  1. GeeksforGeeks
  2. MIT OpenCourseware:

Related Articles:



Previous Article
Next Article

Similar Reads

Difference between Greedy Algorithm and Divide and Conquer Algorithm
Greedy algorithm and divide and conquer algorithm are two common algorithmic paradigms used to solve problems. The main difference between them lies in their approach to solving problems. Greedy Algorithm:The greedy algorithm is an algorithmic paradigm that follows the problem-solving heuristic of making the locally optimal choice at each stage wit
3 min read
Greedy Approximate Algorithm for Set Cover Problem
Given a universe U of n elements, a collection of subsets of U say S = {S1, S2...,Sm} where every subset Si has an associated cost. Find a minimum cost subcollection of S that covers all elements of U. Example: U = {1,2,3,4,5} S = {S1,S2,S3} S1 = {4,1,3}, Cost(S1) = 5 S2 = {2,5}, Cost(S2) = 10 S3 = {1,4,3,2}, Cost(S3) = 3 Output: Minimum cost of se
6 min read
Greedy Approximate Algorithm for K Centers Problem
Given n cities and distances between every pair of cities, select k cities to place warehouses (or ATMs or Cloud Server) such that the maximum distance of a city to a warehouse (or ATM or Cloud Server) is minimized. For example consider the following four cities, 0, 1, 2, and 3, and the distances between them, how to place 2 ATMs among these 4 citi
9 min read
Comparison among Greedy, Divide and Conquer and Dynamic Programming algorithm
Greedy algorithm, divide and conquer algorithm, and dynamic programming algorithm are three common algorithmic paradigms used to solve problems. Here's a comparison among these algorithms: Approach:Greedy algorithm: Makes locally optimal choices at each step with the hope of finding a global optimum.Divide and conquer algorithm: Breaks down a probl
4 min read
Activity Selection Problem | Greedy Algo-1
Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. Greedy algorithms are used for optimization problems. An optimization problem can be solved using Greedy if the problem has the following property: At every step, we can make a choice that lo
15+ min read
Boruvka's algorithm | Greedy Algo-9
We have discussed the following topics on Minimum Spanning Tree.Applications of Minimum Spanning Tree Problem Kruskal’s Minimum Spanning Tree Algorithm Prim’s Minimum Spanning Tree AlgorithmIn this post, Boruvka's algorithm is discussed. Like Prim's and Kruskal's, Boruvka’s algorithm is also a Greedy algorithm. Below is a complete algorithm. 1) Inp
15+ min read
What is Greedy Algorithm in DSA?
A Greedy Algorithm is defined as a problem-solving strategy that makes the locally optimal choice at each step of the algorithm, with the hope that this will lead to a globally optimal solution. In other words, a greedy algorithm always chooses the option that seems the best at the moment, without considering the future consequences or possibilitie
4 min read
Minimum number of subsequences required to convert one string to another using Greedy Algorithm
Given two strings A and B consists of lowercase letters, the task to find the minimum number of subsequence required to form A from B. If it is impossible to form, print -1.Examples: Input: A = "aacbe", B = "aceab" Output: 3 Explanation: The minimum number of subsequences required for creating A from B is "aa", "cb" and "e".Input: A = "geeks", B =
13 min read
Graph Coloring Using Greedy Algorithm
We introduced graph coloring and applications in previous post. As discussed in the previous post, graph coloring is widely used. Unfortunately, there is no efficient algorithm available for coloring a graph with minimum number of colors as the problem is a known NP Complete problem. There are approximate algorithms to solve the problem though. Fol
12 min read
Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
We recommend reading the following two posts as a prerequisite for this post. Greedy Algorithms | Set 7 (Dijkstra’s shortest path algorithm) Graph and its representations We have discussed Dijkstra's algorithm and its implementation for adjacency matrix representation of graphs. The time complexity for the matrix representation is O(V^2). In this p
15+ min read
Greedy Algorithm for Egyptian Fraction
Every positive fraction can be represented as sum of unique unit fractions. A fraction is unit fraction if numerator is 1 and denominator is a positive integer, for example 1/3 is a unit fraction. Such a representation is called Egyptian Fraction as it was used by ancient Egyptians. Following are a few examples: Egyptian Fraction Representation of
11 min read
Greedy Algorithm to find Minimum number of Coins
Given a value of V Rs and an infinite supply of each of the denominations {1, 2, 5, 10, 20, 50, 100, 500, 1000} valued coins/notes, The task is to find the minimum number of coins and/or notes needed to make the change? Examples: Input: V = 70Output: 2Explanation: We need a 50 Rs note and a 20 Rs note. Input: V = 121Output: 3Explanation: We need a
12 min read
Greedy Best first search algorithm
What is the Greedy-Best-first search algorithm?Greedy Best-First Search is an AI search algorithm that attempts to find the most promising path from a given starting point to a goal. It prioritizes paths that appear to be the most promising, regardless of whether or not they are actually the shortest path. The algorithm works by evaluating the cost
5 min read
Is Dijkstra a greedy algorithm?
In the world of computer science and algorithms, there's a lot of talk about Dijkstra's algorithm and its classification as a "greedy" algorithm. In this article, we will explore what Dijkstra's algorithm is, understand the concept of a greedy algorithm, and discuss whether or not Dijkstra's algorithm qualifies as a greedy algorithm. Understanding
4 min read
Widest Path Problem | Practical application of Dijkstra's Algorithm
It is highly recommended to read Dijkstra's algorithm using the Priority Queue first.Widest Path Problem is a problem of finding a path between two vertices of the graph maximizing the weight of the minimum-weight edge in the path. See the below image to get the idea of the problem: Practical Application Example: This problem is a famous variant of
10 min read
Maximum profit by buying and selling a share at most K times | Greedy Approach
In share trading, a buyer buys shares and sells on a future date. Given the stock price of N days, the trader is allowed to make at most K transactions, where a new transaction can only start after the previous transaction is complete. The task is to find out the maximum profit that a share trader could have made. Examples: Input: prices[] = {10, 2
11 min read
Greedy Algorithms (General Structure and Applications)
The general structure of a greedy algorithm can be summarized in the following steps:Identify the problem as an optimization problem where we need to find the best solution among a set of possible solutions.Determine the set of feasible solutions for the problem.Identify the optimal substructure of the problem, meaning that the optimal solution to
9 min read
Difference Between Greedy Knapsack and 0/1 Knapsack Algorithms
The 0/1 Knapsack algorithm is a dynamic programming approach where items are either completely included or not at all. It considers all combinations to find the maximum total value. On the other hand, the Greedy Knapsack algorithm, also known as the Fractional Knapsack, allows for items to be broken into fractions, selecting items with the highest
9 min read
Difference between 0/1 Knapsack problem and Fractional Knapsack problem
What is Knapsack Problem?Suppose you have been given a knapsack or bag with a limited weight capacity, and each item has some weight and value. The problem here is that "Which item is to be placed in the knapsack such that the weight limit does not exceed and the total value of the items is as large as possible?". Consider the real-life example. Su
13 min read
Algorithms | Greedy Algorithms | Question 3
A networking company uses a compression technique to encode the message before transmitting over the network. Suppose the message contains the following characters with their frequency: C/C++ Code character Frequency a 5 b 9 c 12 d 13 e 16 f 45 Note : Each character in input message takes 1 byte. If the compression technique used is Huffman Coding,
1 min read
Algorithms | Greedy Algorithms | Question 3
What is the time complexity of Huffman Coding? (A) O(N) (B) O(NlogN) (C) O(N(logN)^2) (D) O(N^2) Answer: (B)Explanation: O(nlogn) where n is the number of unique characters. If there are n nodes, extractMin() is called 2*(n – 1) times. extractMin() takes O(logn) time as it calls minHeapify(). So, overall complexity is O(nlogn). Quiz of this Questio
1 min read
Algorithms | Greedy Algorithms | Question 4
In question #2, which of the following represents the word \"dead\"? (A) 1011111100101 (B) 0100000011010 (C) Both A and B (D) None of these Answer: (C)Explanation: character code-word f 0 c 100 d 101 a 1100 b 1101 e 111 The word dead can be represented as: 101 111 1100 101 However, the alternative codeword can also be found by assigning 1 to the le
1 min read
Algorithms | Greedy Algorithms | Question 5
Which of the following is true about Kruskal and Prim MST algorithms? Assume that Prim is implemented for adjacency list representation using Binary Heap and Kruskal is implemented using union by rank. (A) Worst case time complexity of both algorithms is same. (B) Worst case time complexity of Kruskal is better than Prim (C) Worst case time complex
2 min read
Coin game of two corners (Greedy Approach)
Consider a two-player coin game where each player gets turned one by one. There is a row of even a number of coins, and a player on his/her turn can pick a coin from any of the two corners of the row. The player that collects coins with more value wins the game. Develop a strategy for the player making the first turn, such that he/she never loses t
9 min read
Scheduling in Greedy Algorithms
In this article, we will discuss various scheduling algorithms for Greedy Algorithms. Many scheduling problems can be solved using greedy algorithms. Problem statement: Given N events with their starting and ending times, find a schedule that includes as many events as possible. It is not possible to select an event partially. Consider the below ev
2 min read
Prim’s MST for Adjacency List Representation | Greedy Algo-6
We recommend reading the following two posts as a prerequisite to this post. Greedy Algorithms | Set 5 (Prim’s Minimum Spanning Tree (MST)) Graph and its representationsWe have discussed Prim's algorithm and its implementation for adjacency matrix representation of graphs. The time complexity for the matrix representation is O(V^2). In this post, O
15+ min read
Algorithms | Greedy Algorithms | Question 6
Which of the following is true about Huffman Coding? (A) Huffman coding may become lossy in some cases (B) Huffman Codes may not be optimal lossless codes in some cases (C) In Huffman coding, no code is prefix of any other code. (D) All of the above Answer: (C) Explanation: Huffman coding is a lossless data compression algorithm. The codes assigned
1 min read
Huffman Coding | Greedy Algo-3
Huffman coding is a lossless data compression algorithm. The idea is to assign variable-length codes to input characters, lengths of the assigned codes are based on the frequencies of corresponding characters. The variable-length codes assigned to input characters are Prefix Codes, means the codes (bit sequences) are assigned in such a way that the
15+ min read
Efficient Huffman Coding for Sorted Input | Greedy Algo-4
We recommend to read following post as a prerequisite for this.Greedy Algorithms | Set 3 (Huffman Coding) Time complexity of the algorithm discussed in above post is O(nLogn). If we know that the given array is sorted (by non-decreasing order of frequency), we can generate Huffman codes in O(n) time. Following is a O(n) algorithm for sorted input.1
15+ min read
Correctness of Greedy Algorithms
A greedy algorithm selects a candidate greedily (local optimum) and adds it to the current solution provided that it doesn't corrupt the feasibility. If the solution obtained by above step is not final, repeat till global optimum or the final solution is obtained. Although there are several mathematical strategies available to proof the correctness
2 min read