C program to detect cycle in undirected graph
there is problem in my DFS_cycle function.
void cycle(struct Graph *graph)
{
int t=0;
for(int i=0;i<graph->V;i++)
{
if(graph->array[i].head!=NULL)
{
t=DFS_cycle(graph,i,-1);
printf("i=%d t=%d",i,t);
}
if(t==1)
{
printf("Graph has cycle%d.",i);
break;
}
}
if(t==0)
{
printf("Graph has no cycle");
}
}
I think something is missing in my DFS_cycle function. So my code terminates before calling DFS_cycle function.
int DFS_cycle(struct Graph *graph,int vi,int vj)
{
struct adjListNode *p;
p=graph->array[vi].head;
if(graph->visited[vi]==1) //if vertex already is visited and neighbour is not parrent.
{
if(p->dest!=vj)
return(1);
}
else //if not visited
{
while(p!=NULL)
{
if(DFS_cycle(graph,p->dest,vi))
return(1);
p=p->next;
}
}
return(0);
}