learngraphtheory.org

Interactive Graph Theory Learning

Guest User

Using app without sign in

Algorithm Selection

Cycle Detection

Detects cycles in directed and undirected graphs

Time: O(V + E)
Space: O(V)
Use Case: Deadlock detection, dependency analysis

Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Cycle Detection

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.

How it works

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.

Applications

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.

Read the full article: Graph Algorithms in Coding Interviews

Related algorithms: Depth-First Search, Topological Sort, Kruskal's MST Algorithm

Interactive Controls
Basic Actions
Double Click → Add Node
Drag → Move Nodes
Shift + Click → Connect Nodes
Right Click → Context Menu
Advanced
Ctrl + Click → Multi-Select
Delete Key → Remove Selected
Double Click Edge → Edit Weight
Ctrl + Drag → Pan View

Zoom Controls

100%
Nodes: 4
Edges: 4