Detect Cycle with Disjoint-set Union Find

use Union Find data structure to find a cycle

Note: This method assumes that the graph doesnโ€™t contain any self-loops.

\

Undirected graph

Directed Graph

Not possible

Use DFS with 3 colors (not visited, visiting in current cycle, visited before)

Last updated