forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetect cycle in a directed graph.cpp
57 lines (46 loc) · 1.28 KB
/
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
52
53
54
55
56
57
/*
Detect cycle in a directed graph
================================
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:
Output: 1
Explanation: 3 -> 3 is a cycle
Example 2:
Output: 0
Explanation: no cycle in the graph
Your task:
You don’t need to read input or print anything. Your task is to complete the function isCyclic() which takes the integer V denoting the number of vertices and adjacency list as input parameters and returns a boolean value denoting if the given directed graph contains a cycle or not.
Expected Time Complexity: O(V + E)
Expected Auxiliary Space: O(V)
Constraints:
1 ≤ V, E ≤ 105
*/
class Solution
{
public:
bool dfs(int curr, vector<int> adj[], vector<int> &rec, vector<int> &vis)
{
if (rec[curr])
return true;
rec[curr] = 1;
for (auto &i : adj[curr])
{
if (!vis[i] && dfs(i, adj, rec, vis))
return true;
}
vis[curr] = 1;
rec[curr] = 0;
return false;
}
//Function to detect cycle in a directed graph.
bool isCyclic(int V, vector<int> adj[])
{
vector<int> recStack(V, 0), visited(V, 0);
for (int i = 0; i < V; ++i)
{
if (dfs(i, adj, recStack, visited))
return true;
}
return false;
}
};