DrawCode Algorithm Visualizer • Union-Find • Easy

Union-Find (Basic)

Tags: Union-Find, DSU, Connectivity, Graph

1. One-Liner

Union-Find maintains a partition of items into disjoint sets and supports find (which set?) and union (merge two sets).


2. The Problem It Solves

You need to know if two nodes are in the same connected component after adding edges, or count components—classic in Kruskal-style processing and offline connectivity.


3. The Core Idea

Each element points to a parent; the set is a tree. Find walks to the root. Union hangs one root under another—simple but trees can skew.


4. How It Works (Step-by-Step)

StepAction
1Initialize parent[i]=i for all i
2find(x): walk until parent[x]==x
3union(a,b): if find(a)==find(b), skip; else parent[ra]=rb
4Optionally track component count on successful union

5. Dry Run Example

Elements 0..3, unions (0,1), (2,3), then (1,2): after first two steps roots {0,1} and {2,3}; third merges all—one component.


6. Key Properties

PropertyDetail
CorrectnessSame root ⇔ same set
Tree shapeCan degenerate to a chain
Opsfind + union per edge

7. Where It Is Used

Domain / SystemUse
NetworkingConnected subnets
KruskalEdge sort + DSU cycle check
GamesGuild / alliance merging

8. Interview Tips

State without rank/path compression: amortized is weaker. Know when union order matters; mention optimizations in follow-up.


9. Comparison with Other Algorithms

TechniqueNotes
List + DFSSlower dynamic connectivity
Union by rankShorter trees
Adjacency matrix BFSOverkill for connectivity only

10. Complexity

----
TimeNearly O(n) worst-case find per op without extras
SpaceO(n) parent array

Implementation Example (PYTHON)

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))

    def find(self, x):
        while self.parent[x] != x:
            x = self.parent[x]
        return x

    def union(self, a, b):
        ra, rb = self.find(a), self.find(b)
        if ra == rb:
            return False
        self.parent[ra] = rb
        return True

Interactive Visualizer Workspace

Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

Launch Interactive Visualizer