Greedy Algorithms

A greedy algorithm builds a solution by making the locally best choice at each step; it is optimal only when the problem has the greedy-choice property and optimal substructure, as in activity selection, Huffman coding and Kruskal’s MST.

Key formulas & points

Skim these first — then read the full notes below.

  • A greedy algorithm takes the locally optimal choice at each step
  • Correctness needs the greedy-choice property and optimal substructure
  • Greedy fails for 0/1 knapsack but works for the fractional version

Topic details

Introduction

This topic covers the greedy design paradigm. You learn to recognise when greedy choices lead to a global optimum, prove correctness through the greedy-choice property and optimal substructure, and study the classic successes (scheduling, MST, Huffman) alongside a notable failure (0/1 knapsack).

Key relations & formulas

Formulas (Indian textbook notation)

  • activityselection:sortbyfinishtime,thentakecompatibleonesO(nlogn)activity selection: sort by finish time, then take compatible ones - O(n log n)

Formulas (Indian textbook notation)

  • Huffmancoding:repeatedlymergethetwoleastfrequentnodesHuffman coding: repeatedly merge the two least-frequent nodes

Formulas (Indian textbook notation)

  • KruskalMST:sortedges,addifitformsnocycleO(ElogE)Kruskal MST: sort edges, add if it forms no cycle - O(E log E)

Notation and sign conventions

Relation 1 —
activityselection:sortbyfinishtime,thentakecompatibleonesOactivity selection: sort by finish time, then take compatible ones - O

Formulas (Indian textbook notation)

  • activityselection:sortbyfinishtime,thentakecompatibleonesO(nlogn)activity selection: sort by finish time, then take compatible ones - O(n log n)
Write this relation with symbols exactly as in Clrs Algorithms — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
Huffmancoding:repeatedlymergethetwoleastfrequentnodesHuffman coding: repeatedly merge the two least-frequent nodes

Formulas (Indian textbook notation)

  • Huffmancoding:repeatedlymergethetwoleastfrequentnodesHuffman coding: repeatedly merge the two least-frequent nodes
Write this relation with symbols exactly as in Clrs Algorithms — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
KruskalMST:sortedges,addifitformsnocycleOKruskal MST: sort edges, add if it forms no cycle - O

Formulas (Indian textbook notation)

  • KruskalMST:sortedges,addifitformsnocycleO(ElogE)Kruskal MST: sort edges, add if it forms no cycle - O(E log E)
Write this relation with symbols exactly as in Clrs Algorithms — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

Greedy algorithms commit to the choice that looks best right now and never reconsider, which makes them fast and simple — but only correct when a local optimum is guaranteed to be part of a global one. That guarantee rests on two properties: the greedy-choice property (a globally optimal solution can start with the greedy pick) and optimal substructure (an optimal solution contains optimal solutions to subproblems). Activity selection works because choosing the earliest finishing activity always leaves the most room; the fractional knapsack works because you can take partial items by value density. The 0/1 knapsack fails precisely because you cannot split items, so a myopic choice can block a better whole.

Assumptions and validity limits

State assumptions explicitly before using any relation for greedy algorithms — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In Algorithms viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in Algorithms papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to greedy algorithms.
4. Use equation 1:
activityselection:sortbyfinishtime,thentakecompatibleonesOactivity selection: sort by finish time, then take compatible ones - O
.
5. Use equation 2:
Huffmancoding:repeatedlymergethetwoleastfrequentnodesHuffman coding: repeatedly merge the two least-frequent nodes
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

Greedy Algorithms appears in competitive programming and backend systems. In Indian it software curricula this topic is tested because it connects theory to design and analysis of algorithms.
GATE and semester exams often combine greedy algorithms with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use greedy algorithms?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students apply greedy to problems lacking the greedy-choice property (like 0/1 knapsack) and get suboptimal answers, and they skip proving why the greedy choice is safe. Choosing the wrong greedy criterion (e.g. sorting activities by start time instead of finish time) is a classic error.

Quick revision checklist

Before attempting greedy algorithms problems, confirm you can:
1. A greedy algorithm takes the locally optimal choice at each step
2. Correctness needs the greedy-choice property and optimal substructure
3. Greedy fails for 0/1 knapsack but works for the fractional version
Revise the solved examples in Clrs Algorithms — Standard reference and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

Activity selection count

Problem

After sorting by finish time, activities are chosen greedily and end at 2, 5, 8, 11 without overlap. How many are selected?

Solution

4 activities. Each next activity starts at or after the previous finish time, which the earliest-finish-time greedy rule guarantees maximises the count.

Conceptual check — Greedy Algorithms

Problem

In a Algorithms semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of greedy algorithms." What should a complete answer include?

📖 Standard books (India)

  • Clrs AlgorithmsStandard reference

    Read: Syllabus unit

    Referenced in Indian B.Tech syllabus