-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS Cycle Detection for Directed Graph.cpp
53 lines (49 loc) · 1.17 KB
/
DFS Cycle Detection for Directed Graph.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
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class Graph {
private:
map <int, vector<int>> graph;
map <int, bool> visited, visitedDFS;
bool detectCycle(int u){
visited[u] = visitedDFS[u] = true;
for(auto v:graph[u]){
if(!visited[v]){
if(detectCycle(v))
return true;
} else if(visited[v] && visitedDFS[v]){
return true;
}
}
return visitedDFS[u] = false;
}
public:
void addEdge(int u, int v){
graph[u].push_back(v);
graph[v];
}
bool isCyclic(){
// Loop for disconnected component(s) in a graph
for(auto u:graph){
if(!visited[u.first])
if(detectCycle(u.first)) return true;
}
return false;
}
};
int main(){
Graph G;
G.addEdge(1, 2);
G.addEdge(2, 3);
G.addEdge(3, 4);
G.addEdge(4, 5);
G.addEdge(3, 6);
G.addEdge(6, 5);
G.addEdge(7, 8);
G.addEdge(8, 9);
G.addEdge(9, 7);
G.addEdge(2, 7);
bool cyclic = G.isCyclic();
cout << (cyclic ? "Cyclic Graph" : "Not a Cyclic Graph") << endl;
}