Given an undirected and connected graph and a number n, count total number of cycles of length n in the graph. A cycle of length n simply means that the cycle contains n vertices and n edges. And we have to count all such cycles that exist.
Input : n = 4 Output : Total cycles = 3 Explanation : Following 3 unique cycles 0 -> 1 -> 2 -> 3 -> 0 0 -> 1 -> 4 -> 3 -> 0 1 -> 2 -> 3 -> 4 -> 1 Note* : There are more cycles but these 3 are unique as 0 -> 3 -> 2 -> 1 -> 0 and 0 -> 1 -> 2 -> 3 -> 0 are same cycles and hence will be counted as 1.
To solve this Problem, DFS(Depth First Search) can be effectively used. Using DFS we find every possible path of length (n-1) for a particular source (or starting point). Then we check if this path ends with the vertex it started with, if yes then we count this as the cycle of length n. Notice that we looked for path of length (n-1) because the nth edge will be the closing edge of cycle.
Every possible path of length (n-1) can be searched using only V – (n – 1) vertices (where V is the total number of vertices).
For above example, all the cycles of length 4 can be searched using only 5-(4-1) = 2 vertices. The reason behind this is quite simple, because we search for all possible path of length (n-1) = 3 using these 2 vertices which include the remaining 3 vertices. So, these 2 vertices cover the cycles of remaining 3 vertices as well, and using only 3 vertices we can’t form a cycle of length 4 anyways.
One more thing to notice is that, every vertex finds 2 duplicate cycles for every cycle that it forms. For above example 0th vertex finds two duplicate cycle namely 0 -> 3 -> 2 -> 1 -> 0 and 0 -> 1 -> 2 -> 3 -> 0. Hence the total count must be divided by 2 because every cycle is counted twice.
Total cycles of length 4 are 3
This article is contributed by Shubham Rana. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Print all the cycles in an undirected graph
- Product of lengths of all cycles in an undirected graph
- Connected Components in an undirected graph
- Clone an undirected graph with multiple connected components
- 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
- Program to count Number of connected components in an undirected graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Program to find the diameter, cycles and edges of a Wheel Graph
- Check if a directed graph is connected or not
- Clone an Undirected Graph
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- All vertex pairs connected with exactly k edges in a graph
- Detect cycle in an undirected graph