Component connectivity visual with path compression and union by rank.
Easy Explanation
Union-Find quickly tracks which elements belong to the same group and merges groups efficiently.
Nodes
0
Components
0
Successful Unions
0
Result
Processing
Press Play or Step to start execution.
Parents
n/a
Ranks
n/a
Active Query
n/a
function find(x):
if parent[x] != x:
parent[x] <- find(parent[x])
return parent[x]
function union(a, b):
rootA <- find(a)
rootB <- find(b)
if rootA == rootB:
return false
if rank[rootA] < rank[rootB]:
swap rootA, rootB
parent[rootB] <- rootA
if rank[rootA] == rank[rootB]:
rank[rootA] <- rank[rootA] + 1
return true