Greedy Algorithms - GeeksforGeeks

Greedy Algorithms

Last Updated : 02 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Greedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. In these algorithms, decisions are made based on the information available at the current moment without considering the consequences of these decisions in the future. The key idea is to select the best possible choice at each step, leading to a solution that may not always be the most optimal but is often good enough for many problems.

In this article, we will understand greedy algorithms with examples. We will also look at problems and their solutions using the greedy approach.

Greedy Algorithms

What is Greedy Algorithm?

A greedy algorithm is a type of optimization algorithm that makes locally optimal choices at each step to find a globally optimal solution. It operates on the principle of “taking the best option now” without considering the long-term consequences.

To learn what is greedy method and how to use the greedy approach, read the given tutorial on the Greedy Algorithm:

Greedy Algorithm Tutorial

Steps for Creating a Greedy Algorithm

The steps to define a greedy algorithm are:

  1. Define the problem: Clearly state the problem to be solved and the objective to be optimized.
  2. Identify the greedy choice: Determine the locally optimal choice at each step based on the current state.
  3. Make the greedy choice: Select the greedy choice and update the current state.
  4. Repeat: Continue making greedy choices until a solution is reached.

Following the given steps, one can learn how to use greedy algorithms to find optimal solutions.

Greedy Algorithm Examples

Examples of greedy algorithms are the best way to understand the algorithm. Some greedy algorithm real-life examples are:

  • Fractional Knapsack: Optimizes the value of items that can be fractionally included in a knapsack with limited capacity.
  • Dijkstra’s algorithm: Finds the shortest path from a source vertex to all other vertices in a weighted graph.
  • Kruskal’s algorithm: Finds the minimum spanning tree of a weighted graph.
  • Huffman coding: Compresses data by assigning shorter codes to more frequent symbols.

Applications of Greedy Algorithm

There are many applications of the greedy method in DAA. Some important greedy algorithm applications are:

  • Assigning tasks to resources to minimize waiting time or maximize efficiency.
  • Selecting the most valuable items to fit into a knapsack with limited capacity.
  • Dividing an image into regions with similar characteristics.
  • Reducing the size of data by removing redundant information.

Disadvantages/Limitations of Using a Greedy Algorithm

Below are some disadvantages of the Greedy Algorithm:

  • Greedy algorithms may not always find the best possible solution.
  • The order in which the elements are considered can significantly impact the outcome.
  • Greedy algorithms focus on local optimizations and may miss better solutions that require considering a broader context.
  • Greedy algorithms are not applicable to problems where the greedy choice does not lead to an optimal solution.

Basics of Greedy Algorithm:

Standard Greedy Algorithms:

Greedy Problems on Array:

Greedy Problems on Operating System:

Greedy Problems on Graph:

Approximate Greedy Algorithm for NP Complete:

Greedy for Special cases of DP:

Easy Problems on Greedy Algorithm:

Medium Problems on Greedy Algorithm:

Hard Problems on Greedy Algorithm:



Previous Article
Next Article

Similar Reads

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
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
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
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
Top 20 Greedy Algorithms Interview Questions
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. Activity Selection Problem Kruskal’s Minimum Spanning Tree Algorithm Huffman Coding Efficient Huffman Coding for Sorted Input Prim’s Mini
1 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
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
Practice Tags :