Here’s a **complete, easy-to-understand, and in-depth note on the *Greedy Method***
— perfect for **GATE, interviews, and coding preparation** 👇
---
# 💰 GREEDY METHOD – COMPLETE NOTES
---
1## 1 Introduction
1️⃣
### **Definition:**
The **Greedy Method** is an algorithmic technique where **a problem is solved step
by step**, choosing the **best (local optimum) option** at each step **with the
hope of finding a global optimum**.
> In simple terms:
> **“Take the best you can get now and hope it leads to the overall best
solution.”**
---
2️⃣
## Characteristics of the Greedy Method
| Property | Description
|
| --------------------- |
------------------------------------------------------------- |
| **Local choice** | At every stage, the algorithm makes the best local
decision. |
| **No backtracking** | Once a choice is made, it is not changed.
|
| **Feasible solution** | Each step must maintain a valid (feasible) solution.
|
| **Optimization** | Used mainly for optimization problems (maximize or
minimize). |
---
3️⃣
## Steps in the Greedy Approach
1. **Identify the candidate set** (all possible options).
2. **Select the best candidate** (according to a greedy criterion).
3. **Check feasibility** – does it still satisfy the problem constraints?
4. **Include** the selected candidate in the solution set.
5. **Repeat** until all elements are considered or goal is reached.
---
4️⃣
## Example to Understand
### **Example 1: Coin Change Problem**
> Problem: Find the minimum number of coins required to make ₹X using available
coin denominations.
**Greedy Algorithm:**
1. Sort coins in decreasing order.
2. Pick the largest coin ≤ remaining amount.
3. Subtract it from amount.
4. Repeat until amount = 0.
**Example:**
Amount = ₹18, coins = [10, 5, 2, 1]
→ 10 + 5 + 2 + 1 = 4 coins ✅
⚠️ **Note:** Greedy may fail for coins like [1, 3, 4] for amount 6 (greedy gives
4+1+1=3 coins; optimal is 3+3=2).
---
5️⃣
## When Does Greedy Work?
A greedy algorithm works **only if** the problem has the following two properties:
### 1. **Greedy Choice Property**
A global optimum can be achieved by choosing local optima at each stage.
### 2. **Optimal Substructure**
An optimal solution to the problem contains **optimal solutions to its
subproblems**.
---
6️⃣
## Advantages and Disadvantages
| Advantage | Disadvantage |
| ----------------------------- | -------------------------------------- |
| Simple and easy to understand | Doesn’t always give optimal result |
| Faster (less computation) | Fails if problem lacks greedy property |
| No need to explore all paths | May require proof of correctness |
---
7️⃣
## Common Problems Solved by Greedy Method
| Problem | Description
| Greedy Criterion |
| --------------------------------- |
-------------------------------------------------- |
--------------------------------------------- |
| **Fractional Knapsack** | Select items to maximize value within weight
limit | Highest value/weight ratio |
| **Job Sequencing with Deadlines** | Maximize profit with limited deadlines
| Highest profit first |
| **Huffman Coding** | Compress data with minimum cost
| Merge least frequent symbols first |
| **Prim’s Algorithm** | Minimum Spanning Tree
| Choose smallest edge that doesn’t form cycle |
| **Kruskal’s Algorithm** | Minimum Spanning Tree
| Choose smallest weight edge globally |
| **Dijkstra’s Algorithm** | Shortest path from a source
| Choose vertex with minimum tentative distance |
| **Activity Selection** | Select maximum non-overlapping activities
| Choose earliest finishing activity |
---
8️⃣
## Example: Fractional Knapsack Problem
**Given:**
Items with weight (W) and value (V); capacity = M.
Can take fractions of items.
**Steps:**
1. Compute ratio = V/W for all items.
2. Sort items by descending ratio.
3. Take items while capacity allows.
4. If next item can’t fit fully, take fraction.
**Example:**
| Item | Weight | Value | Ratio (V/W) |
| ---- | ------ | ----- | ----------- |
| A | 10 | 60 | 6 |
| B | 20 | 100 | 5 |
| C | 30 | 120 | 4 |
Capacity = 50
Take A (10), B (20), part of C (20/30)
→ Value = 60 + 100 + (20×4)= **240**
✅ Greedy gives optimal result (fractional knapsack supports greedy).
---
9️⃣
## Example: Activity Selection Problem
**Given:** Start & finish times of activities.
**Goal:** Select maximum number of non-overlapping activities.
**Algorithm:**
1. Sort activities by finish time.
2. Select the first activity.
3. For every next activity:
* If its start time ≥ finish time of last selected → select it.
**Example:**
| Activity | Start | Finish |
| -------- | ----- | ------ |
| A1 | 1 | 3 |
| A2 | 2 | 5 |
| A3 | 4 | 6 |
| A4 | 6 | 7 |
| A5 | 5 | 9 |
Sorted by finish time: A1, A3, A4
✅ Maximum activities = 3
---
## 🔟 Applications of Greedy Algorithms
* Network routing (Dijkstra)
* Spanning tree construction (Prim, Kruskal)
* Data compression (Huffman coding)
* Scheduling (Activity selection, Job sequencing)
* Resource allocation
* Graph optimization problems
---
## 🧮 Time Complexities of Famous Greedy Algorithms