Sub topic 3
Huffman Coding, Spanning Trees, and the Limits of Greedy
This chapter assumes you can use Python's heapq module, read adjacency lists, and follow an exchange argument from the previous chapter.
Huffman Coding: Optimal Prefix Codes
A prefix code gives each symbol a bit string, and no string is the start of another, so decoding is never ambiguous. Huffman's algorithm builds the code by repeatedly merging the two least frequent subtrees, using a min-heap. An exchange argument shows that the two rarest symbols can always be siblings at the deepest level of some optimal tree. That makes the first merge safe, and induction handles the rest. With n symbols the algorithm runs in O(n log n).
Minimum Spanning Trees with Kruskal and Prim
The cut property says the lightest edge crossing any cut belongs to some minimum spanning tree. Kruskal's algorithm sorts the edges and adds each one unless it would close a cycle. Prim's algorithm grows a single tree and always adds the lightest edge leaving that tree. Both rely on the cut property.
def kruskal(n, edges):
parent = list(range(n))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
mst = []
for w, u, v in sorted(edges):
ru, rv = find(u), find(v)
if ru != rv:
parent[ru] = rv
mst.append((u, v, w))
return mst
edges = [(1, 0, 1), (4, 0, 2), (2, 1, 2), (6, 1, 3), (3, 2, 3)]
print(kruskal(4, edges)) # [(0, 1, 1), (1, 2, 2), (2, 3, 3)]Dijkstra's Algorithm as a Greedy Algorithm
Dijkstra's algorithm finalizes the closest unsettled vertex at each step. The correctness proof is a stays-ahead argument: any other path to that vertex must leave the settled set at a point that is already at least as far away. That argument only holds when no edge weight is negative.
Greedy or Dynamic Programming? Choosing the Right Tool
| Signal | Use |
|---|---|
| A safe first choice can be proven (stays ahead or exchange argument) | Greedy |
| Optimal substructure holds, but the best choice depends on subproblem results | Dynamic programming |
| The problem is NP-hard and a bounded error is acceptable | Greedy approximation |
The 0/1 knapsack problem and coin change with denominations {1, 3, 4} are the standard warnings. Both have optimal substructure but lack the greedy choice property.
Greedy Approximation: Set Cover and Beyond
Greedy set cover repeatedly picks the set that covers the most uncovered elements. It is not optimal, but it is guaranteed to come within a factor of H(n) ≈ ln n of the optimum. Unless P = NP, no polynomial-time algorithm can do substantially better.
Matroids: Why Greedy Works When It Works
A matroid is a family of "independent" sets with two rules. Every subset of an independent set is independent. A smaller independent set can always be extended with some element from a larger one. The Rado–Edmonds theorem states that greedy finds a maximum-weight independent set for every choice of weights exactly when the structure is a matroid. The forests of a graph form a matroid, which is why Kruskal's algorithm works.
Exercise
In a graph with edges A→B (weight 1), A→C (weight 2), and C→B (weight −2), what does Dijkstra's algorithm report as the distance from A to B, and what is the true distance?
Show answer
Dijkstra settles B first with distance 1 and never revisits it. The true shortest distance is 0, along the path A→C→B (2 − 2). The negative edge breaks the stays-ahead argument.