Union-Find (DSU)
Keep track of which items belong to the same group while groups keep merging, answering “are these two connected?” in almost constant time.
After this lesson you will be able to
- Explain the problem Union-Find solves: merging groups and asking whether two items share one.
- Represent each group as a tree in a
parentarray and find its root. - Apply the two optimisations, union by size and path compression, and say why each helps.
- Use it to count components, detect a redundant edge, and build toward Kruskal's minimum spanning tree.
- Know when a graph search is simpler and when Union-Find is the better tool.
Before you start. Arrays and loops are enough to start. Seeing DFS first helps, since it solves the same “connected?” question for a fixed graph.