-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathdetectCycle.cpp
62 lines (55 loc) · 1013 Bytes
/
detectCycle.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
#include<bits/stdc++.h>
using namespace std;
class Graph {
int v;
list<int> *l;
public:
Graph(int v) {
this->v = v;
l = new list<int>[v];
}
void addEdge(int x, int y) {
l[x].push_back(y);
}
bool cycle_helper(int src, bool *visited, bool *stack) {
visited[src] = true;
stack[src] = true;
for (auto nbr : l[src]) {
if (stack[nbr] == true) {
return true;
} else if (!visited[nbr]) {
bool found = cycle_helper(nbr, visited, stack);
if (found) {
return true;
}
}
}
stack[src] = false;
return false;
}
bool dfs() {
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.dfs()) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}