← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Disjoint Set / Union-Find

data_structures Advanced

Also Known As

Union-Find disjoint set union DSU

TL;DR

A data structure tracking which elements belong to the same group — supporting near-O(1) union and find operations. Used for network connectivity, Kruskal's MST, and cycle detection.

Explanation

Disjoint Set (Union-Find) maintains a forest of trees where each tree represents a set. find(x) returns the root of x's tree (the set representative). union(x, y) merges the two sets. Optimisations: path compression (flatten tree on find), union by rank (always attach smaller tree to larger). Combined: near-O(1) amortised — technically O(α(n)) where α is the inverse Ackermann function, practically constant. Applications: Kruskal's Minimum Spanning Tree, network connectivity (are A and B in the same network?), image segmentation, and cycle detection in undirected graphs.

Common Misconception

Disjoint Set requires O(n) per operation — without optimisations yes; with path compression and union by rank, amortised O(α(n)) which is effectively O(1) for all practical inputs.

Why It Matters

Kruskal's MST algorithm using Union-Find runs in O(E log E) instead of O(E * V) — the difference between seconds and hours for large graphs.

Common Mistakes

  • Not implementing path compression — find() is O(n) without it.
  • Not using union by rank — trees become linked lists without it.
  • Using for directed graphs — Union-Find tracks connected components in undirected graphs only.
  • Forgetting that find() modifies the structure (path compression) — not purely read-only.

Code Examples

✗ Vulnerable
// No optimisations — O(n) find:
class NaiveUnionFind {
    private array $parent;
    public function find(int $x): int {
        while ($this->parent[$x] !== $x) {
            $x = $this->parent[$x]; // Walk up — O(n) in worst case
        }
        return $x;
    }
}
✓ Fixed
// Path compression + union by rank — O(alpha n) amortised:
class UnionFind {
    private array $parent;
    private array $rank;

    public function __construct(int $n) {
        $this->parent = range(0, $n - 1);
        $this->rank   = array_fill(0, $n, 0);
    }

    public function find(int $x): int {
        if ($this->parent[$x] !== $x)
            $this->parent[$x] = $this->find($this->parent[$x]); // Path compression
        return $this->parent[$x];
    }

    public function union(int $x, int $y): void {
        $rx = $this->find($x); $ry = $this->find($y);
        if ($rx === $ry) return;
        if ($this->rank[$rx] < $this->rank[$ry]) [$rx, $ry] = [$ry, $rx];
        $this->parent[$ry] = $rx;
        if ($this->rank[$rx] === $this->rank[$ry]) $this->rank[$rx]++;
    }
}

Added 16 Mar 2026
Edited 22 Mar 2026
Views 23
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 1 ping T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 8 Perplexity 6 Ahrefs 2 Google 1 Unknown AI 1
crawler 18
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Medium
⚡ Quick Fix
Use a disjoint set (union-find) when you need to efficiently determine if two elements are in the same connected component — used in Kruskal's MST and cycle detection
📦 Applies To
any web cli
🔗 Prerequisites
🔍 Detection Hints
Cycle detection in graph using DFS where union-find would be more efficient; connected component check with repeated BFS traversals
Auto-detectable: ✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: Medium ✗ Manual fix Fix: Medium Context: Class Tests: Update

✓ schema.org compliant