SNAP Library 2.1, User Reference
2013-09-25 10:47:25
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
00001 namespace TSnap { 00002 00004 // Plot graph properties 00005 00009 template <class PGraph> void PlotInDegDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr=TStr(), const bool& PlotCCdf=false, const bool& PowerFit=false); 00013 template <class PGraph> void PlotOutDegDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr=TStr(), const bool& PlotCCdf=false, const bool& PowerFit=false); 00015 template <class PGraph> void PlotWccDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr=TStr()); 00017 template <class PGraph> void PlotSccDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr=TStr()); 00019 template <class PGraph> void PlotClustCf(const PGraph& Graph, const TStr& FNmPref, TStr DescStr=TStr()); 00022 template <class PGraph> void PlotHops(const PGraph& Graph, const TStr& FNmPref, TStr DescStr=TStr(), const bool& IsDir=false, const int& NApprox=32); 00024 template <class PGraph> void PlotShortPathDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr=TStr(), int TestNodes=TInt::Mx); 00026 template <class PGraph> void PlotKCoreNodes(const PGraph& Graph, const TStr& FNmPref, TStr DescStr=TStr()); 00028 template <class PGraph> void PlotKCoreEdges(const PGraph& Graph, const TStr& FNmPref, TStr DescStr=TStr()); 00029 00031 void PlotEigValRank(const PUNGraph& Graph, const int& EigVals, const TStr& FNmPref, TStr DescStr=TStr()); 00033 void PlotEigValDistr(const PUNGraph& Graph, const int& EigVals, const TStr& FNmPref, TStr DescStr=TStr()); 00036 void PlotInvParticipRat(const PUNGraph& Graph, const int& MaxEigVecs, const int& TimeLimit, const TStr& FNmPref, TStr DescStr=TStr()); 00038 void PlotSngValRank(const PNGraph& Graph, const int& SngVals, const TStr& FNmPref, TStr DescStr=TStr()); 00040 void PlotSngValDistr(const PNGraph& Graph, const int& SngVals, const TStr& FNmPref, TStr DescStr=TStr()); 00042 void PlotSngVec(const PNGraph& Graph, const TStr& FNmPref, TStr DescStr=TStr()); 00043 00045 // Implementation 00046 template <class PGraph> 00047 void PlotInDegDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr, const bool& PlotCCdf, const bool& PowerFit) { 00048 TIntPrV DegCntV; 00049 TSnap::GetInDegCnt(Graph, DegCntV); 00050 const double AvgDeg = 2*Graph->GetEdges()/double(Graph->GetNodes()); 00051 int AboveAvg=0, Above2Avg=0; 00052 for (int i = 0; i < DegCntV.Len(); i++) { 00053 if (DegCntV[i].Val1 > AvgDeg) { AboveAvg += DegCntV[i].Val2; } 00054 if (DegCntV[i].Val1 > 2*AvgDeg) { Above2Avg += DegCntV[i].Val2; } 00055 } 00056 if (PlotCCdf) { 00057 DegCntV = TGUtil::GetCCdf(DegCntV); } 00058 if (DescStr.Empty()) { DescStr = FNmPref; } 00059 TGnuPlot::PlotValV(DegCntV, (PlotCCdf?"inDegC.":"inDeg.")+FNmPref, 00060 TStr::Fmt("%s. G(%d, %d). %d (%.4f) nodes with in-deg > avg deg (%.1f), %d (%.4f) with >2*avg.deg", DescStr.CStr(), 00061 Graph->GetNodes(), Graph->GetEdges(), AboveAvg, AboveAvg/double(Graph->GetNodes()), AvgDeg, Above2Avg, Above2Avg/double(Graph->GetNodes())), 00062 "In-degree", PlotCCdf?"Count (CCDF)":"Count", gpsLog10XY, PowerFit, gpwLinesPoints); 00063 } 00064 00065 template <class PGraph> 00066 void PlotOutDegDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr, const bool& PlotCCdf, const bool& PowerFit) { 00067 TIntPrV DegCntV; 00068 TSnap::GetOutDegCnt(Graph, DegCntV); 00069 const double AvgDeg = 2*Graph->GetEdges()/double(Graph->GetNodes()); 00070 int AboveAvg=0, Above2Avg=0; 00071 for (int i = 0; i < DegCntV.Len(); i++) { 00072 if (DegCntV[i].Val1 > AvgDeg) { AboveAvg += DegCntV[i].Val2; } 00073 if (DegCntV[i].Val1 > 2*AvgDeg) { Above2Avg += DegCntV[i].Val2; } 00074 } 00075 if (PlotCCdf) { 00076 DegCntV = TGUtil::GetCCdf(DegCntV); } 00077 if (DescStr.Empty()) { DescStr = FNmPref; } 00078 TGnuPlot::PlotValV(DegCntV, (PlotCCdf?"outDegC.":"outDeg.")+FNmPref, 00079 TStr::Fmt("%s. G(%d, %d). %d (%.4f) nodes with out-deg > avg deg (%.1f), %d (%.4f) with >2*avg.deg", DescStr.CStr(), 00080 Graph->GetNodes(), Graph->GetEdges(), AboveAvg, AboveAvg/double(Graph->GetNodes()), AvgDeg, Above2Avg, Above2Avg/double(Graph->GetNodes())), 00081 "Out-degree", PlotCCdf?"Count (CCDF)":"Count", gpsLog10XY, PowerFit, gpwLinesPoints); 00082 } 00083 00084 template <class PGraph> 00085 void PlotWccDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr) { 00086 TIntPrV WccSzCnt; 00087 TSnap::GetWccSzCnt(Graph, WccSzCnt); 00088 if (DescStr.Empty()) { DescStr = FNmPref; } 00089 TGnuPlot GnuPlot("wcc."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest component has %f nodes", 00090 DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), WccSzCnt.Last().Val1/double(Graph->GetNodes()))); 00091 GnuPlot.AddPlot(WccSzCnt, gpwLinesPoints, "", "pt 6"); 00092 GnuPlot.SetXYLabel("Size of weakly connected component", "Number of components"); 00093 GnuPlot.SetScale(gpsLog10XY); 00094 GnuPlot.SavePng(); 00095 } 00096 00097 template <class PGraph> 00098 void PlotSccDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr) { 00099 TIntPrV SccSzCnt; 00100 TSnap::GetSccSzCnt(Graph, SccSzCnt); 00101 if (DescStr.Empty()) { DescStr = FNmPref; } 00102 TGnuPlot GnuPlot("scc."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest component has %f nodes", 00103 DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), SccSzCnt.Last().Val1/double(Graph->GetNodes()))); 00104 GnuPlot.AddPlot(SccSzCnt, gpwLinesPoints, "", "pt 6"); 00105 GnuPlot.SetXYLabel("Size of strongly connected component", "Number of components"); 00106 GnuPlot.SetScale(gpsLog10XY); 00107 GnuPlot.SavePng(); 00108 } 00109 00110 template <class PGraph> 00111 void PlotClustCf(const PGraph& Graph, const TStr& FNmPref, TStr DescStr) { 00112 TFltPrV DegToCCfV; 00113 int64 ClosedTriads, OpenTriads; 00114 const double CCF = GetClustCf(Graph, DegToCCfV, ClosedTriads, OpenTriads); 00115 if (DescStr.Empty()) { DescStr = FNmPref; } 00116 TGnuPlot GnuPlot("ccf."+FNmPref, 00117 TStr::Fmt("%s. G(%d, %d). Average clustering: %.4f OpenTriads: %d (%.4f) ClosedTriads: %d (%.4f)", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), 00118 CCF, OpenTriads, OpenTriads/double(OpenTriads+ClosedTriads), ClosedTriads, ClosedTriads/double(OpenTriads+ClosedTriads))); 00119 GnuPlot.AddPlot(DegToCCfV, gpwLinesPoints, "", "pt 6"); 00120 GnuPlot.SetXYLabel("Node degree", "Average clustering coefficient"); 00121 GnuPlot.SetScale(gpsLog10XY); 00122 GnuPlot.SavePng(); 00123 } 00124 00125 template <class PGraph> 00126 void PlotHops(const PGraph& Graph, const TStr& FNmPref, TStr DescStr, const bool& IsDir, const int& NApprox) { 00127 TIntFltKdV DistNbrsV; 00128 TSnap::GetAnf(Graph, DistNbrsV, -1, IsDir, NApprox); 00129 const double EffDiam = TSnap::TSnapDetail::CalcEffDiam(DistNbrsV, 0.9); 00130 if (DescStr.Empty()) { DescStr = FNmPref; } 00131 TGnuPlot GnuPlot("hop."+FNmPref, TStr::Fmt("%s. Hop plot. EffDiam: %g, G(%d, %d)", 00132 DescStr.CStr(), EffDiam, Graph->GetNodes(), Graph->GetEdges())); 00133 GnuPlot.SetXYLabel("Number of hops", "Number of pairs of nodes"); 00134 GnuPlot.SetScale(gpsLog10Y); 00135 GnuPlot.AddPlot(DistNbrsV, gpwLinesPoints, "", "pt 6"); 00136 GnuPlot.SavePng(); 00137 } 00138 00139 template <class PGraph> 00140 void PlotShortPathDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr, int TestNodes) { 00141 TIntH DistToCntH; 00142 TBreathFS<PGraph> BFS(Graph); 00143 // shotest paths 00144 TIntV NodeIdV; 00145 Graph->GetNIdV(NodeIdV); NodeIdV.Shuffle(TInt::Rnd); 00146 for (int tries = 0; tries < TMath::Mn(TestNodes, Graph->GetNodes()); tries++) { 00147 const int NId = NodeIdV[tries]; 00148 BFS.DoBfs(NId, true, false, -1, TInt::Mx); 00149 for (int i = 0; i < BFS.NIdDistH.Len(); i++) { 00150 DistToCntH.AddDat(BFS.NIdDistH[i]) += 1; } 00151 } 00152 DistToCntH.SortByKey(true); 00153 TFltPrV DistNbrsPdfV; 00154 for (int i = 0; i < DistToCntH.Len(); i++) { 00155 DistNbrsPdfV.Add(TFltPr(DistToCntH.GetKey(i)(), DistToCntH[i]())); 00156 } 00157 const double EffDiam = TSnap::TSnapDetail::CalcEffDiamPdf(DistNbrsPdfV, 0.9); 00158 const double AvgDiam = TSnap::TSnapDetail::CalcAvgDiamPdf(DistNbrsPdfV); 00159 const int FullDiam = (int) DistNbrsPdfV.Last().Val1; 00160 if (DescStr.Empty()) { DescStr = FNmPref; } 00161 TGnuPlot::PlotValV(DistNbrsPdfV, "diam."+FNmPref, 00162 TStr::Fmt("%s. G(%d, %d). Diam: avg:%.2f eff:%.2f max:%d", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), 00163 AvgDiam, EffDiam, FullDiam), "Number of hops", "Number of shortest paths", gpsLog10Y, false, gpwLinesPoints); 00164 } 00165 00166 template <class PGraph> 00167 void PlotKCoreNodes(const PGraph& Graph, const TStr& FNmPref, TStr DescStr) { 00168 TIntPrV CoreNodesV; 00169 TSnap::GetKCoreNodes(Graph, CoreNodesV); 00170 if (DescStr.Empty()) { DescStr = FNmPref; } 00171 TGnuPlot::PlotValV(CoreNodesV, "coreNodes."+FNmPref, TStr::Fmt("%s. G(%d, %d).", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges()), "k-Core", "Number of nodes in the k-Core", gpsLog10Y, false, gpwLinesPoints); 00172 } 00173 00174 template <class PGraph> 00175 void PlotKCoreEdges(const PGraph& Graph, const TStr& FNmPref, TStr DescStr) { 00176 TIntPrV CoreEdgesV; 00177 TSnap::GetKCoreEdges(Graph, CoreEdgesV); 00178 if (DescStr.Empty()) { DescStr = FNmPref; } 00179 TGnuPlot::PlotValV(CoreEdgesV, "coreEdges."+FNmPref, TStr::Fmt("%s. G(%d, %d).", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges()), "k-Core", "Number of edges in the k-Core", gpsLog10Y, false, gpwLinesPoints); 00180 } 00181 00182 //TIntPrV CoreNV, CoreEV; 00183 //TSnap::GetKCoreNodes(UG, CoreNV); 00184 //TSnap::GetKCoreEdges(UG, CoreEV); 00185 //TGnuPlot::PlotValV(CoreNV, "kcoreN.fullUndir", "kCore nodes size", "k-Core", "Number of nodes", gpsLog10Y); 00186 //TGnuPlot::PlotValV(CoreEV, "kcoreE.fullUndir", "kCore edges size", "k-Core", "Number of edges", gpsLog10Y); 00187 00188 00189 } // namespace TSnap