-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathgraphlet_core.cpp
2166 lines (2011 loc) · 78 KB
/
graphlet_core.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
/**
============================================================================
Name : Parallel Parameterized Graphlet Decomposition (PGD) Library
Author : Nesreen K. Ahmed, (nesreen.k.ahmed@intel.com),
Ryan A. Rossi (rrossi@parc.com)
Description : A general high-performance parallel framework for computing
the graphlet decomposition. The library is designed to be fast
for both large sparse graphs as well as dense graphs.
Copyright (C) 2012-2015,
Nesreen K. Ahmed (http://nesreenahmed.com), All rights reserved.
Please cite the following paper:
Nesreen K. Ahmed, Jennifer Neville, Ryan A. Rossi, Nick Duffield,
Efficient Graphlet Counting for Large Networks, IEEE International
Conference on Data Mining (ICDM), pages 10, 2015.
Download PDF: http://www.nesreenahmed.com/publications/ahmed-et-al-icdm2015.pdf
@inproceedings{ahmed2015icdm,
title={Efficient Graphlet Counting for Large Networks},
author={Nesreen K. Ahmed and Jennifer Neville and Ryan A. Rossi and Nick Duffield},
booktitle={ICDM},
pages={1--10},
year={2015}
}
See http://nesreenahmed.com/graphlets for more information.
============================================================================
*/
#include "graphlet_core.h"
#include <algorithm>
using namespace graphlet;
using namespace std;
void graphlet_core::initialize() {
max_degree = 0;
min_degree = 0;
avg_degree = 0;
max_core = 0;
max_t_edge = 0;
total_t = 0;
fn = "";
is_gstats = false;
is_adj = false;
verbose = false;
block_size = 64;
density_threshold = 0.8;
}
graphlet_core::~graphlet_core() { }
/**
* @brief Constructor to initialize params, including the parallel params, and reads the graph
*
*/
graphlet_core::graphlet_core(params & p) {
initialize();
fn = p.graph;
is_gstats = p.graph_stats;
block_size = p.block_size;
read_graph(p.graph);
preprocess(p.ordering_csc_neighbor, p.is_small_to_large_csc_neighbor, p.adj_limit, p.graph_representation);
}
/**
* @brief Order neighbors of each vertex in csc/csr rep., create e_v and e_u arrays for fast lookup and optimize alg/data struct, etc.
*
* @param ordering_csc_neighbor
* @param is_small_to_large
* @param adj_limit
* @param rep
*/
void graphlet_core::preprocess(string ordering_csc_neighbor, bool is_small_to_large, int adj_limit, string rep) {
/// ordering neighbors of each vertex
order_vertex_neighbors(ordering_csc_neighbor, is_small_to_large);
/**
* @brief e_v and e_u are used for PGD, |e_v| = |E|/2.
* edge-csc (for fast computations on the edges)
*
* NOTE: Create ordered e_v and e_u arrays by utilizing the fact that csc neighbors are ordered
*/
create_edge_list_arrays();
/**
* @brief Automatically choose the optimal graph representation
*/
optimize_graph_ops(adj_limit, rep);
}
/**
* @brief Initialize and read graph with various custom settings
*
* @param filename of graph to read
*/
graphlet_core::graphlet_core(const string& filename, string ordering_technique, bool is_small_to_large, int adj_limit, string rep, int block_sz) {
initialize();
fn = filename;
block_size = block_sz;
read_graph(filename);
preprocess(ordering_technique, is_small_to_large, adj_limit, rep);
}
/**
* @brief Initialize and read graph!
*
* @param filename of graph to read
*/
graphlet_core::graphlet_core(const string& filename) {
initialize();
fn = filename;
read_graph(filename);
preprocess();
}
graphlet_core::graphlet_core(bool graph_stats, const string& filename) {
initialize();
fn = filename;
is_gstats = graph_stats; // construct e_u and e_v
read_graph(filename);
preprocess();
}
graphlet_core::graphlet_core(int nverts, int* heads, int* tails) {
initialize();
int num_vs = nverts, num_es = heads[nverts];
vector<long long> V(nverts,0);
vector<int> E;
E.reserve(num_es);
int start = 0;
for (int i = 0; i < nverts; i++) {
start = E.size();
for (long long j = heads[i]; j < heads[i + 1]; j++ ) { E.push_back(tails[j]); }
V[i] = start;
V[i + 1] = E.size();
}
vertices = V;
edges = E;
vertex_degrees();
}
/**
* pgd library interface
*
* Basic assumptions:
* - for undirected graphs, we assume one unique pair for each edge
- vertex ids are assumed to start at 0
*/
graphlet_core::graphlet_core(int nverts, int nedges, std::pair<int,int>* edge_pairs) {
initialize();
int num_vs = nverts, num_es = nedges;
map< int, vector<int> > vert_list;
int v = 0, u = 0, self_edges = 0;
for (int i = 0; i < num_es; ++i) {
v = edge_pairs[i].first;
u = edge_pairs[i].second;
if (v == u) self_edges++;
else {
vert_list[v].push_back(u);
vert_list[u].push_back(v);
if (is_gstats) {
e_v.push_back(v);
e_u.push_back(u);
}
}
}
vertices.push_back(edges.size());
for (int i=0; i < vert_list.size(); i++) {
edges.insert(edges.end(),vert_list[i].begin(),vert_list[i].end());
vertices.push_back(edges.size());
}
vert_list.clear();
vertex_degrees();
if (verbose) cout << "self-loops: " << self_edges <<endl;
vert_list.clear();
}
bool graphlet_core::detect_weighted_graph(string & line, string & delim) {
int num_tokens = 0;
string buf; // Have a buffer string
stringstream ss(line); // Insert the string into a stream
vector<string> tokens; // Create vector to hold our words
while (ss >> buf) { tokens.push_back(buf); }
if (verbose) printf("number of tokens in line = %lu \n", tokens.size());
if (tokens.size() == 3) return true; // weighted graph (3rd column)
return false; // unweighted, only edge list with two columns
}
void graphlet_core::detect_delim(string & line, string & delim) {
if (delim == "") {
std::size_t prev = 0, pos;
std::string tab_spaces(" ");
if ((pos = line.find_first_of(',', prev)) != std::string::npos) {
delim = ',';
}
else if ((pos = line.find_first_of('\t', prev)) != std::string::npos) {
delim = '\t';
}
else if ((pos = line.find(tab_spaces)) != std::string::npos) {
printf("found tab-spaces delimiter \n");
delim = " ";
}
else if ((pos = line.find_first_of(' ', prev)) != std::string::npos) {
delim = ' ';
}
}
if (delim == "") {
if (get_file_extension(fn) == "csv")
delim = ',';
else if (get_file_extension(fn) == "tab")
delim = '\t';
else if (get_file_extension(fn) == "mtx")
delim = ' ';
else
delim = ' ';
if (verbose) cout << "[pgd] no delimiter recognized, using \"" << delim.c_str()[0] << "\" as delimiter!" <<endl;
}
else
if (verbose) cout << "[pgd] detected \"" << delim << "\" as delimiter! " <<endl;
}
inline
void graphlet_core::get_token(int & v, string & line, string & delim, size_t & pos, size_t & prev) {
if ((pos = line.find(delim, prev)) != std::string::npos) {
if (pos > prev) {
v = atoi(line.substr(prev, pos-prev).c_str());
}
prev = pos+1;
}
else if (prev < line.length())
v = atoi(line.substr(prev, std::string::npos).c_str());
}
inline
void graphlet_core::get_token(double & weight, string & line, string & delim, size_t & pos, size_t & prev, bool & is_weighted_graph) {
if ((pos = line.find(delim, prev)) != std::string::npos) {
if (pos > prev) {
weight = atof(line.substr(prev, pos-prev).c_str());
}
prev = pos+1;
}
else if (prev < line.length())
weight = atof(line.substr(prev, std::string::npos).c_str());
}
/**
* @brief Selects appropriate reader for graph
*
* @param filename or path of the graph to read
*/
void graphlet_core::read_graph(const string& filename) {
is_gstats = true;
fn = filename;
double sec = get_time();
string ext = get_file_extension(filename);
if (verbose) cout << "[pgd: graph reader] All graphs are assumed to be undirected" <<endl;
if (verbose) cout << "[pgd: graph reader] Self-loops and weights (if any) are discarded" <<endl;
if (ext == "edges" || ext == "eg2" || ext == "txt" || ext == "csv") {
if (verbose) cout << "[pgd: general graph reader] reading the edge list" <<endl;
read_edge_list(filename);
}
else if (ext == "mtx") {
if (verbose) cout << "[pgd: mtx graph reader] Assuming matrix is undirected, and upper-triangular " <<endl;
read_mtx(filename);
}
else {
if (verbose) cout << "[pgd: general graph reader] Reading the graph" <<endl;
read_edge_list(filename);
}
if (verbose) cout << "Reading time " << get_time() - sec << endl;
vertex_degrees();
basic_stats();
}
/**
* /brief Reads a general edge list, makes limited assumptions about the graph
*
* WEIGHTS: All weights are discarded, unless the graph is temporal
* LABELS: Vertices are relabeled, and the old ids are discarded, unless specified.
*/
void graphlet_core::read_edge_list(const string& filename) {
cout << "[reading generic edge list: read_edge_list func] filename: " << filename <<endl;
map< int, vector<int> > vert_list;
int v = -1, u = -1, num_es = 0, self_edges = 0;
double weight;
string delimiters = " ,\t", delim="", line="", token="";
string graph_exif = "";
ifstream file (filename.c_str());
if (!file) { if (verbose) cout << filename << "File not found!" <<endl; return; }
// check if vertex ids start at 0 or 1
is_weighted = false;
bool fix_start_idx = true, ignore_first_line = false;
stringstream iss;
// save graph info/comments at top of file
while(std::getline(file, line) && (line[0] == '#' || line[0] == '%')) {
graph_exif += line;
if (line.find("MatrixMarket matrix coordinate pattern symmetric") != std::string::npos) {
delim = ' ';
ignore_first_line = true;
}
}
int num_verts = 0, num_edges = 0;
if (get_file_extension(filename) == ".mtx") {
if (verbose) cout << "[pgd: graph reader] mtx file detected!" <<endl;
iss << line;
int cols = 0;
iss >> num_verts >> cols >> num_edges;
if(num_verts!=cols) { cout<<"[pgd] error: this is not a square matrix, attempting to proceed."<<endl; }
}
// detect the delim for reading the graph
detect_delim(line,delim);
// detect if line has three columns, third is assumed to be for weights
is_weighted = detect_weighted_graph(line,delim);
if (is_weighted) printf("weighted graph detected \n");
// handle the first line (find starting vertex id)
if (line != "") {
iss.clear();
iss.str(line);
iss >> v >> u; //>> weight;
if (v == 0 || u == 0) { fix_start_idx = false; }
}
int max = 0; // largest vertex id (assumed to be ints)
if (verbose) cout << "[pgd: graph reader] reading a general edge list (limited assumptions)" <<endl;
// find starting vertex id, compute the number of vertices to expect (since gaps in vertex ids are possible)
while(std::getline(file, line)) {
if (line != "") { // ensure line actually contains data
iss << line;
// ignore comments
if (line[0] == '%' || line[0] == '#') continue;
std::size_t prev = 0, pos;
get_token(v,line,delim,pos,prev);
get_token(u,line,delim,pos,prev);
if (v == 0 || u == 0) { fix_start_idx = false; }
if (v > max) max = v;
if (u > max) max = u;
}
}
if (verbose) cout << "[pgd: graph reader] largest vertex id is " << max <<endl;
file.close();
if (verbose) {
if (fix_start_idx) cout << "[pgd: graph reader] vertex ids from the file begin at 1" <<endl;
else cout << "[pgd: graph reader] vertex ids begin at 0" <<endl;
}
ifstream fin (filename.c_str());
if (!fin) { cout << filename << "Error: file not found!" <<endl; return; }
int vertex_id = 0;
vector<int> vert_lookup(max+1,-1);
if (is_weighted) {
while(std::getline(fin, line)) {
if (line != "") { // ensure line actually contains data
iss << line;
// ignore comments
if (line[0] == '%' || line[0] == '#') continue;
std::size_t prev = 0, pos; // prev is last location in the line
get_token(v,line,delim,pos,prev);
get_token(u,line,delim,pos,prev);
// get the weight (3rd column)
get_token(weight,line,delim,pos,prev,is_weighted);
if (fix_start_idx) {
v--;
u--;
}
if (v == u) self_edges++;
else {
if (vert_lookup[v] == -1) { // new vertex
vert_lookup[v] = vertex_id; // store the new id
vertex_id++;
}
v = vert_lookup[v]; // get the consistent vertex id
if (vert_lookup[u] == -1) { // new vertex
vert_lookup[u] = vertex_id; // store the new id
vertex_id++;
}
u = vert_lookup[u]; // get the consistent vertex id
vert_list[v].push_back(u);
vert_list[u].push_back(v);
}
}
}
}
else { // unweighted graph (two columns)
while(std::getline(fin, line)) {
if (line != "") { // ensure line actually contains data
iss << line;
if (line[0] == '%' || line[0] == '#') continue;
std::size_t prev = 0, pos; // prev is last location in the line
get_token(v,line,delim,pos,prev);
get_token(u,line,delim,pos,prev);
if (fix_start_idx) {
v--;
u--;
}
if (v == u) self_edges++;
else {
if (vert_lookup[v] == -1) { // new vertex
vert_lookup[v] = vertex_id; // store the new id
vertex_id++;
}
v = vert_lookup[v]; // get the consistent vertex id
if (vert_lookup[u] == -1) { // new vertex
vert_lookup[u] = vertex_id; // store the new id
vertex_id++;
}
u = vert_lookup[u]; // get the consistent vertex id
vert_list[v].push_back(u);
vert_list[u].push_back(v);
}
}
}
}
fin.close();
vert_lookup.clear();
if (verbose) cout << "vert_list size: " << vert_list.size() <<endl;
vertices.push_back(edges.size());
for (int i=0; i < vert_list.size(); i++) {
edges.insert(edges.end(),vert_list[i].begin(),vert_list[i].end());
vertices.push_back(edges.size());
}
vert_list.clear();
bool delete_multiple_edges = true; /// todo: set via command line
if (delete_multiple_edges) { remove_multiple_edges(); }
if (verbose) cout << "self-loops: " << self_edges <<endl;
}
int graphlet_core::read_mtx(const string& filename) {
string line = "";
map< int, vector<int> > vert_list;
int v = -1, u = -1, num_es = 0, self_edges = 0;
string delim = " ", graph_exif = "";
ifstream file (filename.c_str());
if (!file) { cout << filename << "Error: file not found!" <<endl; return 0; }
// check if vertex ids start at 0 or 1
bool fix_start_idx = true, ignore_first_line = false;
string token;
stringstream iss;
// save graph info/comments at top of file
while(std::getline(file, line) && (line[0] == '#' || line[0] == '%')) { graph_exif += line; }
int num_verts = 0, num_edges = 0, cols = 0;
iss << line;
iss >> num_verts >> cols >> num_edges;
if(num_verts!=cols) cout<<"[pgd] error: this is not a square matrix, attempting to proceed."<<endl;
// handle the first line (find starting vertex id)
if (line != "") {
iss.clear();
iss.str(line);
iss >> v >> u;
if (v == 0 || u == 0) { fix_start_idx = false; }
}
double value;
if (verbose) cout << "[pgd: graph reader] reading mtx file" <<endl;
/*
* @brief Assume there are no gaps in vertex ids (mtx standard), break if a zero is encountered
* Find starting vertex id (break asap)
*/
while(std::getline(file, line) && fix_start_idx) {
if (line != "") { // ensure line actually contains data
// ignore comments
if (line[0] == '%' || line[0] == '#') continue;
iss.clear();
iss.str(line);
iss >> v >> u >> value;
v--;
u--;
if (v == u) self_edges++;
else {
vert_list[v].push_back(u);
vert_list[u].push_back(v);
}
}
}
vertices.push_back(edges.size());
for (int i=0; i < vert_list.size(); i++) {
edges.insert(edges.end(),vert_list[i].begin(),vert_list[i].end());
vertices.push_back(edges.size());
}
vert_list.clear();
if (verbose) cout << "self-loops: " << self_edges <<endl;
return 1;
}
/**
* @brief Specifically designed to be _fast_ for very large graphs
* Impossible to store full adj of large sparse graphs, instead
* we create a lookup table for each vertex, and build it on the fly,
* using this info to mark and essentially remove the multiple edges
*/
void graphlet_core::remove_multiple_edges() {
vector<int> ind(vertices.size(),0);
vector<long long> vs(vertices.size(),0);
vector<int> es;
es.reserve(edges.size());
int start = 0;
for (int i = 0; i < vertices.size()-1; i++) {
start = es.size();
for (long long j = vertices[i]; j < vertices[i + 1]; j++) {
int u = edges[j];
if (ind[u] == 0) {
es.push_back(edges[j]);
ind[u] = 1;
}
}
vs[i] = start;
vs[i + 1] = es.size();
for (long long j = vertices[i]; j < vertices[i + 1]; j++) { ind[edges[j]] = 0; }
}
if (verbose) cout << "[pgd: graph reader] removed " << (edges.size() - es.size())/2 << " duplicate edges (multigraph)" <<endl;
if (verbose) cout << "[remove multiple edges] " << edges.size() <<endl;
vertices = vs;
edges = es;
vs.clear();
es.clear();
}
/**
* @brief Output basic graph stats such as |V|, |E|, density, max degree, avg degree
* Examples: basic_stats("", "\n"), or basic_stats("", ", ");
*/
void graphlet_core::basic_stats(string prefix, string suffix) {
cout << prefix << "|V|: " << num_vertices() <<suffix;
cout << prefix << "|E|: " << num_edges() <<suffix;
cout << prefix << "p: " << density() <<suffix;
cout << prefix << "d_max: " << get_max_degree() <<suffix;
cout << prefix << "d_avg: " << get_avg_degree() <<suffix;
}
string graphlet_core::basic_names_line(string delim, string prefix, string suffix) {
ostringstream str_stream;
str_stream << prefix << "|V|" << delim <<suffix;
str_stream << prefix << "|E|" << delim <<suffix;
str_stream << prefix << "p" << delim <<suffix;
str_stream << prefix << "d_max" << delim <<suffix;
str_stream << prefix << "d_avg" << delim <<suffix;
if (max_core>0) str_stream << prefix << "K" << delim <<suffix;
return str_stream.str();
}
string graphlet_core::basic_stats_line(string delim, string prefix, string suffix) {
ostringstream str_stream;
str_stream << prefix << num_vertices() << delim <<suffix;
str_stream << prefix << num_edges() << delim <<suffix;
str_stream << prefix << density() << delim <<suffix;
str_stream << prefix << get_max_degree() << delim <<suffix;
str_stream << prefix << get_avg_degree() << delim <<suffix;
if (max_core>0) str_stream << prefix << max_core << delim <<suffix;
return str_stream.str();
}
/**
* @brief adapt graph representation based on some simple statistics
* and memory requirements. This is meant for performance and
* flexibility.
*
* @todo remove this from the user, simply decide when graph is read
* or created via other graph operations
*
* @param adj_limit Threshold for deciding if an adjacency matrix should be constructed for checking edge existence in O(1) time.
*/
void graphlet_core::optimize_graph_ops(int adj_limit, string rep) {
if (num_vertices() < adj_limit && density() > density_threshold) {
create_adj_mat();
if (verbose) cout << "DENSE and/or SMALL graph detected. Optimizing internal data structures" <<endl;
}
else {
if (verbose) cout << "SPARSE graph detected. Optimizing internal data structures" <<endl;
}
set_rep_manually(rep);
}
/**
* @brief Creates an adjacency matrix for checking edge existence in O(1)
*
* Used for graphlet decomposition of DENSE and/or small networks (offers speedup for both types)
*
* \return An adjacency matrix "A" where A[i][j] = 1 if $(i,j) \in E$
*/
void graphlet_core::create_adj_mat() {
if (is_adj==false) {
is_adj = true;
double sec = get_time();
int size = num_vertices();
A.resize(size);
for (int i = 0; i < size; i++) { A[i].resize(size,0); }
for (int i = 0; i < size; i++) {
for (long long j = vertices[i]; j < vertices[i + 1]; j++ )
A[i][edges[j]] = 1;
}
if (verbose) cout << "Created adjacency matrix in " << get_time() - sec << " seconds" <<endl;
}
}
enum { // representation
AUTO, // 0
CSC, // 1
ADJ, // 2
HYBRID, // 3
};
/**
* @brief Get the appropriate representation id/enum.
* Note that auto indicates to automatically determine optimal (default).
*
* @param rep
* @return
*/
static int get_representation_enum(string &rep) {
if (rep == "auto" || rep == "") return 0;
else if (rep == "csc" || rep == "sparse") return 1;
else if (rep == "adj" || rep == "adjcency") return 2;
else if (rep == "hybrid" || rep == "csc_adj") return 3;
return 0;
}
/**
* @brief Allows the user to set the representation manually (for specific or special cases).
* NOTE that if adj structure already exists, then we return immediately to avoid recomputing the data structure.
*
* @param rep is a string indicating the type of representation to use
*/
void graphlet_core::set_rep_manually(string &rep) {
switch(get_representation_enum(rep)) {
case AUTO: {
break;
}
case CSC: {
is_adj = false;
break;
}
case ADJ: {
create_adj_mat();
break;
}
case HYBRID: {
create_adj_mat();
break;
}
default: {
break;
}
}
}
enum { // order and pruning methods
NATURAL, // 0
RAND, // 1
DEGREE, // 2
KCORE, // 3
KCORE_DEG, // 4
DEGREE_VOL, // 5
KCORE_VOL, // 6
DEGREE_KCORE_VOL, // 7
VAR, // 8
TRIANGLES, // 9
WEDGES, // 10
TRIANGLE_CORES, // 11
COLORING, // 12
TRIANGLE_VOL, // 13
TRIANGLE_CORE_VOL, // 14
TRIANGLES_ONLY, // 15 (for global pruning)
DYNAMIC_LARGEST_FIRST, // 16
TRIANGLE_CORE_MAX, // 17
DEGREE_TRIANGLES, // 18
KCORE_TRIANGLES, // 19
KCORE_DEG_TRI, // 20
KCORE_TRIANGLE_VOL, // 21
DEGREE_KCORE_TRIANGLE_VOL, // 22
DIST_TWO_LARGEST_FIRST, // 23
DIST_TWO_DYNAMIC_LARGEST_FIRST, // 24
DIST_TWO_SMALLEST_LAST, // 25
INCIDENCE_DEGREE, // 26
DIST_TWO_INCIDENCE_DEGREE, // 27
TRIANGLE_CORE_DIST_TWO, // 28
TRIANGLE_CORE_MIN, // 29
COMMON_NEIGHBORS, // 30
};
static int get_ordering_enum(string &ordering) {
if (ordering == "natural") return 0;
else if (ordering == "rand" || ordering == "random") return 1;
else if (ordering == "deg" || ordering == "degree") return 2;
else if (ordering == "kcore" || ordering == "kcores") return 3;
else if (ordering == "kcore_deg" || ordering == "kcore_degree") return 4;
else if (ordering == "deg_vol" || ordering == "degree_vol") return 5;
else if (ordering == "kcore_vol" || ordering == "kcore_volume") return 6;
else if (ordering == "kcore_deg_vol" || ordering == "kcore_degree_vol") return 7;
else if (ordering == "var") return 8;
else if (ordering == "triangles" || ordering == "tri" || ordering == "triangle") return 9;
else if (ordering == "wedges") return 10;
else if (ordering == "tcores" || ordering == "triangle_cores" ||
ordering == "triangle_core" || ordering == "tcore") return 11;
else if (ordering == "coloring" || ordering == "greedy_coloring") return 12;
else if (ordering == "tri_vol" || ordering == "triangle_vol") return 13;
else if (ordering == "tcore_vol" || ordering == "tcore_volume" ||
ordering == "triangle_core_vol") return 14;
else if (ordering == "triangles_only" || ordering == "triangle_only") return 15;
else if (ordering == "dynamic_largest_first" || ordering == "lfo" ||
ordering =="LFO") return 16; // graph coloring
else if (ordering == "triangle_core_max" || ordering == "tcore_max" ||
ordering == "tcores_max") return 17;
else if (ordering == "degree_triangles" || ordering == "degree_tri" ||
ordering == "deg_tri" || ordering == "deg_triangles") return 18;
else if (ordering == "kcore_tri" || ordering == "kcore_triangles" ||
ordering =="kcore_triangle") return 19; // graph coloring
else if (ordering == "kcore_deg_tri" || ordering == "kcore_degree_triangle" ||
ordering == "kcore_degree_tri") return 20;
else if (ordering == "kcore_tri_vol" || ordering == "kcore_triangle_vol") return 21;
else if (ordering == "kcore_deg_tri_vol" || ordering == "kcore_degree_tri_vol") return 22;
else if (ordering == "dist_two_lfo" || ordering == "dist_two_largest_first") return 23;
else if (ordering == "dist_two_dlfo" || ordering == "dist_two_dynamic_lfo") return 24;
else if (ordering == "dist_two_slo" || ordering == "dist_two_smallest_last") return 25;
else if (ordering == "ido" || ordering == "incidence" ||
ordering == "incidence_deg") return 26;
else if (ordering == "dist_two_ido" || ordering == "dist_two_incidence") return 27;
else if (ordering == "dist_two_tcore" || ordering == "dist_two_triangle_core") return 28;
else if (ordering == "tcore_min" || ordering == "triangle_core_min" ||
ordering =="tri_core_min") return 29; // graph coloring
else if (ordering == "common" || ordering=="common_neighbors") return 30;
else return 0; // default natural
}
/**
* @brief
*
* @param ordering enum from get_ordering_enum
* @param v is a vertex id for edge e (src)
* @param u is a vertex id for edge e (dst)
* @param val that is modified and returned via reference
*/
inline
void graphlet_core::get_ordering_value(int &ordering, long long &v, long long &u, long long &val) {
switch (ordering) {
case NATURAL: {
break;
}
case RAND: {
val = get_cust_rand_int();
break;
}
case DEGREE: {
val = degree[u]+degree[v];
break;
}
case KCORE: {
val = kcore[u]+kcore[v];
break;
}
case KCORE_DEG: {
val = (degree[u]+degree[v]) * (kcore[u]+kcore[v]);
break;
}
case DEGREE_VOL: {
val = 0;
for (long long j = vertices[u]; j < vertices[u + 1]; j++) {
val = val + vertices[edges[j] + 1] - vertices[edges[j]];
}
for (long long j = vertices[v]; j < vertices[v + 1]; j++) {
val = val + vertices[edges[j] + 1] - vertices[edges[j]];
}
break;
}
case KCORE_VOL: {
val = 0;
for (long long j = vertices[u]; j < vertices[u + 1]; j++) {
val = val + kcore[edges[j]];
}
for (long long j = vertices[v]; j < vertices[v + 1]; j++) {
val = val + kcore[edges[j]];
}
break;
}
case DEGREE_KCORE_VOL: {
val = 0;
for (long long j = vertices[u]; j < vertices[u + 1]; j++) {
val = val + ((vertices[edges[j] + 1] - vertices[edges[j]]) * kcore[edges[j]]);
}
for (long long j = vertices[v]; j < vertices[v + 1]; j++) {
val = val + ((vertices[edges[j] + 1] - vertices[edges[j]]) * kcore[edges[j]]);
}
break;
}
case VAR: {
val = (kcore[u] * ((int)degree[u]/kcore[u])) + (kcore[v] * ((int)degree[v]/kcore[v]));
break;
}
default: {
val = vertices[u + 1] - vertices[u];
break;
}
}
}
/** @brief Create an ordered edge list from a set of sampled edges E_s such that |E_s| < |E| */
void graphlet_core::sort_edges(string ordering_technique, bool is_small_to_large, vector<unsigned long> & E_s) {
int ordering = get_ordering_enum(ordering_technique);
E_ordered.reserve(E_s.size());
set_custom_seed(get_time());
for (int e=0; e<E_s.size(); e++) {
int edge_id = E_s[e];
long long v = e_v[edge_id], u = e_u[edge_id];
long long val = e;
get_ordering_value(ordering, v, u, val);
E_ordered.push_back(Vertex(edge_id,val));
}
if (is_small_to_large) { std::sort(E_ordered.begin(), E_ordered.end(), incr_bound); }
else { std::sort(E_ordered.begin(), E_ordered.end(), decr_bound); }
}
/** @brief Create an ordered edge list from FULL set of edges E */
void graphlet_core::sort_edges(string ordering_technique, bool is_small_to_large) {
int ordering = get_ordering_enum(ordering_technique);
int m = e_v.size();
E_ordered.reserve(m);
set_custom_seed(get_time());
for (int e=0; e<m; e++) {
long long v = e_v[e], u = e_u[e];
long long val = e;
get_ordering_value(ordering, v, u, val);
E_ordered.push_back(Vertex(e,val));
}
if (is_small_to_large) { std::sort(E_ordered.begin(), E_ordered.end(), incr_bound); }
else { std::sort(E_ordered.begin(), E_ordered.end(), decr_bound); }
}
/**
* @brief Compute the vertex degrees, and the max vertex degree.
*
* Returns the vertex degrees in the "degree" array as well as:
* - the maximum vertex degree stored in "max_degree"
* - average vertex degree "avg_degree"
*
*/
void graphlet_core::vertex_degrees() {
int n = vertices.size() - 1;
degree.resize(n);
int max_degree_tmp = vertices[1] - vertices[0];
#pragma omp parallel for schedule(schedule_type,block_size) \
reduction(max:max_degree_tmp)
for (long long v=0; v<n; v++) {
degree[v] = vertices[v+1] - vertices[v];
if (max_degree_tmp < degree[v]) max_degree_tmp = degree[v];
}
max_degree = max_degree_tmp;
avg_degree = (double)edges.size()/n;
return;
}
/**
* @brief Get the extension from a full filename given as input
*
* @param filename a filename
* @return file extension, e.g., "filename.txt", then ".txt" is returned.
*/
string graphlet_core::get_file_extension(const string& filename) {
string::size_type result;
string fileExtension = "";
result = filename.rfind('.', filename.size() - 1);
if(result != string::npos)
fileExtension = filename.substr(result+1);
return fileExtension;
}
/**
* @brief Gets the filename from an arbitrary path
* @param s string containing the path of the file
* @return filename consisting of the name and extension, that is, "file.txt"
*/
string graphlet_core::get_filename_from_path(const string& s) {
char sep = '/';
#ifdef _WIN32
sep = '\\';
#endif
size_t i = s.rfind(sep, s.length( ));
if (i != string::npos) {
return(s.substr(i+1, s.length( ) - i));
}
return("");
}
/**
* @brief Create e_u and e_v arrays
*
*/
void graphlet_core::create_edge_list_arrays() {
int n = vertices.size() - 1;
long long m = edges.size();
long long unique_edges = m/2;
e_v.reserve(unique_edges+1);
e_u.reserve(unique_edges+1);
for (long long v=0; v<n; v++) {
for (long long j=vertices[v]; j<vertices[v+1]; j++) {
long long u = edges[j];
if (v<u) {
if ((vertices[v+1]-vertices[v]) < (vertices[u+1]-vertices[u])) {
e_v.push_back(v);
e_u.push_back(u);
} else {
e_v.push_back(u);
e_u.push_back(v);
}
}
}
}
}
/**
* \brief Compute K-Core Decomposition
*
* Computes degeneracy ordering, as well as
* - k-core number for each vertex
* - maximum k-core number
*/
void graphlet_core::compute_cores() {
long long j;
int n = vertices.size(), d, i, start, num, md;
int v, u, w, du, pu, pw, md_end;
if (kcore.size()>0) return;
vector <int> pos(n);
if (kcore_order.size() > 0) {
vector<int> tmp(n,0);
kcore = tmp;
kcore_order = tmp;
}
else {
kcore_order.resize(n);
kcore.resize(n);
}
md = 0;
#pragma omp parallel for schedule(schedule_type,block_size) reduction(max:md)
for (int v=1; v<n; v++) {
kcore[v] = vertices[v] - vertices[v-1];
if (kcore[v] > md) md = kcore[v];
}
md_end = md+1;
vector < int > bin(md_end,0);
for (v=1; v < n; v++) bin[kcore[v]]++;
start = 1;
for (d=0; d < md_end; d++) {
num = bin[d];
bin[d] = start;
start = start + num;
}
for (v=1; v<n; v++) {
pos[v] = bin[kcore[v]];
kcore_order[pos[v]] = v;
bin[kcore[v]]++;
}
for (d=md; d > 1; d--) bin[d] = bin[d-1];
bin[0] = 1;
for (i=1; i<n; i++) {
v=kcore_order[i];
for (j=vertices[v-1]; j<vertices[v]; j++) {
u = edges[j] + 1;
if (kcore[u] > kcore[v]) {