-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycleDetection.cpp
174 lines (161 loc) · 4.32 KB
/
cycleDetection.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <bits/stdc++.h>
using namespace std;
class CycleInDirected
{
bool checkCycle(int node, vector<int> adj[], vector<bool> &visited, vector<bool> &dfsVisit)
{
visited[node] = true;
dfsVisit[node] = true;
for (auto it : adj[node])
if ((!visited[it] and checkCycle(it, adj, visited, dfsVisit)) or dfsVisit[it])
return true;
dfsVisit[node] = false;
return false;
}
public:
// Function to detect cycle in a directed graph.
bool isCycleByDFS(int V, vector<int> adj[])
{
// code here
vector<bool> visited(V, false), dfsVisit(V, false);
for (int i = 0; i < V; i++)
{
if (!visited[i])
if (checkCycle(i, adj, visited, dfsVisit))
return true;
}
return false;
}
// Kahn's algorithm
bool isCycleByBFS(int V, vector<int> adj[])
{
// code here
vector<int> inDegree(V, 0);
for (int i = 0; i < V; i++)
for (auto it : adj[i])
inDegree[it]++;
queue<int> q;
for (int i = 0; i < V; i++)
if (inDegree[i] == 0)
q.push(i);
int count = 0;
while (!q.empty())
{
int node = q.front();
count++;
q.pop();
for (auto it : adj[node])
{
inDegree[it]--;
if (inDegree[it] == 0)
q.push(it);
}
}
return !(count == V);
}
};
class CycleInUndirected
{
vector<bool> visited;
bool DFSUtil(int node, int parent, vector<int> adj[])
{
visited[node] = true;
for (auto it : adj[node])
{
if (!visited[it] && DFSUtil(it, node, adj))
return true;
else if (it != parent)
return true;
}
return false;
}
public:
bool isCycleByDFS(int V, vector<int> adj[])
{
vector<bool> visited(V, false);
this->visited = visited;
for (int i = 0; i < V; i++)
{
if (!visited[i] && DFSUtil(i, -1, adj))
return true;
}
return false;
}
bool isCycleByBFS(int V, vector<int> adj[])
{
// Code here
vector<bool> visited(V, false);
for (int i = 0; i < V; i++)
{
if (!visited[i])
{
queue<pair<int, int>> que;
que.push({i, -1});
visited[i] = true;
while (!que.empty())
{
int node = que.front().first;
int prevNode = que.front().second;
que.pop();
for (auto it : adj[node])
if (!visited[it])
{
que.push({it, node});
visited[it] = true;
}
else
if (it != prevNode)
return true;
}
}
}
return false;
}
};
int main()
{
bool directed;
cout << "Directed, then press 1:\nUndirected, then press 0:\n";
cin >> directed;
int V, E;
cout << "Enter number of vertices and edges: " << endl;
cin >> V >> E;
if (directed)
{
vector<int> adj[V];
cout << "please enter u -> v :\n";
for (int i = 0; i < E; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
CycleInDirected obj;
bool isCycle = obj.isCycleByDFS(V, adj);
if (isCycle)
cout << "There is a cycle" << endl;
else
cout << "There is no cycle" << endl;
}
else
{
vector<int> adj[V];
vector<vector<int>> edges(E, vector<int>(3));
cout << "please enter as: vertex vertex :\n";
for (int i = 0; i < E; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
CycleInUndirected obj;
bool isCycle = obj.isCycleByBFS(V, adj);
if (isCycle)
cout << "There is a cycle" << endl;
else
cout << "There is no cycle" << endl;
}
return 0;
}
// } Driver Code Ends