ZiliangZiliang

Site navigation

  • Mortgage comparison
  • Japan tax calculator
  • Programming
  • Algorithms
  • Machine learning
  • Misc
Engineering
Contact
中文

Site navigation

  • Mortgage comparison
  • Japan tax calculator
  • Programming
  • Algorithms
  • Machine learning
  • Misc
Engineering
Contact

Article directory

  • Programming Languages

    • Overview
    • Basics
    • Collections
    • Flow Control Statements
    • Function
    • Libraries and Modules
    • IO, File, and OS
    • Errors and Exceptions
    • Object-Oriented Design
    • Namespaces and Scopes
  • Data Structures and Algorithms

    • Overview
    • Math Formula
    • Math Code
    • Misc
    • String
    • Tree Traversal
    • Balanced Binary Trees
    • Heap
    • Segment Tree
    • Dynamic Programming
    • Tree Misc
    • Java
    • Disjoint Sets
    • Graph Traversal
    • Minimum Spanning Tree
    • Single-Source Shortest Paths
    • Strongly Connected Components
    • Cut Vertices and Bridges
    • Cache
    • Binary Search
    • Quicksort
    • Knapsack Problem
    • Vertex Cover Problem
    • Set Cover Problem
    • Principle Component Analysis
    • K-Center Problem

Disjoint Sets

Union-Find Data Structure

Two optimization strategies:

  1. Path compression

  2. Union by rank

    Initially a set has one element and a rank of zero. If two sets are unioned and have the same rank, the resulting set's rank is one larger

Amortized Complexity

AlgorithmAverageWorst case
SpaceO(n)O(n)
FindO(α(n))O(α(n))
UnionO(α(n))O(α(n))

α(⋅) is inverse Ackermann function, for the generally possible value n, α(n) is less than 5

python
class UnionFind(object):
    def __init__(self):
        self.parent = {}
        self.rank = {}

    def make_set(self, x):
        self.parent[x] = x
        self.rank[x] = 0

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

    def union(self, x, y):
        x_root = self.find(x)
        y_root = self.find(y)
        if x_root == y_root:
            return

        if self.rank[x_root] == self.rank[y_root]:
            self.parent[y_root] = x_root
            self.rank[x_root] += 1
        elif self.rank[x_root] > self.rank[y_root]:
            self.parent[y_root] = x_root
        else:
            self.parent[x_root] = y_root

java
class UnionFind {
    Map<Integer, Integer> parent = new HashMap<>();
    Map<Integer, Integer> rank = new HashMap<>();

    public void makeSet(int x) {
        parent.put(x, x);
        rank.put(x, 0);
    }

    public int find(int x) {
        int p = parent.get(x);
        if (p != x) {
            parent.put(x, find(p));
        }
        return parent.get(x);
    }

    public void union(int x, int y) {
        int xRoot = find(x);
        int yRoot = find(y);
        if (xRoot == yRoot) {
            return;
        }

        int xRank = rank.get(xRoot);
        int yRank = rank.get(yRoot);

        if (xRank == yRank) {
            parent.put(yRoot, xRoot);
            rank.put(xRoot, xRank + 1);
        } else if (xRank > yRank) {
            parent.put(yRoot, xRoot);
        } else {
            parent.put(xRoot, yRoot);
        }
    }
}

Tests

python

java

Edit this page on GitHub
Last Updated: 9/10/26, 7:37 AM
Contributors: Lucien
Prev
Java
Next
Graph Traversal