Abstract


Dynamic Relation

A Dynamic Relation is a Relation that has a different set of elements inside the relation depending on the given input. In Union-Find, the operation union(x, y) gives the dynamic property, combining 2 relations into one relation.

All relations in Union-Find are Equivalence Relation!

2 main operations

find(x)

  • Determines the representative element of the disjoint set that contains element
  • The representative acts as the unique identifier for the whole disjoint set

union(x, y)

  • Combines the two disjoint sets containing elements and respectively into a single disjoint set

What does it mean 'efficiently'?

find(x)

union(x, y)

  • Time complexity is
  • We use find(x) and find(y) to obtain the representative element of both disjoint sets in
  • Then attach the representative element of one disjoint set to another elements on the path in
  • Overall complexity is

Solve Grouping Problems

Union-Find can tell us if two elements are connected by a certain relationship in

Key Optimisations


Path Compression

  • When finding an element’s root, update all nodes along the path to point directly to the root
  • Flattens the tree structure for future operations
  • Reduces time complexity to nearly constant time
  • Mechanism: During find(x), directly link each node on the path to the root node
  • This flattens the trees, making subsequent find(x) operations much faster

Weighted Union

  • When merging sets, attach the smaller tree to the larger tree’s root
  • Maintains balance and minimizes tree height
  • Prevents degenerate tree structures

Rank vs Height

The rank in Union-Find is an upper bound on the height, not the exact height. This distinction is crucial:

  1. Rank vs Height:
    • Height is the actual length of the longest path from root to leaf
    • Rank is a property that’s only incremented when merging trees of equal rank
    • Due to path compression, the actual height can be much smaller than the rank
  2. Why use rank instead of height?:
    • Tracking exact height would be expensive to maintain after path compression
    • Rank is easier to maintain and still provides the balancing we need
    • The rank property is sufficient to guarantee the O(α(n)) time complexity
  3. Rank Updates During Union:
    • When attaching smaller rank tree to larger rank tree: rank stays the same
    • When attaching trees of equal rank: rank increases by 1
    • Actual height might increase temporarily, but path compression will flatten it in future operations

Time Complexity

  • With both optimizations: O(α(n)) where α is the inverse Ackermann function
  • Without optimizations: O(n) for worst-case operations
  • α(n) grows extremely slowly, making operations effectively constant time

Core Operations


Find Operation

  • Returns the root/representative of the set containing element x
  • Implements path compression to optimize future operations
  • Key insight: All nodes along the path to root are updated to point directly to root

Union Operation

  • Merges two sets by connecting their roots
  • Uses weighted union to maintain balanced trees
  • Key insight: Smaller tree is always attached to larger tree’s root

Python Implementation


Applications


  1. Graph Algorithms

  2. Dynamic Connectivity

    • Network connectivity problems
    • Image processing (pixel connectivity)
    • Social network analysis
  3. Percolation Problems

    • Modeling physical systems
    • Grid-based simulations
    • Phase transitions

Common Mistakes


Common Pitfalls

  1. Forgetting Path Compression
    • Without path compression, operations can degrade to O(n)
    • Always implement recursive path compression in find operation
  2. Ignoring Weighted Union
    • Can lead to unbalanced trees
    • Degrades performance to O(n) in worst case
  3. Incorrect Initialization
    • Each element must start as its own parent
    • Rank array must be initialized to zeros

References