how to find the number of cycles in a graph C++
C++
// CPP Program to count cycles of length n
// in a given graph.
#include <bits/stdc++.h>
using namespace std;
// Number of vertices
const int V = 5;
void DFS(bool graph[][V], bool marked[], int n,
int vert, int start, int &count)
{
// mark the vertex vert as visited
marked[vert] = true;
// if the path of length (n-1) is found
if (n == 0) {
// mark vert as un-visited to make
// it usable again.
marked[vert] = false;
// Check if vertex vert can end with
// vertex start
if (graph[vert][start])
{
count++;
return;
} else
return;
}
// For searching every possible path of
// length (n-1)
for (int i = 0; i < V; i++)
if (!marked[i] && graph[vert][i])
// DFS for searching path by decreasing
// length by 1
DFS(graph, marked, n-1, i, start, count);
// marking vert as unvisited to make it
// usable again.
marked[vert] = false;
}
// Counts cycles of length N in an undirected
// and connected graph.
int countCycles(bool graph[][V], int n)
{
// all vertex are marked un-visited initially.
bool marked[V];
memset(marked, 0, sizeof(marked));
// Searching for cycle by using v-n+1 vertices
int count = 0;
for (int i = 0; i < V - (n - 1); i++) {
DFS(graph, marked, n-1, i, i, count);
// ith vertex is marked as visited and
// will not be visited again.
marked[i] = true;
}
return count/2;
}
int main()
{
bool graph[][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 0, 1},
{0, 1, 0, 1, 0},
{1, 0, 1, 0, 1},
{0, 1, 0, 1, 0}};
int n = 4;
cout << "Total cycles of length " << n << " are "
<< countCycles(graph, n);
return 0;
}
Also in C++:
- Title
- dfenwick tree code c++
- Category
- C++
- Title
- c++ char print width
- Category
- C++
- Title
- git branch in my bash prompt
- Category
- C++
- Title
- user input c++
- Category
- C++
- Title
- how to create object in c++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- statement that causes a function to end in c++
- Category
- C++
- Title
- c++ sql
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- max in c++
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- body parser
- Category
- C++
- Title
- compare function in sort c++ stl
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- how to ensure the user inouts a int and not anything else c++
- Category
- C++
- Title
- c++ random numbers
- Category
- C++
- Title
- how to turn int into string c++
- Category
- C++
- Title
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- Category
- C++
- Title
- c++ try
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- trovare il valore massimo in un array c++ w3
- Category
- C++
- Title
- caesar cipher program in c++
- Category
- C++
- Title
- convert string to stream c++
- Category
- C++
- Title
- c++ string to vector int
- Category
- C++
- Title
- switch statement c++
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- how to delete something in an array c++
- Category
- C++
- Title
- Convert binary tree to a doubly linked list
- Category
- C++
- Title
- size of a matrix using vector c++
- Category
- C++
- Title
- how to measure program run time in c++
- Category
- C++
- Title
- preorder traversal
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- c++ how to add something at the start of a vector
- Category
- C++
- Title
- how to use max_element in c++ with vector
- Category
- C++
- Title
- recursive in c++
- Category
- C++
- Title
- counting valleys hackerrank solution in c++
- Category
- C++
- Title
- appending a double to a string c++
- Category
- C++
- Title
- Rectangle area hackerrank solution in c++
- Category
- C++
- Title
- linear search in c++
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- count a character in a string c++
- Category
- C++
- Title
- new class * [] c++
- Category
- C++
- Title
- calling by reference and pointers c++
- Category
- C++
- Title
- converting char to int in c++
- Category
- C++
- Title
- floor() in c++
- Category
- C++
- Title
- declaring 2d vector in c++
- Category
- C++
- Title
- maximum subarray sum in c++
- Category
- C++
- Title
- assegnare valori in c++
- Category
- C++
- Title
- case label in c++
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- create copy of range of string c++
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- c++ append to list
- Category
- C++
- Title
- struct c++
- Category
- C++
- Title
- c++ loop through int array
- Category
- C++
- Title
- COnvert string to char * C++
- Category
- C++
- Title
- sort a pair using c++ stl
- Category
- C++
- Title
- how to make string get spaces c++
- Category
- C++
- Title
- create a bitset of 1024 bits,
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- double max value c++
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- c++ string contains
- Category
- C++
- Title
- reverse a linked list using recursion
- Category
- C++
- Title
- queue c++
- Category
- C++
- Title
- compare string c++
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- list conda environments
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- stringstream in c++
- Category
- C++
- Title
- insert function in c++ vector
- Category
- C++
- Title
- declaring vector c++
- Category
- C++
- Title
- to_string c++
- Category
- C++
- Title
- can you use a return to print a string when referencing an integer c++
- Category
- C++
- Title
- sfml basic program
- Category
- C++
- Title
- convert int to string c++
- Category
- C++
- Title
- how to inject a dll into a game c++
- Category
- C++
- Title
- basic ex of maps in c++
- Category
- C++
- Title
- nginx linux
- Category
- C++
- Title
- memset c++
- Category
- C++
- Title
- using namespace std in c++
- Category
- C++
- Title
- c++ variable argument
- Category
- C++
- Title
- c++ multidimensional vector
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- howt o initialize 3d vector in c++
- Category
- C++
- Title
- nan c++ example
- Category
- C++
- Title
- how to use winmain function
- Category
- C++
- Title
- c++ function to find minimum element in array
- Category
- C++
- Title
- if vector contains value c++
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- *max_element in c++
- Category
- C++
- Title
- c++ uint32_t
- Category
- C++
- Title
- pause the console c++
- Category
- C++