Union-Find

T: O(α(n)) amortized
Feedback

Union-Find

Union-Find initialized. Each element is its own set.
01234567
union(0, 1)
union(2, 3)
union(4, 5)
union(1, 3)
union(5, 7)
find(3)
union(0, 7)
find(5)
RootPathOperand
SpeedNormal (500ms)
Parameters

{ n: number, operations: [{type:"union"|"find", args: number[]}] }

Variables
parent[0, 1, 2, 3, 4, 5, 6, 7]
opIndex0
opunion(0,1)
1function find(x):
2 if parent[x] != x:
3 parent[x] = find(parent[x]) // path compression
4 return parent[x]
5
6function union(a, b):
7 rootA = find(a)
8 rootB = find(b)
9 if rootA == rootB: return
10 if rank[rootA] < rank[rootB]:
11 parent[rootA] = rootB
12 else if rank[rootA] > rank[rootB]:
13 parent[rootB] = rootA
14 else:
15 parent[rootB] = rootA
16 rank[rootA]++
Output
Union-Find initialized. Each element is its own set.