Track connected components efficiently. Used in network connectivity and Kruskal's algorithm.
1 algorithm3 problems
A disjoint set union, also called union-find, keeps items split into groups that can merge but never split. It answers one question quickly: are these two items in the same group. That question sits behind Kruskal's minimum spanning tree, network connectivity checks, merging accounts that share an email address, and interview problems such as Number of Provinces and Redundant Connection.
Each group is a small tree stored in a parent array, and its root names the group. Two items are together exactly when they climb to the same root, and joining two groups means hanging one root under the other. These pages link by rank, so the root of lower rank goes under the other and no climb is longer than log N hops. Path compression, which points every node passed straight at the root, brings the average cost per operation close to constant; on these pages only the Find button compresses.
Components and cycle detection run the same loop over the edges and differ only in what they watch. Components counts the edges that join two groups; cycle detection reports the edges whose two ends were already in one group. Pick union-find over BFS or DFS when edges arrive over time or when you will ask many connectivity questions about one set of edges. Pick a graph search from the graph section when you need the actual path, when edges can be deleted, or when the graph is directed, because union-find has no notion of direction and cannot undo a merge.