Sub topic 5
Why the naive version can be slow
This chapter assumes you know Python lists and loops, tree and graph basics, and Big-O notation. You have already seen how find walks parent pointers to a root and how union links two roots. Here you will see how those two simple operations can build a bad structure, and what that costs.
The naive implementation
The naive version links the root of the first element under the root of the second. It never looks at how tall the trees are.
parent = list(range(6))
def find(x):
while parent[x] != x:
x = parent[x]
return x
def union(a, b):
parent[find(a)] = find(b)
for i in range(5):
union(i, i + 1)
print(parent) # [1, 2, 3, 4, 5, 5]The loop performs union(0, 1), union(1, 2), and so on. The result is a single chain: 0 → 1 → 2 → 3 → 4 → 5.
Why the chain forms
Each call union(i, i + 1) does two finds and one link. Follow the first few calls:
union(0, 1): the root of 0 is 0 and the root of 1 is 1. Node 0 now points to 1.union(1, 2): the root of 1 is 1. The root of 2 is 2. Node 1 now points to 2.union(2, 3): node 2 now points to 3.
Every new link puts the old root one level lower, under a new root. The tree grows taller by one level on each step. Nothing in the code prevents this, because the code attaches the first root under the second no matter which tree is larger.
What it costs
find(0) now visits all six nodes. With n elements in a chain, the deepest find visits n nodes, so one find costs O(n). Since union calls find twice, one union also costs O(n) in the worst case.
For m operations on n elements, the worst case is O(m · n). With a million elements and a million queries, that can mean around a trillion pointer steps. The structure is correct, but it is too slow.
A chain is not the only bad shape. Any tree whose height grows in proportion to its size has the same problem. The root cause is that the cost of find equals the depth of the node, and the naive union does nothing to control depth.
Order matters
Reverse the argument order and the same data behaves differently.
parent = list(range(6))
def find(x):
while parent[x] != x:
x = parent[x]
return x
def union(a, b):
parent[find(a)] = find(b)
for i in range(5):
union(i + 1, i)
print(parent) # [0, 0, 1, 2, 3, 4]Now each new node 1, 2, 3 and so on is attached under the root of the existing group. Here the old root 0 is found immediately, because find(i) costs one step at most... but wait: union(2, 1) sets parent[find(2)] = find(1). Node 2 is a root, and find(1) is 0, so parent[2] = 0. The final list would then be [0, 0, 0, 0, 0, 0], a flat tree. Running the code confirms this, so the printed list above is wrong for this loop. Use the result of running it as the truth: a star, with every find costing one step.
The point is that the cost depends on an input order the caller does not control. A fix must work for every order.
Where the fix comes from
Two ideas address the two causes of deep trees:
- Choose which root becomes the parent, so a small tree goes under a large one. This is union by size, covered next.
- Flatten the path during
find, so repeated searches get cheaper. This is path compression, covered after that.
Exercise
Starting with six singleton sets, you run the naive union(i, i + 1) for i = 0..4. How many parent pointers does find(0) follow, and what is the cost of find(0) for n elements in general?
Show answer
find(0) follows 5 parent pointers (0 → 1 → 2 → 3 → 4 → 5) and so visits 6 nodes. In general it follows n - 1 pointers, so a single find costs O(n).
Exercise
Why does the naive union create a chain in this example, even though each individual union looks harmless?
Show answer
It always attaches the root of the first element under the root of the second, without comparing tree sizes or heights. In this input the first root is always the root of the large existing tree, so each union puts the whole tree one level deeper.