Repository navigation
Expand file tree
/
Copy pathGraph_cycle_detect_directed.cpp
More file actions
168 lines (147 loc) · 3.75 KB
/
Copy pathGraph_cycle_detect_directed.cpp
File metadata and controls
168 lines (147 loc) · 3.75 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
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
#include <iostream>
#include <unordered_map>
#include <list>
using namespace std;
/***********************************************************************************
* Cycle detection in directed graph
* READ : slides rough work : Graph#5.pdf also read Graph#6.pdf
* ********************************************************************************/
unordered_map<int, list<int>> adj;
unordered_map<int, bool> visited;
unordered_map<int, bool> dfsvisited;
void prepare_adjlist(int a, int b, bool direction)
{
adj[a].push_back(b);
if (direction == 0)
{
adj[b].push_back(a);
}
}
bool cycleDetectDFS(int node, unordered_map<int, bool> &visited, unordered_map<int, bool> &dfsvisited, unordered_map<int, list<int>> &adj)
{
visited[node] = true;
dfsvisited[node] = true;
for (auto neighbour : adj[node])
{
if (!visited[neighbour])
{
bool cycleDetected = cycleDetectDFS(neighbour, visited, dfsvisited, adj);
if (cycleDetected)
return true;
}
else if (dfsvisited[neighbour])
{
return true;
}
}
dfsvisited[node] = false;
return false;
}
bool detectCycleInDirectedGraph(int n, unordered_map<int, list<int>> &adj)
{
// call dfs for all components
for (int i = 1; i <= n; i++)
{
if (!visited[i])
{
bool cycleFound = cycleDetectDFS(i, visited, dfsvisited, adj);
if (cycleFound)
return true;
}
}
return false;
}
int main()
{
int n;
cout << "Enter the no of node :";
cin >> n;
int m;
cout << "Enter the no of edges :";
cin >> m;
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
prepare_adjlist(u, v, 1);
}
bool ans = detectCycleInDirectedGraph(n, adj);
if (ans)
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
return 0;
}
// input :
// Enter a number of Nodes :
// 8
// Enter a number of Edges :
// 9
// 1 2
// 2 3
// 2 4
// 4 5
// 5 6
// 6 4
// 3 7
// 3 8
// 7 8
/*****************************************************************************************
* Using Topological Sort and using bfs
* ***************************************************************************************/
// #include <unordered_map>
// #include <list>
// #include <queue>
// bool isCyclic(vector<vector<int>> &edges, int v, int e)
// {
// // create a adj list
// unordered_map<int, list<int>> adj;
// for (int i = 0; i < edges.size(); i++)
// {
// int u = edges[i][0];
// int v = edges[i][1];
// adj[u].push_back(v);
// }
// // find indegrees of all node
// vector<int> indegree(v);
// for (auto i : adj)
// {
// for (auto j : i.second)
// {
// indegree[j]++;
// }
// }
// // 0th degree walo ko queue me push kardo
// queue<int> q;
// for (int i = 0; i < v; i++)
// {
// if (indegree[i] == 0)
// q.push(i);
// }
// // do bfs
// int cnt = 0;
// while (!q.empty())
// {
// int frontNode = q.front();
// q.pop();
// // increment count
// cnt++;
// for (auto neighbour : adj[frontNode])
// {
// indegree[neighbour]--;
// if (indegree[neighbour] == 0)
// q.push(neighbour);
// }
// }
// if (cnt == v)
// {
// // means valid topological sort -> means DAG -> means No cycle
// return false;
// }
// else
// return true;
// }