-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathGraphs_cycle_detection_dfs.cpp
63 lines (61 loc) · 1.73 KB
/
Graphs_cycle_detection_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
// Graphs Cycle detection using DFS
// Program Author : Abhisek Kumar Gupta
#include<bits/stdc++.h>
using namespace std;
template<typename T>
class Graph{
map<T, list<T> > L;
public:
Graph(){
}
void add_edge(T u, T v, bool bidir = true){
L[u].push_back(v);
if(bidir){
L[v].push_back(u);
}
}
bool is_cyclic_helper(T node, map<T, bool> &visited, map<T, bool> &in_stack){
visited[node] = true;
in_stack[node] = true;
for(T neighbour : L[node]){
if((!visited[neighbour] && is_cyclic_helper(neighbour, visited, in_stack)) || in_stack[neighbour]){
return true;
}
}
in_stack[node] = false;
return false;
}
bool is_cyclic(){
map<T, bool> visited;
map<T, bool> in_stack;
for(auto x: L){
T node = x.first;
if(!visited[node]){
bool answer = is_cyclic_helper(node, visited, in_stack);
if(answer){
return true;
}
else{
return false;
}
}
}
}
};
int main(){
Graph<int> g;
g.add_edge(0, 2, false);
g.add_edge(0, 1, false);
g.add_edge(2, 3, false);
g.add_edge(2, 4, false);
// g.add_edge(3, 0, false);
g.add_edge(4, 5, false);
g.add_edge(1, 5, false);
if(g.is_cyclic()){
cout << "Cycle present" << endl;
}
else{
cout << "Cycle not present" << endl;
}
return 0;
}