Sub topic 8
Why It Is Nearly Constant Time: Amortized Cost and Inverse Ackermann
This chapter assumes you have the DisjointSet class from the previous chapters (parent array, union by size, path compression) and know Big-O notation. It explains why the two optimizations together make each operation cost almost nothing on average.
Worst case versus amortized cost
The worst-case cost of one operation is the most work that single call can ever do. The amortized cost is the total work of a whole sequence of operations, divided by the number of operations.
A single find can still be slow. If it walks a long path, it pays for that walk once, and path compression then flattens the path. The next finds on those nodes are cheap. The expensive call pays for itself over the sequence.
So the guarantee is about sequences. For m operations on n elements, the total cost is O(m α(n)). The cost per operation is O(α(n)) amortized, not per call.
What each optimization gives alone
| Variant | Cost of m operations on n elements |
|---|---|
| Naive union, no compression | O(m·n) worst case |
| Union by size only | O(m log n) |
| Path compression only | O(m log n) amortized |
| Both together | O(m α(n)) |
Union by size caps tree height at log₂ n. A node's tree at least doubles in size each time its depth grows by one, so depth can grow at most log₂ n times. Path compression alone has no height cap, but it repairs long paths as it finds them. Each fix alone gives a logarithmic bound. Together they give a much smaller one.
The inverse Ackermann function
The Ackermann function A(k, k) grows faster than any tower of exponentials you would write by hand. The inverse Ackermann function α(n) is the smallest k such that A(k, k) ≥ n. It grows extremely slowly.
- α(n) ≤ 4 for every n that could be stored in the memory of any real computer.
- A(4, 4) is vastly larger than the number of atoms in the observable universe.
In practice you can treat each operation as constant time. Strictly, α(n) is not a constant, and Tarjan proved in 1975 that O(m α(n)) is the right bound for this structure. Later work showed no pointer-based structure for this problem can do better in the worst case.
Measuring it yourself
This script counts how many parent pointers find follows with and without path compression. Both runs use union by size.
def run(n, compress):
parent = list(range(n))
size = [1] * n
hops = 0
def find(x):
nonlocal hops
root = x
while parent[root] != root:
root = parent[root]
hops += 1
if compress:
while parent[x] != root:
parent[x], x = root, parent[x]
return root
for i in range(n - 1):
ra, rb = find(i), find(i + 1)
if ra != rb:
if size[ra] < size[rb]:
ra, rb = rb, ra
parent[rb] = ra
size[ra] += size[rb]
for i in range(n):
find(i)
return hops
n = 100_000
print("without compression:", run(n, False))
print("with compression: ", run(n, True))Run it and compare the two numbers. With compression, the final loop of find calls costs about one hop per element. Try larger n. The count per element stays nearly flat, as the bound predicts.
Common errors
- Reading O(α(n)) as a per-call bound. One
findcan still walk about log n nodes. Do not use the bound for hard real-time limits on a single call. - Dropping one optimization. Union by size without compression gives O(log n), and compression without union by size gives O(log n) amortized. Only the pair reaches α(n).
- Linking non-roots.
unionmust link roots. If you attach one element to another without callingfindfirst, the size bookkeeping and the height cap break. - Trusting stale sizes.
size[x]is only meaningful for roots. Path compression does not update sizes, and it does not need to. - Counting rollback structures the same way. A union-find that supports undo cannot use path compression, so its cost is O(log n) per operation.
Exercise
A union-find uses union by size but no path compression. n = 1,000,000. What is the largest depth any node can have, roughly, and why?
Show answer
About 20, since log₂(1,000,000) ≈ 19.9. Each time a node's depth grows by one, the size of its tree at least doubles, because the node's root was attached under a root with at least as many elements. A tree of at most n elements can therefore double at most log₂ n times.
Exercise
A single call to find takes 500 steps on a structure with path compression and union by size. Does this contradict the O(α(n)) amortized bound?
Show answer
No. The bound applies to the average over a sequence of operations. The 500-step call flattens its path, and later finds on those nodes are nearly free, so the total over the sequence stays near m·α(n).