SNAP Library , Developer Reference  2013-01-07 14:03:36
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
agm.h
Go to the documentation of this file.
00001 #ifndef snap_agm_h
00002 #define snap_agm_h
00003 #include "Snap.h"
00004 
00005 class TAGM {
00006 public:
00007         static void RndConnectInsideCommunity(PUNGraph& Graph, const TIntV& CmtyV, const double& Prob, TRnd& Rnd=TInt::Rnd);
00008         static PUNGraph GenAGM(TVec<TIntV >& CmtyVV, const double& DensityCoef, const double& ScaleCoef, TRnd& Rnd=TInt::Rnd);
00009         static void GetNodeMembership(THash<TInt,TIntV >& NIDComVH, const THash<TInt,TIntV >& CmtyVH);
00010         static void GetNodeMembership(THash<TInt,TIntV >& NIDComVH, const TVec<TIntV >& CmtyVV);
00011 
00012         template<class PGraph>
00013         static void GVizComGraph(const PGraph& Graph,const TVec<TIntV >& CmtyVV, const TStr& OutFNm, const TStr& Desc = TStr()) {
00014                 TStrV Colors = TStrV::GetV("red","blue","green","pink","cyan");
00015                 TStrV Shapes = TStrV::GetV("ellipse","triangle","square","pentagon","hexagon");
00016                 THash<TInt,TIntV> NIDComVH;
00017                 GetNodeMembership(NIDComVH,CmtyVV);
00018 
00019           const TStr Ext = OutFNm.GetFExt();
00020           const TStr GraphFNm = OutFNm.GetSubStr(0, OutFNm.Len()-Ext.Len()) + "dot";
00021                 const bool IsDir = HasGraphFlag(typename PGraph::TObj, gfDirected);
00022                 FILE *F = fopen(GraphFNm.CStr(), "wt");
00023                 if (! Desc.Empty()) fprintf(F, "/*****\n%s\n*****/\n\n", Desc.CStr());
00024                 if (IsDir) { fprintf(F, "digraph G {\n"); } else { fprintf(F, "graph G {\n"); }
00025                 fprintf(F, "  graph [splines=false overlap=false]\n"); //size=\"12,10\" ratio=fill
00026                 // node  [width=0.3, height=0.3, label=\"\", style=filled, color=black]
00027                 // node  [shape=box, width=0.3, height=0.3, label=\"\", style=filled, fillcolor=red]
00028                 fprintf(F, "  node  [width=0.3, height=0.3]\n");
00029                 // node colors
00030                 //for (int i = 0; i < NIdColorH.Len(); i++) {
00031                 for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
00032                         int NID = NI.GetId();
00033                         TIntV& CIDV = NIDComVH.GetDat(NID);
00034                         IAssert(CIDV.Len()>0);
00035                         TStr ShapeNm = Shapes[(CIDV.Len()-1)%Shapes.Len()];
00036                         TStr ColorNm = Colors[CIDV[0]%Colors.Len()];
00037                         TStr NodeComLabel = TStr::Fmt("%d(",NID);
00038                         for(int i=0;i<CIDV.Len();i++) {
00039                                 TStr TmpStr = TStr::Fmt("%d",int(CIDV[i]));
00040                                 NodeComLabel += TmpStr;
00041                                 if(i<CIDV.Len()-1){NodeComLabel+=",";}
00042                         }
00043                         NodeComLabel += ")";
00044                         fprintf(F, "  %d [style=filled, shape=\"%s\" fillcolor=\"%s\" label=\"%s\"];\n", NI.GetId(), ShapeNm.CStr(),ColorNm.CStr(), NodeComLabel.CStr());
00045                 }
00046 
00047                 // edges
00048                 for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
00049                         if (NI.GetOutDeg()==0 && NI.GetInDeg()==0  ) {
00050                                 fprintf(F, "%d;\n", NI.GetId()); }
00051                         else {
00052                                 for (int e = 0; e < NI.GetOutDeg(); e++) {
00053                                         if (! IsDir && NI.GetId() > NI.GetOutNId(e)) { continue; }
00054                                         fprintf(F, "  %d %s %d;\n", NI.GetId(), IsDir?"->":"--", NI.GetOutNId(e));
00055                                 }
00056                         }
00057                 }
00058                 if (! Desc.Empty()) {
00059                         fprintf(F, "  label = \"\\n%s\\n\";", Desc.CStr());
00060                         fprintf(F, "  fontsize=24;\n");
00061                 }
00062                 fprintf(F, "}\n");
00063                 fclose(F);
00064                 TSnap::TSnapDetail::GVizDoLayout(GraphFNm, OutFNm, gvlNeato);
00065         }
00066 };
00067 #endif