-
Notifications
You must be signed in to change notification settings - Fork 4
/
graphUtils.cpp
2344 lines (2105 loc) · 74.1 KB
/
graphUtils.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "graphUtils.h"
//typedef std::pair<int, int> pairs;
std::vector<std::vector<std::vector<int>>> cyclic_innode, cyclic_outnode;
std::vector<std::vector<int64_t>> D_cyclic;
int edges_rm=0;
bool cyclic_flag=false;
int PathCover_LB, PathCover_UB;
const int INF = 1e9;
std::vector<std::vector<int>> path_MF(3);
//Cyclic to DAG conversion
//Function to get the vertex order for SCC computation Using DFS
void Fillorder(std::vector<std::vector<int>> &un_adj, std::vector<bool> &visited, int u, std::stack<int> &stack)
{
visited[u]=true;
for (auto v:un_adj[u])
{
if (!visited[v])
{
Fillorder(un_adj, visited, v, stack);
}
}
//// std::cerr<<u<<" ";
stack.push(u);
}
//Function to get the Transpose of graph
void Transpose_G(std::vector<std::vector<int>> &un_adj, std::vector<std::vector<int>> &T_adj, int n_vtx)
{
for (int v = 0; v < n_vtx; ++v)
{
for (auto x : un_adj[v])
T_adj[x].push_back(v);
}
}
//DFS to find SCC
void DFS_SCC(std::vector<std::vector<int>> &adj, std::vector<bool> &visited, int u, std::vector<int> &component, int num_components, std::stack<int> &stack)
{
visited[u]=true;
//// std::cerr<<u<<" ";
component[u]=num_components;
for (auto x : adj[u])
if(!visited[x])
DFS_SCC(adj, visited, x, component, num_components, stack);
}
/*void SCC_DFS(std::vector<std::vector<int>> &adj, std::vector<bool> &visited, std::vector<bool> &mask, int u, std::vector<int> &v_seq)
{
if(mask[u]==true)
{
v_seq.push_back(u);
visited[u]=true;
for(auto &v : adj[u])
if(visited[v]==false)
SCC_DFS(adj, visited, mask, v, v_seq);
}
}*/
//Computing SCC
void SCC(std::vector<std::vector<int> > &adj, int n_vtx)
{
int vtx;
std::stack<int> stack, dfs_order;
size_t num_components, comp_size;
std::vector<std::vector<int> > conn_comp, T_adj(n_vtx), adj_(n_vtx), comp_adj;
std::vector<int> component, v_seq, s_index(n_vtx), v_sequence;
for (int v = 0; v < n_vtx; ++v)
{
for (auto x : adj[v])
{
if(v!=x)
adj_[v].push_back(x);
//edge++;
}
}
//printGraph(adj, n_vtx);
adj.clear();
adj.resize(n_vtx);
//printGraph(adj_, n_vtx);
//Get the vertex order
std::vector<bool> visited(n_vtx,false), mask(n_vtx, false);
for (int u = 0; u < n_vtx; u++)
{
if (visited[u] == false)
{
Fillorder(adj_, visited, u, stack);
}
}
visited.clear();
//get the transpose graph
Transpose_G(adj_, T_adj, n_vtx);
//Get the SCC
for (int i = 0; i < n_vtx; i++)
visited.push_back(false);
num_components = 0;
component.resize(n_vtx);
while (!stack.empty())
{
int s = stack.top();
stack.pop();
if(visited[s]== false)
{
DFS_SCC(T_adj, visited, s, component, num_components, stack);
num_components++;
//// std::cerr<<"\n";
}
}
visited.clear();
for (int i = 0; i < n_vtx; i++)
visited[i]=false;
// Storing Connected Components
conn_comp.resize(num_components);
for (int i = 0; i < n_vtx; i++)
{
conn_comp[component[i]].push_back(i); // Add Vertex to it's component
}
/*// std::cerr<<"Total number of SCCs are: "<<num_components<<"\n";
for(size_t i=0; i< num_components; i++)
{
// std::cerr<<"SCC "<<i<<" : [";
for (auto &v: conn_comp[i])
{
// std::cerr<<v<<" ";
}
// std::cerr<<"]\n";
}*/
/*for(size_t i=0; i< num_components; i++)
{
if(conn_comp[i].size() == 1)
{
for(auto &j : conn_comp[i])
for(auto &v : adj_[j])
adj[j].push_back(v);
}
else
{
for(auto &k : conn_comp[i])
mask[k]=true;
for (auto &a: conn_comp[i])
{
if (visited[a]==false)
SCC_DFS(adj_, visited, mask, a, v_seq);
}
//vtx=conn_comp[i][0];
//// std::cerr<<"starting vertex"<<vtx;
//SCC_DFS(adj_, visited, mask, vtx, v_seq);
for(size_t j=0; j< conn_comp[i].size(); j++)
{
//// std::cerr<<"\n"<<v_seq[j]<<" ";
s_index[v_seq[j]]=j;
}
for(auto &k : conn_comp[i])
for(auto &v : adj_[k])
{
if(mask[v]==false)
adj[k].push_back(v);
else if(s_index[k] < s_index[v])
adj[k].push_back(v);
}
for(auto &k : conn_comp[i])
mask[k]=false;
v_seq.clear();
visited.clear();
for (int i = 0; i < n_vtx; i++)
visited[i]=false;
}
}*/
int num_cid=num_components;
std::vector<std::vector<int> > component_idx(num_cid), idx_component(num_cid);
// Map Components
for (size_t cid = 0; cid < num_cid; cid++)
{
//// std::cerr<<conn_comp[cid].size()<<" ";
component_idx[cid].resize(conn_comp[cid].size());
idx_component[cid].resize(n_vtx);
for (size_t j = 0; j < conn_comp[cid].size(); j++)
{
//// std::cerr<<conn_comp[cid][j]<<" ";
component_idx[cid][j] = conn_comp[cid][j]; // map global to local
//// std::cerr<<component_idx[cid][j]<<"\n";
idx_component[cid][conn_comp[cid][j]] = j; // map local to global
//// std::cerr<<idx_component[cid][conn_comp[cid][j]]<<"\n";
}
}
// Adjacency list for all strongly connected components
std::vector<std::vector<int>> *adj_scc;
adj_scc = new std::vector<std::vector<int>>[num_components];
for (size_t cid = 0; cid < num_cid; cid++)
{
for (size_t i=0; i<conn_comp[cid].size(); i++)
{
visited[conn_comp[cid][i]]=true;
}
adj_scc[cid].resize(conn_comp[cid].size());
for (auto &j : conn_comp[cid])
{
for (size_t k = 0; k < adj_[j].size(); k++)
{
if(visited[adj_[j][k]]==true)// && idx_component[cid][j]!= idx_component[cid][adj_[j][k]])
{
adj_scc[cid][idx_component[cid][j]].push_back(idx_component[cid][adj_[j][k]]);
}
}
}
for (size_t i=0; i<conn_comp[cid].size(); i++)
{
visited[conn_comp[cid][i]]=false;
}
}
visited.clear();
//Printing adjancency list of each SCC
for (size_t cid = 0; cid < num_cid; cid++)
{
comp_size=adj_scc[cid].size();
comp_adj.resize(comp_size);
std::vector<bool> visit(comp_size, false);
std::vector<int> s_index(comp_size, 0);
for (size_t j = 0; j < comp_size; j++)
{
for (auto &k : adj_scc[cid][j])
{
comp_adj[j].push_back(k);
}
}
//printGraph(comp_adj, comp_size);
if(comp_size > 1)
{
/*for (size_t u = 0; u < comp_size; u++)
{
if (visit[u] == false)
{
Fillorder(comp_adj, visit, u, dfs_order);
}
}*/
Fillorder(comp_adj, visit, 0, dfs_order);
for (size_t u = 0; u < comp_size; u++)
{
v_sequence.push_back(dfs_order.top());
dfs_order.pop();
}
for (size_t i = 0; i < comp_size; i++)
{
//// std::cerr<<v_sequence[i]<<" ";
s_index[v_sequence[i]]=i;
}
adj_scc[cid].clear();
adj_scc[cid].resize(comp_size);
for (unsigned int v = 0; v < comp_size; ++v)
{
for (auto x : comp_adj[v])
{
if(s_index[v] < s_index[x])
adj_scc[cid][v].push_back(x);
}
}
comp_adj.clear();
visit.clear();
s_index.clear();
v_sequence.clear();
}
/*// std::cerr<<"DAG converted components \n";
for (size_t j = 0; j < comp_size; j++)
{
// std::cerr<<j;
for (auto &k : adj_scc[cid][j])
{
// std::cerr<<"->"<<k;
}
// std::cerr<<"\n";
}*/
}
for (int i = 0; i < n_vtx; i++)
{
visited[i]=false;
}
//Converted DAG
adj.clear();
adj.resize(n_vtx);
for (size_t cid = 0; cid < num_cid; cid++)
{
for (size_t i=0; i<conn_comp[cid].size(); i++)
{
visited[conn_comp[cid][i]]=true;
}
for (auto &j : conn_comp[cid])
{
for(auto &v : adj_[j])
if(visited[v]==false)
adj[j].push_back(v);
for(auto &u : adj_scc[cid][idx_component[cid][j]])
adj[j].push_back(component_idx[cid][u]);
}
for (size_t i=0; i<conn_comp[cid].size(); i++)
{
visited[conn_comp[cid][i]]=false;
}
}
//printGraph(adj, n_vtx);
}
graphUtils::graphUtils(gfa_t *g)
{
this->g = g;
}
// Read and store the Graph in Adjacency list
void graphUtils::read_graph()
{
// std::cerr<<"Starting to read the graph\n";
uint32_t v;
n_vtx = gfa_n_vtx(g);
// Resize node_len
node_len.resize(n_vtx, 0);
// Array of vectors
adj_ = new std::vector<int>[n_vtx];
/* Node_len */
for (int v = 0; v < n_vtx/2; v++)
{
gfa_seg_t segment = (g)->seg[v];
int len = segment.len;
node_len[2*v] = len;
node_len[2*v + 1] = len;
}
// look for all the edges , if the sum of all the
// edges are zero then, that's a linear reference
u_int32_t num_edges = 0;
for (v = 0; v < n_vtx; v++)
{
gfa_arc_t *av = gfa_arc_a(g, v);
int n_edges = gfa_arc_n(g, v);
int v_ = av->v_lv >> 32;
// std::cerr << " node : " << v << " node_len : " << node_len[v] << std::endl;
for (int i = 0; i < n_edges; i++)
{
num_edges++;
}
}
for (v = 0; v < n_vtx; v++)
{
gfa_arc_t *av = gfa_arc_a(g, v);
int n_edges = gfa_arc_n(g, v);
if (num_edges == 0) // Linear Reference
{
lin_ref = 1; // Mark as a linear reference
}else
{
int v_ = av->v_lv >> 32;
// std::cerr << " node : " << v << " node_len : " << node_len[v] << std::endl;
for (int i = 0; i < n_edges; i++)
{
uint32_t w = av[i].w;
adj_[v_].push_back(w);
}
}
}
//std::cerr<<"Graph has been read \n";
// std::cerr<< "Number of nodes and edges in Cyclic graph : " <<n_vtx<<" "<<num_edges<<std::endl;
if(param_z)
{
std::cerr<< "Number of nodes and edges in Cyclic graph : " <<n_vtx<<" "<<num_edges<<std::endl;
// std::cerr<<"\nEdges in cyclic graph: "<<num_edges;
// std::cerr<<"\nNumber of nodes in cyclic graph: "<<n_vtx<<std::endl;
}
}
void graphUtils::print_graph()
{
std::cerr << " This is Graph " << std::endl;
for (size_t i = 0; i < n_vtx; i++)
{
std::cerr << i ;
for (int &x : adj_[i])
{
std::cerr << "->"<<x;
}
std::cerr << std::endl;
}
}
void DFS(std::vector<std::vector<int>> un_adj, std::vector<bool> &visited, int u, std::vector<int> &component, int num_components)
{
std::stack<int> stack;
// Push the source node
stack.push(u);
while (!stack.empty())
{
int s = stack.top();
stack.pop();
component[s] = num_components;
if (!visited[s])
{
visited[s] = true;
}
for (auto v:un_adj[s])
{
if (!visited[v])
{
stack.push(v);
}
}
}
}
void graphUtils::Connected_components()
{
//print_graph();
//std::cerr<<"Graph has been stored in adjacency list (adj_) \n";
std::vector<std::vector<int>> comp_adj;
int comp_size, dag_edge=0, dag_vertex=0, adj_edge=0;
size_t num_components;
if (lin_ref == 1)
{
num_components = n_vtx;
component.resize(n_vtx);
for (int u = 0; u < n_vtx; u++) // Put unique nodes in unique cids
{
component[u] = u;
}
}else
{
// Create Adjacency list
std::vector<std::vector<int>> un_adj;
un_adj.resize(n_vtx); // u -> v
for (int u = 0; u < n_vtx; u++)
{ //std::vector<std::vector<int>> adj(n_vtx);
for (auto v:adj_[u])
{
un_adj[u].push_back(v);
un_adj[v].push_back(u);
}
}
num_components = 0;
component.resize(n_vtx);
// run DFS
std::vector<bool> visited(n_vtx,false);
for (int u = 0; u < n_vtx; u++)
{
if (visited[u] == false)
{
DFS(un_adj,visited,u,component,num_components);
num_components++;
}
}
un_adj.clear();
visited.clear();
}
// std::cerr<<"Total number of connected components "<<num_components<<"\n";
num_cid = num_components;
// Storing Connected Components
//std::cerr<<"Conversion to DAG is started \n";
auto start = std::chrono::high_resolution_clock::now();
conn_comp.resize(num_components);
for (int i = 0; i < n_vtx; i++)
{
conn_comp[component[i]].push_back(i); // Add Vertex to it's component
}
component_idx.resize(num_cid);
idx_component.resize(num_cid);
// Map Components
for (size_t cid = 0; cid < num_cid; cid++)
{
component_idx[cid].resize(conn_comp[cid].size());
idx_component[cid].resize(n_vtx);
for (size_t j = 0; j < conn_comp[cid].size(); j++)
{
component_idx[cid][j] = conn_comp[cid][j]; // map global to local
idx_component[cid][conn_comp[cid][j]] = j; // map local to global
}
}
// Create Adjacency list for all connected components
cyclic_innode.resize(num_cid);
cyclic_outnode.resize(num_cid);
adj_cc = new std::vector<std::vector<int>>[num_components];
for (size_t cid = 0; cid < num_cid; cid++)
{
adj_cc[cid].resize(conn_comp[cid].size());
for (auto const &j : conn_comp[cid])
{
for (int k = 0; k < adj_[j].size(); k++)
{
adj_cc[cid][idx_component[cid][j]].push_back(idx_component[cid][adj_[j][k]]); // Map local index
}
}
}
// In degree and Outdegree computation
//// std::cerr<<"No. of vertices in each component are:"<<std::endl;
for (size_t cid = 0; cid < num_cid; cid++)
{
/*std::cerr<<"Cyclic component"<<cid;
for (size_t j = 0; j < adj_cc[cid].size(); j++)
{
std::cerr<<j;
for (auto const &k : adj_cc[cid][j])
{
std::cerr<<"->"<<k;
}
std::cerr<<"\n";
}*/
comp_size=conn_comp[cid].size();
comp_adj.resize(comp_size);
cyclic_innode[cid].resize(comp_size);
cyclic_outnode[cid].resize(comp_size);
for (size_t j = 0; j < adj_cc[cid].size(); j++)
{
for (auto const &k : adj_cc[cid][j])
{
//// std::cerr<<"->"<<adj_[j][k];
cyclic_outnode[cid][j].push_back(k);
if(k!= j)
{
cyclic_innode[cid][k].push_back(j);
}
comp_adj[j].push_back(k);
adj_edge++;
}
}
SCC(comp_adj, comp_size);
adj_cc[cid].clear();
adj_cc[cid].resize(comp_size);
//std::cerr<<"DAG Component"<<cid<<"\n";
for (size_t j = 0; j < comp_adj.size(); j++)
{
//std::cerr<<j;
for (auto const &k : comp_adj[j])
{
adj_cc[cid][j].push_back(k);
//std::cerr<<"->"<<k;
dag_edge++;
}
dag_vertex++;
//std::cerr<<"\n";
}
//std::cerr<<"Total number of edges in Acyclic Component "<<cid<<" :"<<"-> [";
//// std::cerr<<comp_count<<"] \n";
comp_adj.clear();
}
//// std::cerr<<"Total number of edges in cyclic graph "<<comp_count1<<"\n";
auto end = std::chrono::high_resolution_clock::now();
//std::cerr<<"Graph has been converted into DAG \n";
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
//std::cout << "Elapsed time for DAG conversion: " << duration << " milliseconds" << std::endl;
// std::cerr<<"Total number of edges in DAG "<<dag_edge<<"\n";
// std::cerr<<"Total number of nodes in DAG "<<dag_vertex<<"\n\n";
// std::cerr<<"Number of edges removed :"<<adj_edge-dag_edge<<"\n\n";
edges_rm=adj_edge-dag_edge;
if(param_z)
{
std::cerr<< "Number of nodes and edges in DAG : " <<dag_vertex<<" "<<dag_edge<<std::endl;
std::cerr<< "Number of edges removed : " <<edges_rm <<std::endl;
}
if(edges_rm > 0)
{
cyclic_flag=true;
std::cerr<<"[M::Cyclicity] Graph is Cyclic"<<"\n";
}
else
{
std::cerr<<"[M::Cyclicity] Graph is Acyclic"<<"\n";
}
}
int graphUtils::is_cyclic() // Check cyclicity of component and convert to acyclic graph iff component has cycles.
{
// construct
std::vector<std::vector<int>> in_degree;
std::vector<std::vector<int>> out_degree;
in_degree.resize(num_cid);
out_degree.resize(num_cid);
// queue
std::vector<std::queue<int>> q;
q.resize(num_cid);
std::vector<bool> cid_cycle;
cid_cycle.resize(num_cid);
int cycle_count = 0;
top_order.resize(num_cid);
#pragma omp parallel for
for (size_t cid = 0; cid < num_cid; cid++)
{
// Intialize
in_degree[cid].resize(adj_cc[cid].size(), 0);
out_degree[cid].resize(adj_cc[cid].size(), 0);
// Compute
for (int v = 0; v < adj_cc[cid].size(); v++)
{
out_degree[cid][v] = adj_cc[cid][v].size(); // [0]-> 1,3 (in adj_cc[cid][0] so out_degree of 0 is 2)
}
for (int u = 0; u < adj_cc[cid].size(); u++)
{
for (auto const &v : adj_cc[cid][u])
{
in_degree[cid][v]++; // [0]-> 1,3 (in adj_cc[cid][0] so in_degree of 1 and 3 is 1 for u = 0 and compute for all u's in ad_cc[cid])
}
}
// kahn's Topological sort
for (int v = 0; v < adj_cc[cid].size(); v++)
{
if (in_degree[cid][v] == 0)
{
q[cid].push(v);
}
}
int count = 0;
while (!q[cid].empty())
{
int u = q[cid].front();
q[cid].pop();
for (auto const &v : adj_cc[cid][u])
{
if (--in_degree[cid][v] == 0)
{
q[cid].push(v); // front -->[0,1,2]--> back , (when you push, queue adds v in incrementing order)
}
}
count++; // make count of vertices in top_order[cid]
}
// verify component is acyclic
if (count != adj_cc[cid].size())
{
cycle_count++;
}
}
for (size_t i = 0; i < num_comp; i++)
{
// std::cerr<<cid_cycle[i]<<" ";
}
q.clear();
in_degree.clear();
out_degree.clear();
if(param_z)
{
std::cerr << "[Connected components : " << num_cid << ", components with cycle : " << cycle_count << "]\n"<< std::endl;
}
return cycle_count;
}
void graphUtils::topologicat_sort()
{
// construct
std::vector<std::vector<int>> in_degree;
std::vector<std::vector<int>> out_degree;
in_degree.resize(num_cid);
out_degree.resize(num_cid);
map_top_sort.resize(num_cid);
// queue
std::vector<std::queue<int>> q;
q.resize(num_cid);
#pragma omp parallel for
for (size_t cid = 0; cid < num_cid; cid++)
{
// /*
// ###########################
// # kahn's Toplogical sort #
// ###########################
// */
// In degree and out degree computation
// Intialize
in_degree[cid].resize(adj_cc[cid].size(), 0);
out_degree[cid].resize(adj_cc[cid].size(), 0);
// Compute
for (int v = 0; v < adj_cc[cid].size(); v++)
{
out_degree[cid][v] = adj_cc[cid][v].size(); // [0]-> 1,3 (in adj_cc[cid][0] so out_degree of 0 is 2)
}
for (int u = 0; u < adj_cc[cid].size(); u++)
{
for (auto const &v : adj_cc[cid][u])
{
in_degree[cid][v]++; // [0]-> 1,3 (in adj_cc[cid][0] so in_degree of 1 and 3 is 1 for u = 0 and compute for all u's in ad_cc[cid])
}
}
// kahn's Topological sort
for (int v = 0; v < adj_cc[cid].size(); v++)
{
if (in_degree[cid][v] == 0)
{
q[cid].push(v);
}
}
int count = 0;
while (!q[cid].empty())
{
int u = q[cid].front();
q[cid].pop();
top_order[cid].push_back(u);
for (auto const &v : adj_cc[cid][u])
{
if (--in_degree[cid][v] == 0)
{
q[cid].push(v); // front -->[0,1,2]--> back , (when you push, queue add v in incrementing order)
}
}
count++; // make count of vertices in top_order[cid]
}
// verify component is acyclic
if (count != adj_cc[cid].size())
{
// std::cerr << " Can't do Topological ordering : cycle exist " << std::endl; // Just to make sure in case boost has failed to compute cycles
}
// std::cerr << " Top sort for cid : " << cid << std::endl;
// for (size_t i = 0; i < top_order[cid].size(); i++)
// {
// std::cerr << component_idx[cid][top_order[cid][i]] << std::endl;
// }
// std::cerr << std::endl;
}
/* Mapping for Top_Sort */
for (int cid = 0; cid < num_cid; cid++)
{
map_top_sort[cid].resize(top_order[cid].size());
for (int v = 0; v < top_order[cid].size(); v++)
{
map_top_sort[cid][top_order[cid][v]] = v;
}
}
in_degree.clear();
out_degree.clear();
// std::cerr << " Computed Topological Order " << std::endl;
}
std::vector<std::vector<int>> graphUtils::shrink(int cid)
{
/*
######################
# shrinking #
######################
*/
// Compute Shrinking
size_t N = adj_cc[cid].size();
std::vector<int> cids;
// fill with vertex id
for (size_t i = 0; i < N; i++)
{
cids.push_back(i);
}
std::vector<int> covered(N, 0);
std::vector<std::vector<int>> ret;
int K = path_cover[cid].size(), inf = path_cover[cid].size();
std::vector<int> starts(N, 0), ends(N, 0);
std::map<std::pair<int, int>, int> edge_covered;
for (auto path : path_cover[cid]) {
for (int i = 0; i < path.size(); i++) {
covered[path[i]]++;
if (i > 0)
edge_covered[{ path[i - 1], path[i] }]++;
}
starts[path[0]]++;
ends[path.back()]++;
}
flowGraph fg(N * 2);
// i_in = i, i_out = i + N
// add r(i, j) = c(j,i) + f(i,j) - l(i,j)
auto add = [&](int i, int j, int cap, int l, int ff) {
// std::cerr << "add edge " << i << " " << j << " " << cap << " " << l << " " <<ff << std::endl;
fg.add_edge(i, j, 0 + ff - l);
fg.add_edge(j, i, cap - ff);
};
for (int i = 0; i < N; i++)
for (size_t jid : out_node[cid][i]) {
size_t j = jid;
int ff = edge_covered.count({i, j}) ? edge_covered[{i, j}] : 0;
add(i + N, j, inf, 0, ff);
}
for (int i = 0; i < N; i++) {
add(i, i + N, inf, 1, covered[i]);
add(fg.S, i, inf, 0, starts[i]);
add(i + N, fg.T, inf, 0, ends[i]);
}
int total = inf;
std::vector<int> Q(fg.N, 0), pre(fg.N, -1), d(fg.N, 0);
while (1) {
int Qsize = 0;
Q[Qsize++] = fg.S;
for (int i = 0; i < fg.N; i++) {
pre[i] = -1;
d[i] = 0;
}
d[fg.S] = 1;
for (int idx = 0; idx < Qsize && d[fg.T] == 0;) {
int i = Q[idx++];
for (int e = fg.f[i]; e; e = fg.t[e]) {
int j = fg.p[e];
if (fg.c[e] > 0 && d[j] == 0) {
d[j] = 1;
pre[j] = e;
Q[Qsize++] = j;
}
}
}
if (d[fg.T] == 0) break;
std::vector<int> tmp;
int flow = fg.c[pre[fg.T]];
for (int i = fg.T; ;) {
tmp.push_back(i);
int e = pre[i];
if (e == -1) break;
flow = std::min(flow, fg.c[e]);
i = fg.p[e ^ 1];
}
for (int i = fg.T; ;) {
int e = pre[i];
if (e == -1) break;
fg.c[e] -= flow;
fg.c[e^1] += flow;
i = fg.p[e ^ 1];
}
if (flow == 0) exit(1);
total -= flow;
// std::cerr << " Now shrink by " << flow << " to " << total << std::endl;
}
// std::cerr << " Minimum Flow : " << total << std::endl;
// convert flow back to path cover
// ret = pc;
// ret.resize(total);
for (int itr = 0; itr < total; itr++) {
std::vector<int> tmp;
for (int i = fg.S; i != fg.T; ) {
if (0 <= i && i < N)
tmp.push_back(cids[i]);
int nxt = -1;
for (int e = fg.f[i]; e; e = fg.t[e]) {
int j = fg.p[e];
int ff = fg.c[e] + ((i < N && i + N == j) ? 1 : 0);
if ((e & 1) == 0 && ff > 0) {
nxt = j;
fg.c[e]--;
break;
}
}
if (nxt == -1) {
std::cerr << i << " not found nxt " << std::endl;
return ret; // return ret
}
i = nxt;
}
ret.push_back(tmp);
}
return ret;
}
bool check_MPC(std::vector<std::vector<int>> adj, std::vector<int> path_verify, int k, int cid){
int count = 0;
for (int i = 0; i < path_verify.size() - 1; i++)
{
int u = path_verify[i];
int v = path_verify[i+1];
for (auto x:adj[u])
{
if (x == v)
{
count++; // count the edges which will be "vertex - 1"
}
}
}
if (count + 1 == path_verify.size())
{
std::cerr << "cid = " << cid << " path #" << k+1<< " MPC is OK! " << std::endl;
return true;
}
}
void graphUtils::MPC()
{
/*
######################
# MPC (greedy cover) #
######################
*/
path_cover.resize(num_cid);
in_node.resize(num_cid);
out_node.resize(num_cid);
#pragma omp parallel for
for (size_t cid = 0; cid < num_cid; cid++)
{
in_node[cid].resize(adj_cc[cid].size()); // all nodes
out_node[cid].resize(adj_cc[cid].size()); // all nodes
// computing in_nodes and out_nodes
for (int u = 0; u < adj_cc[cid].size(); u++)
{
for (auto const &v : adj_cc[cid][u])
{
in_node[cid][v].push_back(u);
out_node[cid][u].push_back(v);
}
}
/* Greedy MPC! */
int T = 0;
int covered_count = 0;
std::vector<int> covered;
std::vector<int> max_cover;
std::vector<int> pre;
std::vector<int> path;
int V = adj_cc[cid].size();
covered.resize(V,0);
max_cover.resize(V,0);
pre.resize(V,-1); // Initialise as "None"
while (covered_count < V)
{
for (size_t i = 0; i < max_cover.size(); i++)
{
max_cover[i] = 0;
pre[i] = -1;
}
for (auto const & v: top_order[cid])
{
if (covered[v] == 0)
{
max_cover[v]++;
}
for (auto const& u : adj_cc[cid][v])
{
if (max_cover[u] < max_cover[v])
{
max_cover[u] = max_cover[v];
pre[u] = v;
}
}
}
auto max = std::max_element(max_cover.begin(),max_cover.end());
T = std::distance(max_cover.begin(),max); // argmax(max_cover[v])
int new_covered = 0;
while (covered_count < V)
{
if (T == -1)
{
break;
}else
{
if (covered[T] == 0)
{