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

Cycle Detection in Directed and Undirected Graphs

College Depth 89 in the knowledge graph I know this Set as goal
5topics build on this
447prerequisites beneath it
See this on the map →
Depth-First Search (DFS)Articulation Points and Bridges in Graphs+1 moreMaximum Flow: Network Flow Problems and Algorithms
graphs cycles dfs

Core Idea

In undirected graphs, a back edge (to a visited neighbor other than the parent) signals a cycle. In directed graphs, back edges are those to ancestors in the DFS tree. Both detections run in O(V+E) during DFS. Cycle detection is essential for dependency resolution and deadlock detection.

How It's Best Learned

Implement DFS with three vertex states (white, gray, black). Use gray edges as back edges. Trace on examples with and without cycles. Apply to topological sorting and deadlock detection.

Common Misconceptions

Explainer

From your study of depth-first search, you know that DFS explores a graph by going as deep as possible before backtracking, and that the edges it encounters can be classified based on the DFS tree it builds. Cycle detection exploits this classification. The core insight is that a cycle exists if and only if DFS encounters an edge that points back to a node already on the current exploration path — a back edge. But the precise definition of "back edge" differs between undirected and directed graphs, and conflating the two is a common source of bugs.

In an undirected graph, every edge is traversed in both directions during DFS. When you visit node A and see neighbor B, you will later visit B and see A as a neighbor too. This means you must distinguish between "B is my parent in the DFS tree" (not a cycle) and "B is a visited node that is not my parent" (a cycle). The algorithm is straightforward: during DFS, if you encounter a neighbor that has already been visited and is not the node you came from, you have found a cycle. For example, in a triangle A-B-C-A, when DFS reaches C (having come from B) and sees A is already visited, that back edge reveals the cycle. No special node coloring is needed — a simple visited/unvisited flag plus parent tracking suffices.

Directed graphs are more subtle. Here, a visited node is not necessarily evidence of a cycle — it might have been fully explored in a previous DFS subtree that has no connection back to the current path. The solution is to track three states for each node: white (undiscovered), gray (discovered, currently being explored — on the recursion stack), and black (fully explored, all descendants processed). A back edge in a directed graph is specifically an edge from a gray node to another gray node — both are on the current DFS path, forming a cycle. An edge from a gray node to a black node is a cross edge or forward edge, not a cycle. This three-color scheme is what makes directed cycle detection work correctly.

These algorithms run in O(V + E) time, matching the cost of DFS itself, because cycle detection adds only constant work per edge. The applications are pervasive in computing. Package managers use cycle detection to verify that dependency graphs are acyclic (a cycle means "A requires B which requires A" — an unresolvable deadlock). Compilers check for circular dependencies between modules. Operating systems detect deadlocks by looking for cycles in resource allocation graphs. And topological sorting — which you will study next — is only possible on directed acyclic graphs (DAGs), so verifying the absence of cycles is a prerequisite for topological ordering.

Practice Questions 5 questions

Prerequisite Chain

Understanding ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsDefining and Calling FunctionsFunctions: Decomposing ProblemsFunction Parameters and Argument PassingReturn ValuesVariable ScopeIntroduction to ClassesObjects and InstancesMethods and AttributesAlgorithm Design BasicsTree Structure and Node PropertiesBinary TreesTree TraversalsBreadth-First Search (BFS)Breadth-First Search: Implementation and ApplicationsQueue Applications: Level-Order Traversal and Breadth-First SearchBipartite Graphs: Detection and Two-ColoringCycle Detection in Directed and Undirected Graphs

Longest path: 90 steps · 447 total prerequisite topics

Prerequisites (3)

Leads To (1)