0
Explore
0

Greedy Algorithm | Definition, Example and Applications

Updated on November 20, 2025

A Greedy Algorithm is a simple way to solve problems by always making the best choice available at the moment without worrying about the future. It works step by step, picking the option that seems most promising or cheapest each time, hoping this will lead to the optimal solution in the end. Greedy algorithms are easy to understand and fast, but they only work correctly for problems where local choices lead to a global best solution.

What is a Greedy Algorithm?

A Greedy Algorithm is a problem-solving method that makes the best choice at each step with the hope of reaching the overall optimal solution. It works in a simple and fast way, especially for optimization problems (finding minimum or maximum values).

Key idea of Greedy Algorithms

  • At each step choose the locally best option.
  • Repeat until the problem is solved.
  • Hope (or prove) that these local choices produce a globally optimal solution.

Properties of Greedy Algorithms

Two properties determine whether greedy methods will work correctly.

1. Greedy-Choice Property

  • A global optimal solution can be reached by making a local optimal choice first.
  • In other words, one of the choices that looks best now is part of some optimal overall solution.
  • Once a choice is made, the algorithm never reverses it, assuming it is safe and optimal for the final result.

2. Optimal Substructure

  • An optimal solution to the whole problem contains optimal solutions to its subproblems.
  • This means you can solve smaller parts optimally and combine them.

Imp points about greedy algorithms are:

  • Greedy Choice: Always pick the option that looks best at the moment, without worrying too much about future steps.
  • Optimization Use: Mainly used to maximize (profit, efficiency) or minimize (cost, distance, time) values.
  • Examples: Problems like Minimum Spanning Tree (Kruskal’s, Prim’s), Activity Selection, Huffman Coding, and Shortest Path in some cases.
  • Fast and Simple: Greedy algorithms are easy to understand and implement.
  • Limitations: They don’t always give the perfect solution; they work only if the problem has greedy-choice property and optimal substructure.

Characteristics of Greedy Algorithms

1. Local Optimal Choice

  • At every step, the greedy algorithm picks the option that looks the best right now, even if it might not seem perfect for the whole problem.
  • This choice is called locally optimal because it is the best for that step only.
  • Example: When giving change using coins, if you always pick the largest coin first, you are making a local optimal choice at each step.

2. Irrevocable Decisions

  • In a greedy algorithm, once you pick an option, you cannot go back and change it.
  • This is why it is called irrevocable. The algorithm commits to every choice it makes.
  • Because of this, greedy algorithms are fast, but they need to make sure each choice is really safe to lead to the best overall solution.

3. Optimal Substructure

  • A problem has optimal substructure if the overall best solution can be made by combining best solutions of smaller subproblems.
  • In other words, solving small pieces optimally helps solve the big problem optimally.
  • Example: In finding shortest paths in a graph, if the shortest path to node A is correct, and the shortest path to node B is correct, combining these paths can help build the shortest path for a larger route.

4. No Backtracking

A greedy algorithm never goes back to change or undo a choice once it is made. Every decision is final. This makes greedy methods fast, but also risky — a wrong early choice can lead to a non-optimal overall solution.

5. Step-by-Step Solution Building

The solution is constructed gradually. At each step, the algorithm selects the best-looking option and immediately adds it to the final answer. Over time, these local decisions combine to form the complete solution.

6. Fast and Simple

Because greedy algorithms do not explore multiple possibilities or revisit earlier decisions, they are usually much faster than exhaustive search, dynamic programming, or backtracking approaches. Their straightforward, one-pass decision-making makes them easy to understand, implement, and run efficiently even on large inputs.

Steps to Solve a Problem Using Greedy Approach

1. Understand the Problem Clearly

  • Before starting, make sure you know exactly what the problem is asking.
  • Identify what needs to be optimized — for example, do you want to minimize cost, maximize profit, or find the shortest path?
  • Understanding the problem helps you choose the right strategy and avoid mistakes later.

2. Identify the Greedy Choice

  • Find the option that looks best at each step.
  • This is the greedy choice, the one that seems locally optimal right now.
  • Example: If you are giving change, the greedy choice is taking the largest coin available first.
  • The key is that this choice should be safe to use for building the final solution.

3. Repeat the Greedy Choice

  • Keep selecting the greedy choice again and again until the problem is fully solved.
  • Add each choice to your solution only if it doesn’t break the rules of the problem (like creating a cycle in MST).
  • This step gradually builds the complete solution.

4. Check if the Solution is Valid and Optimal

  • Once all steps are done, verify that the solution meets all problem requirements.
  • Check if the solution is actually the best possible (total cost is minimum, total profit is maximum, etc.).
  • If everything is correct, you have successfully used the Greedy Approach.

Example : Coin Change Problem by Greedy Algorithm

Problem:

You have coins of 1, 5, 10, 25 units. You need to give a total of 37 units as change using the minimum number of coins.

Greedy Approach:

The idea is to always pick the largest coin possible that does not exceed the remaining amount. Then reduce the total and repeat until the total becomes 0.

Steps:

  1. Choose the largest coin ≤ remaining amount.
  2. Subtract its value from the remaining amount.
  3. Repeat until the remaining amount becomes 0.

Step-by-Step Solution Table:

StepRemaining AmountCoin Chosen
13725
21210
321
411

Step 1:

  • We start with 37.
  • Which coin is the largest that doesn’t exceed 37? That’s 25 (a quarter).
  • We take one quarter. Subtract 25 from 37 → 12 remains.
  • Why pick 25? Because picking the biggest possible coin now usually reduces the number of coins needed later.

Step 2:

  • Remaining amount is 12.
  • Largest coin ≤ 12 is 10 (a dime).
  • Take a dime. Subtract 10 from 12 → 2 remains.

Step 3:

  • Remaining amount is 2.
  • Largest coin ≤ 2 is 1 (a penny). (We don’t have a 2-cent coin.)
  • Take one penny. Subtract 1 from 2 → 1 remains.

Step 4:

  • Remaining amount is 1.
  • Largest coin ≤ 1 is 1 (penny).
  • Take one more penny. Subtract 1 → 0 remains. We’re done.
StepRemaining amount before stepLargest coin ≤ remainingNew remaining after picking
13725 (quarter)37 − 25 = 12
21210 (dime)12 − 10 = 2
321 (penny)2 − 1 = 1
411 (penny)1 − 1 = 0

Result:

  • Coins chosen: 25, 10, 1, 1
  • Total coins used = 4

The greedy approach quickly finds the minimum number of coins in this case.

Advantages of Greedy Algorithm

  • Simple and easy: Very easy to understand and implement.
  • Fast and efficient: Works quickly for problems where choosing the best option step by step gives the best overall solution.
  • Good for optimization problems: Perfect for problems like Minimum Spanning Tree (MST), shortest path, coin change (with suitable coins), and activity selection.
  • Step-by-step solution: Builds the solution gradually by making small best choices.

Disadvantages of Greedy Algorithm

  • Not always optimal: Doesn’t always give the best solution for every problem.
  • Depends on problem type: Works only if the problem has greedy-choice property and optimal substructure.
  • Irrevocable decisions: Once a choice is made, it cannot be changed, so a wrong choice can lead to a bad solution.
  • Limited use: Cannot be used for all problems, only for certain types of optimization problems.

Famous Problems Solved by Greedy Algorithm

ProblemApproach/Algorithm
Minimum Spanning Tree (MST)Kruskal’s, Prim’s
Shortest Path (single source, positive weights)Dijkstra’s
Activity SelectionGreedy by earliest finish time
Fractional KnapsackGreedy by value/weight ratio
Huffman CodingGreedy tree construction

Conclusion

The greedy algorithm is a problem-solving method where we make the best possible choice at each step with the hope of reaching the overall optimal solution. It is simple, fast, and easy to implement, especially for optimization problems like Minimum Spanning Tree, shortest path, and activity selection.

However, it does not always guarantee the best solution unless the problem has the greedy-choice property and optimal substructure. Therefore, it is important to analyze the problem carefully before using the greedy approach.