TEC-Bridge Logo

Disjoint Set (Union-Find) Data Structure Visualizer

STEM Interactive Visual Learning Program at TEC-Bridge AI

Disjoint Set Setup

Parent Array

Union-Find Operations

Statistics

Disjoint Set Visualization

Operation Steps

How to Use

  1. Initialize: Set size (e.g., 8) and click "Initialize"
  2. Sample Data: Click Sample Data to see example unions.
  3. Union: Enter two elements (e.g., X=1, Y=3) and click "Union"
  4. Find: Enter element to find its root representative
  5. Connected: Check if two elements are in the same set

Disjoint Set Concept

Disjoint Set (Union-Find) maintains a collection of disjoint (non-overlapping) sets.

Key Operations:

  • Find: Determine which set an element belongs to
  • Union: Merge two sets into one
  • Connected: Check if two elements are in the same set

Optimizations:

  • Path Compression: Make trees flatter during Find
  • Union by Rank: Attach smaller tree under larger tree
  • Nearly O(1): Amortized time complexity with optimizations

How it Works: Each element initially forms its own set. Union operations merge sets by making one root point to another. Find operations trace parent pointers to reach the root.

Use Cases

  • Kruskal's minimum spanning tree algorithm
  • Network connectivity problems
  • Image processing (connected components)
  • Social network analysis (friend groups)
  • Percolation theory and physics simulations

Time Complexity

OperationNaiveOptimized
FindO(n)O(α(n))
UnionO(n)O(α(n))
ConnectedO(n)O(α(n))

α(n) = inverse Ackermann function (practically constant)

Space Complexity

ComponentSpace
Parent ArrayO(n)
Rank ArrayO(n)

Advantages

  • Nearly constant time operations
  • Simple implementation
  • Memory efficient
  • Excellent for connectivity queries

Optimizations

Path Compression: During Find, make all nodes point directly to root

Union by Rank: Always attach tree with smaller rank to tree with larger rank

Combined: Both optimizations together give α(n) amortized time

Code Implementation

© 2025 TEC-Bridge AI. All rights reserved. | stemists.com@gmail.com | https://stemists.com