Fiveable

🧩Intro to Algorithms Unit 13 Review

QR code for Intro to Algorithms practice questions

13.2 Disjoint set data structure and union-find algorithms

13.2 Disjoint set data structure and union-find algorithms

Written by the Fiveable Content Team • Last updated August 2025
Written by the Fiveable Content Team • Last updated August 2025
🧩Intro to Algorithms
Unit & Topic Study Guides

Disjoint set data structures and union-find algorithms are powerful tools for managing non-overlapping sets efficiently. They support operations like MakeSet, Find, and Union, enabling quick set membership checks and merging of sets.

These algorithms showcase the power of amortized analysis in revealing better average-case performance. With optimizations like path compression and union by rank, they achieve near-constant time complexity, making them invaluable for solving connectivity problems in various applications.

Disjoint Set Data Structure

Core Concepts and Operations

  • Disjoint set data structure maintains collection of non-overlapping sets identified by representative element
  • Supports three primary operations
    • MakeSet creates new set with single element
    • Find determines representative (root) of a set
    • Union merges two sets
  • Commonly represented using trees
    • Each node points to its parent
    • Root of tree serves as set's representative
  • Implemented using various data structures (arrays, linked lists, forests)
    • Each implementation offers different trade-offs in time and space complexity

Applications and Use Cases

  • Solves dynamic connectivity problems and manages equivalence relations
  • Used in several graph algorithms
    • Kruskal's algorithm for minimum spanning trees
    • Finding connected components in graphs
    • Cycle detection in undirected graphs
  • Practical applications
    • Network connectivity (computer networks, social networks)
    • Image processing (connected component labeling)
    • Percolation theory (materials science, epidemiology)

Union-Find Algorithms

Basic Implementation

  • MakeSet operation
    • Creates new set containing single element
    • Typically implemented by setting element as its own parent
    • Example: MakeSet(x) sets parent[x] = x
  • Find operation
    • Determines representative (root) of a set
    • Follows parent pointers until reaching self-pointing element
    • Example: Find(x) returns root of tree containing x
  • Union operation
    • Merges two sets
    • Makes root of one set point to root of the other set
    • Example: Union(x, y) connects trees containing x and y
Core Concepts and Operations, Disjoint-set data structure - Wikipedia

Implementation Considerations

  • Careful selection of underlying data structure impacts performance
  • Basic implementation limitations
    • Find operation worst-case time complexity O(n)
    • Union operation worst-case time complexity O(n)
      • Involves two Find operations and single pointer update
  • Potential optimizations
    • Path compression during Find
    • Union by rank or size
    • Balancing trees to reduce height

Time Complexity of Union-Find

Amortized Analysis Concepts

  • Evaluates average performance of operation sequence rather than individual worst-case scenarios
  • Reveals better average-case performance than naive worst-case analysis
  • Amortized time complexity for m operations on n elements O(m α(n))
    • α(n) inverse Ackermann function grows extremely slowly
    • Effectively constant for all practical values of n
  • Analysis techniques
    • Accounting method assigns credits to operations
    • Potential method uses potential function to measure data structure state

Complexity Analysis

  • Naive implementation
    • Worst-case time complexity O(n) per operation
    • Sequence of m operations worst-case O(mn)
  • Amortized analysis reveals tighter bounds
    • With optimizations, amortized time becomes O(m α(n))
    • Significant improvement over naive analysis
  • Space complexity remains O(n) regardless of optimizations
    • Constant extra space per element for rank information
Core Concepts and Operations, Disjoint-set data structure - Wikipedia

Optimizing Union-Find Algorithms

Path Compression Technique

  • Applied during Find operation
  • Makes all nodes along path to root point directly to root
  • Implementation
    • Recursively traverse path to root
    • Set each node's parent to root during backtracking
  • Benefits
    • Flattens tree structure
    • Reduces future traversal times
  • Example: Finding element x updates all ancestors to point to root

Union by Rank Heuristic

  • Applied during Union operation
  • Attaches shorter tree to root of taller tree
  • Implementation
    • Maintain rank (approximate height) for each tree
    • Compare ranks when performing union
    • Increment rank of root only when joining trees of equal rank
  • Benefits
    • Keeps overall tree height logarithmic
    • Balances tree structure for faster operations
  • Example: Unioning trees with ranks 2 and 3 attaches rank 2 tree to rank 3 root

Combined Optimizations

  • Implementing both path compression and union by rank together
  • Yields near-constant time complexity for practical purposes
  • Amortized time complexity becomes O(m α(n)) for m operations
    • α(n) inverse Ackermann function grows extremely slowly
  • Significantly improves performance for large-scale applications
    • Graph theory problems (minimum spanning trees, connectivity)
    • Set manipulation tasks (equivalence classes, partitioning)
Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly → and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot

2,589 studying →