forked from Vishal1003/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycleDetectionDirectedGraph.cpp
More file actions
84 lines (75 loc) · 1.51 KB
/
CycleDetectionDirectedGraph.cpp
File metadata and controls
84 lines (75 loc) · 1.51 KB
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
// Author: rishab_1128
//Cycle Detection in a Directed Graph using DFS
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define SIZE 100001
const int MOD = 1000000007;
vector<int>adj[SIZE];
bool vis[SIZE];
bool rec[SIZE];
bool dfs_detect_cycle(int node)
{
if(!vis[node])
{
//Mark the current node as visited and part of recursion stack
vis[node]=1;
rec[node]=1;
//Recur for all the vertices adjacent to this vertex
for(int child: adj[node])
{
if(!vis[child]&&dfs_detect_cycle(child))
return true;
else if(rec[child])
return true;
}
}
rec[node]=0; // remove the vertex from recursion stack
return false;
}
void solve()
{
//Creating the adjacency list
adj[1].push_back(2);
adj[1].push_back(3);
adj[2].push_back(3);
adj[3].push_back(1);
adj[4].push_back(4);
// Mark all the vertices as not visited and not part of recursion stack
memset(vis,0,sizeof(vis));
memset(rec,0,sizeof(rec));
for(int i=1; i<=4; i++)
{
if(!vis[i])
{
//Call the recursive function to detect cycle in different DFS trees
int flag=dfs_detect_cycle(i);
if(flag)
{
cout<<"CYCLE DETECTED"<<endl;
return;
}
else
{
cout<<"NO CYCLE DETECTED"<<endl;
return;
}
}
}
}
int32_t main()
{
/*std::ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif*/
int t=1;
//cin>>t;
while(t--)
{
solve();
}
return 0;
}