forked from waynebhayes/BLANT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynthetic.c
1633 lines (1404 loc) · 63.7 KB
/
synthetic.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 <math.h>
#include "misc.h"
#include "tinygraph.h"
#include "graph.h"
#include "blant.h"
#include "sets.h"
#include "syntheticDS.h"
char USAGE[] = "synthetic -k maxk -s STAGNATED Gtarget.el Gsynth.el [--blant.Gt.index files--] [--blant.Gs.index files--]\n - output is new Gsynth.el\n";
Boolean _supportNodeNames = false;
// 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 int _k[maxK]; // stores what values of k have to be considered e.g. [3,4,5,6] or [3,5,7] or [2,7,8]. Will be followed by '-1's
static int _numCanon[maxK]; // canonicals for particular value of k. So for k=5, _numCanon[5-1] stores ~32
static SET *_connectedCanonicals[maxK];
static int _maxNumCanon = -1; // max number of canonicals
static int _numSamples = -1; // same number of samples in each blant index file
static int _numNodes = -1; // number of nodes in the target/synthetic network
static int maxdegree = -1; // equals _numNodes (a node can be connected to every other node)
static int _canonList[maxK][MAX_CANONICALS];
static int _stagnated = 1000, _numDisconnectedGraphlets;
#define PRINT_INTERVAL 100000
#define ISZERO(w) (fabs(w)<0.00001)
#define NC2(n) ((n* (n-1)) / 2)
/* khop distribution is computed (bfs from each node) and used to make
the synthetic more/less like a small-world network (or keep things as they are).
So, compute khop of target and synthetic & then compare the MEDIANS of the two distributions.
Now, do node-selection to make the network more/less small-worldy (details in GetNodes function)
This has the effect of tuning the centrality measures - node eccentricity; node & edge betweenness; KHOP; diameter; ....
*/
#define DEFAULT_KHOP_INTERVAL 400 // accepted-iterations after which khop distribution is computed (if things are to be kept the same)
#define FIX_KHOP_INTERVAL 200 // accepted-iterations after which khop distribution is computed (if network needs to made more/less small-worldy)
#define KHOP_QUALITY 0.8 // pick these fraction of nodes, do a BFS on each of these nodes (for computing KHOP)
// node-selection
#define NODE_SEL_ALWAYS_RANDOM 0
#define NODE_SEL_SHORT_PATH 1 // join & disconnect nodes which have more/less Shortest Paths going through them
#define NODE_SEL_BY_HOPS 2 // join & disconnect nodes which are a specific BFS hops apart (SLOW)
static int node_selection = NODE_SEL_SHORT_PATH;
// node-selection-strategy can be set using an env variable
// USAGE: export SYNTHETIC_NODE_SELECTION = 0 # 0 for random, 1 for shortestpaths, 2 for hops
// objective functions
#define IGNORE_DISCONNECTED_GRAPHLETS 1
#define NUMPROPS 7
#define GraphletEuclidean 0
#define GraphletKernel 1 // let u & v be two graphlet vectors (all k). Then, GK(u,v) = u dot v / ||u|| ||v||
#define SGKDiff 2 // sum of abs(observed-ideal)/ideal ; for all graphlets, all k
#define GraphletGDV 3
#define EdgeHammingDistance 4
#define DegreeDist 5 // euclidean distance between the two degree distribution vectors
#define ClustCoff 6 // binned histogram difference between LOCAL clustering coefficient distributions
static double weights[NUMPROPS] =
// weights: 0 GraphletEuclidean; 1 GraphletKernel; 2 SGKDiff; 3 GDV; 4 EdgeHammingDistance, 5 DegreeDist; 6 ClustCoff
{0.1, 0, 0, 0, 0.35, 0.05, 0.50};
// weights can be set using an env variable
// USAGE: export SYNTHETIC_GRAPHLET_WEIGHTS = 'a b c d e f g' #a+b+c+d+e+f+g == 1
static double max_abscost[NUMPROPS];
// 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[maxK];
// 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){
unsigned int _Bk;
int i;
for(i=0; i<maxK; i++){ // for all values of 'k'
if (_k[i] == -1)
break;
assert(3 <= _k[i] && _k[i] <= 8);
_Bk = (1 <<(_k[i]*(_k[i]-1)/2));
char BUF[BUFSIZ];
_connectedCanonicals[_k[i]-1] = SetAlloc(_Bk);
_numCanon[_k[i]-1] = canonListPopulate(BUF, _canonList[_k[i]-1], _connectedCanonicals[_k[i]-1], _k[i]);
_maxNumCanon = MAX(_maxNumCanon, _numCanon[_k[i]-1]); // set max number of canonicals for a k
_K[_k[i]-1] = (short int*) aligned_alloc(8192, MAX(_Bk * sizeof(short int), 8192));
assert(_K[_k[i]-1] != NULL);
mapCanonMap(BUF, _K[_k[i]-1], _k[i]);
sprintf(BUF, "%s/%s/perm_map%d.bin", _BLANT_DIR, CANON_DIR, _k[i]);
int pfd = open(BUF, 0*O_RDONLY);
kperm *Pf = Mmap(Permutations, _Bk*sizeof(Permutations[0]), pfd);
assert(Pf == Permutations);
}
}
// 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 < Gv->n; i++) for(j=i+1; j < Gv->n; j++)
if(GraphAreConnected(G, Varray[i], Varray[j]))
TinyGraphConnect(Gv, i, j);
return Gv;
}
// Incrementally returns new GraphletEuclidean objective cost
// O(1) time
double FastEuclideanObjective(double oldcost, double olddelta, double change){
// oldcost is the original cost
// olddelta is the original difference b/w 2 graphlet counts D[1][...canon...] - D[0][...canon...]
// change = +1 (-1) if count of a graphlet increased (decreased) in the caller code
double unchanged = (oldcost * oldcost) - (olddelta * olddelta);
unchanged = MAX(0, unchanged);
double newcost_sq = unchanged + SQR(olddelta+change);
newcost_sq = MAX(0, newcost_sq);
return sqrt(newcost_sq);
}
// Incrementally returns new GraphletGDV objective cost
// O(1) time
double FastGDVObjective(double oldcost, int olddelta, double change){
// oldcost is the original cost
// olddelta is the original difference b/w 2 graphlet counts D[1][...canon...] - D[0][...canon...]
// change = +1 (-1) if count of a graphlet increased (decreased) in the caller code
double unchanged = (oldcost * oldcost) - (olddelta * olddelta);
unchanged = MAX(0, unchanged);
double newcost_sq = unchanged + (double) SQR(olddelta+change);
newcost_sq = MAX(0, newcost_sq);
return sqrt(newcost_sq);
}
// Incrementally returns new SGKDiff objective cost
// O(1) time
double FastSGKDiffObjective(double oldcost, int k, int canonNum, int D0, int D1, int change){
// oldcost is the original cost
// k and canonNum specify a graphlet
// D0 and D1 are the graphlet counts - D[0] and D[1]; for a particular k and canonNum
// change = +1 (-1) if count of a graphlet increased (decreased) in the caller code
double oldratio, newratio;
assert(abs(change) == 1);
int diff;
// compute old ratio
diff = abs(D1-change-D0);
if (D0 == 0){
if (diff == 0)
oldratio = (double) 1; // 0/0
else
oldratio = (double) diff; // diff/0
}else{
oldratio = (double) diff/D0; // diff/target
}
// compute new ratio
diff = abs(D1-D0);
if (D0 == 0){
if (diff == 0)
newratio = (double) 1;
else
newratio = (double) diff;
}else{
newratio = (double) diff/D0;
}
double returnVal = oldcost - oldratio + newratio;
return returnVal;
}
// Updates the GraphletKernel object (GKState), and incrementally returns new GraphletKernel cost
// O(1) time
double AdjustGraphletKernel(int D0, int D1, int change, GKState* gkstate){
// D0 and D1 are the graphlet counts - D[0] and D[1]; for a particular k and canonNum
// change = +1 (-1) if count of a graphlet increased (decreased) in the caller code
// GKState stores 3 integers - (1) u dot v (2) ||u||^2 (3) ||v||^2
assert(abs(change) == 1);
// update u dot v
gkstate->udotv += ((long) D0 * (long) change); // after simplification : += [(d1*d0) - ((d1-change)*d0)]
// update ||v|| ** 2
gkstate->sq_length_v += (long) ((2*D1*change) - 1); // after simplification : += [-(d1-change)**2 + (d1)**2]
assert(sqrt(gkstate->sq_length_u) >= 0);
assert(sqrt(gkstate->sq_length_v) >= 0);
double gk = ((double) gkstate->udotv) / (((double) sqrt(gkstate->sq_length_u)) * ((double) sqrt(gkstate->sq_length_v)));
double cost = 1-gk;
assert(ISZERO(cost) || ISZERO(1-cost) || ((cost>0) && (cost<1)));
return cost;
}
// Updates GDV matrices, and incrementally returns new GraphletGDV cost
double AdjustGDV(int k, int canon, int change, int line[k+1], Dictionary GDVhistograms[2][maxK][_maxNumCanon], int GDVbinsize[2][maxK][_maxNumCanon], int GDV[2][maxK][_maxNumCanon][_numNodes], double oldcost){
// k and canon identify a graphlet
// change = +1 (-1) if count of a graphlet increased (decreased) in the caller code
// line is the BLANT sample line
assert(abs(change) == 1);
assert(line[0] == canon);
int n, b, c, node, key, value, olddelta;
double newcost = oldcost;
b = GDVbinsize[1][k-1][canon];
for (n=1; n<=k; n++){
node = line[n];
// existing GDV key gets decremented
c = -1;
key = GDV[1][k-1][canon][node];
key = (int) ((int) key/b) * b;
value = dictionary_get(&(GDVhistograms[1][k-1][canon]), key, 0);
assert(value > 0);
olddelta = value - dictionary_get(&(GDVhistograms[0][k-1][canon]), key, 0); // difference in the GDVhistograms
dictionary_set(&(GDVhistograms[1][k-1][canon]), key, value-1);
newcost = FastGDVObjective(newcost, olddelta, c); // update cost
GDV[1][k-1][canon][node] += change;
if (change == -1)
assert(GDV[1][k-1][canon][node] >= 0);
// new GDV key gets incremented
c = 1;
key = GDV[1][k-1][canon][node];
key = (int) ((int) key/b) * b;
value = dictionary_get(&(GDVhistograms[1][k-1][canon]), key, 0);
olddelta = value - dictionary_get(&(GDVhistograms[0][k-1][canon]), key, 0);
dictionary_set(&(GDVhistograms[1][k-1][canon]), key, value+1);
newcost = FastGDVObjective(newcost, olddelta, c); // update cost
}
assert(newcost >= 0);
return newcost;
}
// Updates Degree Distribution arrays, and incrementally returns new DegreeDist cost
// Should be called AFTER calling GraphConnect or GraphDisconnect
// O(1) time
double AdjustDegree(const int x, const int y, const int connected, GRAPH* G, int Degree[2][maxdegree+1], double oldcost){
// x and y are the 2 nodes which were connected or disconnected
// connected = +1 (-1) if x and y were connected (disconnected)
assert(abs(connected) == 1);
assert(oldcost >= 0);
double newcost, olddelta, change;
// for node x
int old_deg_x = G->degree[x] - connected;
olddelta = Degree[1][old_deg_x] - Degree[0][old_deg_x];
newcost = FastEuclideanObjective(oldcost, olddelta, -1);
olddelta = Degree[1][G->degree[x]] - Degree[0][G->degree[x]];
newcost = FastEuclideanObjective(newcost, olddelta, 1);
Degree[1][old_deg_x] -= 1;
Degree[1][G->degree[x]] += 1;
// for node y
int old_deg_y = G->degree[y] - connected;
olddelta = Degree[1][old_deg_y] - Degree[0][old_deg_y];
newcost = FastEuclideanObjective(newcost, olddelta, -1);
olddelta = Degree[1][G->degree[y]] - Degree[0][G->degree[y]];
newcost = FastEuclideanObjective(newcost, olddelta, 1);
Degree[1][old_deg_y] -= 1;
Degree[1][G->degree[y]] += 1;
// sanity check
// number of elements
int i, elts=0;
for (i=0; i<=maxdegree; i++)
elts += Degree[1][i];
assert(elts == (G->n));
assert(newcost >= 0);
return newcost;
}
// Updates Local Clustering CONNECTIONS arrays, and incrementally returns new ClustCoff objective cost
// Should be called AFTER calling GraphConnect or GraphDisconnect
// O(d) time, where d is the number of nodes connected to both x and y
double AdjustClustCoff(const int x, const int y, const int connected, GRAPH* G, int localConnections[2][_numNodes], ClustCoffState* ccstate, double oldcost){
// x and y are the 2 nodes which were connected or disconnected
// connected = +1 (-1) if x and y were connected (disconnected)
// localConnections is an array where lc[i] stores number of edges b/w the neighbors of node `i`
// ccstate->histograms are binned histograms storing how many nodes lie in a particular interval of local-clustering-coefficient
/*
Local clustering coefficient calculation is more precise if number of connections in the neighboorhood
of a node (integers) are updated, instead of the exact local clustering coefficent (which is a float)
*/
assert(abs(connected) == 1);
assert(ccstate->histograms_size[0] == ccstate->histograms_size[1]);
int i, j, node, nodecount, c, nc2, oldkey, newkey;
double oldcc, newcc;
double newcost = oldcost;
assert((newcost == newcost) && (newcost >= 0));
SET* xn = SetAlloc(G->n);
for(i=0; i < G->degree[x]; i++)
if (G->neighbor[x][i] != y)
SetAdd(xn, G->neighbor[x][i]);
nodecount = 0; // nodes which are connected both to x & y
for(i=0; i < G->degree[y]; i++){
node = G->neighbor[y][i];
if ((node!=x) && (SetIn(xn, node))){
nodecount += 1;
assert(G->degree[node] >= 2);
nc2 = (G->degree[node] * (G->degree[node] - 1))/2;
c = localConnections[1][node]; // the original num of connections
localConnections[1][node] = c + connected; // the new num of connections
if (nc2 > 0){
oldcc = (double) c / (double) nc2;
oldkey = (int) (oldcc / ccstate->histograms_bin_size[1]);
assert(oldkey < ccstate->histograms_size[1]);
newcc = (double) (c+connected) / (double) nc2;
newkey = (int) (newcc / ccstate->histograms_bin_size[1]);
assert(newkey < ccstate->histograms_size[1]);
newcost = FastEuclideanObjective(newcost, (double) (ccstate->histograms[1][oldkey] - ccstate->histograms[0][oldkey]), (double) -1.0);
assert((newcost == newcost) && (newcost >= 0));
assert(ccstate->histograms[1][oldkey] > 0);
ccstate->histograms[1][oldkey] -= 1;
newcost = FastEuclideanObjective(newcost, (double) (ccstate->histograms[1][newkey] - ccstate->histograms[0][newkey]), (double) 1.0);
assert((newcost == newcost) && (newcost >= 0));
ccstate->histograms[1][newkey] += 1;
}
else{
oldcc = 0.0;
oldkey = 0;
newcc = 0.0;
newkey = 0;
}
}
}
SetFree(xn);
// for x
if ((G->degree[x] - connected) < 2){ // original degree
c = localConnections[1][x]; // the old connections
assert(c == 0);
oldcc = 0;
oldkey = 0;
}else{
nc2 = ((G->degree[x] - connected) * ((G->degree[x] - connected) - 1))/2;
c = localConnections[1][x]; // the old connections
oldcc = ((double) c) / ((double) nc2);
oldkey = (int) (oldcc / ccstate->histograms_bin_size[1]);
}
if(G->degree[x] < 2){ // current degree
c += (connected * nodecount);
assert(c == 0);
newcc = 0;
newkey = 0;
}else{
nc2 = (G->degree[x] * (G->degree[x] - 1))/2;
c += (connected * nodecount);
newcc = ((double) c) / ((double) nc2);
newkey = (int) (newcc / ccstate->histograms_bin_size[1]);
}
localConnections[1][x] = c;
if(oldkey != newkey){
newcost = FastEuclideanObjective(newcost, (double) (ccstate->histograms[1][oldkey] - ccstate->histograms[0][oldkey]), (double) -1.0);
assert((newcost == newcost) && (newcost >= 0));
assert(ccstate->histograms[1][oldkey] > 0);
ccstate->histograms[1][oldkey] -= 1;
newcost = FastEuclideanObjective(newcost, (double) (ccstate->histograms[1][newkey] - ccstate->histograms[0][newkey]), (double) 1.0);
assert((newcost == newcost) && (newcost >= 0));
ccstate->histograms[1][newkey] += 1;
}
// for y
if ((G->degree[y] - connected) < 2){ // original degree
c = localConnections[1][y]; // the old connections
assert(c == 0);
oldcc = 0;
oldkey = 0;
}else{
nc2 = ((G->degree[y] - connected) * ((G->degree[y] - connected) - 1))/2;
c = localConnections[1][y]; // the old connections
oldcc = ((double) c) / ((double) nc2);
oldkey = (int) (oldcc / ccstate->histograms_bin_size[1]);
}
if(G->degree[y] < 2){ // current degree
c += (connected * nodecount);
assert(c == 0);
newcc = 0;
newkey = 0;
}else{
nc2 = (G->degree[y] * (G->degree[y] - 1))/2;
c += (connected * nodecount);
newcc = ((double) c) / ((double) nc2);
newkey = (int) (newcc / ccstate->histograms_bin_size[1]);
}
localConnections[1][y] = c;
if(oldkey != newkey){
newcost = FastEuclideanObjective(newcost, (double) (ccstate->histograms[1][oldkey] - ccstate->histograms[0][oldkey]), (double) -1.0);
assert((newcost == newcost) && (newcost >= 0));
assert(ccstate->histograms[1][oldkey] > 0);
ccstate->histograms[1][oldkey] -= 1;
newcost = FastEuclideanObjective(newcost, (double) (ccstate->histograms[1][newkey] - ccstate->histograms[0][newkey]), (double) 1.0);
assert((newcost == newcost) && (newcost >= 0));
ccstate->histograms[1][newkey] += 1;
}
return newcost;
}
// EHD is a matrix which tells the ehd b/w 2 canonicals
// EHDaway is a matrix which tells which canonicals are 'x' distance away from 'y'th canonical
// D[ ] is the raw graphlet count matrix
double EHDObjective(int D[2][maxK][_maxNumCanon], int CanonicalEdges[maxK][_maxNumCanon], int EHD[maxK][_maxNumCanon][_maxNumCanon], int EHDaway[maxK][_maxNumCanon][NC2(maxK)+1][1 + _maxNumCanon]){
int i,j,k,l,m,x,diff,temp;
double sum = 0;
for(i=0; i<maxK; i++){
k = _k[i];
if (k == -1)
break;
// loop through all canonicals for this k value
for(j=0; j<_numCanon[k-1]; j++){
diff = CanonicalEdges[k-1][j] * abs(D[0][k-1][j] - D[1][k-1][j]);
for(l=1; l<=NC2(k); l++){
temp = 0;
// loop through all the canonicals which are 'l' EHD away from jth canonical
for(m=0; m<EHDaway[k-1][j][l][0]; m++){ // EHDaway[k-1][j][l][0] gives number of canonicals which are 'l' away from 'j'
x = EHDaway[k-1][j][l][m+1]; // a canonical which is 'l' EHD edges away from j
assert(EHD[k-1][j][x] == l);
temp += abs(D[0][k-1][j] - D[1][k-1][x]);
}
temp = temp * l;
assert(temp >= 0);
diff = MIN(diff, temp);
}
// diff for 'j'th canonical is fixed now
assert(diff >= 0);
sum += diff;
assert(sum >= 0);
}
}
return (double) sum;
}
// updates the BLANT sample, and updates the cost for the GRAPHLET BASED objective functions : GraphletEuclidean, GraphletKernel, SGKDiff, GDV
void ReBLANT(int D[2][maxK][_maxNumCanon], GKState* gkstate, Dictionary GDVhistograms[2][maxK][_maxNumCanon], int GDVbinsize[2][maxK][_maxNumCanon], int GDV[2][maxK][_maxNumCanon][_numNodes], int CanonicalEdges[maxK][_maxNumCanon], int EHD[maxK][_maxNumCanon][_maxNumCanon], int EHDaway[maxK][_maxNumCanon][NC2(maxK)+1][1 + _maxNumCanon], GRAPH *G, SET ***samples, int ***Varrays, int ***BLANT, int v1, int v2, double oldcost[NUMPROPS], RevertStack* rvStack){
// D stores the squiggly plot vectors
// gkstate maintains variables for GraphletKernel objective
// GDV histograms and matrices for GraphletGDV objective
// SET*** samples stores which lines (samples) is a particular node present in
// Varrays is the same thing as SET*** samples; used because there is no set iterator
// BLANT stores the samples [canonNum n1 n2 n3 .... nk]
// v1 and v2 are the nodes which WERE connected or disconnected, by the caller
// oldcost contains the current cost of the 4 GRAPHLET BASED objectives
// rvStack is a stack used to revert changes to the BLANT samples, if a move is rejected (optimization to avoid calling ReBLANT)
int i, j, line, s, change;
int canon;
static TINY_GRAPH *g[maxK];
double oldcanondiff, temp_gdv_newcost;
for (i=0; i<maxK; i++){
if (_k[i] == -1)
break;
int k = _k[i];
// allocate a tiny graph
if (!g[k-1])
g[k-1] = TinyGraphAlloc(k);
for (s=1; line=Varrays[k-1][v1][s], s<=Varrays[k-1][v1][0]; s++)
if(SetIn(samples[k-1][v2], line)){
// decrement a graphlet
canon = BLANT[k-1][line][0];
Boolean wasConnected = SetIn(_connectedCanonicals[k-1], canon);
oldcanondiff = D[1][k-1][canon] - D[0][k-1][canon];
--D[1][k-1][canon];
change = -1;
// recompute cost
if ((!IGNORE_DISCONNECTED_GRAPHLETS) || wasConnected){
if (!ISZERO(weights[GraphletEuclidean]))
oldcost[0] = FastEuclideanObjective(oldcost[0], oldcanondiff, change);
if (!ISZERO(weights[GraphletKernel]))
oldcost[1] = AdjustGraphletKernel(D[0][k-1][canon], D[1][k-1][canon], change, gkstate);
if (!ISZERO(weights[SGKDiff]))
oldcost[2] = FastSGKDiffObjective(oldcost[2], k, canon, D[0][k-1][canon], D[1][k-1][canon], change);
if (!ISZERO(weights[GraphletGDV]))
oldcost[3] = AdjustGDV(k, canon, change, BLANT[k-1][line], GDVhistograms, GDVbinsize, GDV, oldcost[3]); // GDV matrices might be out of sync from ideal if GDV weight = 0
}
// Change object (to be pushed on the stack)
Change newchange;
newchange.k = k;
newchange.linenum = line;
newchange.original = (int) canon;
TinyGraphInducedFromGraph(g[k-1], G, &(BLANT[k-1][line][1])); // address of the Varray without element 0
BLANT[k-1][line][0] = _K[k-1][TinyGraph2Int(g[k-1], k)];
Boolean isConnected = SetIn(_connectedCanonicals[k-1], BLANT[k-1][line][0]);
if(wasConnected && !isConnected)
++_numDisconnectedGraphlets;
if(!wasConnected && isConnected)
--_numDisconnectedGraphlets;
// increment a graphlet
canon = BLANT[k-1][line][0];
oldcanondiff = D[1][k-1][canon] - D[0][k-1][canon];
++D[1][k-1][canon];
change = 1;
// recompute cost
if ((!IGNORE_DISCONNECTED_GRAPHLETS) || isConnected){
if (!ISZERO(weights[GraphletEuclidean]))
oldcost[0] = FastEuclideanObjective(oldcost[0], oldcanondiff, change);
if (!ISZERO(weights[GraphletKernel]))
oldcost[1] = AdjustGraphletKernel(D[0][k-1][canon], D[1][k-1][canon], change, gkstate);
if (!ISZERO(weights[SGKDiff]))
oldcost[2] = FastSGKDiffObjective(oldcost[2], k, canon, D[0][k-1][canon], D[1][k-1][canon], change);
if (!ISZERO(weights[GraphletGDV]))
oldcost[3] = AdjustGDV(k, canon, change, BLANT[k-1][line], GDVhistograms, GDVbinsize, GDV, oldcost[3]);
}
// change object
newchange.new = (int) canon;
assert(push(rvStack, newchange) == 0);
}
}
// recomputed from scratch, can be outside 'iterate over k' loop
if (!ISZERO(weights[EdgeHammingDistance]))
oldcost[4] = EHDObjective(D, CanonicalEdges, EHD, EHDaway);
for(i=0; i<5; i++)
assert(oldcost[i] >= 0);
}
// restores the BLANT samples, given a stack (rvStack)
void Revert(int ***BLANT, int D[2][maxK][_maxNumCanon], Dictionary GDVhistograms[2][maxK][_maxNumCanon], int GDVbinsize[2][maxK][_maxNumCanon], int GDV[2][maxK][_maxNumCanon][_numNodes], RevertStack* rvStack){
// restore the BLANT line
// restore D vectors AND _numDisconnectedGraphlets
// restore GDV[1] matrix and the GDVhistograms
/*
a stack entry contains the BLANT line-number, k, original and new graphlet number
so, count of 'new' gets decremented and 'original' gets incremented (in D)
*/
Change change;
int n, node, key, value, b;
int* line;
while (pop(rvStack, &change) == 0){
line = BLANT[change.k-1][change.linenum];
Boolean wasConnected = SetIn(_connectedCanonicals[change.k-1], BLANT[change.k-1][change.linenum][0]);
if ((!IGNORE_DISCONNECTED_GRAPHLETS) || wasConnected){
if (!ISZERO(weights[GraphletGDV]))
AdjustGDV(change.k, change.new, -1, line, GDVhistograms, GDVbinsize, GDV, 1000); // the 1000 has no meaning
}
Boolean isConnected = SetIn(_connectedCanonicals[change.k-1], change.original);
BLANT[change.k-1][change.linenum][0] = change.original;
if ((!IGNORE_DISCONNECTED_GRAPHLETS) || isConnected){
if (!ISZERO(weights[GraphletGDV]))
AdjustGDV(change.k, change.original, 1, line, GDVhistograms, GDVbinsize, GDV, 1000);
}
if(wasConnected && !isConnected)
++_numDisconnectedGraphlets;
if(!wasConnected && isConnected)
--_numDisconnectedGraphlets;
--D[1][change.k-1][change.new];
++D[1][change.k-1][change.original];
}
}
// global objective function
// returns WEIGHTED and NORMALIZED total costs, given current absolute costs
double Objective(double abscost[NUMPROPS]){
double cost = 0;
int i = 0;
for(i=0; i<NUMPROPS; i++){
assert(abscost[i] >= 0);
assert(max_abscost[i] >= 0);
if (max_abscost[i] == 0)
cost += ((double) weights[i] * (abscost[i] / 1));
else
cost += ((double) weights[i] * (abscost[i] / max_abscost[i]));
}
assert(cost >= 0);
return cost;
}
// graphlet euclidean cost
// slow, used once to initialize. O(n) where n is num of canonicals for all k
double GraphletEuclideanObjective(int D[2][maxK][_maxNumCanon]){
// returns ABSOLUTE cost
int i,j;
double logP = 0, sum2 = 0;
for (i=0; i<maxK; i++){
if (_k[i] == -1) break;
for (j=0; j<_numCanon[_k[i]-1]; j++){
/*
double pd = PoissonDistribution(D[0][_k[i]-1][j], D[1][_k[i]-1][j]);
if(pd > 1)
Fatal("umm.... PoissonDistribution returned a number greater than 1");
if(pd>0)
logP += log(pd); // if we're close, use probability
*/
// use this one when we're so far away the probability is zero
double term = SQR((double)D[0][_k[i]-1][j] - D[1][_k[i]-1][j]);
sum2 += term;
}
}
double returnVal = sqrt(sum2);
assert(returnVal == returnVal);
assert(returnVal >= 0);
return returnVal; //exp(logP);
}
// graphlet kernel cost; GK(u, v) = (u dot v) / ||u|| ||v||; where u and v are two graphlet count vectors (single big vector for all k)
// As GK is a similarity measure, 1-GK is used as the objective function COST
// slow, used once to initialize. O(n) where n is num of canonicals for all k
double GraphletKernelObjective(const int D[2][maxK][_maxNumCanon], GKState* gkstate){
// GKState stores 3 integers - (1) u dot v (2) ||u||^2 (3) ||v||^2
// calculations are precise if we maintain 3 integers, rather than the exact GK value (which is a float)
int i,j;
gkstate->udotv = (long) 0;
gkstate->sq_length_u = (long) 0;
gkstate->sq_length_v = (long) 0;
for(i=0; i<maxK; i++){
int k = _k[i];
if (k == -1)
break;
for(j=0; j<_numCanon[k-1]; j++){
if ((!IGNORE_DISCONNECTED_GRAPHLETS) || (SetIn(_connectedCanonicals[k-1], j))){
gkstate->sq_length_u += SQR((long) D[0][k-1][j]);
assert(gkstate->sq_length_u >= 0);
gkstate->sq_length_v += SQR((long) D[1][k-1][j]);
assert(gkstate->sq_length_v >= (long) 0);
gkstate->udotv += ((long) D[0][k-1][j] * (long) D[1][k-1][j]);
assert(gkstate->udotv >= 0);
}
}
}
assert(sqrt(gkstate->sq_length_u) > 0);
assert(sqrt(gkstate->sq_length_v) > 0);
double gk = ((double) gkstate->udotv) / (((double) sqrt(gkstate->sq_length_u)) * ((double) sqrt(gkstate->sq_length_v)));
double cost = 1-gk;
assert(ISZERO(cost) || ISZERO(1-cost) || ((cost>0) && (cost<1)));
return cost;
}
// Senatorial Graphlet Difference; SGK = sum of abs(observed-real)/real for all canonicals (all k)
// slow, used once to initialize. O(n) where n is num of canonicals for all k
double SGKDiffObjective(int D[2][maxK][_maxNumCanon]){
int i,j;
double sum = 0;
for(i=0; i<maxK; i++){
int k = _k[i];
if (k == -1)
break;
for (j=0; j<_numCanon[k-1]; j++){
int diff = abs(D[0][k-1][j] - D[1][k-1][j]);
int target = D[0][k-1][j];
if ((!IGNORE_DISCONNECTED_GRAPHLETS) || (SetIn(_connectedCanonicals[k-1], j))){
if (target == 0){
if (diff == 0)
sum += (double) 1;
else
sum += (double) diff;
}else{
sum += (double) diff/target;
}
}
}
}
return sum;
}
// computes the difference between the GDV distribution histograms (traget vs synthetic)
// these histograms are initialized/populated in the main driver code
// slow, used once to initialize
double GDVObjective(Dictionary GDVhistograms[2][maxK][_maxNumCanon]){
double sum = 0;
int j,k,canon;
int key_tar, val_tar, key_syn, val_syn;
Dictionary hist_tar, hist_syn;
KeyValue *iter_tar, *iter_syn;
for(j=0; j<maxK; j++){
k = _k[j];
if (k == -1) break;
for(canon=0; canon < _numCanon[k-1]; canon++){
// skip this canon if it is disconnected
if ((IGNORE_DISCONNECTED_GRAPHLETS) && (!SetIn(_connectedCanonicals[k-1], canon)))
continue;
// the 2 GDVhistograms (target & synthetic)
hist_tar = GDVhistograms[0][k-1][canon];
iter_tar = getIterator(&hist_tar);
hist_syn = GDVhistograms[1][k-1][canon];
iter_syn = getIterator(&hist_syn);
double newsum = sum;
// find 'difference' b/w the 2 GDVhistograms
// iterate over all keys in target (these could be in synthetic or not)
while(getNext(&iter_tar, &key_tar, &val_tar) == 0){
assert((key_tar >= 0) && (val_tar >= 0));
key_syn = key_tar;
val_syn = dictionary_get(&hist_syn, key_syn, 0); // default value is 0
newsum += (double) SQR(val_tar - val_syn);
}
// iterate over all keys *ONLY* in synthetic
while(getNext(&iter_syn, &key_syn, &val_syn) == 0){
assert((key_syn >= 0) && (val_syn >= 0));
key_tar = key_syn;
val_tar = dictionary_get(&hist_tar, key_tar, -1); // -1 will only be returned if the key doesn't exist in target
if (val_tar == -1)
newsum += (double) SQR(val_syn);
}
assert(newsum >= sum); // sum cannot decrease
sum = newsum;
assert(sum >= 0);
}
}
double returnval = (double) sqrt(sum);
assert(returnval == returnval);
assert(returnval >= 0);
return returnval;
}
// euclidean distance between two degree distribution vectors
// slow, used once to initialize
double DegreeDistObjective(int Degree[2][maxdegree+1]){
// returns ABSOLUTE cost
double sum = 0;
int i;
for(i=0; i<=maxdegree; i++)
sum += SQR((double) Degree[0][i] - Degree[1][i]);
double returnVal = sqrt(sum);
assert(returnVal == returnVal);
assert(returnVal >= 0);
return returnVal;
}
// computes the number of connections in the neighborhood (used for local-clustering coefficient)
// slow, used once to initialize
void GetConnections(GRAPH *G, int localConnections[G->n]){
// localConnections[i] will contain the number of edges b/w the neighbors of node `i`
// used for local-clustering-coeffienct computation later on
int n, x, node, degree;
int i, j, edges;
// set for marking neighbors
int scratch[G->n];
for(i=0; i<G->n; i++){
localConnections[i] = 0;
scratch[i] = -1;
}
for(n=0; n < G->n; n++){
edges = 0;
degree = G->degree[n];
if (degree < 2){
localConnections[n] = 0;
continue;
}
for(i=0; i<degree; i++)
scratch[(G->neighbor[n])[i]] = n;
for(i=0; i<degree; i++){
node = (G->neighbor[n])[i];
assert(node != n);
for(j=0; j < G->degree[node]; j++){
x = (G->neighbor[node])[j];
if ((x!=n) && (scratch[x] == n))
edges += 1;
}
}
edges = edges/2;
localConnections[n] = edges;
}
}
// computes euclidean distance between the local-clustering-coefficient histograms
double ClustCoffObjective(const ClustCoffState* ccstate){
double cost = 0;
double temp;
int j;
assert(ccstate->histograms_size[0] == ccstate->histograms_size[1]);
for(j=0; j < ccstate->histograms_size[0]; j++){
temp = (double) SQR(ccstate->histograms[0][j] - ccstate->histograms[1][j]);
assert(temp >= 0);
cost += temp;
}
double returnVal = sqrt(cost);
assert(returnVal == returnVal);
assert(returnVal >= 0);
return returnVal;
}
// node selection (to connect or disconnect): as per `static int node_selection`
void GetNodes(GRAPH* G, const SmallWorld sw, int nodesBySp[G->n], int index_in_nodesBySp[G->n], int* u1, int* u2, int* v1, int* v2){
// sw specifies if the network should be made more/less small-worldy (or nothing) --> read the definition in syntheticDS.h
// nodesBySp is an array of nodes, where nodes at lower indexes have smaller number of shortest paths going through them, as compared to nodes at higher indexes
// index_in_nodesBySp is a lookup table for nodesBySp. So, index_in_nodesBySp[i] provides the index of node `i` in nodesBySp
// u1 u2 v1 v2 are the RETURN values from this function (nodes v1,v2 should be disconnected) (nodes u1,u2 should be connected)
/*
Case 1: Random selection explicitly set OR k-hop distributions match (medians)
$>: randomly select edge and non-edge
Case 2: k-hop distributions don't match (medians)
todo: make the synthetic more/less like a small-world network (this tunes - betweenness, eccentricity, diameter, and khop)
How to do this?
Method 1: By BFS hops (select nodes to connect & disconnect that are at a distance 'd' away from eachother)
Method 2: Use nodesBySp array to connect/disconnect nodes
Method 1 -
If you need to make the synthetic MORE small-world; connect nodes which are further away, disconnect any random edge
If you need to make the synthetic LESS small-world; connect nodes which are closer, disconnect any random edge
(these conditions are set on line 1358)
$>:
get a random node1
get a random number of hops from a range (ex [2,5]), ex. 3
do a bfs starting from the node, and get a random node2 at that distance from node1
connect node1-node2
disconnect any random edge
Method 2 -
If you need to make the synthetic MORE small-world; connect nodes with lesser num. of SPs through them, disconnect nodes with higher num. of SPs
If you need to make the synthetic LESS small-world; connect nodes with higher num. of SPs through them, disconnect nodes with lower num. of SPs
(these conditions are set on line 1358)
$>:
take random n1 & n2 in a particular interval (lower 35% or higher 35% indexes in nodesBySp)
connect n1 & n2
do:
take random n3 in a particular interval (lower 35% or higher 35% indexes in nodesBySp)
get random n4 connected to n3
while:
n4 not in same interval as n3
disconnect n3 & n4
*/
int x, y;
if ((node_selection==NODE_SEL_ALWAYS_RANDOM) || (sw.make == 0)){
// random selection
// existing edge
do {
x = drand48()*G->n;
y = getRandomConnectedNode(G, x);
} while((G->degree[x] <= 1) || (G->degree[y] <= 1));
assert(GraphAreConnected(G, x, y));
assert(x!=y);
memcpy(v1, &x, sizeof(int));
memcpy(v2, &y, sizeof(int));
// existing non-edge
do {
x = drand48()*G->n;
y = drand48()*G->n;
}while((x==y) || (GraphAreConnected(G, x, y)));
assert(!GraphAreConnected(G, x, y));
assert(x!=y);
memcpy(u1, &x, sizeof(int));
memcpy(u2, &y, sizeof(int));
return;
}
// for node selection by BFS hops
if(node_selection == NODE_SEL_BY_HOPS){
int d = sw.lowerHops + (int)((sw.upperHops - sw.lowerHops + 1) * drand48());
assert(d>1);
// existing edge
do{
x = drand48()*G->n;
y = getRandomConnectedNode(G, x);
} while((G->degree[x] <= 1) || (G->degree[y] <= 1));
assert(GraphAreConnected(G, x, y));
assert(x!=y);
memcpy(v1, &x, sizeof(int));
memcpy(v2, &y, sizeof(int));
// existing non-edge
do {
x = drand48()*G->n;
y = getRandomNodeAtHops(G, x, d);
}while((x==y) || (GraphAreConnected(G, x, y)));
assert(x!=y);
assert(!GraphAreConnected(G, x, y));
memcpy(u1, &x, sizeof(int));
memcpy(u2, &y, sizeof(int));
return;
}
// for node selection by node ShortestPaths
if(node_selection == NODE_SEL_SHORT_PATH){
double lower = 0.0;
double pd_interval = 0.05;
double interval = 0.35;
int random;
int l, u, yi;
// existing edge
do{
random = drand48() * interval * G->n;
if (sw.make == -1){ // make network LESS small-world
// disconnect a node with HIGH SP
x = nodesBySp[(G->n -1) - (int)(MAX(lower, pd_interval) * G->n) - random];
l = (G->n -1) - (int)(MAX(lower, pd_interval) * G->n) - (interval * G->n);
u = G->n - 1;
}else if(sw.make == 1){ // make network MORE small-world
// disconnect a node with LOW SP
x = nodesBySp[0 + (int)(lower*G->n) + random];
l = 0;
u = (int)(lower*G->n) + (interval * G->n);
}
y = getRandomConnectedNode(G, x);
yi = index_in_nodesBySp[y];
assert((yi>=0) && (yi<G->n));
}while((G->degree[x] <= 1) || (G->degree[y] <= 1) || (yi<l) || (yi>u));
assert(GraphAreConnected(G, x, y));
assert(x!=y);
memcpy(v1, &x, sizeof(int));
memcpy(v2, &y, sizeof(int));
// existing non-edge
do{
if(sw.make == -1){ // make network LESS small world
// connect nodes with LOW SP
random = drand48() * interval * G->n;
x = nodesBySp[0 + (int)(lower*G->n) + random];
random = drand48() * interval * G->n;
y = nodesBySp[0 + (int)(lower*G->n) + random];
}else if(sw.make == 1){ // make network MORE small world
// connect nodes with HIGH SP
random = drand48() * interval * G->n;
x = nodesBySp[(G->n -1) - (int)(lower*G->n) - random];