A topic in the Open Knowledge Graph — a free, open map of 15,495 topics and the order to learn them in.

Cycle Detection in Directed Graphs

College Depth 86 in the knowledge graph ☐ I know this ☆ Set as goal
2topics build on this
418prerequisites beneath it
See this on the map →
Depth-First Search (DFS)Directed Graphs and Digraphs→→Directed Acyclic Graphs (DAGs)
directed-graphs cycles algorithms

Core Idea

Cycle detection determines whether a directed graph contains any cycles. Algorithms like DFS-based backtracking identify cycles by marking vertices as visited, visiting, and done. Detecting cycles is essential for deadlock detection, dependency validation, and proving acyclicity.

Explainer

A cycle in a directed graph is a path that starts and ends at the same vertex, following edge directions the whole way. If your prerequisite on directed graphs introduced the idea that edges have a "one-way" quality, cycles are the case where a sequence of one-way steps somehow loops back to the origin — like a set of task dependencies where A requires B, B requires C, and C requires A. Such a cycle makes the dependency impossible to satisfy.

The standard algorithm for detecting cycles is a depth-first search (DFS) augmented with three vertex states, often called white, gray, and black (or unvisited, visiting, and done). When DFS begins exploring from a vertex, that vertex turns gray — it is "on the current path." When all its descendants have been fully explored, it turns black. The key insight: if DFS ever encounters a gray vertex through a forward edge, it has found a back edge, meaning the current path leads back to a vertex already on the current recursion stack. That is a cycle.

Why does the three-color distinction matter? A black vertex has already been fully processed — following an edge to it does not create a cycle, because that vertex's subtree contained no cycle and the black vertex itself has already been resolved. Only gray vertices signal a cycle, because gray means "ancestor in the current DFS path." Two colors (visited/unvisited) would confuse finished subtrees with active ancestors. The three-color scheme solves this precisely.

Real applications anchor this idea. Dependency resolution — package managers, build systems, task schedulers — must verify that their dependency graphs are acyclic before attempting to order tasks. Deadlock detection in operating systems reduces to finding cycles in a resource-allocation graph. Topological sort (which you'll study in directed acyclic graphs) is only valid when the graph has no cycles; cycle detection is the prerequisite check. Mastering this algorithm gives you both a practical tool and a concrete model for how directed graph structure constrains what computations are possible.

The DFS cycle-detection algorithm runs in O(V + E) time — linear in the size of the graph — because each vertex and edge is visited at most once. That efficiency makes it practical for real dependency graphs with thousands of nodes. As you move toward strongly connected components, you will see how cycle structure at a finer scale organizes the entire graph into a meaningful hierarchy.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Combining Small Groups Within 5 → Number Bonds Within 5 → Number Bonds to 10 → Tens and Ones Place Value → Reading and Writing Three-Digit Numbers → Comparing Three-Digit Numbers → Comparing and Ordering Three-Digit Quantities → Comparing and Ordering Whole Numbers → Comparing Numbers 1 to 20 → Ordering Numbers to 20 → Number Line 0 to 20 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Opposites and Additive Inverses → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Dividing Integers → Unit Rates → Proportions → Percent Concept → Converting Between Fractions, Decimals, and Percents → Operations with Rational Numbers → Two-Step Equations → Solving Multi-Step Equations → Equations with Variables on Both Sides → Literal Equations → Slope-Intercept Form → Point-Slope Form → Writing Linear Equations → Parallel and Perpendicular Line Slopes → Graphing Linear Equations → Piecewise Functions → Step Functions → Composition of Functions → Inverse Functions → Radical Functions and Graphs → Rational Exponents → Exponential Functions and Graphs → Logarithms Introduction → Big-O Notation and Asymptotic Analysis → Depth-First Search (DFS) → Cycle Detection in Directed Graphs

Longest path: 87 steps · 418 total prerequisite topics

Prerequisites (2)

Leads To (1)