Disjoint Set

Intro

Disjoint Set Union (Union-Find) is a classic structure for tracking connectivity as you merge groups over time. It shows up in graph problems, clustering, and any scenario where you repeatedly union sets and query whether two items are connected. Example: Kruskal's algorithm uses DSU to build a minimum spanning tree while avoiding cycles.

Diagram

flowchart TD
  A[Need connectivity queries] --> B[Initialize parent and rank arrays]
  B --> C{Operation type}
  C -->|Find| D[Follow parent links to root]
  D --> E[Compress path to root]
  E --> C
  C -->|Union| F[Find roots of both nodes]
  F --> G{Roots equal}
  G -->|Yes| C
  G -->|No| H[Attach smaller rank tree under larger rank tree]
  H --> C

Questions


Whats next