I am trying to find the number of nodes in a cycle of a graph. I am using recursion and DFS to calculate the number of nodes in all the cycles of the graph.Here is the calculating function in C++.
int iscyclic(int node,bool visited[],bool rec[],vector<int>g[])
{
if(!visited[node])
{
visited[node] = true;
rec[node] = true;
vector<int>::iterator it;
for(it=g[node].begin();it!=g[node].end();it++)
{
if(!visited[*it] && iscyclic(*it,visited,rec,g))
{
kount++;
}
else if(rec[*it])
kount++;
}
}
rec[node] = false;
return kount;
}
The Visited
and rec
array are set to false by default and kount
has been globally set as 0
. The kount
is supposed to calculate the number of nodes in a cycle of the directed graph.However there are cases where the answer is wrong. Please help.I Have recently started learning graph theory.