-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFG Graph-3 IsCyclic.cpp
83 lines (73 loc) · 1.41 KB
/
GFG Graph-3 IsCyclic.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
#include <vector>
#include <queue>
using namespace std;
vector <vector <int> > v;
vector <bool> vis;
void addedge(int u,int b)
{
v[u].push_back(b);
}
int iscyclicutil(int i)
{
vis[i]=true;
int k=0;
for (int j=0;j<v[i].size();j++)
{
if (vis[v[i][j]]==true)
{
cout << "Cycle Exisits" << endl;
return 1;
}
if (vis[v[i][j]]==false)
k = iscyclicutil(v[i][j]);
if (k==1)
return 1;
}
return 0;
}
void iscyclic()
{
int k=0;
for (int i=0;i<v.size() && v.size()!=0;i++)
{
if (vis[i]==false)
k = iscyclicutil(i);
if(k==1)
{
// cout << i << "Cycle Exisits" << endl;
break;
}
}
if (k==0)
cout << "cycle doesn't exist" << endl;
}
void printgraph()
{
int i=0;
for (i=0;i<v.size();i++)
{
cout << i << " adjacent nodes: ";
for (int j=0;j<v[i].size() && v.size()!=0;j++)
{
cout << v[i][j] << " ";
}
cout << endl;
}
}
int main()
{
v.assign(4,vector <int> () );
vis.assign(4,false);
addedge(0,1);
addedge(0,2);
addedge(1,2);
addedge(2,0);
addedge(2,3);
addedge(3,3);
printgraph();
iscyclic();
return 0;
}