-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJohnson's Algorithm.cpp
More file actions
258 lines (201 loc) · 5.5 KB
/
Johnson's Algorithm.cpp
File metadata and controls
258 lines (201 loc) · 5.5 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include<bits/stdc++.h>
using namespace std;
#define loi long long
#define lod long double
#define doubel double
#define pb push_back
#define run ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define mod 1000000007
#define lim 5000000000000000000
#define in(ar, n) for(loi i = 0; i < n; i++) cin >> ar[i];
#define out(ar, n) for (loi i = 0; i < n; i++) cout << ar[i] << " "; cout << "\n";
#define rep(i, n) for(loi i = 0; i < n; i++)
#define rep1(i, n) for(loi i = 1; i <= n; i++)
#define rev(i, n) for(loi i = n - 1; ~i; i--)
#define rep3(i, l, r) for(loi i = l; i <= r; i++)
#define rev3(i, r, l) for(loi i = r; i >= l; i--)
#define rep4(i, r, l) for(loi i = r; i >= l; i--)
#define all(x) x.begin(), x.end ()
#define mkp make_pair
#define vii vector<loi>
#define mii map<loi, loi>
#define sii set<loi>
#define pii pair<loi, loi>
#define mxdepth 23 // FOR LCA til N = 1e6
#define mxn 2000001
/**
*Author Deepak Mehrotra (StealthX2k20)
*Finding all Simple Cycles in a Directed Graph using Johnson's Algorithm
*Time Complexity O(E + V) * (c + 1) where c is the number of cycles in the directed graph
*Space Complexity O(E + V + s) where s is the sum of length of all cycles in the graph
*References : https://github.com/mission-peace/interview/blob/master/src/com/interview/graph/AllCyclesInDirectedGraphJohnson.java
**/
mii node_num;
map < loi, vii > comp_num;
void dfs(vii graph[], loi root, loi vis[], stack <loi> &stk)
{
vis[root] = 1;
rep(i, graph[root].size())
{
if(!vis[graph[root][i]])
dfs(graph, graph[root][i], vis, stk);
}
stk.push(root);
return;
}
void tour(vii graph[], loi root, loi vis[], vii &scc)
{
vis[root] = 1;
scc.pb(root);
rep(i, graph[root].size())
{
if(!vis[graph[root][i]])
tour(graph, graph[root][i], vis, scc);
}
return;
}
//Kosaraju's Algorithm for finding SCC in a directed graph
void Kosaraju(vii graph[], vii rgraph[], loi n, loi m, vector < vii > &SCC)
{
stack < loi > stk;
loi node;
loi vis[n];
memset(vis, 0, sizeof(vis));
rep(i, n)
{
if(!vis[i])
{
dfs(graph, i, vis, stk);
}
}
memset(vis, 0, sizeof(vis));
vii scc;
loi cur = 0;
while(!stk.empty())
{
node = stk.top();
stk.pop();
if(!vis[node])
{
scc.clear();
tour(rgraph, node, vis, scc);
sort(all(scc));
SCC.pb(scc);
rep(i, scc.size())
node_num[scc[i]] = cur;
comp_num[cur++] = scc;
}
}
return;
}
void unlock_all(sii &st, mii &mp, loi root)
{
if(mp.find(root) == mp.end() or mp[root] == -1)
{
if(st.find(root) != st.end())
st.erase(st.find(root));
return;
}
unlock_all(st, mp, mp[root]);
st.erase(st.find(root));
mp[root] = -1;
return;
}
//Johnson's Algorithm for Cycle counting in a Directed Graph
void cycleDFS(vii graph[], loi root, loi vis[], stack <loi> &stk, sii &st, mii &mp, loi start, loi &cnt)
{
stk.push(root);
st.insert(root);
rep(i, graph[root].size())
{
if(graph[root][i] != start and st.find(graph[root][i]) == st.end())
{
cycleDFS(graph, graph[root][i], vis, stk, st, mp, start, cnt);
}
else if(graph[root][i] == start)
{
cnt++;
stack <loi> stk1;
stk1 = stk;
while(!stk1.empty())
vis[stk1.top()] = cnt, stk1.pop();
}
else mp[graph[root][i]] = root;
}
stk.pop();
if(vis[root])
unlock_all(st, mp, root);
return;
}
int main()
{
loi n, m, u, v, node, vertx;
//n -> number of vertices
//m -> number of edges
cin >> n >> m;
vii graph[n], rgraph[n];
rep(i, m)
{
cin >> u >> v;
u--, v--;
graph[u].pb(v);
rgraph[v].pb(u); // this reverse graph is being made to find the SCCs of the directed graph using Kosaraju's Algorithm
}
vector < vii > SCC;
// This is used to store the nodes in a storngly-connected-component
vii scc;
Kosaraju(graph, rgraph, n, m, SCC);
loi cnt = 0;
vii dummy[n]; // This is used to store the subgraph of the vertices belonging to the same strongly connected component
loi vis[n];
stack <loi> stk;
sii st;
mii mp;
rep(i, n)
{
node = i;
scc = comp_num[node_num[node]];
if(scc.size() == 1) continue;
rep(j, scc.size())
{
vertx = scc[j];
rep(k, graph[vertx].size())
{
if(binary_search(all(scc), graph[vertx][k]))
dummy[vertx].pb(graph[vertx][k]);
}
}
while(!stk.empty()) stk.pop();
st.clear();
memset(vis, 0, sizeof(vis));
cycleDFS(dummy, node, vis, stk, st, mp, node, cnt);
rep(j, n) dummy[j].clear();
rep(j, n)
{
if(j == node) continue;
rep(k, graph[j].size())
{
if(graph[j][k] != node)
{
dummy[j].pb(graph[j][k]);
}
}
}
rep(j, n) graph[j] = dummy[j];
SCC.clear();
node_num.clear();
comp_num.clear();
rep(j, n) rgraph[j].clear();
rep(j, n)
{
rep(k, graph[j].size())
{
rgraph[graph[j][k]].pb(j);
}
}
Kosaraju(graph, rgraph, n, m, SCC);
rep(j, n) dummy[j].clear();
}
cout << "Total cycles are : " << cnt << "\n";
return 0;
}