#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
🔗 FREE · DISJOINT SETS

Union-Find Visualizer: Disjoint Sets, Rank and Path Compression

Type operations like union 0 1 and find 3, then replay how the forest changes.

  • 🌳 Forest drawing
  • 📉 Path compression
  • ⚖️ Union by rank

How to use the Union-Find Visualizer

  1. 1. Set it upChoose how many elements (2–16) and list operations.
  2. 2. RunPress Run operations.
  3. 3. ReplayStep through each union, find and connected check; the parent array is shown under the forest.

Worked example

With 8 elements, the sample unions 0–1 and 2–3, then union 1 3 joins the two pairs under root 0 (equal ranks, so 0 becomes rank 2). find 3 follows 3 → 2 → 0 and path compression points 3 straight at 0. After union 4 5, connected 0 2 is yes and connected 0 4 is no. union 5 6 adds 6 under 4, then union 6 0 attaches root 4 under root 0 because 0 has the higher rank. The final find 6 compresses 6 → 4 → 0. The result is 2 sets: {0, 1, 2, 3, 4, 5, 6} and {7}.

Why the two tricks matter

Union by rank always hangs the shorter tree under the taller one, so trees stay shallow. Path compression makes every node on a find path point directly at the root, so later finds are almost instant. Together they make each operation run in near-constant amortised time, O(α(n)), where α is the inverse Ackermann function, which is below 5 for any practical n.

Where it's used

  • Kruskal's minimum spanning tree algorithm.
  • Counting connected components and detecting cycles in undirected graphs (Graph Valid Tree, Redundant Connection).
  • Merging accounts, friend circles and image segmentation.
  • Online problems where connections arrive one at a time and you must answer whether two items are linked yet after each one.

Frequently asked questions

What does r2 under a node mean?

It's the root's rank, an upper bound on its tree's height.

Why number elements from 0?

That matches the parent array used in code. Element i's parent is parent[i].

Can I add elements later?

Set the number of elements up front, up to 16.

Is the union-find visualizer free?

Yes. You can use the union-find visualizer without signing up. The first time you run it, we ask once for your name and WhatsApp number; after that the union-find visualizer and every other Uncodemy tool open straight away.

More free tools

See all free tools