-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkronecker.cpp
2756 lines (2534 loc) · 107 KB
/
kronecker.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 "stdafx.h"
#include "kronecker.h"
#include "math.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include <sstream>
/////////////////////////////////////////////////
// Kronecker Graphs
const double TKronMtx::NInf = -DBL_MAX;
TRnd TKronMtx::Rnd = TRnd(0);
TKronMtx::TKronMtx(const TFltV& SeedMatrix) : SeedMtx(SeedMatrix) {
MtxDim = (int) sqrt((double)SeedMatrix.Len());
IAssert(MtxDim*MtxDim == SeedMtx.Len());
}
void TKronMtx::SaveTxt(const TStr& OutFNm) const {
FILE *F = fopen(OutFNm.CStr(), "wt");
for (int i = 0; i < GetDim(); i++) {
for (int j = 0; j < GetDim(); j++) {
if (j > 0) fprintf(F, "\t");
fprintf(F, "%f", At(i,j)); }
fprintf(F, "\n");
}
fclose(F);
}
TKronMtx& TKronMtx::operator = (const TKronMtx& Kronecker) {
if (this != &Kronecker){
MtxDim=Kronecker.MtxDim;
SeedMtx=Kronecker.SeedMtx;
}
return *this;
}
bool TKronMtx::IsProbMtx() const {
for (int i = 0; i < Len(); i++) {
if (At(i) < 0.0 || At(i) > 1.0) return false;
}
return true;
}
void TKronMtx::SetRndMtx(const int& PrmMtxDim, const double& MinProb) {
MtxDim = PrmMtxDim;
SeedMtx.Gen(MtxDim*MtxDim);
for (int p = 0; p < SeedMtx.Len(); p++) {
do {
SeedMtx[p] = TKronMtx::Rnd.GetUniDev();
} while (SeedMtx[p] < MinProb);
}
}
void TKronMtx::SetEpsMtx(const double& Eps1, const double& Eps0, const int& Eps1Val, const int& Eps0Val) {
for (int i = 0; i < Len(); i++) {
double& Val = At(i);
if (Val == Eps1Val) Val = double(Eps1);
else if (Val == Eps0Val) Val = double(Eps0);
}
}
// scales parameter values to allow Edges
void TKronMtx::SetForEdges(const int& Nodes, const int& Edges) {
const int KronIter = GetKronIter(Nodes);
const double EZero = pow((double) Edges, 1.0/double(KronIter));
const double Factor = EZero / GetMtxSum();
for (int i = 0; i < Len(); i++) {
At(i) *= Factor;
if (At(i) > 1) { At(i) = 1; }
}
}
void TKronMtx::AddRndNoise(const double& SDev) {
Dump("before");
double NewVal;
int c =0;
for (int i = 0; i < Len(); i++) {
for(c = 0; ((NewVal = At(i)*Rnd.GetNrmDev(1, SDev, 0.8, 1.2)) < 0.01 || NewVal>0.99) && c <1000; c++) { }
if (c < 999) { At(i) = NewVal; } else { printf("XXXXX\n"); }
}
Dump("after");
}
TStr TKronMtx::GetMtxStr() const {
TChA ChA("[");
for (int i = 0; i < Len(); i++) {
ChA += TStr::Fmt("%g", At(i));
if ((i+1)%GetDim()==0 && (i+1<Len())) { ChA += "; "; }
else if (i+1<Len()) { ChA += ", "; }
}
ChA += "]";
return TStr(ChA);
}
void TKronMtx::ToOneMinusMtx() {
for (int i = 0; i < Len(); i++) {
IAssert(At(i) >= 0.0 && At(i) <= 1.0);
At(i) = 1.0 - At(i);
}
}
void TKronMtx::GetLLMtx(TKronMtx& LLMtx) {
LLMtx.GenMtx(MtxDim);
for (int i = 0; i < Len(); i++) {
if (At(i) != 0.0) { LLMtx.At(i) = log(At(i)); }
else { LLMtx.At(i) = NInf; }
}
}
void TKronMtx::GetProbMtx(TKronMtx& ProbMtx) {
ProbMtx.GenMtx(MtxDim);
for (int i = 0; i < Len(); i++) {
if (At(i) != NInf) { ProbMtx.At(i) = exp(At(i)); }
else { ProbMtx.At(i) = 0.0; }
}
}
void TKronMtx::Swap(TKronMtx& KronMtx) {
::Swap(MtxDim, KronMtx.MtxDim);
SeedMtx.Swap(KronMtx.SeedMtx);
}
int TKronMtx::GetNodes(const int& NIter) const {
return (int) pow(double(GetDim()), double(NIter));
}
int TKronMtx::GetEdges(const int& NIter) const {
return (int) pow(double(GetMtxSum()), double(NIter));
}
int TKronMtx::GetKronIter(const int& Nodes) const {
return (int) ceil(log(double(Nodes)) / log(double(GetDim()))); // upper bound
//return (int) TMath::Round(log(double(Nodes)) / log(double(GetDim()))); // round to nearest power
}
int TKronMtx::GetNZeroK(const PNGraph& Graph) const {
return GetNodes(GetKronIter(Graph->GetNodes()));
}
double TKronMtx::GetEZero(const int& Edges, const int& KronIters) const {
return pow((double) Edges, 1.0/double(KronIters));
}
double TKronMtx::GetMtxSum() const {
double Sum = 0;
for (int i = 0; i < Len(); i++) {
Sum += At(i); }
return Sum;
}
double TKronMtx::GetRowSum(const int& RowId) const {
double Sum = 0;
for (int c = 0; c < GetDim(); c++) {
Sum += At(RowId, c); }
return Sum;
}
double TKronMtx::GetColSum(const int& ColId) const {
double Sum = 0;
for (int r = 0; r < GetDim(); r++) {
Sum += At(r, ColId); }
return Sum;
}
double TKronMtx::GetEdgeProb(int NId1, int NId2, const int& NKronIters) const {
double Prob = 1.0;
for (int level = 0; level < NKronIters; level++) {
Prob *= At(NId1 % MtxDim, NId2 % MtxDim);
if (Prob == 0.0) { return 0.0; }
NId1 /= MtxDim; NId2 /= MtxDim;
}
return Prob;
}
double TKronMtx::GetNoEdgeProb(int NId1, int NId2, const int& NKronIters) const {
return 1.0 - GetEdgeProb(NId1, NId2, NKronIters);
}
double TKronMtx::GetNoEdgeLL(int NId1, int NId2, const int& NKronIters) const {
return log(1.0 - exp(GetEdgeLL(NId1, NId2, NKronIters)));
}
double TKronMtx::GetEdgeLL(int NId1, int NId2, const int& NKronIters) const {
double LL = 0.0;
for (int level = 0; level < NKronIters; level++) {
const double& LLVal = At(NId1 % MtxDim, NId2 % MtxDim);
//if(NId1>Nodes)
//printf("***************** NID1 %d NID2 %d MTXDIM %d LLVal %d ************************ \n", NId1, NId2, MtxDim, LLVal);
if (LLVal == NInf) return NInf;
LL += LLVal;
NId1 /= MtxDim; NId2 /= MtxDim;
}
return LL;
}
// 2nd order Taylor approximation log(1-x) ~ -x - 0.5x^2
double TKronMtx::GetApxNoEdgeLL(int NId1, int NId2, const int& NKronIters) const {
const double EdgeLL = GetEdgeLL(NId1, NId2, NKronIters);
return -exp(EdgeLL) - 0.5*exp(2*EdgeLL);
}
double TKroneckerLL::CalcApxGraphLL() {
LogLike = GetApxEmptyGraphLL(); // O(N_0)
for (int nid = 0; nid < Nodes; nid++) {
const TNGraph::TNodeI Node = Graph->GetNI(nid);
const int SrcNId = NodePerm[nid];
for (int e = 0; e < Node.GetOutDeg(); e++) {
const int DstNId = NodePerm[Node.GetOutNId(e)];
//printf("***************** SrcNId %d, nid %d, DstNid %d, outNodeNID %d ************************ \n", SrcNId, nid, DstNId, Node.GetOutNId(e) );
LogLike = LogLike - LLMtx.GetApxNoEdgeLL(SrcNId, DstNId, KronIters) + LLMtx.GetEdgeLL(SrcNId, DstNId, KronIters);
}
}
/*Change Starts Here */
const int aggravatedN = ceil(log(Nodes*1.0)/log(2.0));
for(int nid = Nodes; nid < pow(2.0, aggravatedN); nid++)
{
for (int e = 0; e < pow(2.0, aggravatedN); e++) {
int dest;
if(e < Nodes)
dest= NodePerm[e];
else
dest = e;
// printf("***************** src %d dest %d ************************ \n", nid, dest );
LogLike = LogLike - 2*LLMtx.GetApxNoEdgeLL(nid, dest, KronIters);
}
}
/* Change Ends Here*/
return LogLike;
}
bool TKronMtx::IsEdgePlace(int NId1, int NId2, const int& NKronIters, const double& ProbTresh) const {
double Prob = 1.0;
for (int level = 0; level < NKronIters; level++) {
Prob *= At(NId1 % MtxDim, NId2 % MtxDim);
if (ProbTresh > Prob) { return false; }
NId1 /= MtxDim; NId2 /= MtxDim;
}
return true;
}
// deriv a*log(x) = a/x
double TKronMtx::GetEdgeDLL(const int& ParamId, int NId1, int NId2, const int& NKronIters) const {
const int ThetaX = ParamId % GetDim();
const int ThetaY = ParamId / GetDim();
int ThetaCnt = 0;
for (int level = 0; level < NKronIters; level++) {
if ((NId1 % MtxDim) == ThetaX && (NId2 % MtxDim) == ThetaY) {
ThetaCnt++; }
NId1 /= MtxDim; NId2 /= MtxDim;
}
return double(ThetaCnt) / exp(At(ParamId));
}
// deriv log(1-x^a*y^b..) = -x'/(1-x) = (-a*x^(a-1)*y^b..) / (1-x^a*y^b..)
double TKronMtx::GetNoEdgeDLL(const int& ParamId, int NId1, int NId2, const int& NKronIters) const {
const int& ThetaX = ParamId % GetDim();
const int& ThetaY = ParamId / GetDim();
int ThetaCnt = 0;
double DLL = 0, LL = 0;
for (int level = 0; level < NKronIters; level++) {
const int X = NId1 % MtxDim;
const int Y = NId2 % MtxDim;
const double LVal = At(X, Y);
if (X == ThetaX && Y == ThetaY) {
if (ThetaCnt != 0) { DLL += LVal; }
ThetaCnt++;
} else { DLL += LVal; }
LL += LVal;
NId1 /= MtxDim; NId2 /= MtxDim;
}
return -ThetaCnt*exp(DLL) / (1.0 - exp(LL));
}
// 2nd order Taylor approximation log(1-x) ~ -x - 0.5x^2
double TKronMtx::GetApxNoEdgeDLL(const int& ParamId, int NId1, int NId2, const int& NKronIters) const {
const int& ThetaX = ParamId % GetDim();
const int& ThetaY = ParamId / GetDim();
int ThetaCnt = 0;
double DLL = 0;//, LL = 0;
for (int level = 0; level < NKronIters; level++) {
const int X = NId1 % MtxDim;
const int Y = NId2 % MtxDim;
const double LVal = At(X, Y); IAssert(LVal > NInf);
if (X == ThetaX && Y == ThetaY) {
if (ThetaCnt != 0) { DLL += LVal; }
ThetaCnt++;
} else { DLL += LVal; }
//LL += LVal;
NId1 /= MtxDim; NId2 /= MtxDim;
}
//return -ThetaCnt*exp(DLL)*(1.0 + exp(LL)); // -x'/(1+x) WRONG!
// deriv = -(ax^(a-1)*y^b..) - a*x^(2a-1)*y^2b..
// = - (ax^(a-1)*y^b..) - a*x*(x^(a-1)*y^b..)^2
return -ThetaCnt*exp(DLL) - ThetaCnt*exp(At(ThetaX, ThetaY)+2*DLL);
}
uint TKronMtx::GetNodeSig(const double& OneProb) {
uint Sig = 0;
for (int i = 0; i < (int)(8*sizeof(uint)); i++) {
if (TKronMtx::Rnd.GetUniDev() < OneProb) {
Sig |= (1u<<i); }
}
return Sig;
}
double TKronMtx::GetEdgeProb(const uint& NId1Sig, const uint& NId2Sig, const int& NIter) const {
Assert(GetDim() == 2);
double Prob = 1.0;
for (int i = 0; i < NIter; i++) {
const uint Mask = (1u<<i);
const uint Bit1 = NId1Sig & Mask;
const uint Bit2 = NId2Sig & Mask;
Prob *= At(int(Bit1!=0), int(Bit2!=0));
}
return Prob;
}
PNGraph TKronMtx::GenThreshGraph(const double& Thresh) const {
PNGraph Graph = TNGraph::New();
for (int i = 0; i < GetDim(); i++) {
Graph->AddNode(i); }
for (int r = 0; r < GetDim(); r++) {
for (int c = 0; c < GetDim(); c++) {
if (At(r, c) >= Thresh) { Graph->AddEdge(r, c); }
}
}
return Graph;
}
PNGraph TKronMtx::GenRndGraph(const double& RndFact) const {
PNGraph Graph = TNGraph::New();
for (int i = 0; i < GetDim(); i++) {
Graph->AddNode(i); }
for (int r = 0; r < GetDim(); r++) {
for (int c = 0; c < GetDim(); c++) {
if (RndFact * At(r, c) >= TKronMtx::Rnd.GetUniDev()) { Graph->AddEdge(r, c); }
}
}
return Graph;
}
int TKronMtx::GetKronIter(const int& GNodes, const int& SeedMtxSz) {
return (int) ceil(log(double(GNodes)) / log(double(SeedMtxSz)));
}
// slow but exaxt procedure (we flip all O(N^2) edges)
PNGraph TKronMtx::GenKronecker(const TKronMtx& SeedMtx, const int& NIter, const bool& IsDir, const int& Seed) {
const TKronMtx& SeedGraph = SeedMtx;
const int NNodes = SeedGraph.GetNodes(NIter);
printf(" Kronecker: %d nodes, %s...\n", NNodes, IsDir ? "Directed":"UnDirected");
PNGraph Graph = TNGraph::New(NNodes, -1);
TExeTm ExeTm;
TRnd Rnd(Seed);
int edges = 0;
for (int node1 = 0; node1 < NNodes; node1++) {
Graph->AddNode(node1); }
if (IsDir) {
for (int node1 = 0; node1 < NNodes; node1++) {
for (int node2 = 0; node2 < NNodes; node2++) {
if (SeedGraph.IsEdgePlace(node1, node2, NIter, Rnd.GetUniDev())) {
Graph->AddEdge(node1, node2);
edges++;
}
}
if (node1 % 1000 == 0) printf("\r...%dk, %dk", node1/1000, edges/1000);
}
} else {
for (int node1 = 0; node1 < NNodes; node1++) {
for (int node2 = node1; node2 < NNodes; node2++) {
if (SeedGraph.IsEdgePlace(node1, node2, NIter, Rnd.GetUniDev())) {
Graph->AddEdge(node1, node2);
Graph->AddEdge(node2, node1);
edges++;
}
}
if (node1 % 1000 == 0) printf("\r...%dk, %dk", node1/1000, edges/1000);
}
}
printf("\r %d edges [%s]\n", Graph->GetEdges(), ExeTm.GetTmStr());
return Graph;
}
// use RMat like recursive descent to quickly generate a Kronecker graph
PNGraph TKronMtx::GenFastKronecker(const TKronMtx& SeedMtx, const int& NIter, const bool& IsDir, const int& Seed) {
const TKronMtx& SeedGraph = SeedMtx;
const int MtxDim = SeedGraph.GetDim();
const double MtxSum = SeedGraph.GetMtxSum();
const int NNodes = SeedGraph.GetNodes(NIter);
const int NEdges = SeedGraph.GetEdges(NIter);
//const double DiagEdges = NNodes * pow(SeedGraph.At(0,0), double(NIter));
//const int NEdges = (int) TMath::Round(((pow(double(SeedGraph.GetMtxSum()), double(NIter)) - DiagEdges) /2.0));
printf(" FastKronecker: %d nodes, %d edges, %s...\n", NNodes, NEdges, IsDir ? "Directed":"UnDirected");
PNGraph Graph = TNGraph::New(NNodes, -1);
TRnd Rnd(Seed);
TExeTm ExeTm;
// prepare cell probability vector
TVec<TFltIntIntTr> ProbToRCPosV; // row, col position
double CumProb = 0.0;
for (int r = 0; r < MtxDim; r++) {
for (int c = 0; c < MtxDim; c++) {
const double Prob = SeedGraph.At(r, c);
if (Prob > 0.0) {
CumProb += Prob;
ProbToRCPosV.Add(TFltIntIntTr(CumProb/MtxSum, r, c));
}
}
}
// add nodes
for (int i = 0; i < NNodes; i++) {
Graph->AddNode(i); }
// add edges
int Rng, Row, Col, Collision=0, n = 0;
for (int edges = 0; edges < NEdges; ) {
Rng=NNodes; Row=0; Col=0;
for (int iter = 0; iter < NIter; iter++) {
const double& Prob = Rnd.GetUniDev();
n = 0; while(Prob > ProbToRCPosV[n].Val1) { n++; }
const int MtxRow = ProbToRCPosV[n].Val2;
const int MtxCol = ProbToRCPosV[n].Val3;
Rng /= MtxDim;
Row += MtxRow * Rng;
Col += MtxCol * Rng;
}
if (! Graph->IsEdge(Row, Col)) { // allow self-loops
Graph->AddEdge(Row, Col); edges++;
if (! IsDir) {
if (Row != Col) Graph->AddEdge(Col, Row);
edges++;
}
} else { Collision++; }
//if (edges % 1000 == 0) printf("\r...%dk", edges/1000);
}
//printf(" %d edges [%s]\n", Graph->GetEdges(), ExeTm.GetTmStr());
printf(" collisions: %d (%.4f)\n", Collision, Collision/(double)Graph->GetEdges());
return Graph;
}
// use RMat like recursive descent to quickly generate a Kronecker graph
PNGraph TKronMtx::GenFastKronecker(const TKronMtx& SeedMtx, const int& NIter, const int& Edges, const bool& IsDir, const int& Seed) {
const TKronMtx& SeedGraph = SeedMtx;
const int MtxDim = SeedGraph.GetDim();
const double MtxSum = SeedGraph.GetMtxSum();
const int NNodes = SeedGraph.GetNodes(NIter);
const int NEdges = Edges;
//const double DiagEdges = NNodes * pow(SeedGraph.At(0,0), double(NIter));
//const int NEdges = (int) TMath::Round(((pow(double(SeedGraph.GetMtxSum()), double(NIter)) - DiagEdges) /2.0));
printf(" RMat Kronecker: %d nodes, %d edges, %s...\n", NNodes, NEdges, IsDir ? "Directed":"UnDirected");
PNGraph Graph = TNGraph::New(NNodes, -1);
TRnd Rnd(Seed);
TExeTm ExeTm;
// prepare cell probability vector
TVec<TFltIntIntTr> ProbToRCPosV; // row, col position
double CumProb = 0.0;
for (int r = 0; r < MtxDim; r++) {
for (int c = 0; c < MtxDim; c++) {
const double Prob = SeedGraph.At(r, c);
if (Prob > 0.0) {
CumProb += Prob;
ProbToRCPosV.Add(TFltIntIntTr(CumProb/MtxSum, r, c));
}
}
}
// add nodes
for (int i = 0; i < NNodes; i++) {
Graph->AddNode(i); }
// add edges
int Rng, Row, Col, Collision=0, n = 0;
for (int edges = 0; edges < NEdges; ) {
Rng=NNodes; Row=0; Col=0;
for (int iter = 0; iter < NIter; iter++) {
const double& Prob = Rnd.GetUniDev();
n = 0; while(Prob > ProbToRCPosV[n].Val1) { n++; }
const int MtxRow = ProbToRCPosV[n].Val2;
const int MtxCol = ProbToRCPosV[n].Val3;
Rng /= MtxDim;
Row += MtxRow * Rng;
Col += MtxCol * Rng;
}
if (! Graph->IsEdge(Row, Col)) { // allow self-loops
Graph->AddEdge(Row, Col); edges++;
if (! IsDir) {
if (Row != Col) Graph->AddEdge(Col, Row);
edges++;
}
} else { Collision++; }
//if (edges % 1000 == 0) printf("\r...%dk", edges/1000);
}
//printf(" %d edges [%s]\n", Graph->GetEdges(), ExeTm.GetTmStr());
printf(" collisions: %d (%.4f)\n", Collision, Collision/(double)Graph->GetEdges());
return Graph;
}
PNGraph TKronMtx::GenDetKronecker(const TKronMtx& SeedMtx, const int& NIter, const bool& IsDir) {
const TKronMtx& SeedGraph = SeedMtx;
const int NNodes = SeedGraph.GetNodes(NIter);
printf(" Deterministic Kronecker: %d nodes, %s...\n", NNodes, IsDir ? "Directed":"UnDirected");
PNGraph Graph = TNGraph::New(NNodes, -1);
TExeTm ExeTm;
int edges = 0;
for (int node1 = 0; node1 < NNodes; node1++) { Graph->AddNode(node1); }
for (int node1 = 0; node1 < NNodes; node1++) {
for (int node2 = 0; node2 < NNodes; node2++) {
if (SeedGraph.IsEdgePlace(node1, node2, NIter, Rnd.GetUniDev())) {
Graph->AddEdge(node1, node2);
edges++;
}
}
if (node1 % 1000 == 0) printf("\r...%dk, %dk", node1/1000, edges/1000);
}
return Graph;
}
void TKronMtx::PlotCmpGraphs(const TKronMtx& SeedMtx, const PNGraph& Graph, const TStr& FNmPref, const TStr& Desc) {
const int KronIters = SeedMtx.GetKronIter(Graph->GetNodes());
PNGraph KronG, WccG;
const bool FastGen = true;
if (FastGen) { KronG = TKronMtx::GenFastKronecker(SeedMtx, KronIters, true, 0); }
else { KronG = TKronMtx::GenKronecker(SeedMtx, KronIters, true, 0); }
TSnap::DelZeroDegNodes(KronG);
WccG = TSnap::GetMxWcc(KronG);
const TStr Desc1 = TStr::Fmt("%s", Desc.CStr());
TGStatVec GS(tmuNodes, TFSet() | gsdInDeg | gsdOutDeg | gsdWcc | gsdHops | gsdScc | gsdClustCf | gsdSngVec | gsdSngVal);
//gsdHops
//gsWccHops, gsdSngVal, gsdSngVec, gsdClustCf
GS.Add(Graph, TSecTm(1), TStr::Fmt("GRAPH G(%d, %d)", Graph->GetNodes(), Graph->GetEdges()));
GS.Add(KronG, TSecTm(2), TStr::Fmt("KRONECKER K(%d, %d)", KronG->GetNodes(), KronG->GetEdges()));
GS.Add(WccG, TSecTm(3), TStr::Fmt("KRONECKER wccK(%d, %d)", WccG->GetNodes(), WccG->GetEdges()));
const TStr Style = "linewidth 1 pointtype 6 pointsize 1";
GS.ImposeDistr(gsdInDeg, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdInDeg, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdOutDeg, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdOutDeg, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdHops, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdClustCf, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdClustCf, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdSngVal, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdSngVal, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdSngVec, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdSngVec, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdWcc, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdWcc, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdScc, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdScc, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
// typedef enum { distrUndef, distrInDeg, distrOutDeg, distrWcc, distrScc,
// distrHops, distrWccHops, distrSngVal, distrSngVec, distrClustCf, distrMx } TGraphDistr;*/
}
void TKronMtx::PlotCmpGraphs(const TKronMtx& SeedMtx1, const TKronMtx& SeedMtx2, const PNGraph& Graph, const TStr& FNmPref, const TStr& Desc) {
const int KronIters1 = SeedMtx1.GetKronIter(Graph->GetNodes());
const int KronIters2 = SeedMtx2.GetKronIter(Graph->GetNodes());
PNGraph KronG1, KronG2;
const bool FastGen = true;
if (FastGen) {
KronG1 = TKronMtx::GenFastKronecker(SeedMtx1, KronIters1, true, 0);
KronG2 = TKronMtx::GenFastKronecker(SeedMtx2, KronIters2, false, 0); } //!!!
else {
KronG1 = TKronMtx::GenKronecker(SeedMtx1, KronIters1, true, 0);
KronG2 = TKronMtx::GenKronecker(SeedMtx2, KronIters2, true, 0); }
TSnap::DelZeroDegNodes(KronG1);
TSnap::DelZeroDegNodes(KronG2);
const TStr Desc1 = TStr::Fmt("%s", Desc.CStr());
TGStatVec GS(tmuNodes, TFSet() | gsdInDeg | gsdOutDeg | gsdWcc | gsdScc | gsdHops | gsdClustCf | gsdSngVec | gsdSngVal | gsdTriadPart);
//gsdHops
//gsWccHops, gsdSngVal, gsdSngVec, gsdClustCf
GS.Add(Graph, TSecTm(1), TStr::Fmt("GRAPH G(%d, %d)", Graph->GetNodes(), Graph->GetEdges()));
GS.Add(KronG1, TSecTm(2), TStr::Fmt("KRONECKER1 K(%d, %d) %s", KronG1->GetNodes(), KronG1->GetEdges(), SeedMtx1.GetMtxStr().CStr()));
GS.Add(KronG2, TSecTm(3), TStr::Fmt("KRONECKER2 K(%d, %d) %s", KronG2->GetNodes(), KronG2->GetEdges(), SeedMtx2.GetMtxStr().CStr()));
const TStr Style = "linewidth 1 pointtype 6 pointsize 1";
// raw data
GS.ImposeDistr(gsdInDeg, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdOutDeg, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdHops, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdClustCf, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdSngVal, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdSngVec, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdWcc, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdScc, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdTriadPart, FNmPref, Desc1, false, false, gpwLinesPoints, Style);
// smooth
GS.ImposeDistr(gsdInDeg, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdOutDeg, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdClustCf, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdScc, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdWcc, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdSngVec, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdSngVal, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
GS.ImposeDistr(gsdTriadPart, FNmPref+"-B", Desc1, true, false, gpwLinesPoints, Style);
}
void TKronMtx::PlotCmpGraphs(const TVec<TKronMtx>& SeedMtxV, const PNGraph& Graph, const TStr& FNmPref, const TStr& Desc) {
const TStr Desc1 = TStr::Fmt("%s", Desc.CStr());
TGStatVec GS(tmuNodes, TFSet() | gsdInDeg | gsdOutDeg | gsdWcc | gsdScc | gsdHops | gsdClustCf | gsdSngVec | gsdSngVal);
GS.Add(Graph, TSecTm(1), TStr::Fmt("GRAPH G(%d, %d)", Graph->GetNodes(), Graph->GetEdges()));
//gsdHops
//gsWccHops, gsdSngVal, gsdSngVec, gsdClustCf
for (int m = 0; m < SeedMtxV.Len(); m++) {
const int KronIters = SeedMtxV[m].GetKronIter(Graph->GetNodes());
PNGraph KronG1 = TKronMtx::GenFastKronecker(SeedMtxV[m], KronIters, true, 0);
printf("*** K(%d, %d) n0=%d\n", KronG1->GetNodes(), KronG1->GetEdges(), SeedMtxV[m].GetDim());
TSnap::DelZeroDegNodes(KronG1);
printf(" del zero deg K(%d, %d) n0=%d\n", KronG1->GetNodes(), KronG1->GetEdges(), m);
GS.Add(KronG1, TSecTm(m+2), TStr::Fmt("K(%d, %d) n0^k=%d n0=%d", KronG1->GetNodes(), KronG1->GetEdges(), SeedMtxV[m].GetNZeroK(Graph), SeedMtxV[m].GetDim()));
// plot after each Kronecker is done
const TStr Style = "linewidth 1 pointtype 6 pointsize 1";
GS.ImposeDistr(gsdInDeg, FNmPref, Desc1, false, false, gpwLines, Style);
GS.ImposeDistr(gsdInDeg, FNmPref+"-B", Desc1, true, false, gpwLines, Style);
GS.ImposeDistr(gsdOutDeg, FNmPref, Desc1, false, false, gpwLines, Style);
GS.ImposeDistr(gsdOutDeg, FNmPref+"-B", Desc1, true, false, gpwLines, Style);
GS.ImposeDistr(gsdHops, FNmPref, Desc1, false, false, gpwLines, Style);
GS.ImposeDistr(gsdClustCf, FNmPref, Desc1, false, false, gpwLines, Style);
GS.ImposeDistr(gsdClustCf, FNmPref+"-B", Desc1, true, false, gpwLines, Style);
GS.ImposeDistr(gsdSngVal, FNmPref, Desc1, false, false, gpwLines, Style);
GS.ImposeDistr(gsdSngVal, FNmPref+"-B", Desc1, true, false, gpwLines, Style);
GS.ImposeDistr(gsdSngVec, FNmPref, Desc1, false, false, gpwLines, Style);
GS.ImposeDistr(gsdSngVec, FNmPref+"-B", Desc1, true, false, gpwLines, Style);
GS.ImposeDistr(gsdWcc, FNmPref, Desc1, false, false, gpwLines, Style);
GS.ImposeDistr(gsdWcc, FNmPref+"-B", Desc1, true, false, gpwLines, Style);
GS.ImposeDistr(gsdScc, FNmPref, Desc1, false, false, gpwLines, Style);
GS.ImposeDistr(gsdScc, FNmPref+"-B", Desc1, true, false, gpwLines, Style);
}
// typedef enum { distrUndef, distrInDeg, distrOutDeg, distrWcc, distrScc,
// distrHops, distrWccHops, distrSngVal, distrSngVec, distrClustCf, distrMx } TGraphDistr;*/
}
void TKronMtx::KronMul(const TKronMtx& Left, const TKronMtx& Right, TKronMtx& Result) {
const int LDim = Left.GetDim();
const int RDim = Right.GetDim();
Result.GenMtx(LDim * RDim);
for (int r1 = 0; r1 < LDim; r1++) {
for (int c1 = 0; c1 < LDim; c1++) {
const double& Val = Left.At(r1, c1);
for (int r2 = 0; r2 < RDim; r2++) {
for (int c2 = 0; c2 < RDim; c2++) {
Result.At(r1*RDim+r2, c1*RDim+c2) = Val * Right.At(r2, c2);
}
}
}
}
}
void TKronMtx::KronSum(const TKronMtx& Left, const TKronMtx& Right, TKronMtx& Result) {
const int LDim = Left.GetDim();
const int RDim = Right.GetDim();
Result.GenMtx(LDim * RDim);
for (int r1 = 0; r1 < LDim; r1++) {
for (int c1 = 0; c1 < LDim; c1++) {
const double& Val = Left.At(r1, c1);
for (int r2 = 0; r2 < RDim; r2++) {
for (int c2 = 0; c2 < RDim; c2++) {
if (Val == NInf || Right.At(r2, c2) == NInf) {
Result.At(r1*RDim+r2, c1*RDim+c2) = NInf; }
else {
Result.At(r1*RDim+r2, c1*RDim+c2) = Val + Right.At(r2, c2); }
}
}
}
}
}
void TKronMtx::KronPwr(const TKronMtx& KronMtx, const int& NIter, TKronMtx& OutMtx) {
OutMtx = KronMtx;
TKronMtx NewOutMtx;
for (int iter = 0; iter < NIter; iter++) {
KronMul(OutMtx, KronMtx, NewOutMtx);
NewOutMtx.Swap(OutMtx);
}
}
void TKronMtx::Dump(const TStr& MtxNm, const bool& Sort) const {
/*printf("%s: %d x %d\n", MtxNm.Empty()?"Mtx":MtxNm.CStr(), GetDim(), GetDim());
for (int r = 0; r < GetDim(); r++) {
for (int c = 0; c < GetDim(); c++) { printf(" %8.2g", At(r, c)); }
printf("\n");
}*/
if (! MtxNm.Empty()) printf("%s\n", MtxNm.CStr());
double Sum=0.0;
TFltV ValV = SeedMtx;
if (Sort) { ValV.Sort(false); }
for (int i = 0; i < ValV.Len(); i++) {
printf(" %10.4g", ValV[i]());
Sum += ValV[i];
if ((i+1) % GetDim() == 0) { printf("\n"); }
}
printf(" (sum:%.4f)\n", Sum);
}
// average difference in the parameters
double TKronMtx::GetAvgAbsErr(const TKronMtx& Kron1, const TKronMtx& Kron2) {
TFltV P1 = Kron1.GetMtx();
TFltV P2 = Kron2.GetMtx();
IAssert(P1.Len() == P2.Len());
P1.Sort(); P2.Sort();
double delta = 0.0;
for (int i = 0; i < P1.Len(); i++) {
delta += fabs(P1[i] - P2[i]);
}
return delta/P1.Len();
}
// average L2 difference in the parameters
double TKronMtx::GetAvgFroErr(const TKronMtx& Kron1, const TKronMtx& Kron2) {
TFltV P1 = Kron1.GetMtx();
TFltV P2 = Kron2.GetMtx();
IAssert(P1.Len() == P2.Len());
P1.Sort(); P2.Sort();
double delta = 0.0;
for (int i = 0; i < P1.Len(); i++) {
delta += pow(P1[i] - P2[i], 2);
}
return sqrt(delta/P1.Len());
}
// get matrix from matlab matrix notation
TKronMtx TKronMtx::GetMtx(TStr MatlabMtxStr) {
TStrV RowStrV, ColStrV;
MatlabMtxStr.ChangeChAll(',', ' ');
MatlabMtxStr.SplitOnAllCh(';', RowStrV); IAssert(! RowStrV.Empty());
RowStrV[0].SplitOnWs(ColStrV); IAssert(! ColStrV.Empty());
const int Rows = RowStrV.Len();
const int Cols = ColStrV.Len();
IAssert(Rows == Cols);
TKronMtx Mtx(Rows);
for (int r = 0; r < Rows; r++) {
RowStrV[r].SplitOnWs(ColStrV);
IAssert(ColStrV.Len() == Cols);
for (int c = 0; c < Cols; c++) {
Mtx.At(r, c) = (double) ColStrV[c].GetFlt(); }
}
return Mtx;
}
TKronMtx TKronMtx::GetRndMtx(const int& Dim, const double& MinProb) {
TKronMtx Mtx;
Mtx.SetRndMtx(Dim, MinProb);
return Mtx;
}
TKronMtx TKronMtx::GetInitMtx(const int& Dim, const int& Nodes, const int& Edges) {
const double MxParam = 0.8+TKronMtx::Rnd.GetUniDev()/5.0;
const double MnParam = 0.2-TKronMtx::Rnd.GetUniDev()/5.0;
const double Step = (MxParam-MnParam) / (Dim*Dim-1);
TFltV ParamV(Dim*Dim);
if (Dim == 1) { ParamV.PutAll(0.5); } // random graph
else {
for (int p = 0; p < ParamV.Len(); p++) {
ParamV[p] = MxParam - p*Step; }
}
//IAssert(ParamV[0]==MxParam && ParamV.Last()==MnParam);
TKronMtx Mtx(ParamV);
Mtx.SetForEdges(Nodes, Edges);
return Mtx;
}
TKronMtx TKronMtx::GetInitMtx(const TStr& MtxStr, const int& Dim, const int& Nodes, const int& Edges) {
TKronMtx Mtx(Dim);
if (TCh::IsNum(MtxStr[0])) { Mtx = TKronMtx::GetMtx(MtxStr); }
else if (MtxStr[0] == 'r') { Mtx = TKronMtx::GetRndMtx(Dim, 0.1); }
else if (MtxStr[0] == 'a') {
const double Prob = TKronMtx::Rnd.GetUniDev();
if (Prob < 0.4) {
Mtx = TKronMtx::GetInitMtx(Dim, Nodes, Edges); }
else { // interpolate so that there are in the corners 0.9, 0.5, 0.1, 0.5
const double Max = 0.9+TKronMtx::Rnd.GetUniDev()/10.0;
const double Min = 0.1-TKronMtx::Rnd.GetUniDev()/10.0;
const double Med = (Max-Min)/2.0;
Mtx.At(0,0) = Max; Mtx.At(0,Dim-1) = Med;
Mtx.At(Dim-1, 0) = Med; Mtx.At(Dim-1, Dim-1) = Min;
for (int i = 1; i < Dim-1; i++) {
Mtx.At(i,i) = Max - double(i)*(Max-Min)/double(Dim-1);
Mtx.At(i, 0) = Mtx.At(0, i) = Max - double(i)*(Max-Med)/double(Dim-1);
Mtx.At(i, Dim-1) = Mtx.At(Dim-1, i) = Med - double(i)*(Med-Min)/double(Dim-1);
}
for (int i = 1; i < Dim-1; i++) {
for (int j = 1; j < Dim-1; j++) {
if (i >= j) { continue; }
Mtx.At(i,j) = Mtx.At(j,i) = Mtx.At(i,i) - (j-i)*(Mtx.At(i,i)-Mtx.At(i,Dim-1))/(Dim-i-1);
}
}
Mtx.AddRndNoise(0.1);
}
} else { FailR("Wrong mtx: matlab str, or random (r), or all (a)"); }
Mtx.SetForEdges(Nodes, Edges);
return Mtx;
}
TKronMtx TKronMtx::GetMtxFromNm(const TStr& MtxNm) {
if (MtxNm == "3chain") return TKronMtx::GetMtx("1 1 0; 1 1 1; 0 1 1");
else if (MtxNm == "4star") return TKronMtx::GetMtx("1 1 1 1; 1 1 0 0 ; 1 0 1 0; 1 0 0 1");
else if (MtxNm == "4chain") return TKronMtx::GetMtx("1 1 0 0; 1 1 1 0 ; 0 1 1 1; 0 0 1 1");
else if (MtxNm == "4square") return TKronMtx::GetMtx("1 1 0 1; 1 1 1 0 ; 0 1 1 1; 1 0 1 1");
else if (MtxNm == "5star") return TKronMtx::GetMtx("1 1 1 1 1; 1 1 0 0 0; 1 0 1 0 0; 1 0 0 1 0; 1 0 0 0 1");
else if (MtxNm == "6star") return TKronMtx::GetMtx("1 1 1 1 1 1; 1 1 0 0 0 0; 1 0 1 0 0 0; 1 0 0 1 0 0; 1 0 0 0 1 0; 1 0 0 0 0 1");
else if (MtxNm == "7star") return TKronMtx::GetMtx("1 1 1 1 1 1 1; 1 1 0 0 0 0 0; 1 0 1 0 0 0 0; 1 0 0 1 0 0 0; 1 0 0 0 1 0 0; 1 0 0 0 0 1 0; 1 0 0 0 0 0 1");
else if (MtxNm == "5burst") return TKronMtx::GetMtx("1 1 1 1 0; 1 1 0 0 0; 1 0 1 0 0; 1 0 0 1 1; 0 0 0 1 1");
else if (MtxNm == "7burst") return TKronMtx::GetMtx("1 0 0 1 0 0 0; 0 1 0 1 0 0 0; 0 0 1 1 0 0 0; 1 1 1 1 1 0 0; 0 0 0 1 1 1 1; 0 0 0 0 1 1 0; 0 0 0 0 1 0 1");
else if (MtxNm == "7cross") return TKronMtx::GetMtx("1 0 0 1 0 0 0; 0 1 0 1 0 0 0; 0 0 1 1 0 0 0; 1 1 1 1 1 0 0; 0 0 0 1 1 1 0; 0 0 0 0 1 1 1; 0 0 0 0 0 1 1");
FailR(TStr::Fmt("Unknow matrix: '%s'", MtxNm.CStr()).CStr());
return TKronMtx();
}
TKronMtx TKronMtx::LoadTxt(const TStr& MtxFNm) {
PSs Ss = TSs::LoadTxt(ssfTabSep, MtxFNm);
IAssertR(Ss->GetXLen() == Ss->GetYLen(), "Not a square matrix");
IAssert(Ss->GetYLen() == Ss->GetXLen());
TKronMtx Mtx(Ss->GetYLen());
for (int r = 0; r < Ss->GetYLen(); r++) {
for (int c = 0; c < Ss->GetXLen(); c++) {
Mtx.At(r, c) = (double) Ss->At(c, r).GetFlt(); }
}
return Mtx;
}
/////////////////////////////////////////////////
// Kronecker Log Likelihood
TKroneckerLL::TKroneckerLL(const PNGraph& GraphPt, const TFltV& ParamV, const double& PermPSwapNd): PermSwapNodeProb(PermPSwapNd) {
InitLL(GraphPt, TKronMtx(ParamV));
}
TKroneckerLL::TKroneckerLL(const PNGraph& GraphPt, const TKronMtx& ParamMtx, const double& PermPSwapNd) : PermSwapNodeProb(PermPSwapNd) {
InitLL(GraphPt, ParamMtx);
}
TKroneckerLL::TKroneckerLL(const PNGraph& GraphPt, const TKronMtx& ParamMtx, const TIntV& NodeIdPermV, const double& PermPSwapNd) : PermSwapNodeProb(PermPSwapNd) {
InitLL(GraphPt, ParamMtx);
NodePerm = NodeIdPermV;
SetIPerm(NodePerm);
}
PKroneckerLL TKroneckerLL::New(const PNGraph& GraphPt, const TKronMtx& ParamMtx, const double& PermPSwapNd) {
return new TKroneckerLL(GraphPt, ParamMtx, PermPSwapNd);
}
PKroneckerLL TKroneckerLL::New(const PNGraph& GraphPt, const TKronMtx& ParamMtx, const TIntV& NodeIdPermV, const double& PermPSwapNd) {
return new TKroneckerLL(GraphPt, ParamMtx, NodeIdPermV, PermPSwapNd);
}
void TKroneckerLL::SetPerm(const char& PermId) {
if (PermId == 'o') { SetOrderPerm(); }
else if (PermId == 'd') { SetDegPerm(); }
else if (PermId == 'r') { SetRndPerm(); }
else if (PermId == 'b') { SetBestDegPerm(); }
else FailR("Unknown permutation type (o,d,r)");
}
void TKroneckerLL::SetOrderPerm() {
NodePerm.Gen(Nodes, 0);
for (int i = 0; i < Graph->GetNodes(); i++) {
NodePerm.Add(i); }
SetIPerm(NodePerm);
}
void TKroneckerLL::SetRndPerm() {
NodePerm.Gen(Nodes, 0);
for (int i = 0; i < Graph->GetNodes(); i++) {
NodePerm.Add(i); }
NodePerm.Shuffle(TKronMtx::Rnd);
SetIPerm(NodePerm);
}
void TKroneckerLL::SetDegPerm() {
TIntPrV DegNIdV;
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
DegNIdV.Add(TIntPr(NI.GetDeg(), NI.GetId()));
}
DegNIdV.Sort(false);
NodePerm.Gen(DegNIdV.Len(), 0);
for (int i = 0; i < DegNIdV.Len(); i++) {
NodePerm.Add(DegNIdV[i].Val2);
}
SetIPerm(NodePerm);
}
/// !!!!! MYUNGHWAN, CHECK!
void TKroneckerLL::SetBestDegPerm() {
NodePerm.Gen(Nodes);
const int NZero = ProbMtx.GetDim();
TFltIntPrV DegV(Nodes), CDegV(Nodes);
TFltV Row(NZero);
TFltV Col(NZero);
for(int i = 0; i < NZero; i++) {
for(int j = 0; j < NZero; j++) {
Row[i] += ProbMtx.At(i, j);
Col[i] += ProbMtx.At(j, i);
}
}
for(int i = 0; i < Nodes; i++) {
TNGraph::TNodeI NodeI = Graph->GetNI(i);
int NId = i;
double RowP = 1.0, ColP = 1.0;
for(int j = 0; j < KronIters; j++) {
int Bit = NId % NZero;
RowP *= Row[Bit]; ColP *= Col[Bit];
NId /= NZero;
}
CDegV[i] = TFltIntPr(RowP + ColP, i);
DegV[i] = TFltIntPr(NodeI.GetDeg(), i);
}
DegV.Sort(false); CDegV.Sort(false);
for(int i = 0; i < Nodes; i++) {
NodePerm[DegV[i].Val2] = CDegV[i].Val2;
}
SetIPerm(NodePerm);
}
/// !!!!! MYUNGHWAN, CHECK!
void TKroneckerLL::SetIPerm(const TIntV& Perm) {
InvertPerm.Gen(Perm.Len());
for (int i = 0; i < Perm.Len(); i++) {
InvertPerm[Perm[i]] = i;
}
}
void TKroneckerLL::SetGraph(const PNGraph& GraphPt) {
Graph = GraphPt;
bool NodesOk = true;
// check that nodes IDs are {0,1,..,Nodes-1}
for (int nid = 0; nid < Graph->GetNodes(); nid++) {
if (! Graph->IsNode(nid)) { NodesOk=false; break; } }
if (! NodesOk) {
TIntV NIdV; GraphPt->GetNIdV(NIdV);
Graph = TSnap::GetSubGraph(GraphPt, NIdV, true);
for (int nid = 0; nid < Graph->GetNodes(); nid++) {
IAssert(Graph->IsNode(nid)); }
}
Nodes = Graph->GetNodes();
IAssert(LLMtx.GetDim() > 1 && LLMtx.Len() == ProbMtx.Len());
KronIters = (int) ceil(log(double(Nodes)) / log(double(ProbMtx.GetDim())));
// edge vector (for swap-edge permutation proposal)
// if (PermSwapNodeProb < 1.0) { /// !!!!! MYUNGHWAN, CHECK! WHY IS THIS COMMENTED OUT
GEdgeV.Gen(Graph->GetEdges(), 0);
for (TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
if (EI.GetSrcNId() != EI.GetDstNId()) {
GEdgeV.Add(TIntTr(EI.GetSrcNId(), EI.GetDstNId(), -1));
}
}
// }
RealNodes = Nodes;
RealEdges = Graph->GetEdges();
LEdgeV = TIntTrV();
LSelfEdge = 0;
}
void TKroneckerLL::AppendIsoNodes() {
Nodes = (int) pow((double)ProbMtx.GetDim(), KronIters);
// add nodes until filling the Kronecker graph model
for (int nid = Graph->GetNodes(); nid < Nodes; nid++) {
Graph->AddNode(nid);
}
}
/// !!!!! MYUNGHWAN, CHECK!
void TKroneckerLL::RestoreGraph(const bool RestoreNodes) {
// remove from Graph
int NId1, NId2;
for (int e = 0; e < LEdgeV.Len(); e++) {
NId1 = LEdgeV[e].Val1; NId2 = LEdgeV[e].Val2;
Graph->DelEdge(NId1, NId2);
// GEdgeV.DelIfIn(LEdgeV[e]);
}
if(LEdgeV.Len() - LSelfEdge)
GEdgeV.Del(GEdgeV.Len() - LEdgeV.Len() + LSelfEdge, GEdgeV.Len() - 1);
LEdgeV.Clr();
LSelfEdge = 0;
if(RestoreNodes) {
for(int i = Graph->GetNodes()-1; i >= RealNodes; i--) {
Graph->DelNode(i);
}
}
}
double TKroneckerLL::GetFullGraphLL() const {
// the number of times a seed matrix element appears in
// the full kronecker adjacency matrix after KronIter
// kronecker multiplications
double ElemCnt = 1;
const double dim = LLMtx.GetDim();
// count number of times x appears in the full kronecker matrix
for (int i = 1; i < KronIters; i++) {
ElemCnt = dim*dim*ElemCnt + TMath::Power(dim, 2*i);
}
return ElemCnt * LLMtx.GetMtxSum();
}