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.
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.