Count of simple cycles in an undirected graph having N vertices


Introduction

Undirected graphs are an essential component of computer science and graph theory, representing a group of nodes connected by edges without any directionality. One common problem associated with undirected graphs is the counting of simple cycles or circuits, which are closed paths that visit each vertex only once. In this article, we will explore how to get total count of the given undirected graph with N vertices using the powerful programming languages C and C++.

Undirected Graph

Before we jump into coding, let's ensure everyone grasps what constitutes a simple cycle within an undirected graph.

Let us consider an undirected graph with four vertices (N=4) as follows −

In this example, every simple cycle starts at one vertex and ends at the same vertex, visiting all four vertices while ensuring only one visit per node.

Representing the Graph

Firstly, creating a suitable representation for our graph is crucial. The most used method involves using adjacency lists or matrices.

  • Adjacency List − Each node stores connections to its neighboring nodes.

  • Adjacency Matrix − Using a square matrix where rows and columns represent vertices, filled values indicate edge connectivity.

Depth-First Search (DFS)

The next step is performing a depth-first search on our graph starting from every possible vertex as follows −

  • Visit each unvisited neighbor vertex recursively until reaching one previously visited node (a cycle closure).

  • To distinguish between different paths being explored simultaneously, mark visited nodes during DFS traversal.

  • Keep track of all unique encountered cycles during DFS implementation for further analysis.

Counting Simple Cycles

To obtain an accurate count across an entire graph effectively incorporates these elements −

  • Run DFS from every vertex individually and maintain information about previously visited nodes using a boolean array.

  • Track the path during each recursive call and maintain a list of visited nodes to detect cycle formation.

  • Store each discovered cycle by appending the initial vertex to create a complete loop.

C++ Program to get the count in given undirected graph having N vertices

The number of simple cycles is counted using the recursive function in the given adjacency matrix.

Algorithm

  • Step 1 − The all-in-one header file used that includes all the functions in C++.

  • Step 2 − The “V” variable is initialized with the integer data type.

  • Step 3 − The visited vertex is represented as “vis” and holds the Boolean array to get the number of cycles.

  • Step 4 − The graph used is dynamic programming and it is the function taking three arguments as the starting vertex, current vertex, and the cycle length.

  • Step 5 − The current vertex is assumed to be visited and the for loop will iterate through every nodes of the adjacency matrix values.

  • Step 6 − When the next vertex is a starting vertex, the count will be incremented.

  • Step 7 − Depending on the matrix value, the undirected graph count will be returned.

Example

//using the required header files which includes all the header within it
#include <bits/stdc++.h> 
using namespace std;

// Adjacency matrix is represented in an array
void Vertex(int V, vector<vector<int> > graph) {
   // Declaring the variable count with a value of 0
   int count = 0;
   int dp[(1 << V)][V];

   // Declaring with a value of 0
   memset(dp, 0, sizeof dp);

   // for loop will iterate through 
   for (int num = 0;
      num < (1 << V); num++) {

         // To get the number of bits
         int nodes
         = __builtin_popcountll(num);
         // To find the first bit
         int first
         = __builtin_ffsl(num);
		
         if (nodes == 1) {

            // Declaring with a value of 1
            dp[num][first] = 1;
         } else {

            // Resetting visited array before each traversal
            // Dynamic programming starts from vertex i where the current cycle has only one node and increments the value by 1
            for (int val = first + 1;
               val < V; val ++) {
				
            if ((num & (1 << val))) {
					
               int newnum = num ^ (1 << val);
   
               for (int k = 0; k < V; k++) {

                  if ((newnum & (1 << k)) && graph[k][val]) {
                     dp[num][val]
                     += dp[newnum][k];

                     //Graph connected to the first node
                     if (graph[val][first] && nodes > 2)
                        count += dp[num][val];
                  }
               }
            }
         }
      }
   }

   // Getting the final answer
   cout << count << endl;
}

// main function
int main() {
   // In the graph, the vertices in initialized with a value of 4
   int V = 4; 
  
   // Declaring the input as adjacency matrix
   vector<vector<int> > graph = { { 1, 1, 1, 1 }, 
      { 1, 0, 1, 1 }, 
      { 1, 1, 0, 1 }, 
      { 1, 1, 1, 0 } };

   Vertex(V, graph);

   return 0;
}

Output

2

Conclusion

With the thorough description in this article and the implementation examples in C++ that were provided, we are now prepared to take on comparable problems involving counting simple cycles. It's essential to count cycles in the given graph while dealing with a variety of challenging issues. Such calculations are possible by making use of depth-first search and appropriate data structures to efficiently describe graphs.

Updated on: 25-Aug-2023

109 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements