-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSCC.cpp
575 lines (442 loc) · 15.6 KB
/
SCC.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
* SCC.cpp
*
* Author: Ervin Dervishaj
* Email: [email protected]
*
*/
#include "SCC.hpp"
#include <random>
#include <vector>
#include <stack>
#include <iostream>
#include <ostream>
#include <algorithm>
/* Print one row for each vertex_t in the format v --> u1 u2 u3 */
void print_graph(const DiGraph& g, std::ostream& os=std::cout){
DiGraph::vertex_iterator v, vend;
DiGraph::out_edge_iterator e, eend;
// Go over each vertex_t
for(boost::tie(v, vend) = vertices(g); v != vend; ++v){
os << g[*v].index << " --> ";
for(boost::tie(e, eend) = out_edges(*v, g); e != eend; ++e) {
vertex_t w = boost::target(*e, g);
os << g[w].index << " ";
}
os << std::endl;
}
}
/* Creates vecor of components subgraphs SCC from the component identifiers given by Pearce's algorithms. */
std::vector<DiGraph> create_scc (std::vector<int> component_ids, DiGraph g) {
std::set<int> components(component_ids.begin(), component_ids.end());
std::vector<DiGraph> scc(components.size());
std::vector<std::set<int>> component_vertices(components.size());
std::vector<vertex_t> vertices(component_ids.size());
//Go over each component
int comp_id = 0;
std::set<int>::iterator it_c;
for (it_c = components.begin(); it_c != components.end(); ++it_c, comp_id++){
//Insert vertices into respective component
for (int j = 0; j < component_ids.size(); j++) {
if (component_ids[j] == *it_c) {
Vertex u = {j, false};
vertices[j] = boost::add_vertex(u, scc[comp_id]);
component_vertices[comp_id].insert(j);
}
}
//Go over vertices of a component
std::set<int>::iterator it;
for (it = component_vertices[comp_id].begin(); it != component_vertices[comp_id].end(); ++it){
int u_idx = *it;
vertex_t u_g = boost::vertex(u_idx, g);
vertex_t u = vertices[u_idx];
//Copy the edges of the main graph whose both vertices are in the same component
DiGraph::out_edge_iterator e, eend;
for(boost::tie(e, eend) = out_edges(u_g, g); e != eend; ++e) {
vertex_t w_g = boost::target(*e, g);
int w_idx = g[w_g].index;
if (component_vertices[comp_id].find(w_idx) != component_vertices[comp_id].end()) {
vertex_t w = vertices[w_idx];
boost::add_edge(u, w, scc[comp_id]);
}
}
}
}
return scc;
}
DiGraph rand_graph(int n_vertices, float edge_prob, int seed){
// Initialize seed for reproducibility
std::mt19937 eng(seed);
// Initialize uniform distribution number generator
std::uniform_real_distribution<float> distribution(0.0, 1.0);
// Initialize DiGraph object
DiGraph g;
// Insert the vertices in the graph
for(int i = 0; i < n_vertices; i++){
Vertex u = {i, false};
boost::add_vertex(u, g);
}
// For each pair of vertices, add edge with probability edge_prob
for(int i = 0; i < n_vertices; i++){
vertex_t u = boost::vertex(i, g);
for(int j = 0; j < n_vertices; j++){
vertex_t v = boost::vertex(j, g);
if(distribution(eng) < edge_prob)
boost::add_edge(u, v, g);
}
}
return g;
}
DiGraph rand_graph(int n_vertices, int m_edges, int seed){
// Initialize seed for reproducibility
std::mt19937 eng(seed);
// Initialize uniform distribution number generator
std::uniform_int_distribution<int> distribution(0, n_vertices-1);
// Initialize DiGraph object
DiGraph g;
// Insert the vertices in the graph
for(int i = 0; i < n_vertices; i++){
Vertex u = {i, false};
boost::add_vertex(u, g);
}
int inserted_edges = 0;
// Insert edges by sampling their endpoints
while(inserted_edges < m_edges){
// Sample source & target vertices
int u_idx = distribution(eng);
int v_idx = distribution(eng);
vertex_t u = boost::vertex(u_idx, g);
vertex_t v = boost::vertex(v_idx, g);
// If the edge is not already in the graph -- do not allow parallel edges
if(!boost::edge(u, v, g).second){
boost::add_edge(u, v, g);
inserted_edges++;
}
}
return g;
}
/* Starting from another graph, we construct a random graph by adding non-parallel edges following Erdős-Rényi model */
DiGraph g_rand_graph(int n_vertices, float edge_prob, int seed, DiGraph& g){
// If n_vertices is less than the number of vertices g already has, then return g
int current_vertices = boost::num_vertices(g);
int residual_vertices = n_vertices - current_vertices;
if(residual_vertices < 0)
return g;
// Initialize seed for reproducibility
std::mt19937 eng(seed);
// Initialize uniform distribution number generator
std::uniform_real_distribution<float> distribution(0.0, 1.0);
// Insert the vertices in the graph
for(int i = 0; i < residual_vertices; i++){
Vertex u = {i + current_vertices, false};
boost::add_vertex(u, g);
}
// For each pair of vertices, add edge with probability edge_prob if it does not exist
for(int i = 0; i < current_vertices + residual_vertices; i++){
vertex_t u = boost::vertex(i, g);
for(int j = 0; j < current_vertices + residual_vertices; j++){
vertex_t v = boost::vertex(j, g);
if(distribution(eng) < edge_prob && !boost::edge(u, v, g).second)
boost::add_edge(u, v, g);
}
}
return g;
}
/* A directed cycle is a scc, so we constract a graph with n cycles */
DiGraph n_rand_graph(const std::vector<int>& n_component, int n_vertices, float edge_prob, bool rand_components, bool rand_graph, int seed){
// Initialize seed for reproducibility
std::mt19937 eng(seed);
// Auxiliary variables
vertex_t v, v_prev, v_init;
int next_idx = -1;
int r_vertices;
DiGraph g;
for(int i = 0; i < n_component.size(); i++){
// Keep track of the first added vertex of the component
v_init = boost::add_vertex({++next_idx, false}, g);
v_prev = v_init;
// If random_components is set, we sample different number of vertices per component
if(rand_components) {
std::uniform_int_distribution<int> distribution(1, 10);
r_vertices = distribution(eng);
}
else r_vertices = n_component[i]; // otherwise use provided number of vertices per component
// Add the vertices keeping track of the previous added vertex so to connect them together
for(int j = 1; j < r_vertices; j++){
v = boost::add_vertex({++next_idx, false}, g);
boost::add_edge(v_prev, v, g);
v_prev = v;
}
// Close the cycle to create the component
boost::add_edge(v, v_init, g);
}
// Call g_rand_graph if you want to randomize starting from the n_components
if(rand_graph) return g_rand_graph(n_vertices, edge_prob, seed, g);
else return g;
}
/* Main loop of Tarjan Algorithm */
void visit(std::vector<DiGraph>& scc, std::stack<vertex_t>& stack, std::vector<bool>& inComponent, std::vector<int>& root, DiGraph& g, vertex_t v){
// Auxiliary edge_iterator variables
DiGraph::out_edge_iterator e, eend;
g[v].visited = true;
int v_idx = g[v].index;
root[v_idx] = v_idx;
stack.push(v);
// Go over all neighbors of v
for(boost::tie(e, eend) = out_edges(v, g); e != eend; ++e){
vertex_t w = boost::target(*e, g);
int w_idx = g[w].index;
if(!g[w].visited)
visit(scc, stack, inComponent, root, g, w);
if(!inComponent[w_idx])
root[v_idx] = (root[v_idx] <= root[w_idx]) ? root[v_idx] : root[w_idx];
}
// Component identified, store in vector scc
if(root[v_idx] == v_idx){
DiGraph h;
int w_id = -1;
while(w_id != g[v].index){
vertex_t w = stack.top();
stack.pop();
w_id = g[w].index;
inComponent[w_id] = true;
root[w_id] = v_idx;
boost::add_vertex({g[w].index, g[w].visited}, h);
}
// Construct the graph of the component
scc.push_back(h);
}
}
std::vector<DiGraph> tarjan_scc(DiGraph g){
// Number of vertices in g
int num_vertices = boost::num_vertices(g);
// Vector of SCC to be returned by the algorithm
std::vector<DiGraph> scc;
// DS used by Tarjan
std::stack<vertex_t> stack;
std::vector<bool> inComponent(num_vertices, false);
std::vector<int> root(num_vertices, num_vertices + 1);
DiGraph::vertex_iterator v, vend;
// Go over all vertices
for(boost::tie(v, vend) = vertices(g); v != vend; ++v) {
if(!g[*v].visited) {
visit(scc, stack, inComponent, root, g, *v);
}
}
return scc;
}
/* Main loop of Nuutila first improvement */
void visit1(std::vector<DiGraph>& scc, std::stack<vertex_t>& stack, std::vector<bool>& inComponent, std::vector<int>& root, DiGraph& g, vertex_t v){
// Auxiliary edge_iterator variables
DiGraph::out_edge_iterator e, eend;
g[v].visited = true;
int v_idx = g[v].index;
root[v_idx] = v_idx;
// Go over all neighbors of v
for(boost::tie(e, eend) = out_edges(v, g); e != eend; ++e){
vertex_t w = boost::target(*e, g);
int w_idx = g[w].index;
if(!g[w].visited)
visit1(scc, stack, inComponent, root, g, w);
if(!inComponent[w_idx])
root[v_idx] = (root[v_idx] <= root[w_idx]) ? root[v_idx] : root[w_idx];
}
// Component identified, store in vector scc
if(root[v_idx] == v_idx){
DiGraph h;
inComponent[v_idx] = true;
boost::add_vertex({v_idx, g[v].visited}, h);
while(stack.size() > 0 && g[stack.top()].index > v_idx){
vertex_t w = stack.top();
int w_idx = g[w].index;
stack.pop();
inComponent[w_idx] = true;
boost::add_vertex({w_idx, g[w].visited}, h);
}
// Construct the graph of the component
scc.push_back(h);
} else stack.push(v);
}
std::vector<DiGraph> nuutila1_scc(DiGraph g){
// Number of vertices in g
int num_vertices = boost::num_vertices(g);
// Vector of SCC to be returned by the algorithm
std::vector<DiGraph> scc;
// DS used by nuutila1
std::stack<vertex_t> stack;
std::vector<bool> inComponent(num_vertices, false);
std::vector<int> root(num_vertices, num_vertices + 1);
DiGraph::vertex_iterator v, vend;
// Go over all vertices
for(boost::tie(v, vend) = vertices(g); v != vend; ++v) {
if(!g[*v].visited) {
visit1(scc, stack, inComponent, root, g, *v);
}
}
return scc;
}
/* Main loop of Nuutila second improvement */
void visit2(std::vector<int>& scc, std::vector<int>& stack, std::vector<bool>& inComponent, std::vector<int>& root, DiGraph& g, vertex_t v){
// Auxiliary edge_iterator variables
DiGraph::out_edge_iterator e, eend;
g[v].visited = true;
int v_idx = g[v].index;
root[v_idx] = v_idx;
// Go over all neighbors of v
for(boost::tie(e, eend) = out_edges(v, g); e != eend; ++e){
vertex_t w = boost::target(*e, g);
int w_idx = g[w].index;
if(!g[w].visited)
visit2(scc, stack, inComponent, root, g, w);
if(!inComponent[root[w_idx]])
root[v_idx] = (root[v_idx] <= root[w_idx]) ? root[v_idx] : root[w_idx];
}
// Component identified, store in vector scc
if(root[v_idx] == v_idx){
DiGraph h;
if(stack.back() >= v_idx){
while(!stack.empty() && stack.back() >= v_idx){
int w_idx = stack.back();
stack.pop_back();
inComponent[w_idx] = true;
}
} else {
inComponent[v_idx] = true;
//scc.push_back(v_idx);
}
} else {
if(std::find(stack.begin(), stack.end(), root[v_idx]) == stack.end()) {
stack.push_back(root[v_idx]);
scc.push_back(v_idx);
}
}
}
std::vector<int> nuutila2_scc(DiGraph g){
// Number of vertices in g
int num_vertices = boost::num_vertices(g);
// Vector of SCC to be returned by the algorithm
std::vector<int> scc;
// DS used by nuutila2
std::vector<int> stack;
std::vector<bool> inComponent(num_vertices, false);
std::vector<int> root(num_vertices, num_vertices + 1);
DiGraph::vertex_iterator v, vend;
stack.push_back(-1);
if(boost::num_vertices(g) != num_vertices)
exit(1);
// Go over all vertices
for(boost::tie(v, vend) = vertices(g); v != vend; ++v) {
if(!g[*v].visited) {
visit2(scc, stack, inComponent, root, g, *v);
}
}
return scc;
}
/* Main loop of Pearce's first improvement */
void visitpearce1(std::vector<int>& rindex, std::vector<int>& S, std::vector<bool>& inComponent, DiGraph& g, vertex_t v, int& c, int& index_p){
// Auxiliary edge_iterator variables
DiGraph::out_edge_iterator e, eend;
int root = true;
g[v].visited = true;
int v_idx = g[v].index;
rindex[v_idx] = index_p;
index_p++;
// Go over all neighbors of v
for(boost::tie(e, eend) = out_edges(v, g); e != eend; ++e){
vertex_t w = boost::target(*e, g);
int w_idx = g[w].index;
if(!g[w].visited)
visitpearce1(rindex, S, inComponent, g, w, c, index_p);
if(!inComponent[w_idx] && (rindex[w_idx] < rindex[v_idx])) {
rindex[v_idx] = rindex[w_idx];
root = false;
}
}
// Component identified, assign component identifiers to the corresponding vertices.
if (root) {
inComponent[v_idx] = true;
while (!S.empty() && (rindex[v_idx] <= rindex[S.back()])){
int w_idx = S.back();
S.pop_back();
rindex[w_idx] = c;
inComponent[w_idx] = true;
}
rindex[v_idx] = c;
c++;
} else {
S.push_back(v_idx);
}
}
std::vector<int> nuutila2_scc(DiGraph g){
// Number of vertices in g
int num_vertices = boost::num_vertices(g);
// Vector of component identifiers to be returned by the algorithm.
std::vector<int> rindex(num_vertices);
int c = 0;
int index_p = 0;
// DS used by nuutila2
std::vector<int> S;
std::vector<bool> inComponent(num_vertices, false);
DiGraph::vertex_iterator v, vend;
// Go over all vertices
for(boost::tie(v, vend) = vertices(g); v != vend; ++v) {
if(!g[*v].visited) {
visitpearce1(rindex, S, inComponent, g, *v, c, index_p);
}
}
return rindex;
}
/* Main loop of Pearce's second improvement */
void visitpearce2(std::vector<int>& rindex, std::vector<int>& S, DiGraph& g, vertex_t v, int& c, int& index_p){
// Auxiliary edge_iterator variables
DiGraph::out_edge_iterator e, eend;
int root = true;
int v_idx = g[v].index;
rindex[v_idx] = index_p;
index_p++;
// Go over all neighbors of v
for(boost::tie(e, eend) = out_edges(v, g); e != eend; ++e){
vertex_t w = boost::target(*e, g);
int w_idx = g[w].index;
if(rindex[w_idx]==0)
visitpearce2(rindex, S, g, w, c, index_p);
if(rindex[w_idx] < rindex[v_idx]) {
rindex[v_idx] = rindex[w_idx];
root = false;
}
}
// Component identified, assign component identifiers to the corresponding vertices.
if (root) {
index_p--;
while (!S.empty() && (rindex[v_idx] <= rindex[S.back()])){
int w_idx = S.back();
S.pop_back();
rindex[w_idx] = c;
index_p--;
}
rindex[v_idx] = c;
c--;
} else {
S.push_back(v_idx);
}
}
std::vector<int> pearce2_scc(DiGraph g){
//No need of .visited in vertex_t
// Number of vertices in g
int num_vertices = boost::num_vertices(g);
// Vector of component identifiers to be returned by the algorithm.
std::vector<int> rindex(num_vertices);
int c = num_vertices-1;
int index_p = 1;
// DS used by pearce2
std::vector<int> S;
DiGraph::vertex_iterator v, vend;
// Go over all vertices
for(boost::tie(v, vend) = vertices(g); v != vend; ++v) {
int v_idx = g[*v].index;
if(rindex[v_idx]==0) {
visitpearce2(rindex, S, g, *v, c, index_p);
}
}
return rindex;
}