11 for (
int i = 0; i <
ResH.
Len(); i++) {
ResH[i]=0.0; }
24 const double PushVal =
ResH.
GetDat(NId) - 0.5*Eps*NIdDeg;
25 const double PutVal = (1.0-
Alpha) * PushVal /
double(NIdDeg);
28 for (
int e = 0; e < NIdDeg; e++) {
34 if (ResVal >= Eps*DstDeg && OldRes < Eps*DstDeg) {
38 if (iter %
Mega(1) == 0) {
41 printf(
"Too many iterations! Stop to save time.\n");
58 int Vol = TopNIdDeg, Cut = TopNIdDeg;
59 double Phi = Cut/double(Vol);
61 for (
int i = 1; i <
ProbH.
Len(); i++) {
66 for (
int e = 0; e < OutDeg; e++) {
68 if ( Rank > -1 && Rank < i) { CutSz -= 2; }
70 Vol += OutDeg; Cut += CutSz;
73 else { Phi = Cut / double(Vol); }
77 IAssert((Phi+1e-6) >=
double(1.0)/
double(i*(i+1)+1));
87 for (
int i = 0; i <
ProbH.
Len(); i++) {
93 for (
int i = 0; i <
PhiV.
Len(); i++) {
94 const double Phi =
PhiV[i];
96 if (Phi < MaxPhi) { MaxPhi = Phi;
BestCutIdx = i; }
102 for (
int i = 0; i <
VolV.
Len(); i++) {
104 if (Desc.
Empty()) { Desc = OutFNm; }
115 for (
int i = 0; i <
CutV.
Len(); i++) {
117 if (Desc.
Empty()) { Desc = OutFNm; }
128 for (
int i = 0; i <
PhiV.
Len(); i++) {
130 if (Desc.
Empty()) { Desc = OutFNm; }
134 GP.
SetXYLabel(
"Rank",
"Conductance (Cut size / Volume)");
140 FILE *F = fopen(
TStr::Fmt(
"%s.net", OutFNm.
CStr()).CStr(),
"wt");
144 TStrV ClrV =
TStrV::GetV(
"Black",
"Gray80",
"Gray60",
"Gray40",
"Gray20",
"RedViolet");
146 ClustSet.AddDat(
NIdV[a], (a+1)/BucketSz);
151 const int NId = NI.GetId();
153 fprintf(F,
"%d \"%d\" diamond x_fact 2 y_fact 2 ic Green fos 10\n", i+1, NId); }
154 else if (ClustSet.IsKey(NId)) {
156 fprintf(F,
"%d \"%d\" box x_fact 2 y_fact 2 ic %s fos 10\n", i+1, NId, ClrV[ClustSet.GetDat(NId)].CStr()); }
158 fprintf(F,
"%d \"%d\" ellipse x_fact 2 y_fact 2 ic Blue fos 10\n", i+1, NId); }
159 NIdToIdH.AddDat(NId, i+1);
164 const int NId = NIdToIdH.GetDat(NI.GetId());
165 for (
int e = 0; e < NI.GetOutDeg(); e++) {
166 fprintf(F,
"%d %d %g c Tan\n", NId, NIdToIdH.GetDat(NI.GetOutNId(e)).Val, 1.0);
177 for (
int i = 0; i < CnComV.
Len(); i++) { SzCntH.
AddDat(CnComV[i].
Len()) += 1; }
179 Graph->
GetEdges()),
"Whisker size (Maximal component connected with a bridge edge)",
"Count",
gpsLog10XY,
false); }
182 for (
int c = 0; c <
TMath::Mn(CnComV.
Len(), PlotN); c++) {
185 if (NI.GetOutDeg() != Graph->
GetNI(NI.GetId()).GetOutDeg()) { BrNodeId=NI.GetId();
break; } }
194 for (
int i = 0; i < NIdV.
Len(); i++) { NIdSet.
AddKey(NIdV[i]); }
195 GetCutStat(Graph, NIdSet, Vol, Cut, Phi, GraphEdges);
199 const int Edges2 = GraphEdges>=0 ? 2*GraphEdges : Graph->
GetEdges();
200 Vol=0; Cut=0; Phi=0.0;
201 for (
int i = 0; i < NIdSet.
Len(); i++) {
202 if (! Graph->
IsNode(NIdSet[i])) {
continue; }
204 for (
int e = 0; e < NI.
GetOutDeg(); e++) {
211 if (2*Vol > Edges2) { Phi = Cut / double(Edges2-Vol); }
212 else if (Vol == 0) { Phi = 0.0; }
213 else { Phi = Cut / double(Vol); }
215 if (Vol == Edges2) { Phi = 1.0; }
219 void TLocClust::PlotNCP(
const PUNGraph& Graph,
const TStr& FNm,
const TStr Desc,
const bool& BagOfWhiskers,
const bool& RewireNet,
const int& KMin,
const int& KMax,
const int& Coverage,
const bool& SaveTxtStat,
const bool& PlotBoltzman) {
220 const double Alpha = 0.001, KFac = 1.5, SizeFrac = 0.001;
222 TLocClustStat ClusStat1(Alpha, KMin, KMax, KFac, Coverage, SizeFrac);
223 ClusStat1.
Run(Graph,
false, PlotBoltzman, SaveTxtStat);
225 TLocClustStat ClusStat2(Alpha, KMin, KMax, KFac, Coverage, SizeFrac);
226 ClusStat1.
ImposeNCP(ClusStat2, FNm, Desc,
"ORIGINAL",
"REWIRED");
236 ClusStat1.
ImposeNCP(ClusStat2, FNm, Desc,
"ORIGINAL",
"REWIRED");
248 MxFrac=1; AvgFrac=1; MedianFrac=1; Pct9Frac=1; Flake=1;
259 for (
int i = 0; i < NI.
GetDeg(); i++) {
260 if (! InNIdSet.IsKey(NI.
GetNbrNId(i))) { EdgesOut++; }
262 const double FracOut = EdgesOut/double(NI.
GetDeg());
263 if (FracOut <= 0.5) { NHalfIn++; }
264 FracDegMom.
Add(FracOut);
267 MxFrac = FracDegMom.
GetMx();
268 AvgFrac = FracDegMom.
GetMean();
271 Flake = 1.0 - double(NHalfIn)/double(
CutNIdV.
Len());
276 const int& _Coverage,
const double& _SizeFrac) :
Alpha(_Alpha),
SizeFrac(_SizeFrac),
KFac(_KFac),
304 printf(
"\nLocal clustering: Graph(%d, %d)\n", Nodes, Edges);
305 printf(
" Alpha: %g\n",
Alpha());
307 printf(
" Coverage: %d\n",
Coverage());
308 printf(
" SizeFrac: %g\n\n",
SizeFrac());
317 if (SaveBestNodesAtK) {
319 double PrevBPos = 1, BPos = 1;
320 while (BPos <=
Mega(100)) {
321 PrevBPos = (
uint) floor(BPos);
323 if (floor(BPos) == PrevBPos) { BPos = PrevBPos + 1; }
327 for (
int K =
KMin, cnt=1; K <
KMax; K = int(
KFac *
double(K))+1, cnt++) {
328 if (K == prevK) { K++; } prevK = K;
330 printf(
"%d] K: %d, %d runs\n", cnt+1, K, Runs);
331 if (NextDone) {
break; }
335 double MeanSz=0.0, MeanVol=0.0, Count=0.0;
336 for (
int run = 0; run < Runs; run++) {
342 if (Sz == 0 || Vol == 0 || Phi == 0) {
continue; }
343 MeanSz+=Sz; MeanVol+=Vol; Count+= 1;
347 for (
int s = 0; s < Clust.
Len(); s++) {
348 const int size = s+1;
349 const int cut = Clust.
GetCut(s);
350 const int edges = (Clust.
GetVol(s)-cut)/2;
351 const double phi = Clust.
GetPhi(s);
352 if (( Clust.
GetPhi(s) != double(cut)/double(2*edges+cut))) {
continue; }
354 IAssert(phi ==
double(cut)/
double(2*edges+cut));
355 IAssert(phi >= 1.0/
double((1+s)*s+1));
361 if (BestPhi >= phi) {
372 if (SaveBestNodesAtK) {
379 if (SAtBestPhi != -1) {
380 const int size = SAtBestPhi+1;
386 printf(
"\r %d / %d \r", run, Runs); }
390 printf(
"\r %d / %d: %s \n", Runs, Runs, ExeTm.
GetStr());
392 MeanSz/=Count; MeanVol/=Count;
393 printf(
" Graph(%d, %d) ", Nodes, Edges);
401 printf(
"DONE. Graph(%d, %d): Total run time: %s\n\n", Nodes, Edges, TotTm.
GetStr());
411 int Vol, Cut;
double Phi;
423 const int K = NIdV.
Len();
424 TCutInfo CutInfo(Graph, NIdV,
true);
430 if (AddBestCond && CurCut.
GetPhi() > CutInfo.
GetPhi()) { CurCut=CutInfo; }
443 for (
int n = 0; n <
BestCutH.Len(); n++) {
444 if (
BestCutH[n].GetPhi() <= bestPhi) {
445 bestPhi =
BestCutH[n].GetPhi(); CutId = n; }
474 for (
int c = 0; c <
SweepsV.Len(); c++) {
476 if (Sweep.
Len() < Nodes) {
continue; }
477 if (Sweep.
Phi(Nodes-1) > bestPhi) {
continue; }
479 for (
int i = 0; i < Nodes; i++) {
480 if (TabuNIdSet.
IsKey(Sweep.
NId(i))) { Tabu=
true;
break; }
482 if (Tabu) {
continue; }
483 bestPhi = Sweep.
Phi(Nodes-1);
492 MeanV.
Clr(
false); MedV.
Clr(
false); Dec1V.
Clr(
false); MinV.
Clr(
false); MaxV.
Clr(
false);
495 for (
int i = 0; i < KvH.
Len(); i++) {
497 const TFltV& YVec = KvH[i];
499 for (
int j = 0; j < YVec.
Len(); j++) { Mom.
Add(YVec[j]); }
521 for (
int i = 0; i <
BestCutH.Len(); i++) {
533 for (
int t = 0; t < TempV.
Len(); t++) {
534 const double T = TempV[t];
538 double V = 0.0, SumW = 0.0;
539 for (
int j = 0; j < PhiV.
Len(); j++) {
540 V += PhiV[j] * exp(-PhiV[j]/T);
541 SumW += exp(-PhiV[j]/T);
559 return TStr::Fmt(
"A=%g, K=%d-%g-%s, Cvr=%d, SzFrc=%g G(%d, %d)",
Alpha(),
KMin(),
KFac(),
TInt::GetMegaStr(
KMax()).CStr(),
Coverage(),
SizeFrac(),
565 if (Desc.
Empty()) { Desc = OutFNm; }
566 TFltPrV MeanV, MedV, Dec1V, MinV, MaxV;
573 if (! MinV.
Empty()) {
583 GP.
SetXYLabel(
"k (number of nodes in the cluster)",
"{/Symbol \\F} (conductance)");
590 if (Desc.
Empty()) { Desc = OutFNm; }
593 for (
int i = 0; i <
BestCutH.Len(); i++) {
598 if (! MinV.
Empty()) {
601 GP.
SetXYLabel(
"k (number of nodes in the cluster)",
"Q (modularity)");
608 if (Desc.
Empty()) { Desc = OutFNm; }
611 int PrevBPos=1, CurBPos=1, CutId;
617 CurBPos = (int) floor(BinPos);
618 if (CurBPos == PrevBPos) { CurBPos=PrevBPos+1; BinPos=CurBPos; }
619 const int Nodes = CurBPos;
622 for (
int t = 0; t < TakeMinN; t++) {
623 const double Phi =
FindBestCut(Nodes, TabuNIdSet, CutId);
624 if (CutId == -1) {
break; }
626 for (
int n = 0; n < Nodes; n++) {
627 TabuNIdSet.AddKey(
SweepsV[CutId].NId(n)); }
630 if (! AddSome) {
break; }
633 for (
int i = 0; i < Curves.
Len(); i++) {
637 GP.
SetXYLabel(
"k (number of nodes in the cluster)",
"{/Symbol \\F} (conductance)");
645 TFltPrV PhiInV, PhiBoundV, PhiRatV;
646 FILE *F = fopen(
TStr::Fmt(
"phiInOut.%s-all.tab", OutFNm.
CStr()).CStr(),
"wt");
647 fprintf(F,
"#Nodes\tEdges\tVol\tCut\tPhi\tInNodes\tInEdges\tInVol\tInCut\tInPhi\n");
649 const double IdxFact = 1.05;
650 int curIdx=1, prevIdx=1;
655 ClustStat2.
Run(ClustG);
660 fprintf(F,
"%d\t%d\t%d\t%d\t%f\t%d\t%d\t%d\t%d\t%f\n", CutInfo.
GetNodes(), CutInfo.
GetEdges(), CutInfo.
GetVol(),
666 if (prevIdx == curIdx) { curIdx++; }
674 GP.
SetXYLabel(
"k (number of nodes in the cluster)",
"{/Symbol \\F} (conductance)");
683 GP.
SetXYLabel(
"Nodes",
"Conductance ratio (inside/boundary) -- higher better");
690 if (Desc.
Empty()) { Desc = OutFNm; }
692 TFltPrV CutV(len, 0), EdgesV(len, 0), PhiV(len,0);
693 for (
int i = 0; i <
BestCutH.Len(); i++) {
694 const double size =
BestCutH.GetKey(i).Val;
707 GP.
AddCmd(
"set logscale xyy2 10");
708 GP.
AddCmd(
"set y2label \"Conductance\"");
710 system(
TStr(
TStr(
"replace_all.py cutEdges.")+OutFNm+
".plt \"title \\\"Conductance\" \"axis x1y2 title \\\"Conductance\"").CStr());
716 if (Desc.
Empty()) { Desc = OutFNm; }
722 for (
int p = 0; p < PhiV.
Len(); p++) {
729 GP.
SetXYLabel(
"k (number of nodes in the cluster)",
"{/Symbol \\F} (conductance)");
739 for (
int i = 0; i < PhiV.Len(); i++) {
741 PhiCntH.
AddDat(Buck) += 1;
748 TFltPrV MeanV1, MedV1, Dec1V1, MinV1, MaxV1;
752 const TFltV TempV =
TFltV::GetV(0.001, 0.005, 0.01, 0.02, 0.05, 0.1, 0.5, 1);
759 for (
int t = 0; t < TempV.
Len(); t++) {
763 GP.
SetXYLabel(
"k (number of nodes in the cluster)",
"{/Symbol \\F} (conductance)");
772 if (K>2 && (pow(10.0, (
int)log10((
double)K))==K || (K >=10 && K<=100 && K%10==0) || (K >=100 && K<=1000 && K%100==0))) {
774 for (
int p = 0; p < PhiV.
Len(); p++) {
779 TStr::Fmt(
"Number of pieces of such conductance, K=%d, NPieces=%d)", K, PhiV.
Len()));
783 "k (cluster size)",
"c(k) (number of extracted clusters)",
gpsLog);
787 if (Desc.
Empty()) { Desc = OutFNm; }
788 TFltPrV MeanV1, MedV1, Dec1V1, MinV1, MaxV1;
789 TFltPrV MeanV2, MedV2, Dec1V2, MinV2, MaxV2;
791 LcStat2.
GetCurveStat(MeanV2, MedV2, Dec1V2, MinV2, MaxV2);
805 GP.
SetXYLabel(
"k (number of nodes in the cluster)",
"{/Symbol \\F} (conductance)");
811 if (Desc.
Empty()) { Desc = OutFNm; }
812 TFltPrV MeanV1, MedV1, Dec1V1, MinV1, MaxV1;
813 TFltPrV MeanV2, MedV2, Dec1V2, MinV2, MaxV2;
814 TFltPrV MeanV3, MedV3, Dec1V3, MinV3, MaxV3;
816 LcStat2.
GetCurveStat(MeanV2, MedV2, Dec1V2, MinV2, MaxV2);
817 LcStat3.
GetCurveStat(MeanV3, MedV3, Dec1V3, MinV3, MaxV3);
836 GP.
SetXYLabel(
"k (number of nodes in the cluster)",
"{/Symbol \\F} (conductance)");
842 printf(
"Save text info...");
847 double MxFrac=0, AvgFrac=0, MedianFrac=0, Pct9Frac=0, Flake=0;
857 ColV[8].
Add(MxFrac); ColV[9].
Add(AvgFrac);
858 ColV[10].
Add(MedianFrac); ColV[11].
Add(Pct9Frac); ColV[12].
Add(Flake);
865 for (
int c = 1; c < ColV.
Len(); c++) {
867 for (
int r = 0; r < ColV[c].
Len(); r++) { MaxVal =
TMath::Mx(MaxVal, ColV[c][r]()); }
868 for (
int r = 0; r < ColV[c].
Len(); r++) { ColV[c][r] /= MaxVal; }
873 FILE *F = fopen(DatFNm.
CStr(),
"wt");
875 fprintf(F,
"#N_inside\tE_inside\tE_across\tConductance\tExpansion\tIntDensity\tCutRatio\tNormCut\tMx_FracDegOut\tAvg_FDO\tMedian_FDO\t90Pct_FDO\tFlake_FDO\tVolume\tExpVolume\tModularity\tModRatio\n");
876 for (
int r = 0; r < ColV[0].
Len(); r++) {
877 fprintf(F,
"%g", ColV[0][r]());
878 for (
int c = 1; c < ColV.
Len(); c++) {
879 fprintf(F,
"\t%g", ColV[c][r]()); }
883 printf(
"[%s]\n", ExeTm.
GetStr());
886 GP.AddPlot(DatFNm, 1, 4,
gpwLines,
"Conductance",
"lw 2");
887 GP.AddPlot(DatFNm, 1, 5,
gpwPoints,
"Expansion",
"pt 3");
888 GP.AddPlot(DatFNm, 1, 6,
gpwPoints,
"Internal Density",
"pt 5 ps 0.8");
889 GP.AddPlot(DatFNm, 1, 7,
gpwPoints,
"Cut Ratio",
"pt 6");
890 GP.AddPlot(DatFNm, 1, 8,
gpwPoints,
"Normalized Cut",
"pt 7");
891 GP.AddPlot(DatFNm, 1, 9,
gpwPoints,
"Maximum FDO",
"pt 9");
892 GP.AddPlot(DatFNm, 1, 10,
gpwPoints,
"Avg FDO",
"pt 11");
893 GP.AddPlot(DatFNm, 1, 13,
gpwPoints,
"Flake FDO",
"pt 13");
895 GP.SetXYLabel(
"k (number of nodes in the cluster)",
"Normalized community score (lower is better)");
906 if (Cn1ComV.
Empty()) { printf(
"** No bridges\n"); SizePhiV.
Clr();
return; }
908 printf(
" 1-connected components: %d\n", Cn1ComV.
Len());
910 for (
int c = 0; c < Cn1ComV.
Len(); c++) {
911 const TIntV& NIdV = Cn1ComV[c].NIdV;
912 const int sz = NIdV.
Len();
913 if (sz < 2) {
continue; }
915 for (
int n = 0; n < sz; n++) {
919 if (1.0/
double(vol) < MaxWhisk.Val2) { MaxWhisk=
TFltPr(NIdV.
Len(), 1.0/double(vol)); }
929 for (
int size = 2; size <=
TMath::Mn(MxSize, 1000); size++) {
930 for (
int item = 0; item <SzVolV.
Len(); item++) {
931 const int smallSz = size-SzVolV[item].Val1;
932 if (ItemSetH.
IsKey(smallSz)) {
934 if (SmallSet.
IsKey(item)) {
continue; }
935 const int SmallVol = VolH.
GetDat(smallSz);
937 const double curCost = CostH.
IsKey(size) ? double(CostH.
GetDat(size)) :
double(10e10);
938 const double newCost = double(SmallSet.
Len()+1) /
double(SmallVol+SzVolV[item].Val2);
939 if (curCost < newCost) {
continue; }
940 VolH.
AddDat(size, SmallVol+SzVolV[item].Val2);
941 ItemSetH.
AddDat(size, SmallSet);
943 CostH.
AddDat(size, newCost);
946 if (VolH.
IsKey(size) && size%100==0) {
947 printf(
"\rSize: %d/%d: vol: %d, items: %d/%d [%s]", size, MxSize, VolH.
GetDat(size).
Val,
952 printf(
"\nAdding sizes > 1000 nodes...");
953 int partSz=0;
double partVol=0.0;
954 for (
int i = 0; i < SzVolV.
Len(); i++) {
955 partSz += SzVolV[i].Val1();
956 partVol += SzVolV[i].Val2();
957 if (partSz < 1000) {
continue; }
958 const double curCost = CostH.
IsKey(partSz) ? double(CostH.
GetDat(partSz)) :
double(10e10);
959 const double partPhi = double(i+1)/partVol;
960 if (partPhi < curCost) {
961 CostH.
AddDat(partSz, partPhi);
966 SizePhiV.
Gen(CostH.
Len(), 0);
968 for (
int s = 0; s < CostH.
Len(); s++) {
969 const int size = CostH.
GetKey(s);
970 const double cond = CostH[s];
981 int MxSize=0, TotVol=0;
982 if (Cn1ComV.
Empty()) { printf(
"** No bridges\n"); SizePhiV.
Clr();
return; }
983 printf(
" 1-connected components: %d\n", Cn1ComV.
Len());
984 for (
int c = 0; c < Cn1ComV.
Len(); c++) {
985 const TIntV& NIdV = Cn1ComV[c].NIdV;
986 const int sz = NIdV.
Len();
987 if (sz < 2) {
continue; }
989 for (
int n = 0; n < sz; n++) {
992 MxSize += sz; TotVol += vol;
994 printf(
" Total size: %d\t Total vol: %d\n", MxSize, TotVol);
998 for (
int i = 0; i < SzVolV.
Len(); i++) {
999 const int Sz = SzVolV[i].Val1();
1000 const double Phi = 1.0/double(SzVolV[i].Val2());
1001 if ((! SizePhiH.
IsKey(Sz)) || SizePhiH.
GetDat(Sz) > Phi) {
1002 SizePhiH.
AddDat(Sz, Phi); }
1004 double partSz=0.0, partVol=0.0;
1005 for (
int i = 0; i < SzVolV.
Len(); i++) {
1006 partSz += SzVolV[i].Val1();
1007 partVol += SzVolV[i].Val2();
1008 const double partPhi = double(i+1)/partVol;
1009 if ((! SizePhiH.
IsKey(partSz)) || partPhi < SizePhiH.
GetDat(partSz)) {
1010 SizePhiH.
AddDat(partSz, partPhi); }
1012 SizePhiV.
Gen(SizePhiH.
Len()+1, 0);
1015 for (
int s = 0; s < SizePhiH.
Len(); s++) {
1021 if (ValV.
Empty()) { NewV.
Clr(
false);
return; }
1023 double PrevBPos = 1, BPos = 1;
1030 int MinI=-1;
double MinCnt=
TFlt::Mx;
1031 while (i < ValV.
Len() && ValV[i].Val1 <= BPos) {
1032 if (ValV[i].Val2 < MinCnt) { MinCnt=ValV[i].Val2; MinI=i; } i++; }
1033 if (MinI>=0 && MinI<ValV.
Len()) {
1034 NewV.
Add(ValV[MinI]); }
1035 PrevBPos = (
uint) floor(BPos);
1037 if (floor(BPos) == PrevBPos) { BPos = PrevBPos + 1; }
1043 if (ValV.
Empty()) { NewV.
Clr(
false);
return; }
1045 double PrevBPos = 1, BPos = 1;
1048 while (BPos <= ValV.
Len()) {
1049 int MinI=-1;
double MinCnt=
TFlt::Mx;
1050 while (i < ValV.
Len() && i <= BPos) {
1051 if (ValV[i] < MinCnt) { MinCnt=ValV[i]; MinI=i; } i++; }
1052 if (MinI>=0 && MinI<ValV.
Len()) {
1053 NewV.
Add(ValV[MinI]); }
1054 PrevBPos = (
uint) floor(BPos);
1056 if (floor(BPos) == PrevBPos) { BPos = PrevBPos + 1; }
1066 for (
TFFile FFile(FNmWc); FFile.
Next(FNm); ) {
1068 int TrueNcpId=-1, WhiskId=-1, RewBestWhiskId=-1, RewId=-1, BestWhiskId=-1;
1070 for (
int f = 0; f < Ss.
GetFlds(); f++) {
1072 if (strstr(Ss[f],
"FWD:")) {
1077 if (strstr(Ss[f],
"ORIGINAL MIN")!=NULL) {
1079 int Nodes=0,Edges=0; sscanf(strchr(Ss[f],
'(')+1,
"%d,%d)", &Nodes, &Edges);
1081 printf(
"%s: %d %d\n",
GNmV.
Last().
CStr(), Nodes, Edges);
1084 if (strstr(Ss[f],
"ORIGINAL whisker")!=NULL || strstr(Ss[f],
"TRUE whisker")!=NULL) { WhiskId=f; }
1085 if (strstr(Ss[f],
"ORIGINAL Best whisker")!=NULL || strstr(Ss[f],
"TRUE Best whisker")!=NULL) { BestWhiskId=f; }
1086 if (strstr(Ss[f],
"REWIRED MIN")!=NULL || strstr(Ss[f],
"RAND MIN")!=NULL) { RewId=f; }
1087 if (strstr(Ss[f],
"REWIRED Best whisker")!=NULL || strstr(Ss[f],
"RAND Best whisker")!=NULL) { RewBestWhiskId=f; }
1089 if (TrueNcpId!=-1 || WhiskId!=-1) {
break; }
1091 if (TrueNcpId < 0) { printf(
"%s\n", FNm.
GetFMid().
CStr());
break; }
1098 bool Once=
false, Once2=
false;
1103 if (BestWhiskId>=0 && BestWhiskId < Ss.
GetFlds() && ! Once) { Once=
true;
1104 int W2=BestWhiskId-1;
while (W2 > 0 && Ss.
GetFlt(W2)!=(double)
int(Ss.
GetFlt(W2))) { W2--; }
1106 if (RewBestWhiskId>=0 && RewBestWhiskId < Ss.
GetFlds() && ! Once2) { Once2=
true;
1107 int W2=RewBestWhiskId-1;
while (W2 > 0 && Ss.
GetFlt(W2)!=(double)
int(Ss.
GetFlt(W2))) { W2--; }
1110 printf(
" ncp:%d whisk:%d rew:%d\n",
NcpV.Last().Len(),
WhiskNcpV.Last().Len(),
RewNcpV.Last().Len());
1115 RewWhiskerV(SIn),NcpV(SIn), RewNcpV(SIn),WhiskNcpV(SIn) {
1134 double MinX1=1, MinY1=1;
1135 for (
int k = 0; k < Ncp.
Len(); k++) {
1136 if (Ncp[k].Val2<MinY1) { MinX1=Ncp[k].Val1; MinY1=Ncp[k].Val2; } }
1137 return MinX1<1 ? 1 : MinX1;
1167 double MinX1=1, MinY1=1;
1168 for (
int k = 0; k < Ncp.
Len(); k++) {
1169 if (Ncp[k].Val2<MinY1) { MinX1=Ncp[k].Val1; MinY1=Ncp[k].Val2; } }
1170 return TFltPr(MinX1<1?1:MinX1, MinY1);
1175 for (
int i = 0; i <
NcpV.Len(); i++) {
1182 const TStr XLabel = VsGraphN ? (!
ParamValV.
Empty()?
"parameter value":
"network number") :
"network size";
1189 for (
int i = 0; i <
NcpV.Len(); i++) {
1195 FILE *F = fopen(
TStr::Fmt(
"bestK-%s.txt", OutFNm.
CStr()).CStr(),
"wt");
1196 fprintf(F,
"#nodes\tbestK\tcondAtBestK\tgraph name\n");
1197 for (
int i = 0; i < GSzMinK.
Len(); i++) {
1198 fprintf(F,
"%d\t%d\t%f\t%s\n", GSzMinK[i].Val1(), GSzMinK[i].Val2(), GSzMinK[i].Val3(), GSzMinK[i].Val4.CStr());
1205 for (
int i = 0; i <
NcpV.Len(); i++) {
1212 const TStr XLabel = VsGraphN ? (!
ParamValV.
Empty()?
"parameter value":
"network number") :
"network size";
1219 for (
int i = 0; i <
GSizeV.
Len(); i++) {
1227 const TStr XLabel = VsGraphN ? (!
ParamValV.
Empty()?
"parameter value":
"network number") :
"network size";
1234 for (
int i = 0; i <
GSizeV.
Len(); i++) {
1242 const TStr XLabel = VsGraphN ? (!
ParamValV.
Empty()?
"parameter value":
"network number") :
"network size";
1250 for (
int i = 0; i < NcpVec.
Len(); i++) {
1251 if (
GSizeV[i].Val1 < MinSz) {
continue; }
1252 const TFltPrV& Ncp = NcpVec[i];
1253 double MinX=1, MinY=1;
1254 for (
int k = 0; k < Ncp.
Len(); k++){
1255 if (Ncp[k].Val2<MinY) { MinX=Ncp[k].Val1; MinY=Ncp[k].Val2; } }
1256 if (MinY > MaxMinY) {
continue; } Cnt++;
1258 for (
int k = 0; k < Ncp.
Len(); k++){
1263 TGnuPlot::PlotValMomH(MomH, OutFNm,
"",
"size of the cluster, k",
"phi(k)",
gpsLog,
gpwLines,
true,
true,
true,
true);
1264 printf(
" minSz: %d, miny %g\t%d\n", MinSz, MaxMinY, Cnt);
1268 FILE *F=fopen(OutFNm.
CStr(),
"wt");
1269 fprintf(F,
"#Nodes\tEdges\tBestK\tPhi(BestK)\tMaxWhiskN\tPhi(MaxWhisk)\tGraph\n");
1270 for (
int i = 0; i <
NcpV.Len(); i++) {
1272 double MinX=1, MinY=1;
1273 for (
int k = 0; k < Ncp.
Len(); k++){
1274 if (Ncp[k].Val2<MinY) { MinX=Ncp[k].Val1; MinY=Ncp[k].Val2; } }
1275 fprintf(F,
"%d\t%d\t%d\t%f\t%d\t%f\t%s\n",
GSizeV[i].Val1(),
GSizeV[i].Val2(),
1286 NcpBs.
Impose(OutFNm+
"5R", 5,
false); NcpBs.
Impose(OutFNm+
"5S", 5,
true);
1287 NcpBs.
Impose(OutFNm+
"R", 10,
false); NcpBs.
Impose(OutFNm+
"S", 10,
true);
double GetModRat(const int &GEdges) const
void Clr(const bool &DoDel=true, const int &NoDelLim=-1)
void DrawGViz(const PGraph &Graph, const TGVizLayout &Layout, const TStr &PltFNm, const TStr &Desc=TStr(), const bool &NodeLabels=false, const TIntStrH &NIdColorH=TIntStrH())
Draws a given Graph using a selected GraphViz Layout engine with nodes colored.
static const T & Mn(const T &LVal, const T &RVal)
TPair< TInt, TInt > TIntPr
void SavePajek(const TStr &OutFNm) const
Saves the network in the Pajek format so it can be visualized. Red node represents the seed and color...
int SearchCh(const char &Ch, const int &BChN=0) const
TVec< TNodeSweep > SweepsV
static void PlotDataset(const TStr &InFNmWc, const TStr &OutFNm, const bool &ImposeNcp=false, const bool &VsGraphN=false)
void PlotAvgNcp(const TStr &OutFNm, const TVec< TFltPrV > &NcpVec, const int &MinSz, const double &MaxMinY)
static void BagOfWhiskers2(const PUNGraph &Graph, TFltPrV &SizePhiV)
#define IAssertR(Cond, Reason)
static void DrawWhiskers(const PUNGraph &Graph, TStr FNmPref, const int &PlotN)
Draws the 'whiskers' of the graph. Whiskers are small sub-graphs that are attached to the rest of the...
int GetVol(const int &Nodes) const
Returns the volume of the set of first NodeN nodes in the sweep vector.
static TStr GetMegaStr(const int &Val)
void SetXRange(const double &Min, const double &Max)
void SupportSweep()
After the function ApproxPageRank() has been run the SupportSweep() computes the volume, cut size, node ids, conductance vectors.
void Sort(const bool &CmpKey, const bool &Asc)
static void GetCutStat(const PUNGraph &Graph, const TIntV &NIdV, int &Vol, int &Cut, double &Phi, int GraphEdges=-1)
For a given Graph and a set of nodes NIdV the function returns the Volume, CutSize and the Conductanc...
int GetCut(const int &Nodes) const
Returns the size of the cut of the first Nodes nodes in the sweep vector.
PUNGraph GenRewire(const PUNGraph &OrigGraph, const int &NSwitch, TRnd &Rnd)
Rewire a random undirected graph. Keeps node degrees the same, but randomly rewires the edges...
const TIntV & GetNIdV() const
Vector of node IDs sorted in the random walk order.
void SavePng(const int &SizeX=1000, const int &SizeY=800, const TStr &Comment=TStr())
void Run(const PUNGraph &Graph, const bool &SaveAllSweeps=false, const bool &SaveAllCond=false, const bool &SaveBestNodesAtK=false)
PGraph GetMxWcc(const PGraph &Graph)
Returns a graph representing the largest weakly connected component on an input Graph.
static const T & Mx(const T &LVal, const T &RVal)
int SearchChBack(const char &Ch, int BChN=-1) const
TVec< TFltPrV > WhiskNcpV
void Save(TSOut &SOut) const
int ApproxPageRank(const int &SeedNode, const double &Eps)
Computes Approximate PageRank from the seed node SeedNId and with tolerance Eps.
TFltPr GetXYAtMinY(const TFltPrV &Ncp, const int &NNodes)
void PlotNcpMin(const TStr &OutFNm, const bool &VsGraphN=false)
void PlotPhiDistr(const int &CmtySz, const TStr &OutFNm, TStr Desc=TStr()) const
void FindBestCut(const int &SeedNode, const int &ClustSz, const double &MinSizeFrac=0.2)
Finds minimum conductance cut in the graph around the seed node.
int GetEdges() const
Returns the number of edges in the graph.
TSizeTy Len() const
Returns the number of elements in the vector.
void PlotPhiDistr(const TStr &OutFNm, TStr Desc=TStr()) const
Plots the cluster conductance vs. cluster size K (cluster is composed of nodes NIdV[1...K]).
Node iterator. Only forward iteration (operator++) is supported.
double GetExpEdgesIn(const int &GEdges) const
void PlotCutDistr(const TStr &OutFNm, TStr Desc=TStr()) const
Plots the cluster cut size vs. cluster size K (cluster is composed of nodes NIdV[1...K]).
void GetBoltzmanCurveStat(const TFltV &TempV, TVec< TFltPrV > &NcpV) const
const TCutInfo & GetKNodeCut(const int &Nodes) const
int Len() const
Returns the support of the approximate random walk, the number of nodes with non-zero PageRank score...
void GetKeyV(TVec< TKey > &KeyV) const
void PlotNCP(const TStr &OutFNm, TStr Desc=TStr()) const
TStr GetSubStr(const int &BChN, const int &EChN) const
void SetXYLabel(const TStr &XLabel, const TStr &YLabel)
double GetNormCut(const int &GEdges) const
const TDat & GetDat(const TKey &Key) const
int GetFlds() const
Returns the number of fields in the current line.
bool IsKey(const TKey &Key) const
int GetNodes() const
Returns the number of nodes in the graph.
double GetCutPhi() const
Conductance of the 'best' (minimum conductance) cut.
void PlotRewBestWhisker(const TStr &OutFNm, const bool &VsGraphN=false)
void Save(TSOut &SOut) const
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the graph.
bool Empty() const
Tests whether the vector is empty.
void PlotNCPModul(const TStr &OutFNm, TStr Desc=TStr()) const
double GetExpansion() const
static void PlotValMomH(const THash< TVal1, TMom > &ValMomH, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const TGpSeriesTy &SeriesTy=gpwLinesPoints, bool PlotAvg=true, bool PlotMed=true, bool PlotMin=false, bool PlotMax=false, bool PlotSDev=false, bool PlotStdErr=true, bool PlotScatter=false)
void Swap(TVec< TVal, TSizeTy > &Vec)
Swaps the contents of the vector with Vec.
int GetDeg() const
Returns degree of the current node.
Local-Spectral-Clustering statistics of a given Graph.
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
static void BagOfWhiskers(const PUNGraph &Graph, TFltPrV &SizePhiV, TFltPr &BestWhisk)
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
void AddCmd(const TStr &Cmd)
void AddToBestCutH(const PUNGraph &Graph, const TIntV &NIdV, const bool &AddBestCond=true)
void Add(const TFlt &Val, const TFlt &Wgt=1)
Local Spectral Clustering algorithm.
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
void SaveTxtNcpMin(const TStr &OutFNm, const bool &VsGraphN=false)
void PlotBestWhisker(const TStr &OutFNm, const bool &VsGraphN=false)
double GetCutRatio(const int &GNodes) const
void Save(TSOut &SOut) const
int SearchStr(const TStr &Str, const int &BChN=0) const
const TFltV & GetPhiV() const
Conducatance of the cut separating first K-nodes in the node-id vector (GetNIdV()) from the rest of t...
PUNGraph GetSubGraph(const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumb...
void PlotRewNcpMin(const TStr &OutFNm, const bool &VsGraphN=false)
double GetIntDens() const
double FindBestCut(const int &Nodes, const TIntSet &TabuNIdSet, int &BestCutId) const
static double Round(const double &Val)
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
int AddKey(const TKey &Key)
const TVal & Last() const
Returns a reference to the last element of the vector.
bool GetFlt(const int &FldN, double &Val) const
If the field FldN is a float its value is returned in Val and the function returns true...
static void MakeExpBins(const TFltPrV &ValV, TFltPrV &NewV)
TPair< TFlt, TFlt > TFltPr
double Phi(const int i) const
double GetPhi(const int &ValId) const
Returns the conductance of the cut separating the first Nodes nodes in the sweep vector from the rest...
int GetCutVol() const
Volume of the 'best' (minimum conductance) cut.
THash< TInt, TFltV > SizePhiH
void PlotNCPScatter(const TStr &OutFNm, TStr Desc=TStr()) const
void PlotPhiInOut(const TStr &OutFNm, TStr Desc=TStr()) const
void GetCurveStat(TFltPrV &MeanV, TFltPrV &MedV, TFltPrV &Dec1V, TFltPrV &MinV, TFltPrV &MaxV) const
double GetFracDegOut(const PUNGraph &Graph, double &MxFrac, double &AvgFrac, double &MedianFrac, double &Pct9Frac, double &Flake) const
void SortByKey(const bool &Asc=true)
THash< TInt, TCutInfo > BestCutH
void SaveTxt(const TStr &OutFNm)
double GetModular(const int &GEdges) const
int GetKeyId(const TKey &Key) const
void Get1CnCom(const PUNGraph &Graph, TCnComV &Cn1ComV)
Returns 1-components: maximal connected components of that can be disconnected from the Graph by remo...
void ImposeNCP(const TLocClustStat &LcStat2, TStr OutFNm, TStr Desc, TStr Desc1, TStr Desc2) const
int AddKey(const TKey &Key)
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
void PlotNcpTop10(const TStr &OutFNm, TStr Desc, const int &TakeMinN) const
void SetScale(const TGpScaleTy &GpScaleTy)
static TStr Fmt(const char *FmtStr,...)
void PlotBestClustDens(TStr OutFNm, TStr Desc=TStr()) const
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
TNcpGraphsBase(const TStr &FNmWc)
int AddPlot(const TIntV &YValV, const TGpSeriesTy &SeriesTy=gpwLinesPoints, const TStr &Label=TStr(), const TStr &Style=TStr())
static void PlotValCntH(const THash< TKey, TVal, THashFunc > &ValCntH, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints, const bool &PlotCCDF=false, const bool &ExpBucket=false)
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
int BestCut() const
Index K of the cut of the minimum conductance around the seed node.
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
bool Next()
Loads next line from the input file.
static void Sweep(const TSparseColMatrix &W, const TFltV &fvec, TFltV &conds, TIntV &order)
void Push(const TVal &Val)
double GetDecile(const int &DecileN) const
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
void Clr(const bool &DoDel=true)
void PlotBoltzmanCurve(const TStr &OutFNm, TStr Desc=TStr()) const
static TVec< TStr, int > GetV(const TStr &Val1)
Returns a vector on element Val1.
void PlotVolDistr(const TStr &OutFNm, TStr Desc=TStr()) const
Plots the cluster volume vs. cluster size K (cluster is composed of nodes NIdV[1...K]).
void AddCut(const TIntV &NIdV)
const char * GetStr() const
bool IsKey(const TKey &Key) const
void Save(TSOut &SOut) const
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
TDat & AddDat(const TKey &Key)
void SaveTxtInfo(const TStr &OutFNmPref, const TStr &Desc, const bool &SetMaxAt1) const
int BestCutNodes() const
Number of nodes inside the 'best' (minimum conductance) cut.
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
double GetXAtMinY(const TFltPrV &Ncp, const int &NNodes)
void Impose(const TStr &OutFNm, const int &TopN, const bool &Smooth)
static void PlotNCP(const PUNGraph &Graph, const TStr &FNm, const TStr Desc="", const bool &BagOfWhiskers=true, const bool &RewireNet=false, const int &KMin=10, const int &KMax=Mega(100), const int &Coverage=10, const bool &SaveTxtStat=false, const bool &PlotBoltzman=false)
void Save(TSOut &SOut) const
Local-Spectral-Clustering for a set of graphs (loads ncp-*.tab files)
const TKey & GetKey(const int &KeyId) const
int NId(const int i) const
bool IsFlt(const int &FldN) const
Checks whether fields FldN is a float.
void GetSubValV(const TSizeTy &BValN, const TSizeTy &EValN, TVec< TVal, TSizeTy > &ValV) const
Fills ValV with elements at positions BValN...EValN.
TLocClustStat(const double &_Alpha, const int &_KMin, const int &_KMax, const double &_KFac, const int &_Coverage, const double &_SizeFrac)
void SortByDat(const bool &Asc=true)