How to detect if a directed graph is cyclic?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In computer science, detecting cycles in a directed graph is a fundamental problem with applications in various areas such as deadlock detection, scheduling, and more. Here's a comprehensive guide on how to detect if a directed graph is cyclic.
Understanding Directed Graphs and Cycles
A directed graph or digraph consists of nodes (or vertices) connected by edges where each edge has a direction, indicating a one-way relationship between two nodes. A cycle in a directed graph occurs when there is a path from a node to itself following the direction of the edges.
Example of Directed Cycle
Consider the directed graph with nodes A
, B
, and C
and edges A -> B
, B -> C
, and C -> A
. This graph contains a cycle since you can start at A
, follow the edges A -> B -> C -> A
, and return to the starting node.
Techniques to Detect Cycles
Here, we describe two primary techniques to detect cycles in a directed graph: Depth First Search (DFS) and Kahn's Algorithm.
1. Cycle Detection Using Depth First Search (DFS)
DFS is a classic recursive algorithm used to explore all nodes and edges of a graph. In cycle detection, DFS visits nodes and tracks the states of vertices: unvisited, visiting, and visited.
- Unvisited: Node has not been processed.
- Visiting: Node is currently being processed; all its descendants are not fully processed yet.
- Visited: Node and all its descendants have been processed.
Steps:
- Initialize all nodes as unvisited.
- For each unvisited node, perform DFS:
- Mark the node as visiting.
- Recursively visit all adjacent unvisited nodes.
- Mark the node as visited.
- If you encounter a node that is currently visiting, a cycle exists.
- Remove a node from the queue and reduce the in-degree of its neighbors by 1.
- If any neighbor’s in-degree becomes zero, add it to the queue.
- Complexity: Both algorithms mentioned above run in time complexity, where is the number of vertices and is the number of edges.
- Space Efficiency: DFS uses additional space for recursion stack, while Kahn’s algorithm uses a queue.
Related reading
- How to detect if an ellipse intersectscollides with a circle
- How to detect if the given graph has a cycle containing all of its nodes? Does the suggested algorithm have any flaws?
- How to determine day of week by passing specific date?
- How to determine if a Delaunay triangle is internal or external?
- How to determine if a JavaScript array contains an object with an attribute that equals a given value
- How to determine if a linked list has a cycle using only two memory locations
- How to determine if a list is subset of another list?
- How to determine if a sequence is bitonic?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.