-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathckbDynamicNodeSet.cpp~
1509 lines (1385 loc) · 46.8 KB
/
ckbDynamicNodeSet.cpp~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include <ctime>
#include <math.h>
#include "PowerlawDegreeSequence.h"
#include <algorithm>
#include <sstream>
#include <string>
#include <string.h>
/*With node changes*/
using namespace std;
/*************************PARAMETERS******************************/
int T = 1000; //Number of time slots
double lambda = 0.2;//how sharply communities will rise and fall
int N1 = 50000; //Number of nodes
int N2; //Number of communities, set in the program
int xmin = 1; //minimum user-community memberships
int xmax = 50; //maximum user-community memberships
double beta1 = -2.5; //community membership exponent
int mmin = 2; //minimum community size
int mmax = 7500; //maximum community size
double beta2 = -2.5; //community size exponent
double alpha = 2; //affects intra community edge probability
double gamma_1 = 0.5; //affects intra community edge probability
double eps = 2; //inter community edge probability, epsilon = eps/N1
double prob = 4; //alternative: Intra community edge probability - not used anymore
double probEvent = 0.1; //Probability of an event happening
int timeToMerge = 10;
int timeToSplit = 10;
double minSplitRatio = 0.3; //A community born of split is atleast 0.3 of original community
/*******************************************************************/
/*************************DATA STRUCTURES***************************/
struct edge{
int sourceId;
int destId;
int communityId;
int startTime;
int endTime;
edge(int isSourceId, int isDestId, int isCommunityId){
this->sourceId = isSourceId;
this->destId = isDestId;
this->communityId = isCommunityId;
this->startTime = -1;
this->endTime = -1;
}
void generateStartExp(int startLimit){
double z = ((double) rand())/((double) RAND_MAX);
double cumul_prob = 0;
for (int i = startLimit; i>=0;i--){
double p_x = lambda * exp( -1* lambda * (startLimit - i));
cumul_prob += p_x;
if (z <= cumul_prob){
this->startTime = i;
return;
}
}
this->startTime = 0;
}
void generateEndExp(int endLimit){
if (this->endTime != -1) return;
double z = ((double) rand())/((double) RAND_MAX);
double cumul_prob = 0;
for (int i = endLimit; i<=T;i++){
double p_x = lambda * exp( -1* lambda * (i - endLimit));
cumul_prob += p_x;
if (z <= cumul_prob){
//cout << "generateEndExp: Setting end time of " << communityId << ": " << sourceId << ", " << destId << ", " << startTime << ", " << endTime << " to " << i << endl << flush;
if (i < this->startTime) i = this->startTime + 1;
this->endTime = i;
return;
}
}
this->endTime = T;
}
};
struct node{
int nodeId;
vector<edge*> adj;
vector<int> communities;
int startTime;
int endTime;
double estimatedDegree;
node(int isNodeId){
this->nodeId = isNodeId;
this->startTime = 0;
this->endTime = -1;
this->estimatedDegree = 0;
}
node(int isNodeId, int isStartTime){
this->nodeId = isNodeId;
this->startTime = isStartTime;
this->endTime = -1;
this->estimatedDegree = 0;
}
bool addEdge(edge *e){
if (e->destId == nodeId){
delete e;
return false;
}//no self loops
bool flag = true;
for (int i=0;i<adj.size();i++){
if ((adj[i]->destId == e->destId)){
if ((adj[i]->endTime == -1)||(adj[i]->endTime > e->startTime)){
flag = false;
break;
}
}
}
if (flag) adj.push_back(e);
else delete e; //no multi edges
return flag;
}
/*to be called on destId*/
edge *findReverseAliveEdge(int srcId){
for (int i=0;i<adj.size();i++){
if ((adj[i]->destId == srcId) && (adj[i]->communityId == -1) && (adj[i]->endTime == -1))
return adj[i];
}
return NULL;
}
void printCommunities(){
for (int i=0;i<communities.size();i++) cout << ", " << communities[i];
cout << endl << flush;
}
void printEdgeList(){
cout << "Edge list of " << nodeId << endl;
for (int i = 0; i < adj.size(); i++)
cout << "(" << adj[i]->destId << ", " << adj[i]->startTime << ", " << adj[i]->endTime << ", " << adj[i]->communityId << ") ";
cout << endl;
}
node(){}
bool hasEdge(int destId){
bool flag = false;
for (int i = 0; i < adj.size(); i++){
if ((adj[i]->destId == destId))
flag = true;
}
return flag;
}
};
/*A node attaches to a community via this structure.
It indicates the joining and leaving time of the node in the
community.*/
struct nodeInCommunity{
int nodeId;
int joinTime;
int leaveTime;
nodeInCommunity(int isNodeId){
this->nodeId = isNodeId;
this->joinTime = -1;
this->leaveTime = -1;
}
nodeInCommunity(int isNodeId, int isJoinTime){
this->nodeId = isNodeId;
this->joinTime = isJoinTime;
this->leaveTime = -1;
}
};
struct community{
vector<nodeInCommunity*> nodeList;
int birthTime;
int expansionTime;
int deathTime;
int contractionTime;
int nextAvailableTimeSlot;
bool isAvailable;
int originFlag; //signifies how the community came into being. 0 for birth, 1 for split, 2 for merge
int deletionFlag; //signifies how the community stopped being. 0 for death, 1 for split, 2 for merge
community(){
this->isAvailable = true;
this->originFlag = 0;
this->deletionFlag = 0;
this->birthTime = 0;
this->deathTime = -1;
}
int indexOfNode(int nodeId){
for (int i=0;i<nodeList.size();i++)
if (nodeList[i]->nodeId == nodeId) return i;
return -1;
}
void swapToEnd(int nodeIndex){
int lastIndex = nodeList.size() - 1;
nodeInCommunity *nic = nodeList[lastIndex];
nodeList[lastIndex] = nodeList[nodeIndex];
nodeList[nodeIndex] = nic;
}
void printNodes(){
for (int i=0;i<nodeList.size();i++) cout << ", " << nodeList[i]->nodeId;
cout << endl << flush;
}
};
struct update{
int updateType; //0: edge delete; 1: node add; 2: edge add; 3: node delete;
int u;
int v;
int t;
update(int isUpdateType, int isU, int isV, int isT){
this->updateType = isUpdateType;
this->u = isU;
this->v = isV;
this->t = isT;
}
};
vector<node*> graph;
vector<community*> communities;
vector<int> nodeMemberships;
vector<int> communitySizes;
/************PROFILING QUANTITIES************************************/
double averageSplitTime = 0.0;
double averageMergeTime = 0.0;
double averageBirthTime = 0.0;
double averageDeathTime = 0.0;
double averageNodeAddTime = 0.0;
double averageNodeDeleteTime = 0.0;
int nSplits = 0;
int nMerges = 0;
int nBirths = 0;
int nDeaths = 0;
int nAdd = 0;
int nDelete = 0;
/*******************************************************************/
/************************UTILITY FUNCTIONS**************************/
double expectedPowerLaw(int xmin, int xmax, double beta){
PowerlawDegreeSequence z(xmin,xmax,beta);
z.run();
double meanValue = z.getExpectedAverageDegree();
return meanValue;
}
vector<int> powerLawDegreeSequence(int xmin, int xmax, double beta, int N){
PowerlawDegreeSequence z(xmin,xmax,beta);
z.run();
vector<int> degreeSequence = z.getDegreeSequence(N);
return degreeSequence;
}
void permuteFY(int *sequence, int size){
for (int i=0;i<size-1;i++){
int j = i + rand()%(size-i);
int temp = sequence[i];
sequence[i] = sequence[j];
sequence[j] = temp;
}
}
int get_next_edge_distance(const double log_cp) {
return (int) (1 + floor(log(1.0 - (((double) rand())/((double) RAND_MAX))) / log_cp));
}
bool isInCommunityAtT(int t, int communityId, int nodeId){
community c = *communities[communityId];
int pos = c.indexOfNode(nodeId);
bool startFlag = false, endFlag = false;
if (c.nodeList[pos]->joinTime <= t) startFlag = true;
if (c.nodeList[pos]->leaveTime == -1) endFlag = true;
if (c.nodeList[pos]->leaveTime >= t) endFlag = true;
return (startFlag && endFlag);
}
bool compareUpdate(update i, update j){
if (i.t < j.t) return true;
if (i.t > j.t) return false;
if ((i.u == j.u) && (i.v == j.v)){
if (i.updateType < j.updateType) return false;
if (i.updateType > j.updateType) return true;
}
if (i.updateType < j.updateType) return true;
if (i.updateType > j.updateType) return false;
return false;
}
/*******************************************************************/
/************************STATIC STRUCTURE***************************/
void generateBigraph(){
double expectedMemberships = expectedPowerLaw(xmin,xmax,beta1);
double expectedCommunitySize = expectedPowerLaw(mmin,mmax,beta2);
double M0 = N1 * expectedMemberships; //Number of edges to be in bigraph
double N2d = M0/ expectedCommunitySize; //Therefore, number of communities
/*Generate power law degree sequences*/
nodeMemberships = powerLawDegreeSequence(xmin,xmax,beta1,N1);
communitySizes = powerLawDegreeSequence(mmin,mmax,beta2,N2d);
N2 = communitySizes.size();
int maxCommSize = 0;
for (int i=0;i<N2;i++)
if (maxCommSize < communitySizes[i]) maxCommSize = communitySizes[i];
for (int i=0;i<N2;i++)
communities.push_back(new community());
for (int i=0;i<N2;i++){
communities[i]->birthTime = 0;
}
int sizeA=0, sizeB=0;
for (int i=0;i<N1;i++)
sizeA += nodeMemberships[i];
for (int i=0;i<N2;i++)
sizeB += communitySizes[i];
/*what do we do if sizeA and sizeB do not agree?
One (and not the best) solution is to randomly
select a community size and change it by 1*/
while (sizeA > sizeB){ //increment community sizes
int randomIndex = rand()%N2;
if (communitySizes[randomIndex] == mmax) continue;
communitySizes[randomIndex] += 1;
sizeB += 1;
}
while (sizeA < sizeB){
int randomIndex = rand()%N2;
if (communitySizes[randomIndex] == mmin) continue;
communitySizes[randomIndex] -= 1;
sizeB -= 1;
}
int *A = new int[sizeA];
int *B = new int[sizeB];
int l=0;
for (int i=0;i<N1;i++)
for (int j=0;j<nodeMemberships[i];j++){
A[l] = i;
l++;
}
l = 0;
for (int i=0;i<N2;i++)
for (int j=0;j<communitySizes[i];j++){
B[l] = i;
l++;
}
permuteFY(A,sizeA);
permuteFY(B,sizeB);
int numEdges = sizeA;
if (numEdges > sizeB) numEdges = sizeB;
/*Generate Edges in Bigraph*/
for (int i=0;i<numEdges;i++){
int nodeIndex = A[i];
int commIndex = B[i];
int index = communities[commIndex]->indexOfNode(nodeIndex);
if (index == -1){
graph[nodeIndex]->communities.push_back(commIndex);
(communities[commIndex]->nodeList).push_back(new nodeInCommunity(nodeIndex,0));
}
else{
/*Attempt rewiring only if communitySize is mmin, because otherwise,
we will end up with constraint violating community size.
We do not do rewiring otherwise*/
/*select a node at random that is not in this community*/
if (communitySizes[commIndex] == mmin){
int nodeId = nodeIndex;
while ((index != -1) || (graph[nodeId]->communities.size() == 0)){
nodeId = rand()%N1;
index = communities[commIndex]->indexOfNode(nodeId);
if (index != -1) continue;
bool x = false;
for (int ci = 0; ci < graph[nodeId]->communities.size(); ci++){
int newCommunityId = graph[nodeId]->communities[ci];
int tempIndex = communities[newCommunityId]->indexOfNode(nodeIndex);
if (tempIndex == -1){
//assign nodeIndex to newCommunityId
graph[nodeIndex]->communities.push_back(newCommunityId);
(communities[newCommunityId]->nodeList).push_back(new nodeInCommunity(nodeIndex,0));
//remove nodeId from newCommunityId, assign nodeId to commIndex
graph[nodeId]->communities[ci] = commIndex;
(communities[commIndex]->nodeList).push_back(new nodeInCommunity(nodeId,0));
community *ctemp = communities[newCommunityId];
for (int ni = 0; ni < ctemp->nodeList.size(); ni++){
if ((ctemp->nodeList[ni])->nodeId == nodeId){
ctemp->nodeList.erase(ctemp->nodeList.begin() + ni);
x = true;
break;
}
}
if (x) break;
}
}
if (x) break;
}
}
}
}
for (int i=0;i<N1;i++){
nodeMemberships[i] = (int) (floor(1.2*nodeMemberships[i]));
}
maxCommSize = 0;
for (int i=0;i<N2;i++)
if (maxCommSize < communitySizes[i]) maxCommSize = communitySizes[i];
}
void generateEdgesForCommunity(int commIndex){
vector<int> nodesInCommunity;
for (int i=0;i<N1;i++){
for (int j=0;j<graph[i]->communities.size();j++)
if (graph[i]->communities[j] == commIndex){
nodesInCommunity.push_back(i);
break;
}
}
int numberOfNodes = nodesInCommunity.size();
if (numberOfNodes <= 1) return;
double probNew = alpha/pow(numberOfNodes, gamma_1);
if (numberOfNodes == 2) probNew = 1;
if (probNew > 1.0) probNew = 1.0;
/*Copied from Networkit implementation of Batagelj Brandes*/
const double log_cp = log(1.0 - probNew); // log of counter probability
// create edges
int curr = 1;
int next = -1;
while (curr < numberOfNodes) {
// compute new step length
next += get_next_edge_distance(log_cp);
// check if at end of row
while ((next >= curr) && (curr < numberOfNodes)) {
// adapt to next row
next = next - curr;
curr++;
}
// insert edge
if (curr < numberOfNodes) {
int a = nodesInCommunity[curr];
int b = nodesInCommunity[next];
edge *fwd = new edge(a,b,commIndex);
fwd->startTime = 0;
bool flagR = graph[a]->addEdge(fwd);
if (flagR){
edge *bwd = new edge(b,a,-1); //communityId = -1 for a reverse edge
bwd->startTime = 0;
flagR = graph[b]->addEdge(bwd);
if (!flagR){
cout << "1.ERROR HERE!" << endl << flush;
exit(0);
}
}
}
}
double expectedDegree = (numberOfNodes-1)*probNew;
for (int i=0;i<nodesInCommunity.size();i++)
graph[i]->estimatedDegree += ((int)(ceil(expectedDegree)));
}
void generateEpsCommunity(){
double epsilon = eps/N1;
int numEdges = (int) floor(epsilon * N1 * (N1-1) * 0.5);
for (int i=0;i<numEdges;i++){
int sourceNode1 = rand()%N1;
int destNode1 = rand()%N1;
int sourceNode2 = rand()%N1;
int destNode2 = rand()%N1;
int switchTime = T/2 + (rand()%200 - 100);
if (switchTime < 0) switchTime = 0;
if (sourceNode1 != destNode1){
edge *fwd = new edge(sourceNode1,destNode1,-2); //communityId = -2 for an external edge
fwd->startTime = 0;
fwd->endTime = switchTime;
bool flagR = graph[sourceNode1]->addEdge(fwd);
if (flagR){
edge *bwd = new edge(destNode1,sourceNode1,-4);
bwd->startTime = 0;
bwd->endTime = switchTime;
flagR = graph[destNode1]->addEdge(bwd);
if (!flagR){
cout << "2.ERROR HERE!" << endl << flush;
exit(0);
}
}
}
if (sourceNode2 != destNode2){
edge *fwd = new edge(sourceNode2,destNode2,-2); //communityId = -2 for an external edge
fwd->startTime = switchTime + 1;
fwd->endTime = -1;
bool flagR = graph[sourceNode2]->addEdge(fwd);
if (flagR){
edge *bwd = new edge(destNode2,sourceNode2,-4);
bwd->startTime = switchTime + 1;
bwd->endTime = -1;
flagR = graph[destNode2]->addEdge(bwd);
if (!flagR){
cout << "3.ERROR HERE!" << endl << flush;
exit(0);
}
}
}
}
}
bool isSanity = true;
void sanityCheck(){
for (int i=0;i<N1;i++){
for (int j=0; j< graph[i]->communities.size(); j++){
int cIndex = graph[i]->communities[j];
community *c = communities[cIndex];
if (c->indexOfNode(i) == -1) isSanity = false;
}
}
}
bool sanityCheck2(){
for (int i=0;i<N1;i++){
for (int j = 0; j< graph[i]->adj.size(); j++){
edge *e = graph[i]->adj[j];
if ((e->startTime > e->endTime) && (e->startTime != -1) && (e->endTime != -1))
return false;
}
}
return true;
}
bool sanityCheck3(){
for (int i=0;i<N1;i++){
for (int j = 0; j < graph[i]->adj.size(); j++){
int dj = graph[i]->adj[j]->destId, sj = graph[i]->adj[j]->startTime, ej = graph[i]->adj[j]->endTime;
for (int k = j+1; k < graph[i]->adj.size(); k++){
int dk = graph[i]->adj[k]->destId, sk = graph[i]->adj[k]->startTime, ek = graph[i]->adj[k]->endTime;
if (dj == dk){
//intervals should be disjoint
bool flag = false;
if ((sj >= ek) && (ek != -1)) flag = true;
if ((sk >= ej) && (ej != -1)) flag = true;
if (!flag){
cout << "( "<< sj << ", " << ej << " ), ( " << sk << ", " << ek << " )" << ", " << graph[i]->adj[j]->communityId << ", " << graph[i]->adj[k]->communityId << endl;
return false;
}
}
}
}
}
return true;
}
void generateStaticStructure(){
for (int i=0;i<N1;i++) graph.push_back(new node(i));
generateBigraph();
int numCommunities = communitySizes.size();
for (int i=0;i<numCommunities;i++){
generateEdgesForCommunity(i);
}
generateEpsCommunity();
sanityCheck();
}
/*******************************************************************/
/*************************COMMUNITY EVENTS***************************/
void deathCommunity(int commIndex, int timeslot){
community *c = communities[commIndex];
c->isAvailable = false;
c->nextAvailableTimeSlot = -1;
c->contractionTime = timeslot;
c->deathTime = timeslot + rand()%10 + 5; //contraction time between 5 and 14 s
if (c->contractionTime < 0) c->contractionTime = 0;
int P = c->nodeList.size();
int coreSize = (int)(0.1*P);
if (coreSize < mmin) coreSize = mmin;
for (int i=0 ; i < c->nodeList.size() ; i++){
if (c->nodeList[i]->leaveTime > -1) continue;
if (i < coreSize) c->nodeList[i]->leaveTime = c->deathTime;
else c->nodeList[i]->leaveTime = (int) floor(c->deathTime - (((double) i)/P)*(c->deathTime - c->contractionTime));
}
for (int i=0;i<c->nodeList.size();i++){
int u = c->nodeList[i]->nodeId;
if (graph[u]->endTime > -1) continue; //this vertex is already dead, and so has left the community
nodeMemberships[u] -= 1;
for (int j=0; j< graph[u]->adj.size();j++){
if (graph[u]->adj[j]->communityId == commIndex){
//generate end time with exponential
int p_u = i;
int p_v = c->indexOfNode(graph[u]->adj[j]->destId);
int uLeaveTime = c->nodeList[p_u]->leaveTime;
int vLeaveTime = c->nodeList[p_v]->leaveTime;
int minLeaveTime = uLeaveTime;
if (minLeaveTime > vLeaveTime) minLeaveTime = vLeaveTime;
int endLimit = minLeaveTime;
graph[u]->adj[j]->generateEndExp(endLimit);
//set end time of reverse edge here
int dj = graph[u]->adj[j]->destId;
edge *reverseEdge = graph[dj]->findReverseAliveEdge(u);
if ((reverseEdge)&&(reverseEdge->endTime == -1)) reverseEdge->endTime = graph[u]->adj[j]->endTime;
}
}
}
}
void birthCommunity(int timeslot){
//draw community size
PowerlawDegreeSequence z(mmin,mmax,beta2);
z.run();
int commSize = z.getDegree();
//draw community members
double *nodeProbs = new double[N1];
int numCandidateNodes = 0;
for (int i=0;i<N1;i++){
nodeProbs[i] = nodeMemberships[i] - graph[i]->communities.size();
if (nodeProbs[i] <= 0) nodeProbs[i] = 0;
else numCandidateNodes += 1;
}
//normalize nodeProbs
if (numCandidateNodes < commSize) return; //could not birth community
double sumNodeProbs = 0;
for (int i=0;i<N1;i++) sumNodeProbs += nodeProbs[i];
for (int i=0;i<N1;i++) nodeProbs[i] /= sumNodeProbs;
//for (int i=1;i<N1;i++) nodeProbs[i] += nodeProbs[i-1];
int commIndex = communities.size();
//cout << "Birthing community " << commIndex << " at " << timeslot << endl;
//cout << "N2 = " << N2 << endl;
communities.push_back(new community());
N2 += 1;
community *c = communities[commIndex];
c->birthTime = timeslot;
c->expansionTime = timeslot + rand()%10 + 5; //contraction time between 5 and 14 s
int currSize = 0;
bool *nodeSelected = new bool[N1]; //keeps track of which nodes have been selected in the community
for (int i=0;i<N1;i++) nodeSelected[i] = false;
int *res = new int[commSize];
double *keys = new double[N1];
for (int i=0;i<N1;i++){
if (nodeProbs[i]==0) continue;
double r = ((double) rand())/((double) RAND_MAX);
keys[i] = pow(r,nodeProbs[i]);
}
double minValue = 1;
int minIndex = -1;
int i;
for (i=0;i<N1;i++){
if (currSize == commSize) break;
if (nodeProbs[i] > 0){
res[currSize] = i;
if (minValue >= keys[i]){
minValue = keys[i];
minIndex = currSize;
}
currSize += 1;
}
}
for (;i<N1;i++){
if (nodeProbs[i]>0){
if (keys[i] > minValue){
res[minIndex] = i;
minValue = 1;
minIndex = -1;
for (int j=0;j<commSize;j++){
if (minValue >= keys[res[j]]){
minValue = keys[res[j]];
minIndex = j;
}
}
}
}
}
if (commSize < mmin) return;
for (i=0;i<commSize;i++){
c->nodeList.push_back(new nodeInCommunity(res[i]));
graph[res[i]]->communities.push_back(commIndex);
}
//generate internal edges
vector<int> nodesInCommunity;
for (int i=0;i<c->nodeList.size();i++)
nodesInCommunity.push_back(c->nodeList[i]->nodeId);
int numberOfNodes = nodesInCommunity.size();
//double probNew = 2*prob/(numberOfNodes-1);
double probNew = alpha/pow(numberOfNodes,gamma_1);
if ((probNew > 1.0) || (numberOfNodes == 2)) probNew = 1;
int coreSize = (int)(0.1*numberOfNodes);
if (coreSize < mmin) coreSize = mmin;
for (int i=0; i<numberOfNodes; i++){
if (i < coreSize) c->nodeList[i]->joinTime = c->birthTime;
else c->nodeList[i]->joinTime = (int) (floor(c->birthTime + (((double) i)/numberOfNodes)*(c->expansionTime - c->birthTime)));
}
/*Copied from Networkit implementation of Batagelj Brandes*/
const double log_cp = log(1.0 - probNew); // log of counter probability
// create edges
int curr = 1;
int next = -1;
c->nextAvailableTimeSlot = -1;
while (curr < numberOfNodes) {
// compute new step length
next += get_next_edge_distance(log_cp);
// check if at end of row
while ((next >= curr) && (curr < numberOfNodes)) {
// adapt to next row
next = next - curr;
curr++;
}
// insert edge
if (curr < numberOfNodes) {
int a = nodesInCommunity[curr];
int b = nodesInCommunity[next];
bool flag = true;
for (int l=0;l < graph[a]->adj.size();l++){
if ((graph[a]->adj[l])->destId == b){
flag = false;
break;
}
}
if (!flag) continue;
edge *fwd = new edge(a,b,commIndex);
//generate start time
int p_u = c->indexOfNode(a);
int p_v = c->indexOfNode(b);
int uJoinTime = c->nodeList[p_u]->joinTime;
int vJoinTime = c->nodeList[p_v]->joinTime;
int maxJoinTime = uJoinTime;
if (maxJoinTime < vJoinTime) maxJoinTime = vJoinTime;
int startLimit = maxJoinTime;
}
}
//mark busy
c->isAvailable = false;
}
void generateEdgesForSplitCommunity(int commIndex, int timeslot){
community *c = communities[commIndex];
int numberOfNodes = c->nodeList.size();
if (numberOfNodes <= 1) return;
double p1 = alpha/pow(numberOfNodes, gamma_1);
double p0 = 0;
for (int i=0; i<numberOfNodes; i++){
int u = c->nodeList[i]->nodeId;
for (int j=0; j < graph[u]->adj.size(); j++){
if (graph[u]->adj[j]->communityId == commIndex)
p0 += 1;
}
}
p0 /= (numberOfNodes*(numberOfNodes-1))/2.0;
if (p1 <= p0) return;
double probNew = (p1-p0)*(1+p0);
if (numberOfNodes == 2) probNew = 1;
if (probNew > 1.0) probNew = 1.0;
/*Copied from Networkit implementation of Batagelj Brandes*/
const double log_cp = log(1.0 - probNew); // log of counter probability
// create edges
int curr = 1;
int next = -1;
while (curr < numberOfNodes) {
// compute new step length
next += get_next_edge_distance(log_cp);
// check if at end of row
while ((next >= curr) && (curr < numberOfNodes)) {
// adapt to next row
next = next - curr;
curr++;
}
// insert edge
if (curr < numberOfNodes) {
int a = c->nodeList[curr]->nodeId;
int b = c->nodeList[next]->nodeId;
edge *fwd = new edge(a,b,commIndex);
fwd->startTime = timeslot;
bool flag = graph[a]->addEdge(fwd);
if (flag){
edge *bwd = new edge(b,a,-1); //communityId = -1 for a reverse edge
bwd->startTime = timeslot;
flag = graph[b]->addEdge(bwd);
if (!flag){
cout << "5.ERROR HERE!" << endl << flush;
graph[a]->printEdgeList();
graph[b]->printEdgeList();
exit(0);
}
}
}
}
}
void splitCommunity(int commIndex, int timeslot){
//cout << "N2 = " << N2 << endl << flush;
community *c = communities[commIndex];
c->isAvailable = false;
c->nextAvailableTimeSlot = -1; //community dead, will never be available again
c->deletionFlag = 1;
double splitPoint = minSplitRatio + (((double) rand())/((double) RAND_MAX))*(1.0 - 2.0*minSplitRatio);
int splitBorder = (floor) (splitPoint * c->nodeList.size());
int L = splitBorder+1;
int R = c->nodeList.size() - L;
//create the two smaller communities
int c1Index = communities.size();
communities.push_back(new community());
community *c1 = communities[c1Index];
c1->originFlag = 1;
c1->isAvailable = false;
int c2Index = communities.size();
communities.push_back(new community());
community *c2 = communities[c2Index];
c2->originFlag = 1;
c2->isAvailable = false;
//cout << "Splitting community " << commIndex << ", of size = " << c->nodeList.size() << ", into " << c1Index << " and " << c2Index << " at " << timeslot << ", originFlag = " << c->originFlag << endl << flush;
int *leaveTimes = new int[c->nodeList.size()];
int numNodesInvolved = 0;
for (int i=0;i < (c->nodeList.size());i++){
int u = (c->nodeList[i])->nodeId;
if (graph[u]->endTime > -1){
leaveTimes[i] = -1;
continue;
}
numNodesInvolved += 1;
c->nodeList[i]->leaveTime = timeslot;
nodeInCommunity *nic = new nodeInCommunity(u,timeslot);
if (i>splitBorder){
leaveTimes[i] = (int) floor(timeslot + (((double) (R+L-i))/R)*timeToSplit);
c2->nodeList.push_back(nic);
graph[u]->communities.push_back(c2Index);
}
else{
leaveTimes[i] = (int) floor(timeslot + (((double) i)/L)*timeToSplit);
c1->nodeList.push_back(nic);
graph[u]->communities.push_back(c1Index);
}
}
//generate edge end times
int maxEndTime = 0;
for (int i=0;i<=splitBorder;i++){ //for all nodes in c1
int u = c->nodeList[i]->nodeId;
for (int j=0;j<graph[u]->adj.size();j++){
if (graph[u]->adj[j]->communityId != commIndex) continue; //is a different edge, no need to generate end time
int v = graph[u]->adj[j]->destId;
if (graph[v]->endTime > -1) continue;
int indexOfv = c->indexOfNode(v);
if (indexOfv > splitBorder){ //if v is in c2
int endLimit = (int) floor(0.5*leaveTimes[i] + 0.5*leaveTimes[indexOfv]);
if (endLimit < timeslot){
cout << "1.Now this problem is there!" << endl;
}
graph[u]->adj[j]->generateEndExp(endLimit);
//set endTime of reverse edge here
int dj = graph[u]->adj[j]->destId;
edge *reverseEdge = graph[dj]->findReverseAliveEdge(u);
if (reverseEdge){
if (reverseEdge->endTime == -1)
reverseEdge->endTime = graph[u]->adj[j]->endTime;
}
if (maxEndTime < graph[u]->adj[j]->endTime) maxEndTime = graph[u]->adj[j]->endTime;
}
else graph[u]->adj[j]->communityId = c1Index;
}
}
/*This loop is required because we are checking if the edge processed has community id = commIndex*/
for (int i=splitBorder+1;i<numNodesInvolved;i++){ //for all nodes in c2
int u = c->nodeList[i]->nodeId;
for (int j=0;j<graph[u]->adj.size();j++){
if (graph[u]->adj[j]->communityId != commIndex) continue; //is a different edge, no need to generate end time
int v = graph[u]->adj[j]->destId;
if (graph[v]->endTime > -1) continue; //v is already dead
int indexOfv = c->indexOfNode(v);
if (indexOfv <= splitBorder && indexOfv >=0){ //if v is in c1
int endLimit = (int) floor(0.5*leaveTimes[indexOfv] + 0.5*leaveTimes[i]);
if (endLimit < timeslot){
cout << "2. Now this problem is there!" << endl;
}
graph[u]->adj[j]->generateEndExp(endLimit);
//set end time of reverse edge here
int dj = graph[u]->adj[j]->destId;
edge *reverseEdge = graph[dj]->findReverseAliveEdge(u);
if ((reverseEdge)&&(reverseEdge->endTime==-1))
reverseEdge->endTime = graph[u]->adj[j]->endTime;
if (maxEndTime < graph[u]->adj[j]->endTime) maxEndTime = graph[u]->adj[j]->endTime;
}
else graph[u]->adj[j]->communityId = c2Index;
}
}
c1->birthTime = timeslot + 1;
c2->birthTime = timeslot + 1;
c->deathTime = timeslot;
c1->nextAvailableTimeSlot = maxEndTime+1;
c2->nextAvailableTimeSlot = maxEndTime+1;
generateEdgesForSplitCommunity(c1Index,timeslot);
generateEdgesForSplitCommunity(c2Index,timeslot);
N2 += 2;
delete [] leaveTimes;
}
void mergeCommunities(int c1Index, int c2Index, int timeslot){
community *c1 = communities[c1Index];
community *c2 = communities[c2Index];
c1->isAvailable = false;
c1->nextAvailableTimeSlot = -1; //community dead, will never be available again
c1->deletionFlag = 2;
c2->isAvailable = false;
c2->nextAvailableTimeSlot = -1;
c2->deletionFlag = 2;
int cIndex = communities.size();
communities.push_back(new community());
community *c = communities[cIndex];
//cout << "Merging communities " << c1Index << " and " << c2Index << " into " << cIndex << " at " << timeslot << endl;
//cout << "N2 = " << N2 << endl << flush;
c->originFlag = 2;
c->isAvailable = false;
int L = c1->nodeList.size();
int R = c2->nodeList.size();
for (int i=0;i<c1->nodeList.size();i++){
int u = c1->nodeList[i]->nodeId;
if (graph[u]->endTime > -1) continue;
c1->nodeList[i]->leaveTime = timeslot + timeToMerge;
nodeInCommunity *nic = new nodeInCommunity(u, timeslot + timeToMerge + 1);
c->nodeList.push_back(nic);
graph[u]->communities.push_back(cIndex);
nodeInCommunity *nic1 = new nodeInCommunity(u, (int) floor(timeslot + (1-(((double) i)/c1->nodeList.size()))*timeToMerge));
nic1->leaveTime = timeslot + timeToMerge;
c2->nodeList.push_back(nic1);
graph[u]->communities.push_back(c2Index);
//cout << "Inserted " << u << " to c2" << endl;
}
for (int i=0;i<R;i++){
int u = c2->nodeList[i]->nodeId;
if (graph[u]->endTime > -1) continue;
c2->nodeList[i]->leaveTime = timeslot + timeToMerge;
int posU = c->indexOfNode(u);
if (posU != -1) continue;
nodeInCommunity *nic = new nodeInCommunity(u, timeslot + timeToMerge + 1);
c->nodeList.push_back(nic);
graph[u]->communities.push_back(cIndex);
nodeInCommunity *nic1 = new nodeInCommunity(u, (int) floor(timeslot + (((double) i)/c2->nodeList.size())*timeToMerge));
nic1->leaveTime = timeslot + timeToMerge;
c1->nodeList.push_back(nic1);
graph[u]->communities.push_back(c1Index);
//cout << "Inserted " << u << " to c1" << endl;
}
/*number of edges to be inserted between nodes of c1 and c2
* depends on the balance number of edges that remains from
* the required number in c minus the existing number in c1 + c2*/
int maxStartTime=0;
int numberOfNodes = c->nodeList.size();
double prob = alpha/pow(numberOfNodes, gamma_1);
double m1 = 0, m2 = 0, m = prob*(numberOfNodes)*(numberOfNodes-1)*0.5;
for (int i=0; i< c1->nodeList.size();i++)