Greedy Method in DAA: Types, Steps, Examples

Published: 23 Aug 2025 | Reading Time: 4 min read

Overview

Greedy algorithm is a widely used method in the discipline of algorithm design that is aimed at solving optimization problems. Greedy algorithm is a method that makes a series of decisions such that each decision appears optimal at a point. This comprehensive guide explores the greedy algorithm in Design and Analysis of Algorithms (DAA), their types, time and space complexity, and the steps involved in their design, along with examples and applications. The guide also covers the benefits, limitations, and characteristics of greedy algorithms.


What is a Greedy Algorithm?

Greedy algorithm is a solution strategy such that each step or choice in the solution procedure is the best. The greedy algorithm never looks back and how the current choice might affect the next steps is never considered; it makes choices that seem best at the time. The algorithm takes a series of steps which it thinks will result in the best solution, but without considering the long-term consequences of these steps.

Greedy algorithms are used chiefly for greedy-choice property problems and optimal substructure problems. The greedy-choice property ensures optimal choices leading to a globally optimal solution. In contrast, optimal substructure indicates that optimal solutions for the problem can be constructed efficiently from optimal subproblem solutions.


Types of Greedy Algorithms

There are seven main types of greedy algorithms, each with distinct characteristics and applications:

1. Fractional Greedy Algorithms

Fractional greedy algorithms choose the optimum option in each step, following the ability to divide objects. Fractional greedy algorithms solve problems that divide resources or objects into fractions. The goal is to obtain a maximal solution by choosing the optimal portions of existing resources. A classic example is the Fractional Knapsack Problem in which objects can be incorporated partially depending on their value/weight ratio.

2. Pure Greedy Algorithms

Pure greedy algorithms choose the best option at every step without considering future consequences. The moment a decision is taken, it cannot be reversed, and there is no chance of backtracking. Such algorithms are concerned with selecting locally optimal solutions for every step, hoping that these selections will result in globally optimal solutions. A case in Activity Selection Problem where activities are selected based on completion times to maximize the number of non-overlapping activities.

3. Recursive Greedy Algorithms

Recursive greedy algorithms divide a problem into subproblems and make a greedy choice in every recursive call. Recursive greedy algorithms divide the problem into simpler subproblems, solve them greedily, and then build the solution from them. They are generally applied to problems with optimal substructure. A typical example is Prim's Algorithm for finding the minimum spanning tree, where the minimum edge is selected and recursively added to the tree.

4. Greedy Choice Property Algorithms

Greedy choice property algorithms assume that the optimal decision at each step will result in an optimal solution. This property ensures a series of locally optimal choices result in a globally optimal solution. Greedy choice property algorithms work optimally in problems where local decisions inherently result in the final solution without needing backtracking. Huffman Coding is one example where characters are given variable-length codes according to their frequency to minimise the overall encoding length.

5. Adaptive Greedy Algorithms

Adaptive greedy algorithms change their strategy depending on the current situation of the problem. Adaptive greedy algorithms backtrack on previous decisions when the problem shifts to try and improve the total solution. Adaptive greedy algorithms are more adaptable than pure greedy algorithms, which are flexible in evolving knowledge or requirements as the solution is developed. One example is Greedy Graph Coloring, where the algorithm dynamically changes the vertices' colouration according to the graph's shifting topology as it goes through more vertices.

6. Constructive Greedy Algorithms

Constructive greedy algorithms construct the solution incrementally by choosing the optimal from the available choices at every step based on a specific criterion. These algorithms develop the final solution piece by piece, enabling each step's solution to exist. One example of this algorithm is Kruskal's Algorithm for finding the minimum spanning tree, where edges are added one at a time in non-decreasing order of the weight, and cycles are prevented to form the tree.

7. Non-Adaptive Greedy Algorithms

Non-adaptive greedy algorithms are final in their decisions and irreversible once taken. Their algorithm does not revisit or modify decisions based on the problem state or intermediate outcomes. Once a decision has been made, the process doesn't change. An example is Dijkstra's Algorithm to find the shortest path within a graph, where once the shortest distance of a vertex has been calculated, no modifications are made to it.


Time and Space Complexity of Common Greedy Algorithms

Algorithm Time Complexity Space Complexity
Activity Selection Problem O(n log n) O(1)
Huffman Coding O(n log n) O(1)
Fractional Knapsack Problem O(n log n) O(1)
Coin Change Problem O(n) O(1)
Job Sequencing with Deadlines O(n log n) O(1)

Steps to Implement a Greedy Algorithm

Implementing a greedy algorithm for a problem is achieved by the following steps:

  1. Determine the Problem Type: Identify the type of problem and the goal (maximize or minimize).

  2. Specify Solution Criteria: Specify what makes a good solution for the current problem.

  3. Select Optimal Choice: Select the optimal choice based on available information at hand. At every step, select the locally optimal one.

  4. Verify Greedy Choice Property: Ensure that the problem has the Greedy Choice Property, i.e., an optimal solution can be built by making the optimal choice at each step.

  5. Iterate Until Complete: Keep on repeating the process of greedy choice until a solution is achieved.

  6. Validate Solution: After the algorithm stops, check the correctness and feasibility of the solution.


Characteristics of Greedy Algorithm

Greedy algorithms share specific distinct characteristics that define under what circumstances and how they might be used:


Components of a Greedy Algorithm

A Greedy Algorithm generally consists of the following components:


How Greedy Algorithm Functions

The Greedy Algorithm works by selecting the best local option at every step with the expectation that it will reach the global optimal. It works as follows:

  1. Begin at the start of the problem.

  2. Evaluate all the available choices from the current state.

  3. Select the optimal choice now, no matter what it could result in later.

  4. Move to the new state according to the selected option.

  5. Keep making the best local decisions until the goal is achieved or no further progress can be made.


Example: Coin Change Problem

Consider the Coin Change Problem: Coin denominations are [1, 2, 5, 10] and the goal is to get change for amount 39 using the fewest coins.

Greedy Approach

  1. Begin with the most valuable denomination, 10.
  2. Subtract 10 from 39 and add one 10-coin.
  3. Subtract 10 from 29 and add a second 10-coin.
  4. Subtract 10 from 19 and add a third 10-coin.
  5. Subtract 5 from 9, which results in 4, and add a 5-coin.
  6. Subtract the last 2, 4 minus 2 equals 2, and add the last 2-coin.
  7. Subtract the last 2, resulting in 0, and add a final 2-coin.

C++ Implementation

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int minCoins(vector<int>& coins, int amount) {
    int n = coins.size();
    sort(coins.begin(), coins.end(), greater<int>());
    int res = 0;

    for (int i = 0; i < n; i++) {
        if (amount >= coins[i]) {
            int count = amount / coins[i];
            res += count;
            amount -= count * coins[i];
        }
        if (amount == 0) break;
    }
    return res;
}

int main() {
    vector<int> coins = {1, 2, 5, 10};
    int amount = 39;
    cout << "Minimum number of coins: " << minCoins(coins, amount);
    return 0;
}

Code Explanation

Output

Minimum number of coins: 6

Applications of Greedy Algorithms

Greedy algorithms have numerous practical applications across various domains:


Benefits of Greedy Algorithms

Greedy algorithms offer several advantages:


Limitations of Greedy Algorithms

Despite their advantages, greedy algorithms have notable limitations:


Conclusion

The greedy method in data structures and algorithms focuses more on optimization problems. The greedy method might not always deliver an optimal solution but its efficiency, speed and simplicity make it desirable for many tasks. To effectively solve the computational problems, understanding how to use a greedy algorithm is crucial.


Frequently Asked Questions

What is a greedy method in DAA?

The greedy technique is a type of algorithmic strategy, at every stage, takes the optimum choice, aiming towards the optimum solution based on local choices and not backtracking.

What is a greedy algorithm with an example?

Greedy algorithm solves optimisation problems by taking the optimal available choice at each step. As an example, the issue of coin change involves using the greedy strategy to keep coin usage minimal.

What is the basic principle of greedy method?

The greedy method is concerned with making the best local choice at every step and hoping these local optimal solutions yield an optimal global solution.

What is the backtracking method?

Backtracking is a method for solving problems in which all possible solutions are tried by building candidates step by step and discarding them if they do not result in a valid solution.


About NxtWave

This article is provided by NxtWave (CCBP), an educational platform offering comprehensive programs in computer science and technology. For more information, visit www.ccbp.in.

Contact Information:

Course Offerings: