How can we count the no. of cycles in an undirected graph?
Below is my algorithm but in this one cycle is counted twice. How can I resolve it?
int NoOfCycle(T src){
int noc=0;
queue q;
map<T,bool> Visited;
map<T,T> Parent;
Visited[src]=true;
Parent[src]=src;
q.push(src);
while(!q.empty()){
T node=q.front();
q.pop();
cout<<"node: "<<node<<endl;
for(T neighbour:adjList[node]){
if (Visited[neighbour]&&neighbour!=Parent[node]){
cout<<"Increment noc: “<<neighbour<<” "<<node<<endl;
noc++;
}
else{
if (!Visited[neighbour]){
Parent[neighbour]=node;
Visited[neighbour]=true;
cout<<“AddQ”<<neighbour<<endl;
q.push(neighbour);
}
}
}
}
return noc;
}
Counting no. of cycle in an undirected graph
Hey!
You are using the code to detect cycles in the graph and just incrementing a variable, that may be the reason for the problem.
You may refer this code for help:
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.