Sub topic 2
Representing sets as a forest of parent pointers
This chapter assumes you can read Python, index into lists, and know what a tree is (nodes, parents, roots). Big-O notation appears only in passing.
The idea
Union-find needs to answer one question quickly: are two elements in the same set? The usual answer is to give each set one representative element and make every member able to reach it.
A forest does this. A forest is a collection of trees. Each element is a node, and each set is one tree. The root of the tree is the set's representative. Every non-root node stores a single pointer to its parent.
Two elements are in the same set exactly when following parent pointers from each one ends at the same root.
The parent array
If the elements are the integers 0 to n - 1, one list is enough. Entry parent[i] holds the parent of element i. A root is an element that is its own parent, so parent[r] == r.
A fresh structure has no merged sets. Every element is its own root:
n = 6
parent = list(range(n))
print(parent) # [0, 1, 2, 3, 4, 5]That is six trees with one node each. Merging sets later means changing a root's entry so it points at another node. No node moves and nothing is copied.
Reading a forest
Suppose the array looks like this:
parent = [0, 0, 2, 2, 3, 5]Read it one index at a time:
parent[0] == 0, so 0 is a root.parent[1] == 0, so 1 hangs under 0.parent[2] == 2, so 2 is a root.parent[3] == 2, so 3 hangs under 2.parent[4] == 3, so 4 hangs under 3, which hangs under 2.parent[5] == 5, so 5 is a root alone.
The sets are {0, 1}, {2, 3, 4} and {5}. The array holds no list of members. It stores only the pointers.
Finding the representative
To find the set an element belongs to, follow the pointers until you reach a root. The full find operation gets its own chapter, so this is only a reading aid:
parent = [0, 0, 2, 2, 3, 5]
def root_of(x):
while parent[x] != x:
x = parent[x]
return x
for x in range(len(parent)):
print(x, "->", root_of(x))
roots = [x for x in range(len(parent)) if parent[x] == x]
print("roots:", roots)This prints:
0 -> 0
1 -> 0
2 -> 2
3 -> 2
4 -> 2
5 -> 5
roots: [0, 2, 5]The number of roots equals the number of sets. Later chapters use this fact to count connected components.
Why a forest and not something simpler
Two simpler designs show what the forest buys you.
| Design | Same-set check | Merging two sets |
|---|---|---|
| `label[i]` stores the set name directly | One lookup | Relabel every member of one set |
| Forest of parent pointers | Walk up to the root | Change one pointer |
Direct labels make queries cheap and merges expensive. The forest makes merges cheap, because you change a single root's pointer, and queries cost the length of a path. That path length is the cost to control. The tree shape decides it, and the chapters on union by size and path compression keep it short.
Common mistakes
- Treating
parent[i]as the set name. It is only the parent. Two elements in one set can have different parents. - Forgetting that roots point at themselves. Code that loops until
parent[x] is Nonenever terminates on this layout. - Reading the array as a list of sets. Members of a set are scattered across indices.
Exercise
Given parent = [1, 1, 1, 4, 4, 4, 6], which indices are roots, and what are the sets?
Show answer
A root satisfies parent[i] == i. Index 1 (parent[1] == 1), index 4 (parent[4] == 4) and index 6 (parent[6] == 6) are roots. The sets are {0, 1, 2} under root 1, {3, 4, 5} under root 4, and {6} under root 6.