Ch. 18 · Data Structures & Algorithms

Union-Find for Connectivity and Components

Union-Find for Connectivity and Components. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readadvancedupdated Oct 3, 2026

Union-find tracks components through parent links. Path compression and union by rank or size improve repeated operations.

Before you start

You should know arrays, loops and basic complexity notation. Start with a small input you can trace by hand. State what each variable means and which invariant is maintained, then use that invariant to explain correctness before discussing performance or writing a more compact solution.

The practical goal is to reason through this situation: Union connected endpoints and test whether two nodes share a representative. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Initialize components

Each node starts with its own representative.

Step 2: Merge representatives

Use union by size or rank and compress paths during find.

Step 3: Match the supported question

Connectivity is available; actual paths and direction are not stored.

Worked scenario

Union connected endpoints and test whether two nodes share a representative.

Union A with B, then B with C. A and C share a representative even though union-find does not retain the path A→B→C. Adding an edge within an existing component can detect a cycle under an appropriate undirected-graph process, but directed reachability requires different reasoning.

Common mistake

Union-find does not retain the actual path between nodes.

Verify the behavior

Test repeated unions, isolated nodes and component counts; do not infer paths from parent pointers.

Interview exercise

Choose it for a graph problem.

Answer and reasoning

Use it for connectivity or component merging; choose traversal or richer structures when paths, direction or distances are required.

Continue learning

Compare the scenario with the Data Structures and Algorithms interview questions and test your understanding with the Data Structures and Algorithms MCQs. For terminology and implementation details, consult the reference material.

More in Data Structures & Algorithms

read ✓Data Structures & Algorithms · mid

Bit Manipulation Patterns for Interviews

Set, test and clear bits, exploit XOR properties, and count set bits with the Brian Kernighan trick.

~2 min readread →
read ✓Data Structures & Algorithms · hard

Backtracking and Safe Pruning Rules

Backtracking and Safe Pruning Rules. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readread →
read ✓Data Structures & Algorithms · easy

Big-O Complexity and Input Growth

Big-O Complexity and Input Growth. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readread →
esc