Given a directed graph, check whether the graph contains a cycle or not. Your function should return true if the given graph contains at least one cycle, else return false.
Input: n = 4, e = 6
0 -> 1, 0 -> 2, 1 -> 2, 2 -> 0, 2 -> 3, 3 -> 3
This diagram clearly shows a cycle 0 -> 2 -> 0.
Input:n = 4, e = 3
0 -> 1, 0 -> 2, 1 -> 2, 2 -> 3
This diagram clearly shows no cycle.
Approach: Depth First Traversal can be used to detect cycle in a Graph. DFS for a connected graph produces a tree. There is a cycle in a graph only if there is a back edge present in the graph. A back edge is an edge that is from a node to itself (selfloop) or one of its ancestor in the tree produced by DFS. In the following graph, there are 3 back edges, marked with cross sign. It can be observed that these 3 back edges indicate 3 cycles present in the graph.
For a disconnected graph, we get the DFS forest as output. To detect cycle, we can check for cycle in individual trees by checking back edges.
In the previous post, we have discussed a solution that stores visited vertices in a separate array which stores vertices of the current recursion call stack.
In this post, a different solution is discussed. The solution is from CLRS book. The idea is to do DFS of a given graph and while doing traversal, assign one of the below three colours to every vertex.
WHITE : Vertex is not processed yet. Initially, all vertices are WHITE.
GRAY: Vertex is being processed (DFS for this vertex has started, but not finished which means that all descendants (in DFS tree) of this vertex are not processed yet (or this vertex is in the function call stack)
BLACK : Vertex and all its descendants are processed. While doing DFS, if an edge is encountered from current vertex to a GRAY vertex, then this edge is back edge and hence there is a cycle.
- Create a recursive function that takes the edge and color array (this can be also kept as a global variable)
- Mark the current node as GREY.
- Traverse all the adjacent nodes and if any node is marked GREY then return true as a loop is bound to exist.
- If any adjacent vertex is WHITE then call the recursive function for that node. If the function returns true. Return true.
- If no adjacent node is grey or has not returned true then mark the current Node as BLACK and return false.
Graph contains cycle
- Time complexity: O(V + E), where V is the number of vertices and E is the number of edges in the graph.
- Space Complexity :O(V).
Since an extra color array is needed of size V.
This article is contributed by Aditya Goel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Detect Cycle in a Directed Graph using BFS
- Detect cycle in Directed Graph using Topological Sort
- Detect Cycle in a Directed Graph
- Detect cycle in the graph using degrees of nodes of graph
- Detect cycle in an undirected graph using BFS
- Detect a negative cycle in a Graph using Shortest Path Faster Algorithm
- Print negative weight cycle in a Directed Graph
- Print Nodes which are not part of any cycle in a Directed Graph
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Detect a negative cycle in a Graph | (Bellman Ford)
- Detect cycle in an undirected graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Convert undirected connected graph to strongly connected directed graph
- Minimum colors required such that edges forming cycle do not have same color
- Detect Cycle in a 2D grid
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Largest subset of Graph vertices with edges of 2 or more colors
- Minimum number of colors required to color a graph
- Find if there is a path between two vertices in a directed graph
- Shortest Path in Directed Acyclic Graph