Given an undirected graph, print all the vertices that form cycles in it.
Pre-requisite: Detect Cycle in a directed graph using colors
In the above diagram, the cycles have been marked with dark green color. The output for the above will be
1st cycle: 3 5 4 6
2nd cycle: 11 12 13
Approach: Using the graph coloring method, mark all the vertex of the different cycles with unique numbers. Once the graph traversal is completed, push all the similar marked numbers to an adjacency list and print the adjacency list accordingly. Given below is the algorithm:
- Insert the edges into an adjacency list.
- Call the DFS function which uses the coloring method to mark the vertex.
- Whenever there is a partially visited vertex, backtrack till the current vertex is reached and mark all of them with cycle numbers. Once all the vertexes are marked, increase the cycle number.
- Once Dfs is completed, iterate for the edges and push the same marked number edges to another adjacency list.
- Iterate in the another adjacency list and print the vertex cycle-number wise.
Below is the implementation of the above approach:
Cycle Number 1: 3 4 5 6 Cycle Number 2: 11 12 13
Time Complexity: O(N + M), where N is number of vertex and M is the number of edges.
Auxiliary Space: O(N + M)
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.
- Product of lengths of all cycles in an undirected graph
- Cycles of length n in an undirected and connected graph
- Count of all cycles without any inner cycle in a given Graph
- Print all shortest paths between given source and destination 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
- Program to find the diameter, cycles and edges of a Wheel Graph
- Sum of the minimum elements in all connected components of an undirected graph
- Maximum number of edges among all connected components of an undirected graph
- Sum of degrees of all nodes of a undirected graph
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Find all cliques of size K in an undirected graph
- Maximum sum of values of nodes among all connected components of an undirected graph
- Largest subarray sum of all connected components in undirected graph
- Minimize cost to color all the vertices of an Undirected Graph using given operation
- Minimize cost to color all the vertices of an Undirected Graph
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Eulerian path and circuit for undirected graph
- Detect cycle in an undirected graph
- Number of Triangles in an Undirected Graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : sanjeev2552