-
Notifications
You must be signed in to change notification settings - Fork 21
/
cycle_detection_undirected.cpp
70 lines (56 loc) · 1.12 KB
/
cycle_detection_undirected.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
#include<iostream>
#include<list>
#include<vector>
using namespace std;
class Graph{
list<int> *l;
int V;
public:
Graph(int V){
this->V = V;
l = new list<int>[V];
}
//undirected graph
void addEdge(int x,int y){
l[x].push_back(y);
l[y].push_back(x);
}
bool dfs(int node, vector<bool> &visited, int parent){
//mark that node visited
visited[node] = true;
for(auto nbr : l[node]){
if(!visited[nbr]){
bool nbrFoundACycle = dfs(nbr,visited,node);
if(nbrFoundACycle){
return true;
}
}
//nbr is visited and its not the parent of current node in the current dfs call
else if(nbr!=parent){
return true;
}
}
return false;
}
bool contains_cycle(){
//Graph is single component
vector<bool> visited(V,false);
return dfs(0, visited, -1);
}
};
bool contains_cycle(int V,vector<pair<int,int> > edges){
//Complete this method
Graph g(V);
for(auto edge:edges){
g.addEdge(edge.first,edge.second);
}
return g.contains_cycle();
}
int main(){
Graph g(3);
g.addEdge(0,1);
g.addEdge(1,2);
//g.addEdge(2,0);
cout << g.contains_cycle() <<endl;
return 0;
}