Given an undirected graph, how to check if there is a cycle in the graph?
Input: n = 4, e = 4
0 1, 1 2, 2 3, 0 2
The diagram clearly shows a cycle 0 to 2 to 1 to 0
Input:n = 4, e = 3
0 1, 1 2, 2 3
The diagram clearly shows no cycle
Articles about cycle detection:
Approach: Run a DFS from every unvisited node. Depth First Traversal can be used to detect a 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 joining a node to itself (self-loop) or one of its ancestor in the tree produced by DFS.
To find the back edge to any of its ancestor keep a visited array and if there is a back edge to any visited node then there is a loop and return true.
- Create the graph using the given number of edges and vertices.
- Create a recursive function that that current index or vertex, visited and recursion stack.
- Mark the current node as visited and also mark the index in recursion stack.
- Find all the vertices which are not visited and are adjacent to the current node. Recursively call the function for those vertices, If the recursive function returns true return true.
- If the adjacent vertices are already marked in the recursion stack then return true.
- Create a wrapper class, that calls the recursive function for all the vertices and if any function returns true, return true.
- Else if for all vertices the function returns false return false.
Graph contains cycle Graph doesn't contain cycle
- Time Complexity: O(V+E).
The program does a simple DFS Traversal of the graph which is represented using adjacency list. So the time complexity is O(V+E).
- Space Complexity: O(V).
To store the visited array O(V) space is required.
Exercise: Can we use BFS to detect cycle in an undirected graph in O(V+E) time? What about directed graphs?
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Detect cycle in an undirected graph using BFS
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Detect cycle in the graph using degrees of nodes of graph
- Detect Cycle in a Directed Graph using BFS
- Detect Cycle in a Directed Graph
- Shortest cycle in an undirected unweighted graph
- Check if there is a cycle with odd weight sum in an undirected graph
- Detect Cycle in a directed graph using colors
- Find minimum weight cycle in an undirected graph
- Number of single cycle components in an undirected graph
- Detect a negative cycle in a Graph | (Bellman Ford)
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- 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
- Clone an Undirected Graph
- Connected Components in an undirected graph
- Sum of degrees of all nodes of a undirected graph
- Number of Triangles in an Undirected Graph
- Find k-cores of an undirected graph
- Eulerian Path in undirected graph