---
id: dsu
title: "Disjoint Set Union"
author: Benjamin Qi, Michael Cao
prerequisites:
- Silver - Depth First Search
description: The Disjoint Set Union (DSU) data structure allows you to add edges to an initially empty graph and test whether two vertices of the graph are connected.
frequency: 3
---
import { Problem } from "../models";
export const metadata = {
problems: {
sample: [
new Problem("YS", "Union Find", "unionfind", "Intro|Easy", false, []),
],
general: [
new Problem("Gold", "Closing the Farm", "646", "Easy", false, [], "Simulate process in reverse and maintain the # of connected components. Similar to [CSES Network Breakdown](https://cses.fi/problemset/task/1677)"),
new Problem("Gold", "Mootube", "789", "Normal", false, [], "Answer queries in decreasing order of $k$. Maintain size of each connected component. Same as [CSES Road Construction](https://cses.fi/problemset/task/1676)"),
],
}
};
## Resources
both optimizations, diagrams
small to large, diagrams
path compression, diagrams
both optimizations, no diagrams
diagrams
Note: You may prefer to use this in place of DFS for computing connected components.
- [$\log^* n$](https://en.wikipedia.org/wiki/Proof_of_O(log*n)\_time_complexity\_of_union%E2%80%93find)
- [$\alpha (m,n)$](https://dl.acm.org/doi/pdf/10.1145/321879.321884)
## Problems