Loading content...
Loading content...
This article introduces the Greedy Algorithm, one of the most important algorithm design techniques in Design and Analysis of Algorithms (DAA). You'll learn how greedy algorithms work, when they should be used, their advantages and limitations, and why they are used in famous algorithms like Dijkstra, Prim, Kruskal, Huffman Coding, and Activity Selection.
Imagine you are standing in a supermarket with ₹500 in your pocket. Your shopping basket can only hold 5 items, but the supermarket contains hundreds of products.
Since you cannot buy everything, what will you do?
Most people start by choosing the most valuable or useful item available at that moment. They don't know what products they might find later, so they make the best decision based on the current situation.
This simple human behavior is the fundamental idea behind the Greedy Algorithm.
A Greedy Algorithm solves a problem by repeatedly making the best possible choice available at the current step, without reconsidering previous decisions. It hopes that these locally optimal decisions eventually produce the globally optimal solution.
This strategy is one of the simplest and fastest algorithm design techniques in computer science and is widely used in networking, operating systems, data compression, scheduling, graph theory, artificial intelligence, and many real-world optimization problems.
Many real-world problems involve optimization.
Optimization means finding the best possible solution among many possible solutions.
Examples include:
Finding the shortest path in Google Maps.
Scheduling college lectures without clashes.
Compressing files to reduce storage.
Selecting projects to maximize profit.
Building a network with minimum cable cost.
Managing CPU processes efficiently.
One solution is to check every possible combination.
However, for large inputs this becomes impossible because the number of combinations grows exponentially.
Instead of checking everything, Greedy Algorithms make one intelligent decision at a time.
This makes them extremely fast.
Suppose you need to pay ₹786.
Available currency notes are
₹500, ₹200, ₹100, ₹50, ₹20, ₹10, ₹5, ₹2, ₹1
A Greedy Algorithm always picks the largest possible note.
Step 1: Take ₹500, Remaining = ₹286
Step 2: Take ₹200, Remaining = ₹86
Step 3: Take ₹50, Remaining = ₹36
Step 4: Take ₹20, Remaining = ₹16
Step 5: Take ₹10, Remaining = ₹6
Step 6: Take ₹5, Remaining = ₹1
Step 7: Take ₹1, Remaining = ₹0
Final Answer
₹500 + ₹200 + ₹50 + ₹20 + ₹10 + ₹5 + ₹1
Notice something interesting.
At no point did we ask
"Will choosing ₹500 create a problem later?"
We simply selected the largest note available. That is exactly how a Greedy Algorithm thinks.
The word Greedy refers to someone who always wants the best thing immediately.
Similarly, a Greedy Algorithm always chooses what appears to be the best option at the current moment.
It never tries to predict the future.
It never goes back to change previous decisions.
It simply says
"This is the best choice right now, so I'll take it."
Let's understand this definition.
Algorithm: A sequence of steps used to solve a problem.
Strategy: The method or approach used to solve the problem.
Locally Optimal: The best choice available right now.
Globally Optimal: The best final answer after considering the entire problem.
Suppose you are climbing a mountain.
There are several possible paths.
A Greedy Algorithm always chooses the path that looks steepest at the current point.
Sometimes this leads to the highest peak.
Sometimes it leads to a smaller hill.
This is why Greedy Algorithms do not always produce the optimal answer.
A Greedy Algorithm has the following characteristics:
Makes one decision at a time.
Always selects the best available option.
Never changes previous decisions.
Usually simple to implement.
Runs faster than many other approaches.
May not always produce the optimal solution.
Most Greedy Algorithms follow the same pattern:
Start
↓
Look at all available choices
↓
Choose the best one
↓
Add it to the solution
↓
Remove that choice
↓
Repeat until the problem is solved
↓
Return the final solution
Although different problems use different rules to determine the "best" choice, the overall structure remains the same.
Greedy Algorithms are widely used in computer science.
Some common applications include:
Network routing
Minimum Spanning Tree construction
GPS navigation
CPU scheduling
Data compression
File merging
Task scheduling
Resource allocation
Graph optimization
Banking and currency systems
Many famous algorithms are based on the Greedy technique, including:
Activity Selection
Fractional Knapsack
Huffman Coding
Dijkstra's Algorithm
Prim's Algorithm
Kruskal's Algorithm
Job Sequencing with Deadlines
Very fast
Easy to implement
Uses less memory
Suitable for many optimization problems
Produces optimal solutions for many practical applications
Despite their speed, Greedy Algorithms have an important limitation.
They do not guarantee the optimal solution for every problem.
A Greedy Algorithm works correctly only when the problem satisfies specific mathematical properties, which we will study in the next articles:
Greedy Choice Property
Optimal Substructure
Without these properties, a Greedy Algorithm may produce an incorrect answer.
Suppose the available coin values are:
₹1, ₹3, ₹4
Target amount = ₹6
A Greedy Algorithm chooses:
₹4
₹1
₹1
Total coins = 3
However, the optimal solution is:
₹3
₹3
Total coins = 2
This demonstrates that Greedy Algorithms do not always guarantee the best solution.
After reading this article, you should remember:
A Greedy Algorithm always chooses the best available option at the current step.
It never changes previous decisions.
It is primarily used for optimization problems.
It is fast and efficient.
It works correctly only for problems that satisfy the Greedy Choice Property and Optimal Substructure.
Many famous algorithms such as Dijkstra, Prim, Kruskal, Huffman Coding, and Activity Selection are based on the Greedy technique.
In the next article, we'll cover "Why is it Called Greedy?" with intuitive real-world stories, animations, and interactive examples. Then we'll move on to the Greedy Choice Property, Optimal Substructure, and finally solve every major greedy algorithm and important LeetCode problem with detailed Java implementations, dry runs, proofs, and interview insights.