codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🧮

Data Structures & Algorithms

32 / 65 topics
32Greedy Algorithms Basics33Activity Selection Problem34Huffman Coding35Fractional Knapsack Problem
Tutorials/Data Structures & Algorithms/Greedy Algorithms Basics
🧮Data Structures & Algorithms

Greedy Algorithms Basics

Updated 2026-04-20
4 min read

Greedy Algorithms Basics

Greedy algorithms are a class of algorithmic techniques used for solving optimization problems. They make a series of choices, each selecting the locally optimal choice at each step with the hope that these local choices will lead to a global optimum. This approach is often simple and efficient but may not always yield the best solution.

What Are Greedy Algorithms?

A greedy algorithm builds up a solution piece by piece, choosing the next piece based on some criteria of optimality. The key characteristic of a greedy algorithm is that it makes locally optimal choices at each step with the hope of finding a global optimum.

Characteristics of Greedy Algorithms

  1. Local Optima: Each step chooses the best option available at the moment.
  2. Irrevocable Choices: Once a choice is made, it cannot be changed.
  3. No Backtracking: The algorithm does not reconsider previous choices.

Common Use Cases for Greedy Algorithms

Greedy algorithms are particularly useful in scenarios where:

  • You need to find an approximate solution quickly.
  • The problem can be broken down into smaller subproblems.
  • Each step's decision is independent of the others.

Examples of Greedy Algorithms

  1. Activity Selection Problem: Selecting non-overlapping activities with maximum profit.
  2. Fractional Knapsack Problem: Maximizing the total value of items in a knapsack without exceeding its capacity.
  3. Huffman Coding: Constructing an optimal prefix code for data compression.

Steps to Implement a Greedy Algorithm

  1. Define the Problem: Clearly understand what needs to be optimized.
  2. Identify the Optimal Substructure: Break down the problem into smaller subproblems.
  3. Choose a Greedy Criterion: Define how to make locally optimal choices.
  4. Implement the Algorithm: Code the algorithm using the chosen criterion.
  5. Analyze Complexity: Evaluate the time and space complexity of the algorithm.

Example: Activity Selection Problem

Problem Statement

Given a set of activities, each with a start and finish time, select the maximum number of non-overlapping activities that can be performed by a single person.

Greedy Criterion

At each step, choose the activity that finishes first among the remaining activities. This ensures that more slots are available for future activities.

Implementation in JavaScript

function activitySelection(activities) {
    // Sort activities based on their finish time
    activities.sort((a, b) => a.finish - b.finish);

    let selectedActivities = [];
    let lastFinishTime = 0;

    for (let i = 0; i < activities.length; i++) {
        if (activities[i].start >= lastFinishTime) {
            selectedActivities.push(activities[i]);
            lastFinishTime = activities[i].finish;
        }
    }

    return selectedActivities;
}

// Example usage
const activities = [
    { start: 1, finish: 2 },
    { start: 3, finish: 4 },
    { start: 0, finish: 6 },
    { start: 5, finish: 7 },
    { start: 8, finish: 9 }
];

console.log(activitySelection(activities));

Explanation

  1. Sorting: The activities are sorted by their finish times to ensure that the earliest finishing activity is considered first.
  2. Iterating and Selecting: We iterate through the sorted list and select an activity if its start time is greater than or equal to the last selected activity's finish time.
  3. Updating Last Finish Time: After selecting an activity, we update the lastFinishTime to the current activity's finish time.

Best Practices for Using Greedy Algorithms

  1. Understand the Problem: Ensure that the problem can be solved using a greedy approach.
  2. Prove Correctness: Use mathematical proofs or counterexamples to verify the correctness of the algorithm.
  3. Optimize Performance: Analyze and optimize the time and space complexity of the algorithm.
  4. Handle Edge Cases: Consider all possible edge cases, such as overlapping activities with the same finish time.

Limitations of Greedy Algorithms

While greedy algorithms are powerful, they have limitations:

  • Suboptimal Solutions: They may not always find the global optimum.
  • Dependence on Problem Structure: The success of a greedy algorithm heavily depends on the problem's structure and the chosen criterion.

Example: 0/1 Knapsack Problem

The 0/1 knapsack problem, where each item must be taken or left behind, cannot be solved using a greedy approach because it does not have an optimal substructure that can be exploited greedily.

Conclusion

Greedy algorithms are a fundamental tool in the algorithmic toolkit. They offer simplicity and efficiency for certain types of optimization problems. By understanding their characteristics, use cases, and limitations, you can effectively apply them to solve real-world challenges.

Further Reading

  • Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein
  • Algorithms Unlocked by Thomas H. Cormen
  • GeeksforGeeks Greedy Algorithms

By mastering greedy algorithms, you'll be better equipped to tackle a wide range of optimization problems in computer science and beyond.


PreviousEdit DistanceNext Activity Selection Problem

Recommended Gear

Edit DistanceActivity Selection Problem