Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Graph cycle finder
Detects cycles in directed and undirected graphs
Select an algorithm and generate steps to begin visualization
Cycle detection determines whether a graph contains a cycle, a path that returns to its starting vertex. The techniques differ between directed graphs, where cycles mean circular dependencies, and undirected graphs, where any extra edge beyond a tree creates a cycle.
In directed graphs, DFS classifies edges: a back edge to a vertex still on the recursion stack proves a cycle, tracked with three vertex states (unvisited, in progress, done). In undirected graphs, DFS finds a cycle when it meets a visited vertex other than its parent, and Union-Find detects one when an edge joins two vertices already in the same set. All approaches run in O(V + E), with Union-Find nearly constant per edge.
Cycle detection prevents deadlocks in operating systems, catches circular imports in build tools and package managers, validates spreadsheets and workflow definitions, and is the gatekeeper for topological sorting. Floyd's tortoise-and-hare variant for linked lists is one of the most asked interview questions of all.
Directed and undirected graphs need genuinely different tests. The directed version tracks the recursion stack; the undirected version tracks the parent.
// Directed: three-colour DFS
WHITE = unvisited, GRAY = on recursion stack, BLACK = finished
hasCycle(u):
color[u] = GRAY
for each neighbor v of u:
if color[v] == GRAY: return true // back edge
if color[v] == WHITE and hasCycle(v): return true
color[u] = BLACK
return false
// Undirected: DFS carrying the parent
hasCycle(u, parent):
visited.add(u)
for each neighbor v of u:
if v == parent: continue
if v in visited: return true
if hasCycle(v, u): return true
return falseThe distinction matters more than it looks. In a directed graph, reaching a BLACK node is a cross edge and is perfectly cycle free, so the naive visited check reports cycles that do not exist. In an undirected graph, skipping the parent is what stops every single edge from being read as a two-node cycle.
Run the directed three-colour test on a graph that contains one cycle and one misleading cross edge.
Example graph: Directed edges A to B, A to C, B to D, C to D and D to B.
The graph does contain a cycle, B to D to B, found through the GRAY test. The A to C to D path is not a cycle, and only the colour distinction separates the two cases.
Time: O(V + E) · Space: O(V)
Both variants are a single DFS with constant extra work per edge, so the cost is the traversal cost. The colour array or visited set is O(V), plus O(V) recursion stack. The Union-Find alternative for undirected graphs runs in O(E alpha(V)), effectively linear, and is preferable when edges arrive one at a time and you want to reject a cycle-closing edge as it appears rather than rescanning the whole graph.
Pick the test that matches both the direction of your edges and whether the graph is static or streaming.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Union-Find | Undirected, and edges arrive incrementally. Rejects the cycle-closing edge in near constant time as it is added. | O(E alpha(V)) |
| Kahn topological sort | Directed, and you also want the ordering when there is no cycle. Leftover nodes after the queue empties are exactly the cyclic part. | O(V + E) |
| Tarjan SCC | Directed, and you want to know which nodes are in cycles rather than just whether one exists. Any component of size above one is a cycle. | O(V + E) |
| Floyd cycle finding | A functional graph or linked list where each node has exactly one successor. Uses O(1) memory. | O(n) |
Read the full article: Graph Algorithms in Coding Interviews
Related algorithms: Depth-First Search, Topological Sort, Kruskal's MST Algorithm