Skip to content
CoreSource checked

Cycle Detection

Determines whether a graph contains a cycle.

Quick Facts

Compact metadata for implementation and discovery.

Category
Graph Traversal
Difficulty
Beginner
Family
Graph Analysis
Tier
Core
Status
Verified
Review
Source Checked
Sources
2
Formulas
0
Relationships
3
Code example
Ready
Visualization
Ready

Overview

Cycle Detection uses traversal state to find edges that prove a loop exists in a directed or undirected graph.

Core Concepts

graph representationcycle statetraversal ordergraph analysisvisited setfrontier expansion

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(V + E)

Average

O(V + E)

Worst

O(V + E)

Space

O(V)

When to Use

  • Use Cycle Detection when you need to track traversal state.
  • Use it for dependency validation with inputs like graph.
  • Use it when relationships are naturally modeled as nodes connected by edges.
  • Use it when the Graph Analysis tradeoff is acceptable for the problem size.

When Not to Use

  • Avoid it when the graph is so large that storing every visited node is not practical.
  • Avoid it when the required output is not cycle found result.

What It Does

  • Tracks traversal state
  • Reports when an edge closes a loop

Use Cases

  • Dependency validation
  • Deadlock detection
  • Graph sanity checks

Inputs

  • Graph

Outputs

  • Cycle found result
  • Optional cycle path

Pseudocode

  1. 1.Visit nodes with DFS
  2. 2.Track nodes currently on the recursion stack
  3. 3.If an edge points to an active node, report a cycle
  4. 4.Mark nodes complete as DFS returns
  5. 5.Continue until all nodes are checked

Canonical Example

Input
graph = {A: [B, C], B: [D], C: [], D: []}, start = A
Output
visited = [A, B, C, D]
Explanation
Cycle Detection: The traversal starts at A and visits reachable nodes by following edges.

Implementation Notes

  • Keep the input contract explicit: Graph.
  • Use this step as the implementation spine: Visit nodes with DFS.
  • Then preserve the next state change: Track nodes currently on the recursion stack.
  • Keep node ids, edge direction, and visited state explicit in the implementation.
  • Represent neighbors consistently and keep traversal state separate from the graph data.

Common Pitfalls

  • Directed and undirected graphs use slightly different cycle checks.
  • Not defining directed versus undirected edges clearly can change the result.
  • Forgetting to mark visited nodes before revisiting cycles or shared neighbors.
  • Returning cycle found result without handling empty, invalid, or boundary inputs.

Interactive Mechanism Walkthrough

Graph Frontier & Edge Relaxation

Show a back edge closing a loop in the graph.

Cycle Detection mechanism walkthrough step 1 of 5suvwxt
Step 1 of 5: Visit nodes with DFS
cycle-highlight

Notes

Cycle Detection operates in O(V + E) time and O(V) space within the Graph Analysis family. Often compared with Depth-First Search and Topological Sort.

Sources (2)

What each reference supports for this entry.

Introduction to Algorithms (CLRS)

Book

Textbook support for Cycle Detection's standard algorithm design, invariants, and complexity analysis.

Open source

The Algorithm Design Manual

Book

Algorithm catalog support for Cycle Detection's problem context, known alternatives, and practical selection tradeoffs.

Open source

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

DFS visitation state is a standard way to detect cycles.

Cycle detection helps determine whether a directed graph is a DAG.

Tags

GraphCycle DetectionTraversal