-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph contain cyle.cpp
More file actions
70 lines (64 loc) · 1.17 KB
/
Copy pathgraph contain cyle.cpp
File metadata and controls
70 lines (64 loc) · 1.17 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
#include<bits/stdc++.h>
using namespace std;
template<typename T>
class graph{
map<int,list<T> >l;
public:
void addedge(int x,int y)
{
l[x].push_back(y);
l[y].push_back(x); //bi-directional
}
bool iscycle(map<int,bool> &vis,T src,int parent)
{
vis[src]=true;
for(auto node:l[src]){
if(!vis[node])
{
bool p=iscycle(vis,node,src);
if(p)
return true;
}
else if(node!=parent)
{
return true;
}
}
return false;
}
bool contain_cycle(T src){
map<int,bool>vis;
for(auto node:l)
{
T nbr=node.first;
vis[nbr]=false;
}
return iscycle(vis,src,-1);
}
};
int main()
{
graph<int>g;
g.addedge(2,1);
g.addedge(2,3);
g.addedge(3,2);
g.addedge(3,0);
g.addedge(1,2);
g.addedge(1,0);
g.addedge(0,1);
g.addedge(3,4);
g.addedge(4,3);
g.addedge(4,5);
g.addedge(5,4);
if(g.contain_cycle(0))
cout<<"yes,graph contains cycle"<<endl;
else
cout<<"graph does'nt contain sycle"<<endl;
graph<int>g2;
g2.addedge(0, 1);
g2.addedge(1, 2);
if(g2.contain_cycle(0))
cout<<"yes,graph contains cycle"<<endl;
else
cout<<"graph does'nt contain sycle"<<endl;
}