Tags

Union-Find / DSU

Union-Find / DSU

1 lesson
6 problems
2 code snippets
1 question bank
3 community items

union-find

Data Structures

1 lesson

Union-Find (Disjoint Set Union)

Intermediate

55 min

2 prereqs

Given a million pairwise 'these two accounts belong to the same person' relations, decide whether two arbitrary accounts are now equivalent. Brute-force flood fills are `O(n)` per query; sorting is the wrong shape entirely. **Union-Find** answers each query in nearly `O(1)` amortized time after the relations are merged, with two primitives and a parent array. This lesson covers the disjoint-set abstraction, the `find` operation that walks parent pointers up to a representative, the `union` operation that links two representatives, and the two optimizations (path compression during `find` and union by rank during `union`) that together push the amortized cost to `O(alpha(n))`, where `alpha` is the inverse Ackermann function (effectively constant for any input you can fit in memory). You will trace the parent array as merges happen, and apply the structure to cycle detection in undirected graphs, connected components, equivalence classes, and Kruskal's minimum spanning tree. In **Arrays & Strings**, you saw that an array can encode a mapping from index to value in `O(1)`; a parent array is exactly that mapping, applied to representative pointers. **Graphs: Representation Basics** gave you the vertex-and-edge vocabulary that Union-Find uses to define connectivity, even though the structure itself stores no edges directly. With Union-Find in your toolkit, the curriculum continues with composite designs that combine multiple primitives, starting with the LRU cache pattern.

Not Started

0%

Union-Find / DSU
Path Compression
Union by Rank
Data Structures
Graphs
Connected Components
Cycle Detection
Intermediate
Premium

Practice Problems

6 problems

Accounts Merge

Not Started
Medium

Given a list of accounts where each account has a name and emails, merge accounts that share a common email address.

Graphs
Union-Find / DSU
DFS
Hash Map / Dictionary
Intermediate

155

5

Number of Connected Components

Not Started
Medium

Given n nodes and a list of undirected edges, find the number of connected components in the graph.

Graphs
DFS
Union-Find / DSU
Connected Components
Undirected Graphs
Intermediate

1k

24

Graph Valid Tree

Not Started
Medium

Given n nodes and a list of undirected edges, determine if these edges form a valid tree (connected and acyclic).

Graphs
DFS
Union-Find / DSU
Cycle Detection
Undirected Graphs
Intermediate

932

16

Min Cost to Connect All Points

Not Started
Medium

Given an array of points on a 2D plane, find the minimum cost to connect all points such that there is a path between every pair, using Manhattan distance as the edge cost.

Graphs
Minimum Spanning Tree
Prim's Algorithm
Kruskal's Algorithm
Union-Find / DSU
Intermediate

1.1k

37

Number of Provinces

Free
Not Started
Medium

Given an adjacency matrix representing connections between cities, find the total number of provinces (connected components).

Graphs
DFS
Union-Find / DSU
Connected Components
Undirected Graphs
Intermediate

1.1k

18

Redundant Connection

Not Started
Medium

Given a graph that started as a tree with one extra edge added, find and return the edge that can be removed to make it a tree again.

Graphs
Union-Find / DSU
Cycle Detection
Undirected Graphs
Intermediate

348

6

Code Snippets

2 snippets
Code Snippet
Premium

Disjoint Set (Union-Find) Template

Disjoint Set Union (DSU) tracks a partition of N elements into disjoint groups, supporting near-constant-time `find` (which group?) and `union` (merge two groups). It is the building block for Kruskal's MST, connected components on dynamic graphs, and the redundant-connection problem. This snippet covers the parent-array skeleton, the path-compression optimisation that flattens trees on every find, and the union-by-rank merge that keeps trees shallow.

JavaScript
data-structures
union-find
code-template
algorithms

710

21

Hard
Code Snippet
Premium

Union-Find in Python

Union-Find (Disjoint Set Union, DSU) tracks 'which group does each element belong to' and answers `find` / `union` in nearly O(1) amortized when you implement both path compression and union by rank. It is the right data structure for connected-components, Kruskal's MST, and 'is X equivalent to Y' under a stream of merges. This entry covers a basic class, the rank + path compression upgrades, and an MST application.

Python
union-find
union-by-rank
data-structures
algorithms

550

4

Hard