SNAP Library, Developer Reference  2012-10-15 15:06:59
SNAP, a general purpose network analysis and graph mining library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
statplot.h
Go to the documentation of this file.
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, const TStr& DescStr, 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);
00025 
00027 void PlotEigValRank(const PUNGraph& Graph, const int& EigVals, const TStr& FNmPref, TStr DescStr=TStr());
00029 void PlotEigValDistr(const PUNGraph& Graph, const int& EigVals, const TStr& FNmPref, TStr DescStr=TStr());
00032 void PlotInvParticipRat(const PUNGraph& Graph, const int& MaxEigVecs, const int& TimeLimit, const TStr& FNmPref, TStr DescStr=TStr());
00034 void PlotSngValRank(const PNGraph& Graph, const int& SngVals, const TStr& FNmPref, TStr DescStr=TStr());
00036 void PlotSngValDistr(const PNGraph& Graph, const int& SngVals, const TStr& FNmPref, TStr DescStr=TStr());
00038 void PlotSngVec(const PNGraph& Graph, const TStr& FNmPref, TStr DescStr=TStr());
00039 
00041 // Implementation
00042 template <class PGraph>
00043 void PlotInDegDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr, const bool& PlotCCdf, const bool& PowerFit) {
00044   TIntPrV DegCntV;
00045   TSnap::GetInDegCnt(Graph, DegCntV);
00046   const double AvgDeg = 2*Graph->GetEdges()/double(Graph->GetNodes());
00047   int AboveAvg=0, Above2Avg=0;
00048   for (int i = 0; i < DegCntV.Len(); i++) {
00049     if (DegCntV[i].Val1 > AvgDeg) { AboveAvg += DegCntV[i].Val2; }
00050     if (DegCntV[i].Val1 > 2*AvgDeg) { Above2Avg += DegCntV[i].Val2; }
00051   }
00052   if (PlotCCdf) {
00053     DegCntV = TGUtil::GetCCdf(DegCntV); }
00054   if (DescStr.Empty()) { DescStr = FNmPref; }
00055   TGnuPlot::PlotValV(DegCntV, (PlotCCdf?"inDegC.":"inDeg.")+FNmPref,
00056     TStr::Fmt("%s. G(%d, %d). %d (%.4f) nodes with in-deg > avg deg (%.1f), %d (%.4f) with >2*avg.deg", DescStr.CStr(),
00057       Graph->GetNodes(), Graph->GetEdges(), AboveAvg, AboveAvg/double(Graph->GetNodes()), AvgDeg, Above2Avg, Above2Avg/double(Graph->GetNodes())),
00058     "In-degree", PlotCCdf?"Count (CCDF)":"Count", gpsLog10XY, PowerFit, gpwLinesPoints);
00059 }
00060 
00061 template <class PGraph>
00062 void PlotOutDegDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr, const bool& PlotCCdf, const bool& PowerFit) {
00063   TIntPrV DegCntV;
00064   TSnap::GetOutDegCnt(Graph, DegCntV);
00065   const double AvgDeg = 2*Graph->GetEdges()/double(Graph->GetNodes());
00066   int AboveAvg=0, Above2Avg=0;
00067   for (int i = 0; i < DegCntV.Len(); i++) {
00068     if (DegCntV[i].Val1 > AvgDeg) { AboveAvg += DegCntV[i].Val2; }
00069     if (DegCntV[i].Val1 > 2*AvgDeg) { Above2Avg += DegCntV[i].Val2; }
00070   }
00071   if (PlotCCdf) {
00072     DegCntV = TGUtil::GetCCdf(DegCntV); }
00073   if (DescStr.Empty()) { DescStr = FNmPref; }
00074   TGnuPlot::PlotValV(DegCntV, (PlotCCdf?"outDegC.":"outDeg.")+FNmPref,
00075     TStr::Fmt("%s. G(%d, %d). %d (%.4f) nodes with out-deg > avg deg (%.1f), %d (%.4f) with >2*avg.deg", DescStr.CStr(),
00076       Graph->GetNodes(), Graph->GetEdges(), AboveAvg, AboveAvg/double(Graph->GetNodes()), AvgDeg, Above2Avg, Above2Avg/double(Graph->GetNodes())),
00077     "Out-degree", PlotCCdf?"Count (CCDF)":"Count", gpsLog10XY, PowerFit, gpwLinesPoints);
00078 }
00079 
00080 template <class PGraph>
00081 void PlotWccDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr) {
00082   TIntPrV WccSzCnt;
00083   TSnap::GetWccSzCnt(Graph, WccSzCnt);
00084   if (DescStr.Empty()) { DescStr = FNmPref; }
00085   TGnuPlot GnuPlot("wcc."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest component has %f nodes",
00086     DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), WccSzCnt.Last().Val1/double(Graph->GetNodes())));
00087   GnuPlot.AddPlot(WccSzCnt, gpwLinesPoints, "", "pt 6");
00088   GnuPlot.SetXYLabel("Size of weakly connected component", "Number of components");
00089   GnuPlot.SetScale(gpsLog10XY);
00090   GnuPlot.SavePng();
00091 }
00092 
00093 template <class PGraph>
00094 void PlotSccDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr) {
00095   TIntPrV SccSzCnt;
00096   TSnap::GetSccSzCnt(Graph, SccSzCnt);
00097   if (DescStr.Empty()) { DescStr = FNmPref; }
00098   TGnuPlot GnuPlot("scc."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest component has %f nodes",
00099     DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), SccSzCnt.Last().Val1/double(Graph->GetNodes())));
00100   GnuPlot.AddPlot(SccSzCnt, gpwLinesPoints, "", "pt 6");
00101   GnuPlot.SetXYLabel("Size of strongly connected component", "Number of components");
00102   GnuPlot.SetScale(gpsLog10XY);
00103   GnuPlot.SavePng();
00104 }
00105 
00106 template <class PGraph>
00107 void PlotClustCf(const PGraph& Graph, const TStr& FNmPref, TStr DescStr) {
00108   TFltPrV DegToCCfV;
00109   int ClosedTriads, OpenTriads;
00110   const double CCF = GetClustCf(Graph, DegToCCfV, ClosedTriads, OpenTriads);
00111   if (DescStr.Empty()) { DescStr = FNmPref; }
00112   TGnuPlot GnuPlot("ccf."+FNmPref,
00113     TStr::Fmt("%s. G(%d, %d). Average clustering: %.4f  OpenTriads: %d (%.4f)  ClosedTriads: %d (%.4f)", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(),
00114     CCF, OpenTriads, OpenTriads/double(OpenTriads+ClosedTriads), ClosedTriads, ClosedTriads/double(OpenTriads+ClosedTriads)));
00115   GnuPlot.AddPlot(DegToCCfV, gpwLinesPoints, "", "pt 6");
00116   GnuPlot.SetXYLabel("Node degree", "Average clustering coefficient");
00117   GnuPlot.SetScale(gpsLog10XY);
00118   GnuPlot.SavePng();
00119 }
00120 
00121 template <class PGraph>
00122 void PlotHops(const PGraph& Graph, const TStr& FNmPref, const TStr& DescStr, const bool& IsDir, const int& NApprox) {
00123   TIntFltKdV DistNbrsV;
00124   TSnap::GetAnf(Graph, DistNbrsV, -1, IsDir, NApprox);
00125   const double EffDiam = TSnap::TSnapDetail::CalcEffDiam(DistNbrsV, 0.9);
00126   TGnuPlot GnuPlot("hop."+FNmPref, TStr::Fmt("%s. Hop plot. EffDiam: %g, G(%d, %d)",
00127     DescStr.CStr(), EffDiam, Graph->GetNodes(), Graph->GetEdges()));
00128   GnuPlot.SetXYLabel("Number of hops", "Number of pairs of nodes");
00129   GnuPlot.SetScale(gpsLog10Y);
00130   GnuPlot.AddPlot(DistNbrsV, gpwLinesPoints, "");
00131   GnuPlot.SavePng();
00132 }
00133 
00134 template <class PGraph>
00135 void PlotShortPathDistr(const PGraph& Graph, const TStr& FNmPref, TStr DescStr, int TestNodes) {
00136   TIntH DistToCntH;
00137   TBreathFS<PGraph> BFS(Graph);
00138   // shotest paths
00139   TIntV NodeIdV;
00140   Graph->GetNIdV(NodeIdV);  NodeIdV.Shuffle(TInt::Rnd);
00141   for (int tries = 0; tries < TMath::Mn(TestNodes, Graph->GetNodes()); tries++) {
00142     const int NId = NodeIdV[tries];
00143     BFS.DoBfs(NId, true, false, -1, TInt::Mx);
00144     for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
00145       DistToCntH.AddDat(BFS.NIdDistH[i]) += 1; }
00146   }
00147   DistToCntH.SortByKey(true);
00148   TFltPrV DistNbrsPdfV;
00149   for (int i = 0; i < DistToCntH.Len(); i++) {
00150     DistNbrsPdfV.Add(TFltPr(DistToCntH.GetKey(i)(), DistToCntH[i]()));
00151   }
00152   const double EffDiam = TSnap::TSnapDetail::CalcEffDiamPdf(DistNbrsPdfV, 0.9);
00153   const double AvgDiam = TSnap::TSnapDetail::CalcAvgDiamPdf(DistNbrsPdfV);
00154   const int FullDiam = (int) DistNbrsPdfV.Last().Val1;
00155   if (DescStr.Empty()) { DescStr = FNmPref; }
00156   TGnuPlot::PlotValV(DistNbrsPdfV, "diam."+FNmPref,
00157     TStr::Fmt("%s. G(%d, %d). Diam: avg:%.2f  eff:%.2f  max:%d", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(),
00158     AvgDiam, EffDiam, FullDiam), "Number of hops", "Number of shortest paths", gpsLog10Y, false, gpwLinesPoints);
00159 }
00160 
00161 } // namespace TSnap