# Count of all cycles without any inner cycle in a given Graph

Given an **undirected graph** consisting of **N** vertices numbered **[0, N-1]** and **E** edges, the task is to count the number of cycles such that any subset of vertices of a cycle does not form another cycle.

**Examples:**

Input:N = 2, E = 2, edges = [{0, 1}, {1, 0}]

Output:1

Explanation:

Only one cycle exists between the two vertices.

Input:N = 6, E = 9, edges = [{0, 1}, {1, 2}, {0, 2}, {3, 0}, {3, 2}, {4, 1}, {4, 2}, {5, 1}, {5, 0}]

Output:4

Explanation:

The possible cycles are shown in the diagram below:

Cycles such as 5 -> 0 -> 2 -> 1 -> 5 are not considered as it comprises of inner cycles {5 -> 0 -> 1} and {0 -> 1 -> 2}

**Approach:**

Since V vertices require V edges to form 1 cycle, these the number of required cycles can be expressed using the formula:

(Edges - Vertices) + 1

**Illustration:**

N = 6, E = 9, edges = [{0, 1}, {1, 2}, {0, 2}, {3, 0}, {3, 2}, {4, 1}, {4, 2}, {5, 1}, {5, 0}]

Number of Cycles = 9 – 6 + 1 = 4

The 4 cycles in the graph are:

{5, 0, 1}, {0, 1, 2}, {3, 0, 2} and {1, 2, 4}

This formula also covers the case when a single vertex may have a self-loop.

Below is the implementation of the above approach:

## C++

`// C++ implementation for the ` `// above approach. ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// count of required cycles ` `int` `numberOfCycles(` `int` `N, ` `int` `E, ` ` ` `int` `edges[][2]) ` `{ ` ` ` `vector<` `int` `> graph[N]; ` ` ` `for` `(` `int` `i = 0; i < E; i++) { ` ` ` `graph[edges[i][0]] ` ` ` `.push_back(edges[i][1]); ` ` ` `graph[edges[i][1]] ` ` ` `.push_back(edges[i][0]); ` ` ` `} ` ` ` ` ` `// Return the number of cycles ` ` ` `return` `(E - N) + 1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 6; ` ` ` `int` `E = 9; ` ` ` `int` `edges[][2] = { { 0, 1 }, ` ` ` `{ 1, 2 }, ` ` ` `{ 2, 0 }, ` ` ` `{ 5, 1 }, ` ` ` `{ 5, 0 }, ` ` ` `{ 3, 0 }, ` ` ` `{ 3, 2 }, ` ` ` `{ 4, 2 }, ` ` ` `{ 4, 1 } }; ` ` ` `int` `k = numberOfCycles(N, E, ` ` ` `edges); ` ` ` ` ` `cout << k << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(E)

**Auxiliary Space:** O(N)

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.

## Recommended Posts:

- Print all the cycles in an undirected graph
- Detect cycle in the graph using degrees of nodes of graph
- Product of lengths of all cycles in an undirected graph
- Cycles of length n in an undirected and connected graph
- Program to find the diameter, cycles and edges of a Wheel Graph
- Coloring a Cycle Graph
- Degree of a Cycle Graph
- Detect cycle in an undirected graph using BFS
- Detect cycle in an undirected graph
- Detect Cycle in a Directed Graph
- Detect Cycle in a Directed Graph using BFS
- Shortest cycle in an undirected unweighted graph
- Detect Cycle in a directed graph using colors
- Check if there is a cycle with odd weight sum in an undirected graph
- Total number of Spanning trees in a Cycle Graph
- Number of single cycle components in an undirected graph
- Detect a negative cycle in a Graph | (Bellman Ford)
- Find minimum weight cycle in an undirected graph
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Check if a cycle of length 3 exists or not in a graph that satisfy a given condition

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.