forked from krish-ag/HacktoberFest22-Repo-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code9.cpp
79 lines (70 loc) · 1.65 KB
/
code9.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
// Detect cycle in a directed graph using BFS
// here we will use topo_sort in topo sort we reduce indegrees but in cycle.. for all the vertex in
// the cycle the indegree will never be zero ... we can find out from there
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 1e9 + 7;
void add_edge(vector<int> vec[], int u, int v)
{
vec[u].push_back(v);
}
bool topo_sort(vector<int> vec[], int v)
{
vector<int> indegree(v, 0);
for (int i = 0; i < v; i++)
{
for (int j = 0; j < vec[i].size(); j++)
{
int adjacent = vec[i][j];
indegree[adjacent]++;
}
}
queue<int> q;
for (int i = 0; i < indegree.size(); i++)
{
if (indegree[i] == 0)
{
q.push(i);
}
}
int count = 0;
while (q.empty() == false)
{
int curr = q.front();
q.pop();
count++;
for (int i = 0; i < vec[curr].size(); i++)
{
int adjacent = vec[curr][i];
indegree[adjacent]--;
if (indegree[adjacent] == 0)
{
q.push(adjacent);
}
}
}
return count == v;
}
int main()
{
int v;
v = 5;
vector<int> vec[v];
// adding edges from this point
add_edge(vec, 0, 1);
add_edge(vec, 0, 3);
add_edge(vec, 1, 2);
add_edge(vec, 2, 4);
add_edge(vec, 3, 1);
add_edge(vec, 4, 3);
if (topo_sort(vec, v))
{
cout << "No cycle " << endl;
}
else
{
cout << "there is a cycle" << endl;
}
return 0;
}