FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

Greedy Method



Greedy Method

The Greedy Method is an algorithm design technique in which the best possible choice is made at every step with the hope of obtaining the overall optimal solution. Instead of considering all possible solutions, a greedy algorithm always selects the option that appears to be the most beneficial at the current stage.

Greedy algorithms are generally simple, efficient and easy to implement. However, they do not always produce the optimal solution for every problem. They work correctly only for problems that satisfy the Greedy Choice Property and the Optimal Substructure Property.



Real-Life Example

Suppose you have ₹100 and want to purchase as many notebooks as possible. If notebooks of different prices are available, you may choose the cheapest notebook first, then the next cheapest, and continue until your money is exhausted. At every step, you make the locally best decision without reconsidering previous choices.

This approach represents the basic idea of the Greedy Method.


Characteristics of Greedy Method

Characteristic Description
Local Optimal Choice Selects the best available option at every step.
No Backtracking Once a decision is made, it is never changed.
Fast Execution Usually requires less computation than exhaustive methods.
Simple Implementation Easy to design and understand.

Working Principle

The Greedy Method solves a problem by repeatedly selecting the best available choice without reconsidering previous decisions.


Problem

    │

Find Best Choice

    │

Select It

    │

Reduce Problem Size

    │

Repeat

    │

Final Solution

Figure 1 : Basic Working of Greedy Method


Example

Suppose you have coins of ₹10, ₹5, ₹2 and ₹1, and you need to make ₹18.

Step Coin Selected Remaining Amount
1 ₹10 ₹8
2 ₹5 ₹3
3 ₹2 ₹1
4 ₹1 ₹0

At every step, the algorithm chooses the largest possible coin. This is a Greedy approach because each decision is made independently without considering future possibilities.


Greedy Algorithm

GreedyAlgorithm(Problem) Step 1 : Identify all possible choices. Step 2 : Select the best available choice. Step 3 : Include the selected choice in the solution. Step 4 : Reduce the problem size. Step 5 : Repeat until the complete solution is obtained.

Properties of Greedy Method

Property Description
Greedy Choice Property The algorithm always makes the locally best decision at every step.
Optimal Substructure The optimal solution of the problem can be constructed from the optimal solutions of its subproblems.

Complexity Analysis

Case Complexity Explanation
Time Complexity Depends on the Problem Different Greedy algorithms have different running times depending on the technique used.
Space Complexity Usually O(1) or O(n) Depends upon the data structures used by the algorithm.

Advantages of Greedy Method

  • Easy to understand and implement.
  • Requires less computation.
  • Generally provides efficient solutions.
  • Consumes less memory.
  • Suitable for optimization problems satisfying Greedy Choice Property.

Limitations of Greedy Method

  • Does not always produce the optimal solution.
  • Works only for specific classes of problems.
  • Once a decision is taken, it cannot be changed.
  • Not suitable for every optimization problem.

Applications of Greedy Method

Problem Application
Activity Selection Selecting the maximum number of compatible activities.
Fractional Knapsack Maximizing profit while filling a knapsack.
Job Sequencing Scheduling jobs to maximize total profit.
Huffman Coding Data compression.
Dijkstra's Algorithm Finding the shortest path in a graph.
Prim's Algorithm Finding the Minimum Spanning Tree.
Kruskal's Algorithm Constructing the Minimum Spanning Tree.

Greedy Method vs Dynamic Programming

Greedy Method Dynamic Programming
Makes the best local decision. Considers all possible subproblems.
No reconsideration of previous decisions. Stores solutions of overlapping subproblems.
Usually faster. May require more memory.
Simpler implementation. More complex implementation.

Important Points

  • Greedy algorithms make the best local choice at every step.
  • Previous decisions are never changed.
  • Not every optimization problem can be solved using the Greedy Method.
  • Greedy algorithms are generally faster than exhaustive search techniques.
  • The problem must satisfy the Greedy Choice Property and Optimal Substructure Property.

Common Greedy Algorithms

Algorithm Purpose
Activity Selection Select maximum non-overlapping activities.
Fractional Knapsack Maximize profit.
Job Sequencing Maximize total profit within deadlines.
Huffman Coding Generate minimum-length binary codes.
Dijkstra's Algorithm Shortest path.
Prim's Algorithm Minimum Spanning Tree.
Kruskal's Algorithm Minimum Spanning Tree.