-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcycle_free.c
More file actions
99 lines (91 loc) · 1.96 KB
/
cycle_free.c
File metadata and controls
99 lines (91 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int val;
struct Node*next;
}node;
typedef struct Graph
{
int v;
node**adj;
}graph;
graph*creategraph(int V) // V is the number of vertices
{
graph*g=malloc(sizeof(graph));
g->v=V;
g->adj=(node**)malloc(sizeof(node*)*g->v);
int i=0;
for(i=0;i<g->v;i++)
g->adj[i]=NULL;
return g;
}
void addedge(graph*g,int u,int v) // Add v to u’s list.
{
node*temp=(node*)malloc(sizeof(node));
temp->val=v;
temp->next=g->adj[u];
g->adj[u]=temp;
}
/**
* This function is a variation of "Depth First Traversal"
*/
int iscyclutil(graph*g,int v,int visited[],int recstack[])
{
if(visited[v]==0)
{
/* Mark all the vertices as not visited and not part of recursion stack */
visited[v]=1;
recstack[v]=1;
node*temp;
temp=g->adj[v];
while(temp)
{
/* Returns 1 if the graph contains a cycle, else 0. */
if(!visited[temp->val] && iscyclutil(g,temp->val,visited,recstack))
return 1;
else if(recstack[temp->val])
return 1;
temp=temp->next;
}
}
recstack[v]=0; // remove the vertex from recursion stack
return 0;
}
/**
* the recursive helper function to detect cycle
* in different DFS trees
*/
int Iscycle(graph*g)
{
int recstack[g->v];
int visited[g->v];
int i;
for(i=0;i<g->v;i++)
{
visited[i]=0;
recstack[i]=0;
}
for(i=0;i<g->v;i++)
{
if(iscyclutil(g,i,visited,recstack))
return 1;
}
return 0;
}
int main()
{
/* Create a graph given in the above diagram */
graph*g=creategraph(4);
addedge(g,0, 1);
addedge(g,0, 2);
addedge(g,1, 2);
addedge(g,2, 0);
addedge(g,2, 3);
addedge(g,3, 3);
if(Iscycle(g))
printf("graph has got cycle\n");
else
printf("no cycle present in graph\n");
return 0;
}