How to spot a greedy algorithm?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Greedy algorithms are a popular approach in the realm of algorithm design, typically used for optimization problems. They follow a simple yet effective paradigm: make the best choice at each step, aiming for a global optimum. However, distinguishing whether a given algorithm is greedy can sometimes be non-trivial. This article provides an in-depth look at how to spot a greedy algorithm, exploring technical aspects and examples of when this approach is successful or not.
Characteristics of Greedy Algorithms
To identify whether an algorithm is greedy, look for the following characteristics:
- Local Optimization Choice: A greedy algorithm makes a series of choices. At each step, it chooses the option that provides the most immediate benefit. The key is that each decision is made independently of the others and aims for local optimization.
- Irrevocable Decisions: Once a decision is made, it cannot be undone or reconsidered later in the process. This lack of backtracking is a hallmark of a greedy approach.
- Feasibility: Every step in the algorithm must maintain a feasible solution. This means that at any stage, the accumulated choices lead towards a valid overall solution.
- Objective Goal: The algorithm aims to either maximize or minimize a certain criterion, typically a cost, profit, time, or distance function.
- Optimal Substructure: Problems suitable for greedy algorithms often have an optimal substructure, meaning that the best solution to a problem can be constructed efficiently from optimal solutions of its subproblems.
Examples of Greedy Algorithms
1. Coin Change Problem
Consider a problem where you need to provide change using the fewest coins possible. Assume you have an infinite supply of coins of denominations like 1, 5, 10, and 25.
Algorithm:
- Start with the largest denomination coin and select as many as possible until the remaining amount is less than the current denomination.
- Move to the next lower denomination and repeat.
Analysis:
- This is a classic example of a greedy algorithm where the local optimal choice is to use the largest coin possible.
- It works perfectly when the currency system is canonical, meaning the denominations are such that the greedy algorithm gives the optimal solution.
2. Activity Selection Problem
Given a set of activities with start and finish times, the goal is to select the maximum number of activities that do not overlap.
Algorithm:
- Sort the activities based on their finish times.
- Start with the first activity and select it.
- For each subsequent activity, select it if its start time is greater than or equal to the finish time of the last selected activity.
Analysis:
- The local optimal choice is selecting the activity that finishes first. This ensures room for selecting more activities later.
Technical Considerations
While greedy algorithms provide efficient solutions, they don't always guarantee success. Here's how to evaluate their effectiveness:
- Correctness: Not every problem can be solved with a greedy approach. A necessary step is to prove that the algorithm produces an optimal solution. This often involves constructing a counterexample to prove it doesn't work generally or using a process like a "greedy stays ahead" argument.
- Complexity: Greedy algorithms usually exhibit reduced complexity and are faster than alternative methods like Dynamic Programming due to their one-shot nature of decision-making.
- Counterexamples: Develop examples where greedy choices lead to suboptimal solutions. This can guide refining the strategy or choosing a different algorithm altogether.
For instance, while the greedy algorithm is optimal for the Activity Selection Problem, it can fail with different problem settings like the Traveling Salesman Problem or the Knapsack Problem when objects are not divisible.
Summary Table
The following table summarizes the main attributes and applicability conditions for greedy algorithms:
| Attribute | Description |
| Local Optimization | Makes the best immediate choice at each step. |
| Irrevocable Decisions | Decisions made cannot be changed later. |
| Feasibility | Ensures all stages maintain a valid solution framework. |
| Objective Goal | Aims to optimize a particular aspect—minimization or maximization. |
| Optimal Substructure | Problems often have built-in optimal subproblems leading to the larger solutions. |
| Applicability Examples | Coin Change (canonical), Activity Selection, Prim's Algorithm for Minimum Spanning Tree, etc. |
| Successful Conditions | Problem must allow local decisions to lead to global optimum. |
Additional Considerations
- Proof Techniques: Familiarize yourself with proof techniques such as induction or contradiction to establish the correctness of greedy algorithms in specific problems.
- Comparison with Other Techniques: Understand the contrast with Dynamic Programming or Backtracking, where these allow for reconsideration of earlier choices.
- Hybrid Strategies: In some complex problems, a combination of greedy and other techniques might yield optimal results.
Ultimately, identifying a greedy algorithm involves understanding its independence of decisions and suitability for specific types of problems. Analyzing its potential efficacy through proofs or counterexamples also informs the decision to employ this approach.
Related reading
- How to subsample a 2D polygon?
- How to tell if an array is a permutation in On?
- How to tell if greedy algorithm suffices for finding minimum coin change?
- How to test a hash function?
- How to spread processes over time getting minimum number of collisions
- How to stop Firebase from logging status updates when app is launched
- How to test if one string is a subsequence of another?
- How to think in recursive way?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.