advanced
The third part is algorithm design methods, but earlier chapters hide some of the most useful ideas Dynamic memory allocation, tournament trees, balanced search trees, lower bounds, NP-hard problems Those are advanced topics in the practical sense
Union find
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, x):
while self.parent[x] != x:
self.parent[x] = self.parent[self.parent[x]]
x = self.parent[x]
return x
def union(self, a, b):
ra, rb = self.find(a), self.find(b)
if ra != rb:
self.parent[rb] = ra
Tracks connected components
The path compression in find is what keeps repeated queries cheap
Where it appears:
- network connectivity
- equivalence classes
- image component labelling
- maze generation
Amortised cost
The performance chapter says amortised complexity comes after queues for a reason Some operations are expensive rarely and cheap usually
A dynamic array doubles its capacity when full That one append is expensive Most appends are cheap Average over the sequence and the cost is constant
That is amortised reasoning Judging one call gives the wrong impression
Lower bounds
Lower bound proofs appear for minmax and sorting A lower bound says no algorithm in a class can do better
Comparison sorting cannot beat n log n in the general case
That is not a challenge to be clever around
It is a boundary
When a tool claims to sort faster, ask what it assumes about the data
Hard problems
NP-hard problems appear through scheduling, bin packing, and optimisation The honest response to a hard problem is not to promise a magic algorithm
The options are:
- exploit constraints in your actual input
- accept an approximation with a known bound
- change the question
- use a heuristic and say so
The greedy chapter calls LPT a heuristic with bounded performance That is the mature framing
Approximation
An approximation algorithm gives a solution within a provable factor of optimal A heuristic gives a good solution without that guarantee Both are legitimate Mixing them up is not
Tournament trees
Chapter thirteen covers winner trees They are useful when repeated selection matters, not just one minimum A heap answers one question well A tournament tree answers a stream of selection questions
Practice
Advanced topics are not badges Union find shows up in a weekend script, amortised reasoning explains why your tool is fast, lower bounds stop you chasing impossible speed, approximation keeps a scheduler honest
Exercises
- Use union find on five nodes and merge pairs until two components remain
- Explain amortised cost for a doubling array with eight appends
- Find a claim about a fast algorithm and identify its hidden assumption
- Name a problem in your work where a bounded approximation is enough
What to remember
Theory is not decoration here It tells you what to stop optimising
Posted