3.7.4Algorithm Paradigms

Greedy problems — activity selection, fractional knapsack, Huffman coding (full algorithm)

2,190 words10 min readdifficulty · medium

1. Activity Selection


2. Fractional Knapsack


3. Huffman Coding (full algorithm)

Figure — Greedy problems — activity selection, fractional knapsack, Huffman coding (full algorithm)

Recall Feynman: explain to a 12-year-old

Activity selection: you want to watch as many movies as possible in one theater — always pick the movie that ends soonest, so you're free again quickest. Fractional knapsack: you can scoop powders into a bag; pour in the most expensive-per-gram powder first until the bag is full. Huffman: you give short secret-codes to words you say a lot and long ones to rare words. To build it, keep gluing together the two rarest words and recombining, so the rarest ones end up deepest (longest codes).


Flashcards

Greedy algorithm definition
Builds solution incrementally, taking the locally best choice and never reconsidering.
Two properties needed for greedy optimality
Greedy-choice property + optimal substructure.
Activity selection greedy rule
Sort by finish time; repeatedly pick earliest-finishing compatible activity.
Why earliest-finish (not shortest/earliest-start)
Finishing first leaves the maximum remaining time window for other activities.
Activity selection complexity
O(nlogn)O(n\log n) (sort) + O(n)O(n) scan.
Fractional knapsack greedy rule
Sort by value/weight density descending; take items fully, fraction the last.
Why fractional knapsack is greedy-solvable
Fractions let you exactly fill capacity with the densest item — exchange argument gives no waste.
Why 0/1 knapsack is NOT greedy
Indivisible items can force gaps; needs dynamic programming. Counterexample W=50, A(60,10),B(100,20),C(120,30).
Huffman core step
Pop two smallest-frequency nodes from a min-heap, merge into a node of summed frequency, push back; repeat n−1 times.
Huffman total cost formula
Sum of frequencies of all internal (merged) nodes.
Why merge the two smallest in Huffman
They are the deepest siblings in an optimal tree; merging them adds the least cost per step (exchange argument).
Huffman complexity
O(nlogn)O(n\log n) using a binary min-heap.
Prefix-free code meaning
No codeword is a prefix of another, so decoding is unambiguous.
General proof technique for greedy correctness
Exchange argument — transform any optimal solution into one containing the greedy choice without increasing cost.

Connections

  • Dynamic Programming — needed when greedy fails (0/1 knapsack, edit distance).
  • Priority Queue / Binary Heap — engine behind Huffman's min-extraction.
  • Exchange Argument — universal proof tool for greedy correctness.
  • Sorting Algorithms — the O(nlogn)O(n\log n) preprocessing in activity selection & knapsack.
  • Prefix Codes & Information Theory — Huffman approaches the Shannon entropy bound.
  • Minimum Spanning Tree (Kruskal/Prim) — other classic greedy + exchange proofs.

Concept Map

needs

needs

no backtrack, risks failure

proves safe

applies to

applies to

applies to

greedy rule

justifies

sort cost O n log n

Greedy algorithm

Greedy-choice property

Optimal substructure

Exchange argument

Activity selection

Earliest finish time

Fractional knapsack

Huffman coding

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Greedy algorithm ka funda simple hai: har step pe jo abhi best dikh raha hai wahi le lo, aur peeche mud ke socho mat. Lekin yeh tabhi sahi answer deta hai jab problem mein do cheezein ho — greedy-choice property (aaj ka best move kisi ek optimal solution ka hissa hai) aur optimal substructure. Proof ka famous tareeka hai exchange argument: koi bhi optimal solution lo, aur dikha do ki greedy ka choice usme swap kar do toh answer kharab nahi hota.

Activity selection mein ek hi hall hai aur max programs fit karne hain. Trick: earliest finish time wala activity pehle pick karo — kyunki jo jaldi khatam hota hai woh sabse zyada future time bacha ke deta hai. Shortest duration ya earliest start se sort karna galat hai, wohi common mistake hai. Fractional knapsack mein bag ki capacity W hai aur items ke tukde le sakte ho — toh value/weight density sabse zyada wala pehle bharo, last item ka fraction le lo. Yaad rakho: 0/1 knapsack (tukde nahi) mein yeh greedy fail karta hai, wahan DP lagta hai.

Huffman coding mein frequent symbols ko chhota code, rare ko lamba code dena hai. Algorithm: ek min-heap banao, baar baar do sabse choti frequency nikaalo, merge karke unka sum wapas daalo — n−1 baar. Total cost = saare internal (merged) nodes ki frequency ka sum. Yeh isliye optimal hai kyunki rare symbols tree mein sabse neeche (deepest) chale jaate hain, jo bilkul wahi hai jo hum chahte hain. Teeno ka mantra: "Finish Fast, Pour Pricey, Merge Mini" — exam mein yeh ratt lo!

Go deeper — visual, from zero

Test yourself — Algorithm Paradigms

Connections