Sub topic 4
Union: Linking Two Roots
This chapter assumes you know the parent-pointer forest and the find function from the previous chapters. Here you will see how union merges two sets, why it must work on roots, and which mistakes break it.
The idea
Each set is a tree, and each tree has one root. The root is the node whose parent is itself. To merge two sets, you make one root point at the other. After that, every node in both trees leads to the same root, so find returns the same answer for all of them.
You do not touch any other node. One assignment merges two sets of any size.
The steps
- Call
find(a)andfind(b)to get the two roots,raandrb. - If
ra == rb, the elements are already in the same set. Do nothing. - Otherwise, set
parent[ra] = rb.
A runnable version
parent = list(range(6))
def find(x):
while parent[x] != x:
x = parent[x]
return x
def union(a, b):
ra, rb = find(a), find(b)
if ra == rb:
return False # already in the same set
parent[ra] = rb
return True
print(union(0, 1)) # True
print(union(2, 3)) # True
print(union(1, 3)) # True
print(union(0, 3)) # False
print(parent) # [1, 3, 3, 3, 4, 5]Trace the third call. find(1) is 1 and find(3) is 3. The code sets parent[1] = 3. Node 0 still points at 1, so find(0) now walks 0, 1, 3. The fourth call finds that both roots are 3 and returns False.
The return value is useful. True means two different sets merged. False means nothing changed. Later chapters use this signal for counting components, detecting cycles, and Kruskal's algorithm.
Common errors
Linking the elements instead of the roots
A common first attempt is parent[a] = b. It looks right but breaks the structure:
parent = list(range(3))
def bad_union(a, b):
parent[a] = b # wrong: a and b may not be roots
def find(x):
while parent[x] != x:
x = parent[x]
return x
bad_union(0, 1) # parent: [1, 1, 2]
bad_union(0, 2) # parent: [2, 1, 2]
print(find(0) == find(1)) # False, but 0 and 1 were already mergedThe second call overwrote the link from 0 to 1. Node 0 left its old tree, and node 1 was stranded. Always call find first and link the roots.
Skipping the same-root check
If both roots are equal and you still run parent[ra] = rb, you set a root to point at itself. That is harmless for a single node. It does hide the "already connected" case, and you lose the False return that later algorithms need.
Forgetting that direction matters
parent[ra] = rb and parent[rb] = ra both give a correct answer to "who is together". They produce different tree shapes, and that has a cost.
Cost
union does two find calls and one assignment. Its cost is the cost of find, which is the depth of the deeper node's tree. With the naive rule above, that depth can reach n - 1.
Exercise
Start with parent = [0, 1, 2, 3, 4]. You run union(0, 1), union(2, 3), and then union(0, 3) with the code from this chapter. What is parent at the end, and what does the last call return?
Show answer
After union(0, 1), parent is [1, 1, 2, 3, 4]. After union(2, 3), it is [1, 1, 3, 3, 4]. For union(0, 3), find(0) is 1 and find(3) is 3. The code sets parent[1] = 3, so parent is [1, 3, 3, 3, 4]. The call returns True because the roots differed.