-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgraph3- Detecting cycle in directed Graph.cpp
87 lines (69 loc) · 1.6 KB
/
graph3- Detecting cycle in 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Problem Statement:
--------------------
Given an directed graph, how to check if there is a cycle in the graph?
Core Logic:
-------------------
For every visited vertex ‘v’, if there is an adjacent ‘u’ such that u is
already visited and u is not parent of v, then there is a cycle in graph.
If we don’t find such an adjacent for any vertex, we say that there is no
cycle. The assumption of this approach is that there are no parallel edges
between any two vertices.
Time complexity:
-------------------
O(V+E)
Space Complexity:
-------------------
O(V)
References:
-------------------
https://www.geeksforgeeks.org/detect-cycle-directed-graph/
http://www.cplusplus.com/
https://g
#include<bits/stdc++.h>
*/
#include<bits/stdc++.h>
using namespace std;
void addEdge(vector<int>adj[],int u,int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
bool iscycle(vector<int>adj[],int v,vector<bool> &visited,vector<bool> &recursion,int parent)
{
visited[v]=true;
recursion[v]=true;
for(int u: adj[v])
{
if(!visited[u])
{
if(iscycle(adj,u,visited,recursion,v))
return true;
}
else if(recursion[u])
return true;
}
recursion[v]=false;
return false;
}
int main()
{
int n=5;
vector<int> adj[n+1];
vector<bool>visited(n+1);
vector<bool>recursion(n+1);
addEdge(adj,1,2);
addEdge(adj,2,3);
addEdge(adj,3,1);
addEdge(adj,1,4);
for(int i=1;i<n;i++)
{
visited[i]=false;
recursion[i]=false;
}
if(iscycle(adj,1,visited,recursion,-1))
cout<<"cycle";
else
cout<<"no cycle";
return 0;
}