-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCycle Detection Directed Graph Using DFS.cpp
71 lines (68 loc) · 1.54 KB
/
Cycle Detection Directed Graph Using DFS.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
#include<iostream>
#include<map>
#include<queue>
#include<list>
#include<cstring>
using namespace std;
class Graph{
list<int> *l;
int V;
public:
Graph(int V){
this->V = V;
l = new list<int>[V];
}
void addEdge(int x, int y, bool directed = true){
l[x].push_back(y);
if(!directed){
l[y].push_back(x);
}
}
bool cycle_helper(int node, bool *visited, bool *stack){
// visite a node
visited[node]=true;
stack[node]= true;
for(int nbr:l[node]){
// two cases
if(stack[nbr]==true){
return true;
}
else if(visited[nbr]== false){
bool cycle_avail = cycle_helper(nbr, visited, stack);
if(cycle_avail==true){
return true;
}
}
}
// leave a node
stack[node]=false;
return false;
}
bool contains_cycle(){
// check for cycle indirected graph
bool *visited = new bool[V];
bool *stack = new bool[V];
for(int i=0;i<V;i++){
visited[i]=stack[i]=false;
}
return cycle_helper(0,visited,stack);
}
};
int main() {
Graph g(7);
g.addEdge(0,1);
g.addEdge(1,2);
g.addEdge(2,3);
g.addEdge(3,4);
g.addEdge(4,5);
g.addEdge(1,5);
g.addEdge(5,6);
g.addEdge(4,2);
if(g.contains_cycle()){
cout<<"Yes it contain tree";
}
else{
cout<<"No it does not contain tree";
}
return 0;
}