Graph Theory37 sections · 1633 units
Open in Course

Kruskal with Union-Find

(Detecting cycles efficiently)

Union-Find (Disjoint Set Union) tracks which nodes are connected. Each node starts in its own component. When you add an edge, you merge two components. Before adding edge (u,v)(u, v), check if uu and vv are already in the same component using find(u) == find(v).

If yes, skip the edge (it would create a cycle). If no, add the edge and merge components with union(u, v). This lets Kruskal detect cycles in nearly constant time per edge.