Skip to main content

Menu

LEVEL 0
0/5 XP
HomeAboutTopicsPricingMy VaultStats

Categories

πŸ€– Artificial Intelligence
☁️ Cloud and Infrastructure
πŸ’Ύ Data and Databases
πŸ’Ό Professional Skills
🎯 Programming and Development
πŸ”’ Security and Networking
πŸ“š Specialized Topics
HomeAboutTopicsPricingMy VaultStats
LEVEL 0
0/5 XP
GitHub
Β© 2026 CheatGridβ„’. All rights reserved.
Privacy PolicyTerms of UseAboutContact

Union-Find and Disjoint Set Union Cheat Sheet

Union-Find and Disjoint Set Union Cheat Sheet

Back to Mathematics and Algorithms
Updated 2026-05-21

Union-Find (also called Disjoint Set Union, DSU) is a data structure that tracks a collection of disjoint sets and supports two primary operations: find (determine which set an element belongs to) and union (merge two sets). It lives at the intersection of graph theory and algorithm design, serving as the backbone of Kruskal's MST algorithm, dynamic connectivity, and dozens of competitive programming problems. The key mental model is a forest of trees where each tree is a set and each tree's root is the set's representative β€” and the art of DSU lies entirely in how you keep those trees flat.

What This Cheat Sheet Covers

This topic spans 13 focused tables and 80 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.

Table 1: Core Concepts and TerminologyTable 2: Find Operation VariantsTable 3: Union Operation OptimizationsTable 4: Time Complexity SummaryTable 5: Classic DSU Implementation (Python Template)Table 6: Graph ApplicationsTable 7: String and Array ProblemsTable 8: Advanced Graph ApplicationsTable 9: DSU with Additional Stored DataTable 10: Small-to-Large MergingTable 11: DSU with Rollback and Persistent DSUTable 12: Common LeetCode DSU Problems (Quick Reference)Table 13: Common Pitfalls and Best Practices

Table 1: Core Concepts and Terminology

The three-operation interface of DSU (make_set, find_set, union_sets) is deliberately minimal, yet it captures everything needed for equivalence-class reasoning. Understanding what a "representative," "root," and "parent array" mean is essential before studying optimizations.

TechniqueExampleDescription
parent array
parent = [0,1,2,3,4] (init)
β€’ Array where parent[i] holds i's ancestor
β€’ parent[i] == i marks a root (representative of its set).
make_set
parent[v] = v
β€’ Initializes a single-element set
β€’ each element starts as its own root in O(1).
find_set (naive)
while v != parent[v]: v = parent[v]
return v
β€’ Climbs parent links until root
β€’ worst-case O(n) on a chain
union_sets (naive)
parent[find(b)] = find(a)
β€’ Merges two sets by attaching one root to the other
β€’ no balancing, can degenerate
representative (leader/root)
find(x) == find(y) β†’ same set
β€’ The unique element satisfying parent[root] == root
β€’ used to check set membership

More in Mathematics and Algorithms

  • Trigonometry Cheat Sheet
  • Abstract Algebra Essentials Cheat Sheet
  • Coding Theory and Error-Correcting Codes Cheat Sheet
  • Dynamic Programming Cheat Sheet
  • Linear Programming and the Simplex Method Cheat Sheet
  • Numerical Stability and Floating Point Arithmetic Cheat Sheet
View all 57 topics in Mathematics and Algorithms