Please wait
Loading technology simulations
The requested content is still loading.
Finished experimenting?
Save one completion to this browser's signed guest ledger, or directly to your account when signed in.
Please wait
Loading technology simulations
The requested content is still loading.
Save one completion to this browser's signed guest ledger, or directly to your account when signed in.
| naive union | up to n = 12 |
| + union by rank | ≤ log₂ n = 4 |
| + path compression | ≈ α(n) ≤ 4 |
1root: parent[x] == x2find(x): while parent[x] ≠ x: x = parent[x]3 (compress: parent[x] = root)4union(a, b):5 ra, rb = find(a), find(b)6 if ra == rb: return // already joined7 attach smaller-rank root under larger
This union-find (disjoint set) simulator shows a forest where each tree is one set: every node points to a parent, and find() walks up to the root that names the set. Union joins two roots; toggle union-by-rank to always hang the shorter tree under the taller, and path compression to re-point every node on a find path straight at the root — flattening the tree before your eyes. A find-cost card contrasts the three regimes: naive union can degrade to O(n), union-by-rank caps height at log n, and both together give amortised O(α(n)) where the inverse Ackermann function is at most 4 for any real input. The signature interaction is hands-on: click one node then another to union them, and switch on Predict mode to guess whether two elements are connected before find() reveals the answer.
union-find · disjoint set union · union by rank · path compression · inverse Ackermann · connected components · amortised analysis
Browse all Technology simulations →
Free to use in your browser — no signup required. Found a bug or have an idea to make it better? Tell us.