-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_cycle_in_udirected_graph.cpp
65 lines (64 loc) · 1.31 KB
/
detect_cycle_in_udirected_graph.cpp
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
#include<bits/stdc++.h>
using namespace std;
struct edge
{
int src,dest;
};
struct graph
{
int V,E;
struct edge*e;
};
struct graph*createGraph(int number,int edges)
{
struct graph*g=(struct graph*)malloc(sizeof(struct graph));
g->V=number;
g->E=edges;
struct edge*e1=(struct edge*)malloc(sizeof(struct edge)*g->E);
g->e=e1;
return g;
}
int find(int parent[],int index)
{
if(parent[index]==-1)
return index;
return find(parent,parent[index]);
}
void union1(int parent[],int srcindex,int destindex)
{
// int index=find(parent,srcindex);
// int index1=find(parent,destindex);
parent[srcindex]=destindex;
}
int cycleun(struct graph*g)
{
int v=g->V;
int*parent=new int[v];
for(int i=0;i<v;i++)
parent[i]=-1;
for(int i=0;i<g->E;i++)
{
int srcindex=find(parent,g->e[i].src);
int destindex=find(parent,g->e[i].dest);
if(srcindex==destindex)
return 1;
union1(parent,srcindex,destindex);
}
return 0;
}
int main()
{
int v=3,e=2;
struct graph*g=createGraph(v,e);
// add edge 0-1
g->e[0].src=0;
g->e[0].dest=1;
// add edge 1-2
g->e[1].src=1;
g->e[1].dest=2;
// add edge 0-2
// g->e[2].src=0;
// g->e[2].dest=2;
cout<<cycleun(g);
return 0;
}