Repository navigation
Expand file tree
/
Copy pathgetTriangles.cpp
More file actions
1030 lines (736 loc) · 26.5 KB
/
Copy pathgetTriangles.cpp
File metadata and controls
1030 lines (736 loc) · 26.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
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
/*
* Copyright (c) 2009 Carnegie Mellon University.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
* For more about this software visit:
*
* http://www.graphlab.ml.cmu.edu
*
*/
#include <boost/unordered_set.hpp>
#include <graphlab.hpp>
#include <graphlab/ui/metrics_server.hpp>
#include <graphlab/util/hopscotch_set.hpp>
#include <graphlab/macros_def.hpp>
#include <limits>
/**
*
* In this program we implement the "hash-set" version of the
* "edge-iterator" algorithm described in
*
* T. Schank. Algorithmic Aspects of Triangle-Based Network Analysis.
* Phd in computer science, University Karlsruhe, 2007.
*
* The procedure is quite straightforward:
* - each vertex maintains a list of all of its neighbors in a hash set.
* - For each edge (u,v) in the graph, count the number of intersections
* of the neighbor set on u and the neighbor set on v.
* - We store the size of the intersection on the edge.
*
* This will count every triangle exactly 3 times. Summing across all the
* edges and dividing by 3 gives the desired result.
*
* The preprocessing stage take O(|E|) time, and it has been shown that this
* algorithm takes $O(|E|^(3/2))$ time.
*
* If we only require total counts, we can introduce a optimization that is
* similar to the "forward" algorithm
* described in thesis above. Instead of maintaining a complete list of all
* neighbors, each vertex only maintains a list of all neighbors with
* ID greater than itself. This implicitly generates a topological sort
* of the graph.
*
* Then you can see that each triangle
*
* \verbatim
A----->C
| ^
| /
v /
B
* \endverbatim
* Must be counted only once. (Only when processing edge AB, can one
* observe that A and B have intersecting out-neighbor sets).
*/
// Radix sort implementation from https://github.com/gorset/radix
// Thanks to Erik Gorset
//
/*
Copyright 2011 Erik Gorset. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY Erik Gorset ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Erik Gorset OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Erik Gorset.
*/
//probability of keeping an edge in the edges sampling process
double sample_prob_keep = 1;
double min_prob = 1;
double max_prob = 1;
double prob_step = 0.5;
//size_t total_edges = 0;
int sample_iter = 1;
void radix_sort(graphlab::vertex_id_type *array, int offset, int end, int shift) {
int x, y;
graphlab::vertex_id_type value, temp;
int last[256] = { 0 }, pointer[256];
for (x=offset; x<end; ++x) {
++last[(array[x] >> shift) & 0xFF];
}
last[0] += offset;
pointer[0] = offset;
for (x=1; x<256; ++x) {
pointer[x] = last[x-1];
last[x] += last[x-1];
}
for (x=0; x<256; ++x) {
while (pointer[x] != last[x]) {
value = array[pointer[x]];
y = (value >> shift) & 0xFF;
while (x != y) {
temp = array[pointer[y]];
array[pointer[y]++] = value;
value = temp;
y = (value >> shift) & 0xFF;
}
array[pointer[x]++] = value;
}
}
if (shift > 0) {
shift -= 8;
for (x=0; x<256; ++x) {
temp = x > 0 ? pointer[x] - pointer[x-1] : pointer[0] - offset;
if (temp > 64) {
radix_sort(array, pointer[x] - temp, pointer[x], shift);
} else if (temp > 1) {
std::sort(array + (pointer[x] - temp), array + pointer[x]);
//insertion_sort(array, pointer[x] - temp, pointer[x]);
}
}
}
}
size_t HASH_THRESHOLD = 64;
// We on each vertex, either a vector of sorted VIDs
// or a hash set (cuckoo hash) of VIDs.
// If the number of elements is greater than HASH_THRESHOLD,
// the hash set is used. Otherwise the vector is used.
struct vid_vector{
std::vector<graphlab::vertex_id_type> vid_vec;
graphlab::hopscotch_set<graphlab::vertex_id_type> *cset;
vid_vector(): cset(NULL) { }
vid_vector(const vid_vector& v):cset(NULL) {
(*this) = v;
}
vid_vector& operator=(const vid_vector& v) {
if (this == &v) return *this;
vid_vec = v.vid_vec;
if (v.cset != NULL) {
// allocate the cuckoo set if the other side is using a cuckoo set
// or clear if I alrady have one
if (cset == NULL) {
cset = new graphlab::hopscotch_set<graphlab::vertex_id_type>(HASH_THRESHOLD);
}
else {
cset->clear();
}
(*cset) = *(v.cset);
}
else {
// if the other side is not using a cuckoo set, lets not use a cuckoo set
// either
if (cset != NULL) {
delete cset;
cset = NULL;
}
}
return *this;
}
~vid_vector() {
if (cset != NULL) delete cset;
}
// assigns a vector of vertex IDs to this storage.
// this function will clear the contents of the vid_vector
// and reconstruct it.
// If the assigned values has length >= HASH_THRESHOLD,
// we will allocate a cuckoo set to store it. Otherwise,
// we just store a sorted vector
void assign(const std::vector<graphlab::vertex_id_type>& vec) {
clear();
if (vec.size() >= HASH_THRESHOLD) {
// move to cset
cset = new graphlab::hopscotch_set<graphlab::vertex_id_type>(HASH_THRESHOLD);
foreach (graphlab::vertex_id_type v, vec) {
cset->insert(v);
}
}
else {
vid_vec = vec;
if (vid_vec.size() > 64) {
radix_sort(&(vid_vec[0]), 0, vid_vec.size(), 24);
}
else {
std::sort(vid_vec.begin(), vid_vec.end());
}
std::vector<graphlab::vertex_id_type>::iterator new_end = std::unique(vid_vec.begin(),
vid_vec.end());
vid_vec.erase(new_end, vid_vec.end());
}
}
void save(graphlab::oarchive& oarc) const {
oarc << (cset != NULL);
if (cset == NULL) oarc << vid_vec;
else oarc << (*cset);
}
void clear() {
vid_vec.clear();
if (cset != NULL) {
delete cset;
cset = NULL;
}
}
size_t size() const {
return cset == NULL ? vid_vec.size() : cset->size();
}
void load(graphlab::iarchive& iarc) {
clear();
bool hascset;
iarc >> hascset;
if (!hascset) iarc >> vid_vec;
else {
cset = new graphlab::hopscotch_set<graphlab::vertex_id_type>(HASH_THRESHOLD);
iarc >> (*cset);
}
}
};
/*
A simple counting iterator which can be used as an insert iterator.
but only counts the number of elements inserted. Useful for
use with counting the size of an intersection using std::set_intersection
*/
template <typename T>
struct counting_inserter {
size_t* i;
counting_inserter(size_t* i):i(i) { }
counting_inserter& operator++() {
++(*i);
return *this;
}
void operator++(int) {
++(*i);
}
struct empty_val {
empty_val operator=(const T&) { return empty_val(); }
};
empty_val operator*() {
return empty_val();
}
typedef empty_val reference;
};
/*
* Computes the size of the intersection of two vid_vector's
*/
// static uint32_t count_set_intersect(
static size_t count_set_intersect(
const vid_vector& smaller_set,
const vid_vector& larger_set) {
if (smaller_set.cset == NULL && larger_set.cset == NULL) {
size_t i = 0;
counting_inserter<graphlab::vertex_id_type> iter(&i);
std::set_intersection(smaller_set.vid_vec.begin(), smaller_set.vid_vec.end(),
larger_set.vid_vec.begin(), larger_set.vid_vec.end(),
iter);
return i;
}
else if (smaller_set.cset == NULL && larger_set.cset != NULL) {
size_t i = 0;
foreach(graphlab::vertex_id_type vid, smaller_set.vid_vec) {
i += larger_set.cset->count(vid);
}
return i;
}
else if (smaller_set.cset != NULL && larger_set.cset == NULL) {
size_t i = 0;
foreach(graphlab::vertex_id_type vid, larger_set.vid_vec) {
i += smaller_set.cset->count(vid);
}
return i;
}
else {
size_t i = 0;
foreach(graphlab::vertex_id_type vid, *(smaller_set.cset)) {
i += larger_set.cset->count(vid);
}
return i;
}
}
/*
* Each vertex maintains a list of all its neighbors.
* and a final count for the number of triangles it is involved in
*/
struct vertex_data_type {
vertex_data_type(): num_triangles(0){ }
// A list of all its neighbors
vid_vector vid_set;
// The number of triangles this vertex is involved it.
// only used if "per vertex counting" is used
size_t num_triangles;
void save(graphlab::oarchive &oarc) const {
oarc << vid_set << num_triangles;
}
void load(graphlab::iarchive &iarc) {
iarc >> vid_set >> num_triangles;
}
};
/*
* Each edge is simply a counter of triangles
*/
// typedef uint32_t edge_data_type;
struct edge_data_type {
size_t n3;
bool sample_indicator;
void save(graphlab::oarchive &oarc) const {
//oarc << vid_set << num_triangles;
oarc << n3 << sample_indicator;
}
void load(graphlab::iarchive &iarc) {
iarc >> n3 >> sample_indicator;
}
};
struct edge_sum_gather {
size_t n3;
edge_sum_gather& operator+=(const edge_sum_gather& other) {
n3 += other.n3;
return *this;
}
// serialize
void save(graphlab::oarchive& oarc) const {
oarc << n3;
}
// deserialize
void load(graphlab::iarchive& iarc) {
iarc >> n3;
}
};
// To collect the set of neighbors, we need a message type which is
// basically a set of vertex IDs
bool PER_VERTEX_COUNT = false;
/*
* This is the gathering type which accumulates an array of
* all neighboring vertices.
* It is a simple wrapper around a vector with
* an operator+= which simply performs a +=
*/
struct set_union_gather {
graphlab::vertex_id_type v;
std::vector<graphlab::vertex_id_type> vid_vec;
set_union_gather():v(-1) {
}
size_t size() const {
if (v == (graphlab::vertex_id_type)-1) return vid_vec.size();
else return 1;
}
/*
* Combining with another collection of vertices.
* Union it into the current set.
*/
set_union_gather& operator+=(const set_union_gather& other) {
if (size() == 0) {
(*this) = other;
return (*this);
}
else if (other.size() == 0) {
return *this;
}
if (vid_vec.size() == 0) {
vid_vec.push_back(v);
v = (graphlab::vertex_id_type)(-1);
}
if (other.vid_vec.size() > 0) {
size_t ct = vid_vec.size();
vid_vec.resize(vid_vec.size() + other.vid_vec.size());
for (size_t i = 0; i < other.vid_vec.size(); ++i) {
vid_vec[ct + i] = other.vid_vec[i];
}
}
else if (other.v != (graphlab::vertex_id_type)-1) {
vid_vec.push_back(other.v);
}
return *this;
}
// serialize
void save(graphlab::oarchive& oarc) const {
oarc << bool(vid_vec.size() == 0);
if (vid_vec.size() == 0) oarc << v;
else oarc << vid_vec;
}
// deserialize
void load(graphlab::iarchive& iarc) {
bool novvec;
v = (graphlab::vertex_id_type)(-1);
vid_vec.clear();
iarc >> novvec;
if (novvec) iarc >> v;
else iarc >> vid_vec;
}
};
/*
* Define the type of the graph
*/
typedef graphlab::distributed_graph<vertex_data_type,
edge_data_type> graph_type;
void init_vertex(graph_type::vertex_type& vertex) {
vertex.data().num_triangles = 0;
}
void sample_edge(graph_type::edge_type& edge) {
if(graphlab::random::rand01() < sample_prob_keep)
edge.data().sample_indicator = 1;
else
edge.data().sample_indicator = 0;
}
/*
* This class implements the triangle counting algorithm as described in
* the header. On gather, we accumulate a set of all adjacent vertices.
* If per_vertex output is not necessary, we can use the optimization
* where each vertex only accumulates neighbors with greater vertex IDs.
*/
class triangle_count :
public graphlab::ivertex_program<graph_type,
set_union_gather>,
/* I have no data. Just force it to POD */
public graphlab::IS_POD_TYPE {
public:
bool do_not_scatter;
// Gather on all edges
edge_dir_type gather_edges(icontext_type& context,
const vertex_type& vertex) const {
return graphlab::ALL_EDGES;
}
/*
* For each edge, figure out the ID of the "other" vertex
* and accumulate a set of the neighborhood vertex IDs.
*/
gather_type gather(icontext_type& context,
const vertex_type& vertex,
edge_type& edge) const {
set_union_gather gather;
if(edge.data().sample_indicator == 0){
gather.v = -1;
}
else{
graphlab::vertex_id_type otherid = edge.target().id() == vertex.id() ?
edge.source().id() : edge.target().id();
// size_t other_nbrs = (edge.target().id() == vertex.id()) ?
// (edge.source().num_in_edges() + edge.source().num_out_edges()):
// (edge.target().num_in_edges() + edge.target().num_out_edges());
// size_t my_nbrs = vertex.num_in_edges() + vertex.num_out_edges();
// if (PER_VERTEX_COUNT || (other_nbrs > my_nbrs) || (other_nbrs == my_nbrs && otherid > vertex.id())) {
//if (PER_VERTEX_COUNT || otherid > vertex.id()) {
gather.v = otherid;
// }
}
return gather;
}
/*
* the gather result now contains the vertex IDs in the neighborhood.
* store it on the vertex.
*/
void apply(icontext_type& context, vertex_type& vertex,
const gather_type& neighborhood) {
do_not_scatter = false;
if (neighborhood.vid_vec.size() == 0) {
// neighborhood set may be empty or has only 1 element
vertex.data().vid_set.clear();
if (neighborhood.v != (graphlab::vertex_id_type(-1))) {
vertex.data().vid_set.vid_vec.push_back(neighborhood.v);
}
}
else {
vertex.data().vid_set.assign(neighborhood.vid_vec);
}
do_not_scatter = vertex.data().vid_set.size() == 0;
} // end of apply
/*
* Scatter over all edges to compute the intersection.
* I only need to touch each edge once, so if I scatter just on the
* out edges, that is sufficient.
*/
edge_dir_type scatter_edges(icontext_type& context,
const vertex_type& vertex) const {
if (do_not_scatter) return graphlab::NO_EDGES;
else return graphlab::OUT_EDGES;
}
/*
* For each edge, count the intersection of the neighborhood of the
* adjacent vertices. This is the number of triangles this edge is involved
* in.
*/
void scatter(icontext_type& context,
const vertex_type& vertex,
edge_type& edge) const {
// vertex_type othervtx = edge.target();
if (edge.data().sample_indicator == 1){
const vertex_data_type& srclist = edge.source().data();
const vertex_data_type& targetlist = edge.target().data();
if (targetlist.vid_set.size() < srclist.vid_set.size()) {
// edge.data().n3 += count_set_intersect(targetlist.vid_set, srclist.vid_set);
edge.data().n3 = count_set_intersect(targetlist.vid_set, srclist.vid_set);
}
else {
// edge.data().n3 += count_set_intersect(srclist.vid_set, targetlist.vid_set);
edge.data().n3 = count_set_intersect(srclist.vid_set, targetlist.vid_set);
}
}
}
};
/*
* This class is used in a second engine call if per vertex counts are needed.
* The number of triangles a vertex is involved in can be computed easily
* by summing over the number of triangles each adjacent edge is involved in
* and dividing by 2.
*/
class get_per_vertex_count :
// public graphlab::ivertex_program<graph_type, size_t>,
public graphlab::ivertex_program<graph_type, edge_sum_gather>,
/* I have no data. Just force it to POD */
public graphlab::IS_POD_TYPE {
public:
// Gather on all edges
edge_dir_type gather_edges(icontext_type& context,
const vertex_type& vertex) const {
return graphlab::ALL_EDGES;
}
// We gather the number of triangles each edge is involved in
gather_type gather(icontext_type& context,
const vertex_type& vertex,
edge_type& edge) const {
edge_sum_gather gather;
if (edge.data().sample_indicator == 1){
gather.n3 = edge.data().n3;
}
else {
gather.n3 = 0;
}
return gather;
}
/* the gather result is the total sum of the number of triangles
* each adjacent edge is involved in . Dividing by 2 gives the
* desired result.
*/
void apply(icontext_type& context, vertex_type& vertex,
const gather_type& num_triangles) {
vertex.data().vid_set.clear();
vertex.data().num_triangles = num_triangles.n3 / 2;
}
// No scatter
edge_dir_type scatter_edges(icontext_type& context,
const vertex_type& vertex) const {
return graphlab::NO_EDGES;
}
};
typedef graphlab::synchronous_engine<triangle_count> engine_type;
/* Used to sum over all the edges in the graph in a
* map_reduce_edges call
* to get the total number of triangles
*/
size_t get_edge_data(const graph_type::edge_type& e) {
if (e.data().sample_indicator == 1)
return e.data().n3;
else
return 0;
}
vertex_data_type get_vertex_data(const graph_type::vertex_type& v) {
return v.data();
}
size_t get_edge_sample_indicator(const graph_type::edge_type& e){
return e.data().sample_indicator;
}
/*
* A saver which saves a file where each line is a vid / # triangles pair
*/
struct save_triangle_count{
std::string save_vertex(graph_type::vertex_type v) {
double nt = v.data().num_triangles;
// double n_followed = v.num_out_edges();
// double n_following = v.num_in_edges();
return graphlab::tostr(v.id()) + "\t" +
graphlab::tostr(nt) + "\t";
// graphlab::tostr(n_followed) + "\t" +
// graphlab::tostr(n_following) + "\n";
}
std::string save_edge(graph_type::edge_type e) {
return "";
}
};
int main(int argc, char** argv) {
std::cout << "This program counts the exact number of triangles in the "
"provided graph.\n\n";
graphlab::command_line_options clopts("Exact Triangle Counting. "
"Given a graph, this program computes the total number of triangles "
"in the graph. An option (per_vertex) is also provided which "
"computes for each vertex, the number of triangles it is involved in."
"The algorithm assumes that each undirected edge appears exactly once "
"in the graph input. If edges may appear more than once, this procedure "
"will over count.");
std::string prefix, format;
std::string per_vertex;
clopts.attach_option("graph", prefix,
"Graph input. reads all graphs matching prefix*");
clopts.attach_option("format", format,
"The graph format");
clopts.attach_option("ht", HASH_THRESHOLD,
"Above this size, hash sets are used");
clopts.attach_option("per_vertex", per_vertex,
"If not empty, will count the number of "
"triangles each vertex belongs to and "
"save to file with prefix \"[per_vertex]\". "
"The algorithm used is slightly different "
"and thus will be a little slower");
clopts.attach_option("sample_keep_prob", sample_prob_keep,
"Probability of keeping edge during sampling");
clopts.attach_option("sample_iter", sample_iter,
"Number of sampling iterations (global count)");
clopts.attach_option("min_prob", min_prob,
"min prob");
clopts.attach_option("max_prob", max_prob,
"max prob");
clopts.attach_option("prob_step", prob_step,
"prob step");
if(!clopts.parse(argc, argv)) return EXIT_FAILURE;
if (prefix == "") {
std::cout << "--graph is not optional\n";
clopts.print_description();
return EXIT_FAILURE;
}
else if (format == "") {
std::cout << "--format is not optional\n";
clopts.print_description();
return EXIT_FAILURE;
}
if (per_vertex != "") PER_VERTEX_COUNT = true;
// Initialize control plane using mpi
graphlab::mpi_tools::init(argc, argv);
graphlab::distributed_control dc;
graphlab::launch_metric_server();
// load graph
graph_type graph(dc, clopts);
graph.load_format(prefix, format);
graph.finalize();
dc.cout() << "Number of vertices: " << graph.num_vertices() << std::endl
<< "Number of edges (before sampling): " << graph.num_edges() << std::endl;
dc.cout() << "sample_prob_keep = " << sample_prob_keep << std::endl;
dc.cout() << "sample_iter = " << sample_iter << std::endl;
size_t reference_bytes;
double total_time;
if(min_prob == max_prob){//i.e., they were not specified by user
min_prob = sample_prob_keep;
max_prob = sample_prob_keep;
}
double new_sample_prob = min_prob;
while(new_sample_prob <= max_prob + 0.000000001){
sample_prob_keep = new_sample_prob;
new_sample_prob += prob_step;
//START ITERATIONS HERE
for (int sit = 0; sit < sample_iter; sit++) {
reference_bytes = dc.network_bytes_sent();
dc.cout() << "Iteration " << sit+1 << " of " << sample_iter << std::endl;
graphlab::timer ti;
// Initialize the vertex data
//graph.transform_vertices(init_vertex);
//Sampling
graph.transform_edges(sample_edge);
//total_edges = graph.map_reduce_vertices<size_t>(get_vertex_degree)/2;
//total_edges = graph.map_reduce_edges<size_t>(get_edge_sample_indicator);
//dc.cout() << "Total edges counted (after sampling):" << total_edges << std::endl;
// create engine to count the number of triangles
dc.cout() << "Counting Triangles..." << std::endl;
// engine_type engine(dc, graph, clopts);
graphlab::synchronous_engine<triangle_count> engine(dc, graph, clopts);
engine.signal_all();
engine.start();
// dc.cout() << "Counted in " << ti.current_time() << " seconds" << std::endl;
if (PER_VERTEX_COUNT == false) {
// dc.cout() << count << " Triangles" << std::endl;
graphlab::synchronous_engine<get_per_vertex_count> engine(dc, graph, clopts);
engine.signal_all();
engine.start();
size_t count = graph.map_reduce_edges<size_t>(get_edge_data);
// vertex_data_type global_counts = graph.map_reduce_vertices<vertex_data_type>(get_vertex_data);
dc.cout() << "Counted in " << ti.current_time() << " seconds" << std::endl;
size_t denom = (graph.num_vertices()*(graph.num_vertices()-1)*(graph.num_vertices()-2))/6.; //normalize by |V| choose 3, THIS IS NOT ACCURATE!
//size_t denom = 1;
dc.cout() << "denominator: " << denom << std::endl;
// dc.cout() << "Global Triangle count: " << global_counts.num_triangles/3 << std::endl;
// dc.cout() << "Global count from estimators: "
// << (global_counts.num_triangles/3)/pow(sample_prob_keep, 3) << std::endl;
//originally, only count triangle when vertices are in 1 order. but must count all if local counts
// run as if local count and divide by 3 at the end
// dc.cout() << "Global Triangle count: " << count/3 << std::endl;
dc.cout() << "Global count from estimators (edge reduce): "
<< (count/3)/pow(sample_prob_keep, 3) << std::endl;
total_time = ti.current_time();
std::ofstream myfile;
char fname[20];
sprintf(fname,"counts_triangles.txt");
bool is_new_file = true;
if (std::ifstream(fname)){
is_new_file = false;
}
myfile.open (fname,std::fstream::in | std::fstream::out | std::fstream::app);
if(is_new_file) myfile << "#graph\tsample_prob_keep\ttriangles\truntime" << std::endl;
myfile << prefix << "\t"
<< sample_prob_keep << "\t"
<< std::setprecision (std::numeric_limits<double>::digits10 + 3)
<< round((count/3)/pow(sample_prob_keep, 3)) << "\t"
<< std::setprecision (6)
<< total_time
<< std::endl;
myfile.close();
sprintf(fname,"netw_triangles_%d.txt",dc.procid());
myfile.open (fname,std::fstream::in | std::fstream::out | std::fstream::app);
myfile << dc.network_bytes_sent() - reference_bytes <<"\n";
myfile.close();
}