SNAP Library , Developer Reference
2013-01-07 14:03:36
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
#include <agm.h>
Static Public Member Functions | |
static void | RndConnectInsideCommunity (PUNGraph &Graph, const TIntV &CmtyV, const double &Prob, TRnd &Rnd=TInt::Rnd) |
static PUNGraph | GenAGM (TVec< TIntV > &CmtyVV, const double &DensityCoef, const double &ScaleCoef, TRnd &Rnd=TInt::Rnd) |
static void | GetNodeMembership (THash< TInt, TIntV > &NIDComVH, const THash< TInt, TIntV > &CmtyVH) |
static void | GetNodeMembership (THash< TInt, TIntV > &NIDComVH, const TVec< TIntV > &CmtyVV) |
template<class PGraph > | |
static void | GVizComGraph (const PGraph &Graph, const TVec< TIntV > &CmtyVV, const TStr &OutFNm, const TStr &Desc=TStr()) |
PUNGraph TAGM::GenAGM | ( | TVec< TIntV > & | CmtyVV, |
const double & | DensityCoef, | ||
const double & | ScaleCoef, | ||
TRnd & | Rnd = TInt::Rnd |
||
) | [static] |
Definition at line 4 of file agm.cpp.
References TVec< TVal >::Add(), TUNGraph::AddNode(), TUNGraph::GetEdges(), TUNGraph::GetNodes(), TVec< TVal >::Len(), TUNGraph::New(), and RndConnectInsideCommunity().
{ TFltV CProbV; double Prob; for(int i=0;i<CmtyVV.Len();i++) { Prob = ScaleCoef*pow(double(CmtyVV[i].Len()),-DensityCoef); if(Prob>1){Prob = 1;} CProbV.Add(Prob); } PUNGraph G = TUNGraph::New(); printf("AGM begins\n"); for(int i=0;i<CmtyVV.Len();i++) { TIntV& CmtyV = CmtyVV[i]; for(int u=0;u<CmtyV.Len();u++) { G->AddNode(CmtyV[u]); } Prob = CProbV[i]; printf("\r%d(%d)/%d",i,CmtyVV[i].Len(),CmtyVV.Len()); RndConnectInsideCommunity(G,CmtyV,Prob,Rnd); } printf("AGM completed (%d nodes %d edges)\n",G->GetNodes(),G->GetEdges()); return G; }
void TAGM::GetNodeMembership | ( | THash< TInt, TIntV > & | NIDComVH, |
const THash< TInt, TIntV > & | CmtyVH | ||
) | [static] |
Definition at line 43 of file agm.cpp.
References TVec< TVal >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::GetKey(), and THash< TKey, TDat, THashFunc >::Len().
Referenced by GetNodeMembership(), and GVizComGraph().
{ for(int i=0;i<CmtyVH.Len();i++){ int CID = CmtyVH.GetKey(i); for(int j=0;j<CmtyVH[i].Len();j++) { int NID = CmtyVH[i][j]; NIDComVH.AddDat(NID).Add(CID); } } }
void TAGM::GetNodeMembership | ( | THash< TInt, TIntV > & | NIDComVH, |
const TVec< TIntV > & | CmtyVV | ||
) | [static] |
Definition at line 53 of file agm.cpp.
References THash< TKey, TDat, THashFunc >::AddDat(), GetNodeMembership(), and TVec< TVal >::Len().
{ THash<TInt,TIntV> CmtyVH; for(int i=0;i<CmtyVV.Len();i++) { CmtyVH.AddDat(i,CmtyVV[i]); } GetNodeMembership(NIDComVH,CmtyVH); }
static void TAGM::GVizComGraph | ( | const PGraph & | Graph, |
const TVec< TIntV > & | CmtyVV, | ||
const TStr & | OutFNm, | ||
const TStr & | Desc = TStr() |
||
) | [inline, static] |
Definition at line 13 of file agm.h.
References TStr::CStr(), TStr::Fmt(), THash< TKey, TDat, THashFunc >::GetDat(), TStr::GetFExt(), GetNodeMembership(), TStr::GetSubStr(), TVec< TVal >::GetV(), gfDirected, TSnap::TSnapDetail::GVizDoLayout(), gvlNeato, HasGraphFlag, IAssert, TVec< TVal >::Len(), and TStr::Len().
{ TStrV Colors = TStrV::GetV("red","blue","green","pink","cyan"); TStrV Shapes = TStrV::GetV("ellipse","triangle","square","pentagon","hexagon"); THash<TInt,TIntV> NIDComVH; GetNodeMembership(NIDComVH,CmtyVV); const TStr Ext = OutFNm.GetFExt(); const TStr GraphFNm = OutFNm.GetSubStr(0, OutFNm.Len()-Ext.Len()) + "dot"; const bool IsDir = HasGraphFlag(typename PGraph::TObj, gfDirected); FILE *F = fopen(GraphFNm.CStr(), "wt"); if (! Desc.Empty()) fprintf(F, "/*****\n%s\n*****/\n\n", Desc.CStr()); if (IsDir) { fprintf(F, "digraph G {\n"); } else { fprintf(F, "graph G {\n"); } fprintf(F, " graph [splines=false overlap=false]\n"); //size=\"12,10\" ratio=fill // node [width=0.3, height=0.3, label=\"\", style=filled, color=black] // node [shape=box, width=0.3, height=0.3, label=\"\", style=filled, fillcolor=red] fprintf(F, " node [width=0.3, height=0.3]\n"); // node colors //for (int i = 0; i < NIdColorH.Len(); i++) { for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { int NID = NI.GetId(); TIntV& CIDV = NIDComVH.GetDat(NID); IAssert(CIDV.Len()>0); TStr ShapeNm = Shapes[(CIDV.Len()-1)%Shapes.Len()]; TStr ColorNm = Colors[CIDV[0]%Colors.Len()]; TStr NodeComLabel = TStr::Fmt("%d(",NID); for(int i=0;i<CIDV.Len();i++) { TStr TmpStr = TStr::Fmt("%d",int(CIDV[i])); NodeComLabel += TmpStr; if(i<CIDV.Len()-1){NodeComLabel+=",";} } NodeComLabel += ")"; fprintf(F, " %d [style=filled, shape=\"%s\" fillcolor=\"%s\" label=\"%s\"];\n", NI.GetId(), ShapeNm.CStr(),ColorNm.CStr(), NodeComLabel.CStr()); } // edges for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { if (NI.GetOutDeg()==0 && NI.GetInDeg()==0 ) { fprintf(F, "%d;\n", NI.GetId()); } else { for (int e = 0; e < NI.GetOutDeg(); e++) { if (! IsDir && NI.GetId() > NI.GetOutNId(e)) { continue; } fprintf(F, " %d %s %d;\n", NI.GetId(), IsDir?"->":"--", NI.GetOutNId(e)); } } } if (! Desc.Empty()) { fprintf(F, " label = \"\\n%s\\n\";", Desc.CStr()); fprintf(F, " fontsize=24;\n"); } fprintf(F, "}\n"); fclose(F); TSnap::TSnapDetail::GVizDoLayout(GraphFNm, OutFNm, gvlNeato); }
void TAGM::RndConnectInsideCommunity | ( | PUNGraph & | Graph, |
const TIntV & | CmtyV, | ||
const double & | Prob, | ||
TRnd & | Rnd = TInt::Rnd |
||
) | [static] |
Definition at line 27 of file agm.cpp.
References TUNGraph::AddEdge(), THashSet< TKey, THashFunc >::AddKey(), TRnd::GetBinomialDev(), TRnd::GetUniDevInt(), THashSet< TKey, THashFunc >::IsKey(), TVec< TVal >::Len(), TMath::Round(), and Swap().
Referenced by GenAGM().
{ const int CNodes = CmtyV.Len(); const int CEdges = (int) TMath::Round(Rnd.GetBinomialDev(Prob,CNodes*(CNodes-1)/2)); THashSet<TIntPr> NewEdgeSet(CEdges); for (int edge = 0; edge < CEdges; ) { int SrcNId = CmtyV[Rnd.GetUniDevInt(CNodes)]; int DstNId = CmtyV[Rnd.GetUniDevInt(CNodes)]; if(SrcNId>DstNId){Swap(SrcNId,DstNId);} if (SrcNId != DstNId && !NewEdgeSet.IsKey(TIntPr(SrcNId,DstNId))) { // is new edge NewEdgeSet.AddKey(TIntPr(SrcNId,DstNId)); Graph->AddEdge(SrcNId,DstNId); edge++; } } }