-
Notifications
You must be signed in to change notification settings - Fork 47
/
Detect_cycle_directed_graph.c++
68 lines (62 loc) Β· 1.29 KB
/
Detect_cycle_directed_graph.c++
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
#include<bits/stdc++.h>
using namespace std;
void addedge(vector<vector<int>> &adj,int u,int v)
{
adj[u].push_back(v);
}
void printgraph(vector<vector<int>>&adj)
{
int i=0;
for(auto x: adj)
{
cout<<i<<" -> ";
for( auto l:x)
{
cout<< l<<" | ";
}
cout<<endl;
++i;
}
}
bool dfs_rec(vector<vector<int>>&adj,int u,vector<bool>&visited,vector<bool>&instack)
{
//process the vertex
visited[u]=true;
instack[u]=true;
for(auto x:adj[u])
{
if(visited[x]==false && dfs_rec(adj,x,visited,instack))
return true;
else if(instack[x])return true;
}
instack[u]=false;
return false;
}
bool dfs(vector<vector<int>>&adj,int size)
{
vector<bool>visited(size,false);
vector<bool>instack(size,false);
for(int i=0;i<size;i++)
{
if(visited[i]==false)
if(dfs_rec(adj,i,visited,instack))
return true;
}
return false;
}
int main()
{
int size=6;
vector<vector<int>>adj(size);
addedge(adj,0,1);
addedge(adj,2,1);
addedge(adj,2,3);
addedge(adj,3,4);
addedge(adj,4,5);
addedge(adj,5,3);
printgraph(adj);
if(dfs(adj,size))cout<<"cycle found"<<endl;
else
cout<<"cycle not found"<<endl;
return 0;
}