forked from waynebhayes/BLANT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCC.c
1233 lines (1150 loc) · 43.6 KB
/
CC.c
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 <sys/file.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "misc.h"
#include "tinygraph.h"
#include "graph.h"
#include "rand48.h"
#include "heap.h"
#include "blant.h"
#include "queue.h"
#include "multisets.h"
#define PARANOID_ASSERTS 1 // turn on paranoid checking --- slows down execution by a factor of 2-3
// Enable the code that uses C++ to parse input files?
#define SHAWN_AND_ZICAN 0
static int *_pairs, _numNodes, _numEdges, _maxEdges=1024, _seed;
char **_nodeNames;
// Below are the sampling methods; pick one on the last line
#define SAMPLE_UNBIASED 1 // makes things REALLY REALLY slow. Like 10-100 samples per second rather than a million.
#define SAMPLE_NODE_EXPANSION 2 // sample using uniform node expansion; about 100,000 samples per second
#define SAMPLE_EDGE_EXPANSION 3 // Fastest, up to a million samples per second
#define SAMPLE_RESERVOIR 4 // Lu Bressan's reservoir sampler, reasonably but not entirely unbiased.
#define SAMPLE_MCMC 5 // MCMC Algorithm estimates graphlet frequency with a random walk
#ifndef RESERVOIR_MULTIPLIER
// this*k is the number of steps in the Reservoir walk. 8 seems to work best, empirically.
#define RESERVOIR_MULTIPLIER 8
#endif
#define SAMPLE_METHOD SAMPLE_NODE_EXPANSION
#define MAX_TRIES 100 // max # of tries in cumulative sampling before giving up
#define ALLOW_DISCONNECTED_GRAPHLETS 0
// The following is the most compact way to store the permutation between a non-canonical and its canonical representative,
// when k=8: there are 8 entries, and each entry is a integer from 0 to 7, which requires 3 bits. 8*3=24 bits total.
// For simplicity we use the same 3 bits per entry, and assume 8 entries, even for k<8. It wastes memory for k<4, but
// makes the coding much simpler.
typedef unsigned char kperm[3]; // The 24 bits are stored in 3 unsigned chars.
static unsigned int _Bk, _k; // _k is the global variable storing k; _Bk=actual number of entries in the canon_map for given k.
static int _numCanon, _canonList[MAX_CANONICALS];
static int _numOrbits, _orbitList[MAX_CANONICALS][maxK]; // Jens: this may not be the array we need, but something like this...
enum OutputMode {undef, indexGraphlets, graphletFrequency, outputODV, outputGDV};
static enum OutputMode _outputMode = undef;
static unsigned long int _graphletCount[MAX_CANONICALS];
// A bit counter-intuitive: we need to allocate this many vectors each of length [_numNodes],
// and then the degree for node v, graphlet/orbit g is _degreeVector[g][v], NOT [v][g].
// We do this simply because we know the length of MAX_CANONICALS so we pre-know the length of
// the first dimension, otherwise we'd need to get more funky with the pointer allocation.
// Only one of these actually get allocated, depending upon outputMode.
static unsigned long int *_graphletDegreeVector[MAX_CANONICALS];
static unsigned long int *_orbitDegreeVector[MAX_ORBITS];
// If you're squeemish then use this one to access the degrees:
#define ODV(node,orbit) _orbitDegreeVector[orbit][node]
#define GDV(node,graphlet) _graphletDegreeVector[graphlet][node]
// number of parallel threads to run. This must be global because we may get called from C++.
static int _THREADS;
// Here's where we're lazy on saving memory, and we could do better. We're going to allocate a static array
// that is big enough for the 256 million permutations from non-canonicals to canonicals for k=8, even if k<8.
// So we're allocating 256MBx3=768MB even if we need much less. I figure anything less than 1GB isn't a big deal
// these days. It needs to be aligned to a page boundary since we're going to mmap the binary file into this array.
static kperm Permutations[maxBk] __attribute__ ((aligned (8192)));
// Here's the actual mapping from non-canonical to canonical, same argument as above wasting memory, and also mmap'd.
// So here we are allocating 256MB x sizeof(short int) = 512MB.
// Grand total statically allocated memory is exactly 1.25GB.
static short int _K[maxBk] __attribute__ ((aligned (8192)));
//
static unsigned L; // walk length for MCMC algorithm
static int _alphaList[MAX_CANONICALS];
/* AND NOW THE CODE */
// You provide a permutation array, we fill it with the permutation extracted from the compressed Permutation mapping.
// There is the inverse transformation, called "EncodePerm", in createBinData.c.
static void ExtractPerm(char perm[_k], int i)
{
int j, i32 = 0;
for(j=0;j<3;j++) i32 |= (Permutations[i][j] << j*8);
for(j=0;j<_k;j++)
perm[j] = (i32 >> 3*j) & 7;
}
// Given the big graph G and a set of nodes in V, return the TINY_GRAPH created from the induced subgraph of V on G.
static TINY_GRAPH *TinyGraphInducedFromGraph(TINY_GRAPH *Gv, GRAPH *G, int *Varray)
{
unsigned i, j;
TinyGraphEdgesAllDelete(Gv);
for(i=0; i < _k; i++) for(j=i+1; j < _k; j++)
if(GraphAreConnected(G, Varray[i], Varray[j]))
TinyGraphConnect(Gv, i, j);
return Gv;
}
// return how many nodes found. If you call it with startingNode == 0 then we automatically clear the visited array
static TSET _visited;
static int NumReachableNodes(TINY_GRAPH *g, int startingNode)
{
if(startingNode == 0) TSetEmpty(_visited);
TSetAdd(_visited,startingNode);
int j, Varray[maxK], numVisited = 0;
int numNeighbors = TSetToArray(Varray, g->A[startingNode]);
assert(numNeighbors == g->degree[startingNode]);
for(j=0; j<numNeighbors; j++)if(!TSetIn(_visited,Varray[j])) numVisited += NumReachableNodes(g,Varray[j]);
return 1+numVisited;
}
// Given the big graph G and an integer k, return a k-graphlet from G
// in the form of a SET of nodes called V. When complete, |V| = k.
// Caller is responsible for allocating the set V and its array Varray.
// The algorithm works by maintaining the exact set of edges that are
// emanating out of the graphlet-in-construction. Then we pick one of
// these edges uniformly at random, add the node at the endpoint of that
// edge to the graphlet, and then add all the outbound edges from that
// new node (ie., edges that are not going back inside the graphlet).
// So the "outset" is the set of edges going to nodes exactly distance
// one from the set V, as V is being built.
static SET *SampleGraphletNodeBasedExpansion(SET *V, int *Varray, GRAPH *G, int k)
{
static SET *outSet;
static int numIsolatedNodes;
if(!outSet)
outSet = SetAlloc(G->n); // we won't bother to free this since it's static.
else if(G->n > outSet->n)
SetResize(outSet, G->n);
else
SetEmpty(outSet);
int v1, v2, i;
int nOut = 0, outbound[G->n]; // vertices one step outside the boundary of V
assert(V && V->n >= G->n);
SetEmpty(V);
int edge = G->numEdges * drand48();
v1 = G->edgeList[2*edge];
v2 = G->edgeList[2*edge+1];
SetAdd(V, v1); Varray[0] = v1;
SetAdd(V, v2); Varray[1] = v2;
// The below loops over neighbors can take a long time for large graphs with high mean degree. May be faster
// with bit operations if we stored the adjacency matrix... which may be too big to store for big graphs. :-(
for(i=0; i < G->degree[v1]; i++)
{
int nv1 = G->neighbor[v1][i];
if(nv1 != v2)
{
assert(!SetIn(V, nv1)); // assertion to ensure we're in line with faye
SetAdd(outSet, (outbound[nOut++] = nv1));
}
}
for(i=0; i < G->degree[v2]; i++)
{
int nv2 = G->neighbor[v2][i];
if(nv2 != v1 && !SetIn(outSet, nv2))
{
assert(!SetIn(V, nv2)); // assertion to ensure we're in line with faye
SetAdd(outSet, (outbound[nOut++] = nv2));
}
}
for(i=2; i<k; i++)
{
int j;
if(nOut == 0) // the graphlet has saturated it's connected component
{
#if ALLOW_DISCONNECTED_GRAPHLETS
while(SetIn(V, (j = G->n*drand48())))
; // must terminate since k <= G->n
outbound[nOut++] = j;
j = 0;
#else
static int depth;
depth++;
// must terminate eventually as long as there's at least one connected component with >=k nodes.
assert(depth < MAX_TRIES); // graph is too disconnected
V = SampleGraphletNodeBasedExpansion(V, Varray, G, k);
depth--;
return V;
#endif
}
else
j = nOut * drand48();
v1 = outbound[j];
SetDelete(outSet, v1);
SetAdd(V, v1); Varray[i] = v1;
outbound[j] = outbound[--nOut]; // nuke v1 from the list of outbound by moving the last one to its place
for(j=0; j<G->degree[v1];j++) // another loop over neighbors that may take a long time...
{
v2 = G->neighbor[v1][j];
if(!SetIn(outSet, v2) && !SetIn(V, v2))
SetAdd(outSet, (outbound[nOut++] = v2));
}
}
assert(i==k);
#if PARANOID_ASSERTS
assert(SetCardinality(V) == k);
assert(nOut == SetCardinality(outSet));
#endif
return V;
}
/*
** This is a faster graphlet sampling routine, although it may produce a
** distribution of graphlets that is further from "ideal" than the above.
** The difference is that in the above method we explicitly build and maintain
** the "outset" (the set of nodes one step outside V), but this can expensive
** when the mean degree of the graph G is high, since we have to add all the
** new edges emanating from each new node that we add to V. Instead, here we
** are not going to explicitly maintain this outset. Instead, we're simply
** going to remember the total number of edges emanating from every node in
** V *including* edges heading back into V, which are techically useless to us.
** This sum is just the sum of all the degrees of all the nodes in V. Call this
** number "outDegree". Then, among all the edges emanating out of every node in
** V, we pick a random node from V where the probablity of picking any node v is
** proportional to its degree. Then we pick one of the edges emanating out of v
** uniformly at random. Thus, every edge leaving every node in V has equal
** probability of being chosen---even though some of them lead back into V. If
** that is the case, then we throw away that edge and try the whole process again.
** The advantage here is that for graphs G with high mean degree M, the probability
** of picking an edge leading back into V is bounded by (k-1)/M, which becomes small
** as M gets large, and so we're very unlikely to "waste" samples. Thus, unlike the
** above algorithm that gets *more* expensive with increasing density of G, this
** algorithm actually get cheaper with increasing density.
** Another way of saying this is that we avoid actually building the outSet
** as we do above, we instead simply keep a *estimated* count C[v] of separate
** outSets of each node v in the accumulating graphlet. Then we pick
** an integer in the range [0,..sum(C)), go find out what node that is,
** and if it turns out to be a node already in V, then we simply try again.
** It turns out that even if the mean degree is small, this method is *still*
** faster. In other words, it's *always* faster than the above method. The
** only reason we may prefer the above method is because it's theoretically cleaner
** to describe the distribution of graphlets that comes from it---although
** empirically this one does reasonably well too. However, if the only goal
** is blinding speed at graphlet sampling, eg for building a graphlet database
** index, then this is the preferred method.
*/
static SET *SampleGraphletEdgeBasedExpansion(SET *V, int *Varray, GRAPH *G, int k)
{
int edge = G->numEdges * drand48(), v1, v2;
assert(V && V->n >= G->n);
SetEmpty(V);
int nOut = 0;
v1 = G->edgeList[2*edge];
v2 = G->edgeList[2*edge+1];
SetAdd(V, v1); Varray[0] = v1;
SetAdd(V, v2); Varray[1] = v2;
int vCount = 2;
int outDegree = G->degree[v1] + G->degree[v2];
static int cumulative[maxK];
cumulative[0] = G->degree[v1]; // where v1 = Varray[0]
cumulative[1] = G->degree[v2] + cumulative[0];
static SET *internal; // mark choices of whichNeigh that are discovered to be internal
static int Gn;
if(!internal) {internal = SetAlloc(G->n); Gn = G->n;}
else if(Gn != G->n) {SetFree(internal); internal = SetAlloc(G->n); Gn=G->n;}
else SetEmpty(internal);
int numTries = 0;
while(vCount < k)
{
int i, whichNeigh;
while(numTries < MAX_TRIES && SetIn(internal, (whichNeigh = outDegree * drand48())))
++numTries; // which edge to choose among all edges leaving all nodes in V so far?
if(numTries >= MAX_TRIES) {
#if ALLOW_DISCONNECTED_GRAPHLETS
// get a new node outside this connected component.
// Note this will return a disconnected graphlet.
while(SetIn(V, (newNode = G->n*drand48())))
; // must terminate since k <= G->n
numTries = 0;
outDegree = 0;
int j;
for(j=0; j<vCount; j++) // avoid picking these nodes ever again.
cumulative[j] = 0;
SetEmpty(internal);
#else
static int depth;
depth++;
assert(depth < MAX_TRIES);
V = SampleGraphletEdgeBasedExpansion(V, Varray, G, k);
depth--;
return V;
#endif
}
for(i=0; cumulative[i] <= whichNeigh; i++)
; // figure out whose neighbor it is
int localNeigh = whichNeigh-(cumulative[i]-G->degree[Varray[i]]); // which neighbor of node i?
int newNode = G->neighbor[Varray[i]][localNeigh];
#if PARANOID_ASSERTS
// really should check some of these a few lines higher but let's group all the paranoia in one place.
assert(i < vCount);
assert(0 <= localNeigh && localNeigh < G->degree[Varray[i]]);
assert(0 <= newNode && newNode < G->n);
#endif
if(SetIn(V, newNode))
{
SetAdd(internal, whichNeigh);
if(++numTries < MAX_TRIES)
continue;
else // graph is too disconnected
{
#if PARANOID_ASSERTS
// We are probably in a connected component with fewer than k nodes.
// Test that hypothesis.
assert(!GraphCCatLeastK(G, v1, k));
#endif
#if ALLOW_DISCONNECTED_GRAPHLETS
// get a new node outside this connected component.
// Note this will return a disconnected graphlet.
while(SetIn(V, (newNode = G->n*drand48())))
; // must terminate since k <= G->n
numTries = 0;
outDegree = 0;
int j;
for(j=0; j<vCount; j++) // avoid picking these nodes ever again.
cumulative[j] = 0;
SetEmpty(internal);
#else
static int depth;
depth++;
assert(depth < MAX_TRIES);
V = SampleGraphletEdgeBasedExpansion(V, Varray, G, k);
depth--;
return V;
#endif
}
}
SetAdd(V, newNode);
cumulative[vCount] = cumulative[vCount-1] + G->degree[newNode];
Varray[vCount++] = newNode;
outDegree += G->degree[newNode];
#if PARANOID_ASSERTS
assert(SetCardinality(V) == vCount);
assert(outDegree == cumulative[vCount-1]);
#endif
}
#if PARANOID_ASSERTS
assert(SetCardinality(V) == k);
assert(vCount == k);
#endif
return V;
}
// From the paper: ``Sampling Connected Induced Subgraphs Uniformly at Random''
// Xuesong Lu and Stephane Bressan, School of Computing, National University of Singapore
// {xuesong,steph}@nus.edu.sg
// 24th International Conference on Scientific and Statistical Database Management, 2012
// Note that they suggest *edge* based expansion to select the first k nodes, and then
// use reservoir sampling for the rest. But we know edge-based expansion sucks, so we'll
// start with a better starting guess, which is node-based expansion.
static SET *SampleGraphletLuBressanReservoir(SET *V, int *Varray, GRAPH *G, int k)
{
// Start by getting the first k nodes using a previous method. Once you figure out which is
// better, it's probably best to share variables so you don't have to recompute the outset here.
#if 1 // the following is copied almost verbatim from NodeEdgeExpansion, just changing for loop to while loop.
static SET *outSet;
static int numIsolatedNodes;
if(!outSet)
outSet = SetAlloc(G->n); // we won't bother to free this since it's static.
else if(G->n > outSet->n)
SetResize(outSet, G->n);
else
SetEmpty(outSet);
int v1, v2, i;
int nOut = 0, outbound[G->n]; // vertices one step outside the boundary of V
assert(V && V->n >= G->n);
SetEmpty(V);
int edge = G->numEdges * drand48();
v1 = G->edgeList[2*edge];
v2 = G->edgeList[2*edge+1];
SetAdd(V, v1); Varray[0] = v1;
SetAdd(V, v2); Varray[1] = v2;
// The below loops over neighbors can take a long time for large graphs with high mean degree. May be faster
// with bit operations if we stored the adjacency matrix... which may be too big to store for big graphs. :-(
for(i=0; i < G->degree[v1]; i++)
{
int nv1 = G->neighbor[v1][i];
if(nv1 != v2) SetAdd(outSet, (outbound[nOut++] = nv1));
}
for(i=0; i < G->degree[v2]; i++)
{
int nv2 = G->neighbor[v2][i];
if(nv2 != v1 && !SetIn(outSet, nv2)) SetAdd(outSet, (outbound[nOut++] = nv2));
}
i=2;
// while(i<k || nOut > 0) // always do the loop at least k times, but i>=k is the reservoir phase.
while(i < RESERVOIR_MULTIPLIER*k) // value of 8 seems to work best from empirical studies.
{
int candidate;
if(nOut ==0) // the graphlet has saturated its connected component before getting to k, start elsewhere
{
#if ALLOW_DISCONNECTED_GRAPHLETS
if(i < k)
{
int tries=0;
while(SetIn(V, (v1 = G->n*drand48())))
assert(tries++<MAX_TRIES); // graph is too disconnected
outbound[nOut++] = v1; // recall that nOut was 0 to enter this block, so now it's 1
candidate = 0; // representing v1 as the 0'th entry in the outbound array
}
else
assert(i==k); // we're done because i >= k and nOut == 0... but we shouldn't get here.
#else
static int depth;
depth++;
assert(depth < MAX_TRIES); // graph is too disconnected
V = SampleGraphletLuBressanReservoir(V, Varray, G, k);
depth--;
return V;
#endif
}
else
{
candidate = nOut * drand48();
v1 = outbound[candidate];
}
assert(v1 == outbound[candidate]);
SetDelete(outSet, v1);
outbound[candidate] = outbound[--nOut];// nuke v1 by moving the last one to its place
if(i < k)
{
Varray[i] = v1;
SetAdd(V, v1);
int j;
for(j=0; j<G->degree[v1];j++) // another loop over neighbors that may take a long time...
{
v2 = G->neighbor[v1][j];
if(!SetIn(outSet, v2) && !SetIn(V, v2))
SetAdd(outSet, (outbound[nOut++] = v2));
}
}
else
{
double reservoir_alpha = drand48();
if(reservoir_alpha < k/(double)i)
{
static TINY_GRAPH *T;
if(!T) T = TinyGraphAlloc(k);
static int graphetteArray[maxK], distArray[maxK];
#if PARANOID_ASSERTS
// ensure it's connected before we do the replacement
TinyGraphEdgesAllDelete(T);
TinyGraphInducedFromGraph(T, G, Varray);
#if 0
printf("NRN = %d\n", NumReachableNodes(T, 0));
printf("BFS = %d\n", TinyGraphBFS(T, 0, k, graphetteArray, distArray));
#endif
assert(NumReachableNodes(T, 0) == TinyGraphBFS(T, 0, k, graphetteArray, distArray));
assert(NumReachableNodes(T, 0) == k);
#endif
int memberToDelete = k*drand48();
v2 = Varray[memberToDelete]; // remember the node delated from V in case we need to revert
Varray[memberToDelete] = v1; // v1 is the outbound candidate.
TinyGraphEdgesAllDelete(T);
TinyGraphInducedFromGraph(T, G, Varray);
assert(NumReachableNodes(T,0) == TinyGraphBFS(T, 0, k, graphetteArray, distArray));
if(NumReachableNodes(T, 0) < k)
Varray[memberToDelete] = v2; // revert the change because the graph is not connected
else // add the new guy and delete the old
{
#if PARANOID_ASSERTS
assert(SetCardinality(V) == k);
#endif
SetDelete(V, v2);
SetAdd(V, v1);
#if PARANOID_ASSERTS
assert(SetCardinality(V) == k);
#endif
int j;
for(j=0; j<G->degree[v1];j++) // another loop over neighbors that may take a long time...
{
v2 = G->neighbor[v1][j];
if(!SetIn(outSet, v2) && !SetIn(V, v2))
SetAdd(outSet, (outbound[nOut++] = v2));
}
}
}
}
i++;
}
#else
SampleGraphletEdgeBasedExpansion(V, Varray, G, k);
#endif
}
// MCMC getNeighbor Gets a random neighbo of a d graphlet as an array of vertices Xcurrent
int *MCMCGetNeighbor(int *Xcurrent, GRAPH *G)
{
static SET *outSet;
static int numIsolatedNodes;
if(!outSet)
outSet = SetAlloc(G->n);
else if(G->n > outSet->n)
SetResize(outSet, G->n);
else
SetEmpty(outSet);
if (mcmc_d == 2)
{
int oldu = Xcurrent[0];
int oldv = Xcurrent[1];
static int numTries = 0;
while (oldu == Xcurrent[0] && oldv == Xcurrent[1]) {
assert(++numTries < MAX_TRIES);
double p = drand48();
// if 0 < p < 1, p < deg(u) + deg(v) then
if (p < ((double)G->degree[Xcurrent[0]])/(G->degree[Xcurrent[0]] + G->degree[Xcurrent[1]])) {
// select randomly from Neigh(u) and swap
int neighbor = (int) (G->degree[Xcurrent[0]] * drand48());
Xcurrent[1] = G->neighbor[Xcurrent[0]][neighbor];
}
else {
// select randomly from Neigh(v) and swap
int neighbor = (int) (G->degree[Xcurrent[1]] * drand48());
Xcurrent[0] = G->neighbor[Xcurrent[1]][neighbor];
}
}
numTries = 0;
#if PARANOID_ASSERTS
assert(Xcurrent[0] != Xcurrent[1]);
assert(oldu != Xcurrent[0] || oldv != Xcurrent[1]);
assert(oldu != Xcurrent[1] || oldv != Xcurrent[0]);
#endif
}
else Fatal("Not implemented. Set d to 2");
return Xcurrent;
}
//Crawls one step along the graph updating our multiset, queue, and newest graphlet array
void crawlOneStep(MULTISET *XLS, QUEUE *XLQ, int* X, GRAPH *G) {
int v, i;
for (i = 0; i < mcmc_d; i++) { //Remove oldest d graphlet from queue and multiset
v = QueueGet(XLQ).i;
MultisetDelete(XLS, v);
}
MCMCGetNeighbor(X, G); //Gets a neighbor graphlet of the most recent d vertices and add to queue and multiset
for (i = 0; i < mcmc_d; i++) {
MultisetAdd(XLS, X[i]);
QueuePut(XLQ, (foint) X[i]);
}
}
// WalkLSteps fills Varray, V, XLS, XLQ with L dgraphlets
void WalkLSteps(int *Varray, SET *V, MULTISET *XLS, QUEUE *XLQ, int* X, GRAPH *G, int k)
{
//For now d must be equal to 2 because we start by picking a random edge
int numNodes = 0;
if (mcmc_d != 2) {
Fatal("mcmc_d must be set to 2 in blant.h for now");
} else {
//Pick a random edge. Add the vertices from it to our data structures
int edge = G->numEdges * drand48();
X[0] = G->edgeList[2*edge];
X[1] = G->edgeList[2*edge+1];
MultisetAdd(XLS, X[0]); QueuePut(XLQ, (foint) X[0]);
MultisetAdd(XLS, X[1]); QueuePut(XLQ, (foint) X[1]);
}
//Get the data structures up to L d graphlets. Start at 1 because 1 d graphlet already there
int l = k - mcmc_d + 1, i, j;
for (i = 1; i < l; i++) {
MCMCGetNeighbor(X, G); //After each call latest graphlet is in X array
for (j = 0; j < mcmc_d; j++) {
MultisetAdd(XLS, X[j]);
QueuePut(XLQ, (foint) X[j]);
}
}
#if PARANOID_ASSERTS
assert(QueueSize(XLQ) == l*mcmc_d);
#endif
//Keep crawling til we have k distinct vertices
static int numTries = 0;
while (MultisetSupport(XLS) < k) {
assert(++numTries < MAX_TRIES); //If we crawl 100 steps without k distinct vertices. Todo restart
crawlOneStep(XLS, XLQ, X, G);
}
numTries = 0;
#if PARANOID_ASSERTS
assert(MultisetSupport(XLS) == k);
#endif
}
//Given preallocated k graphlet and d graphlet. Assumes Gk is connected
int ComputeAlpha(TINY_GRAPH *Gk, TINY_GRAPH *Gd, int k, int L) {
// generate all the edges of g
// 0 can result in DIVIDE BY ZERO ERROR
assert(k >= 3 && k <=8); //TSET used limits to 8 bits of set represntation.
int alpha = 0;
unsigned combinArrayD[mcmc_d]; //Used to hold combinations of d graphlets from our k graphlet
unsigned combinArrayL[L]; //Used to hold combinations of L d graphlets from Darray
COMBIN * Dcombin = CombinZeroth(k, mcmc_d, combinArrayD);
int numDGraphlets = CombinChoose(k, mcmc_d); //The number of possible d graphlets in our k graphlet
int Darray[numDGraphlets * mcmc_d]; //Vertices in our d graphlets
//Fill the S array with all connected d graphlets from Gk
int SSize = 0;
int i, j;
if (mcmc_d != 2) {
do {
TSET mask = 0;
for (i = 0; i < mcmc_d; i++) {
TSetAdd(mask, Dcombin->array[i]);
}
Gd = TinyGraphInduced(Gd, Gk, mask);
if (TinyGraphDFSConnected(Gd, 0))
{
for (i = 0; i < mcmc_d; i++)
{
Darray[SSize*mcmc_d+i] = Dcombin->array[i];
}
SSize++;
}
} while (CombinNext(Dcombin));
} else {
do { //if there is an edge between any two vertices in the graphlet
if (TinyGraphAreConnected(Gk, combinArrayD[0], combinArrayD[1]))
{ //add it to the Darray
Darray[SSize*mcmc_d] = Dcombin->array[i];
Darray[SSize*mcmc_d+1] = Dcombin->array[i];
SSize++;
}
} while (CombinNext(Dcombin));
}
//for s over all combinations of L elements in S
COMBIN *Lcombin = CombinZeroth(SSize, L, combinArrayL);
do {
//add vertices in combinations to set.
TSET mask = 0;
for (i = 0; i < L; i++) {
for (j = 0; i < mcmc_d; j++) {
TSetAdd(mask, Darray[Lcombin->array[i]*mcmc_d+j]);
}
}
//if Size of nodeset of nodes in each element of s == k then
if (TSetCardinality(mask) == k)
{
int g1, g2;
for (g1 = 0; g1 < L; g1++) {
for (g2 = g1; g2 < L; g2++) {
TSET tset = 0;
for (i = 0; i < mcmc_d; i++) {
TSetAdd(tset, Darray[Lcombin->array[g1]*mcmc_d+i]);
TSetAdd(tset, Darray[Lcombin->array[g2]*mcmc_d+i]);
}
if (TSetCardinality(tset) == mcmc_d-1) {
alpha += 2;
}
}
}
}
} while (CombinNext(Lcombin));
CombinFree(Dcombin);
CombinFree(Lcombin);
}
// MCMC sampleGraphletMCMC. This as associated functions are not reentrant.
static SET *SampleGraphletMCMC(SET *V, int *Varray, GRAPH *G, int k) {
static Boolean setup = false;
static MULTISET *XLS; //A multiset holding L dgraphlets as separate vertex integers
static QUEUE *XLQ; //A queue holding L dgraphlets as separate vertex integers
static int Xcurrent[mcmc_d]; //d vertices for MCMCgetneighbor
if (!XLQ || !XLS) {
XLQ = QueueAlloc(k*mcmc_d);
XLS = MultisetAlloc(G->n);
}
//The first time we run this, or when we restart. We want to find our initial L d graphlets.
if (!setup) {
setup = true;
MultisetEmpty(XLS);
while (QueueSize(XLQ) > 0) QueueGet(XLQ);
WalkLSteps(Varray, V, XLS, XLQ, Xcurrent, G, k);
} else {
#if PARANOID_ASSERTS
assert(QueueSize(XLQ) == 2 * (k-mcmc_d+1));
#endif
//Keep crawling til we have k distinct vertices. Crawl at least once
do {
crawlOneStep(XLS, XLQ, Xcurrent, G);
} while (MultisetSupport(XLS) != k);
}
#if PARANOID_ASSERTS
assert(MultisetSupport(XLS) == k);
#endif
//Our queue now contains k distinct nodes. Fill the set V and array Varray with them
int num, numNodes = 0, i;
SetEmpty(V);
for (i = 0; i < XLQ->length; i++) {
int num = (XLQ->queue[(XLQ->front + i) % XLQ->maxSize]).i;
if (!SetIn(V, num)) {
Varray[numNodes++] = num;
SetAdd(V, num);
}
}
#if PARANOID_ASSERTS
assert(numNodes == k);
#endif
return V; //and return
}
void initializeMCMC(int k) {
L = k - mcmc_d + 1;
TINY_GRAPH *gk = TinyGraphAlloc(k);
TINY_GRAPH *gd = TinyGraphAlloc(mcmc_d);
int i;
// create the alpha list
for (i = 0; i < _numCanon; i++) {
BuildGraph(gk, _canonList[i]);
TinyGraphEdgesAllDelete(gd);
if (TinyGraphDFSConnected(gk, 0)) {
//_alphaList[i] = ComputeAlpha(gk, gd, k, L);
}
else _alphaList[i] = 0; // set to 0 if unconnected graphlet
}
TinyGraphFree(gk);
TinyGraphFree(gd);
}
// Compute the degree of the state in the state graph (see Lu&Bressen)
// Given the big graph G, and a set of nodes S (|S|==k), compute the
// degree of the *state* represented by these nodes, which is:
// Degree(S) = k*|NN2| + (k-1)*|NN1|
// where NN1 is the set of nodes one step outside S that have only 1 connection back into S,
// and NN2 is the set of nodes one step outside S that have more than 1 connection back into S.
static int StateDegree(GRAPH *G, SET *S)
{
#if PARANOID_ASSERTS
assert(SetCardinality(S) == _k);
#endif
int Varray[_k]; // array of elements in S
SetToArray(Varray, S);
SET *outSet=SetAlloc(G->n); // the set of nodes in G that are one step outside S, from *anybody* in S
int connectCount[G->n]; // for each node in the outset, count the number of times it's connected into S
memset(connectCount, 0, G->n * sizeof(int)); // set them all to zero
int i, j, numOut = 0;
for(i=0;i<_k;i++) for(j=0; j<G->degree[Varray[i]]; j++)
{
int neighbor = G->neighbor[Varray[i]][j];
if(!SetIn(S, neighbor))
{
SetAdd(outSet, neighbor);
if(connectCount[neighbor] == 0) ++numOut;
++connectCount[neighbor];
}
}
#if PARANOID_ASSERTS
assert(SetCardinality(outSet) == numOut);
#endif
int outArray[numOut], Sdeg = 0;
i = SetToArray(outArray, outSet);
assert(i == numOut);
for(i=0; i < numOut; i++)
{
switch(connectCount[outArray[i]])
{
case 0: break;
case 1: Sdeg += _k-1; break;
default: Sdeg += _k; break;
}
}
SetFree(outSet);
return Sdeg;
}
static SET *SampleGraphletLuBressan_MCMC_MHS_without_Ooze(SET *V, int *Varray, GRAPH *G, int k) { } // slower
static SET *SampleGraphletLuBressan_MCMC_MHS_with_Ooze(SET *V, int *Varray, GRAPH *G, int k) { } // faster!
/*
* Very slow: sample k nodes uniformly at random and throw away ones that are disconnected.
*/
static unsigned long int unbiasedTotalTries;
static SET *SampleGraphletUnbiased(SET *V, int *Varray, GRAPH *G, int k)
{
int arrayV[k], i;
int nodeArray[G->n], distArray[G->n];
TINY_GRAPH *g = TinyGraphAlloc(k);
int graphetteArray[k];
int tries = 0;
do
{
SetEmpty(V);
// select k nodes uniformly at random from G without regard to connectivity
for(i=0; i<k; i++)
{
do
Varray[i] = G->n * drand48();
while(SetIn(V, Varray[i]));
SetAdd(V, Varray[i]);
}
TinyGraphEdgesAllDelete(g);
TinyGraphInducedFromGraph(g, G, Varray);
#if PARANOID_ASSERTS
assert(SetCardinality(V)==k);
assert(NumReachableNodes(g,0) == TinyGraphBFS(g, 0, k, graphetteArray, distArray));
#endif
++tries;
} while(NumReachableNodes(g, 0) < k);
unbiasedTotalTries += tries;
return V;
}
static int IntCmp(const void *a, const void *b)
{
int *i = (int*)a, *j = (int*)b;
return (*i)-(*j);
}
// Assuming the global variable _k is set properly, go read in and/or mmap the big global
// arrays related to canonical mappings and permutations.
void SetGlobalCanonMaps(void)
{
assert(3 <= _k && _k <= 8);
_Bk = (1 <<(_k*(_k-1)/2));
char BUF[BUFSIZ];
int i;
_numCanon = canonListPopulate(BUF, _canonList, _k);
_numOrbits = orbitListPopulate(BUF, _orbitList, _k);
// Jens: this is where you'd insert a _numOrbits = orbitListPopulate(...) function;
// put the actual function in libblant.[ch]
mapCanonMap(BUF, _K, _k);
sprintf(BUF, CANON_DIR "/perm_map%d.bin", _k);
int pfd = open(BUF, 0*O_RDONLY);
kperm *Pf = Mmap(Permutations, _Bk*sizeof(Permutations[0]), pfd);
assert(Pf == Permutations);
}
void SampleGraphlet(GRAPH *G, SET *V, unsigned Varray[], int k) {
#if SAMPLE_METHOD == SAMPLE_UNBIASED
SampleGraphletUnbiased(V, Varray, G, k); // REALLY REALLY SLOW
#elif SAMPLE_METHOD == SAMPLE_NODE_EXPANSION
SampleGraphletNodeBasedExpansion(V, Varray, G, k);
#elif SAMPLE_METHOD == SAMPLE_RESERVOIR
SampleGraphletLuBressanReservoir(V, Varray, G, k); // pretty slow but not as bad as unbiased
#elif SAMPLE_METHOD == SAMPLE_EDGE_EXPANSION
SampleGraphletEdgeBasedExpansion(V, Varray, G, k); // This one is faster but less well tested and less well understood.
#elif SAMPLE_METHOD == SAMPLE_MCMC
SampleGraphletMCMC(V, Varray, G, k);
#else
#error unknown sampling method
#endif
}
void ProcessGraphlet(GRAPH *G, SET *V, unsigned Varray[], char perm[], TINY_GRAPH *g, int k) {
// We should probably figure out a faster sort? This requires a function call for every comparison.
qsort((void*)Varray, k, sizeof(Varray[0]), IntCmp);
TinyGraphInducedFromGraph(g, G, Varray);
int Gint = TinyGraph2Int(g,k), j, GintCanon=_K[Gint];
#if PARANOID_ASSERTS
assert(0 <= GintCanon && GintCanon < _numCanon);
#endif
switch(_outputMode)
{
case graphletFrequency:
++_graphletCount[GintCanon];
break;
case indexGraphlets:
memset(perm, 0, k);
ExtractPerm(perm, Gint);
printf("%d", GintCanon); // Note this is the ordinal of the canonical, not its bit representation
#if SHAWN_AND_ZICAN
for(j=0;j<k;j++) printf(" %s", _nodeNames[Varray[(int)perm[j]]]);
#else
for(j=0;j<k;j++) printf(" %d", Varray[(int)perm[j]]);
#endif
puts("");
break;
case outputGDV:
for(j=0;j<k;j++) ++GDV(Varray[j], GintCanon);
break;
case outputODV: // Jens, here...
//Abort("Sorry, ODV is not implemented yet");
memset(perm, 0, k);
ExtractPerm(perm, Gint);
#if PERMS_CAN2NON
for(j=0;j<k;j++) ++ODV(Varray[(int)perm[j]], _orbitList[GintCanon][j]);
#else
for(j=0;j<k;j++) ++ODV(Varray[j], _orbitList[GintCanon][(int)perm[j]]);
#endif
break;
default: Abort("unknown or un-implemented outputMode");
break;
}
}
// This is the single-threaded BLANT function. YOU SHOULD PROBABLY NOT CALL THIS.
// Call RunBlantInThreads instead, it's the top-level entry point to call once the
// graph is finished being input---all the ways of reading input call RunBlantInThreads.
int RunBlantFromGraph(int k, int numSamples, GRAPH *G)
{
int i,j;
char perm[maxK+1];
assert(k <= G->n);
srand48(_seed);
SET *V = SetAlloc(G->n);
TINY_GRAPH *g = TinyGraphAlloc(k);
unsigned Varray[maxK+1];
#if SAMPLE_METHOD == SAMPLE_MCMC
initializeMCMC(k);
#endif
for(i=0; i<numSamples; i++)
{
SampleGraphlet(G, V, Varray, k);
ProcessGraphlet(G, V, Varray, perm, g, k);
}
switch(_outputMode)
{
int canon;
case indexGraphlets:
#if SAMPLE_METHOD == SAMPLE_MCMC
Warning("Sampling method MCMC overcounts graphlets by varying amounts.");
#endif
break; // already output on-the-fly above
case graphletFrequency:
for(canon=0; canon<_numCanon; canon++)
printf("%lu %d\n", _graphletCount[canon], canon);
break;
case outputGDV:
for(i=0; i < G->n; i++)
{
printf("%lu", GDV(i,0));
for(canon=1; canon < _numCanon; canon++)
printf(" %lu", GDV(i,canon));
puts("");
}
break;
case outputODV:
for(i=0; i<G->n; i++) {for(j=0; j<_numOrbits; j++) printf("%d ", ODV(i,j)); printf("\n");}
break;
default: Abort("unknown or un-implemented outputMode");
break;
}
#if PARANOID_ASSERTS // no point in freeing this stuff since we're about to exit; it can take significant time for large graphs.
TinyGraphFree(g);
SetFree(V);
GraphFree(G);
if(_outputMode == outputGDV) for(i=0;i<MAX_CANONICALS;i++)
Free(_graphletDegreeVector[i]);
if(_outputMode == outputODV) for(i=0;i<MAX_ORBITS;i++)
Free(_orbitDegreeVector[i]);
#endif
#if SAMPLE_METHOD == SAMPLE_UNBIASED
fprintf(stderr,"Average number of tries per sample is %g\n", unbiasedTotalTries/(double)numSamples);
#endif
return 0;
}
FILE *ForkBlant(int k, int numSamples, GRAPH *G)
{
int fds[2];
pipe(fds);
int pid = fork();
if(pid > 0) // we are the parent
{
close(fds[1]); // we will not be writing to the pipe, so close it.
return fdopen(fds[0],"r");
}
else if(pid == 0) // we are the child
{
close(fds[0]); // we will not be reading from the pipe, so close it.
close(1); // close our usual stdout
dup(fds[1]); // copy the write end of the pipe to fd 1.
close(fds[1]); // close the original write end of the pipe since it's been moved to fd 1.
RunBlantFromGraph(k, numSamples, G);
exit(0);
}
else
Abort("fork failed");
}