-
Notifications
You must be signed in to change notification settings - Fork 1
/
Q7_detect_cycle_in_a_directed_graph.cpp
51 lines (42 loc) · 1.24 KB
/
Q7_detect_cycle_in_a_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
// Given a Directed Graph with V vertices (Numbered from 0 to V-1) and E edges, check whether it contains any cycle or not.
// Example 1:
// Input:
// Output: 1
// Explanation: 3 -> 3 is a cycle
// Example 2:
// Input:
// Output: 0
// Explanation: no cycle in the graph
#include<bits/stdc++.h>
using namespace std;
class Solution {
private:
bool doesCycleExist(vector<int> adj[], int src, vector<int>& visited, vector<int>& callStack) {
visited[src] = 1;
callStack[src] = 1;
for(int v : adj[src]) {
if(!visited[v]) {
if(doesCycleExist(adj, v, visited, callStack)) {
return true;
}
}
else if(callStack[v]) return true;
}
callStack[src] = 0;
return false;
}
public:
bool isCyclic(int V, vector<int> adj[]) {
vector<int> visited(V);
vector<int> callStack(V);
for(int i = 0; i < V; i++) {
if(!visited[i] && doesCycleExist(adj, i, visited, callStack)) {
return true;
}
}
return false;
}
};
// Time Complexity : O(V + E)
// Space Complexity : O(V)
// where, E = no. of edges and V = no. of vertices