SNAP Library , Developer Reference
2013-01-07 14:03:36
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
00001 #include "stdafx.h" 00002 #include "agm.h" 00003 00004 PUNGraph TAGM::GenAGM(TVec<TIntV>& CmtyVV, const double& DensityCoef, const double& ScaleCoef, TRnd& Rnd){ 00005 TFltV CProbV; 00006 double Prob; 00007 for(int i=0;i<CmtyVV.Len();i++) { 00008 Prob = ScaleCoef*pow(double(CmtyVV[i].Len()),-DensityCoef); 00009 if(Prob>1){Prob = 1;} 00010 CProbV.Add(Prob); 00011 } 00012 PUNGraph G = TUNGraph::New(); 00013 printf("AGM begins\n"); 00014 for(int i=0;i<CmtyVV.Len();i++) { 00015 TIntV& CmtyV = CmtyVV[i]; 00016 for(int u=0;u<CmtyV.Len();u++) { 00017 G->AddNode(CmtyV[u]); 00018 } 00019 Prob = CProbV[i]; 00020 printf("\r%d(%d)/%d",i,CmtyVV[i].Len(),CmtyVV.Len()); 00021 RndConnectInsideCommunity(G,CmtyV,Prob,Rnd); 00022 } 00023 printf("AGM completed (%d nodes %d edges)\n",G->GetNodes(),G->GetEdges()); 00024 return G; 00025 } 00026 00027 void TAGM::RndConnectInsideCommunity(PUNGraph& Graph, const TIntV& CmtyV, const double& Prob, TRnd& Rnd){ 00028 const int CNodes = CmtyV.Len(); 00029 const int CEdges = (int) TMath::Round(Rnd.GetBinomialDev(Prob,CNodes*(CNodes-1)/2)); 00030 THashSet<TIntPr> NewEdgeSet(CEdges); 00031 for (int edge = 0; edge < CEdges; ) { 00032 int SrcNId = CmtyV[Rnd.GetUniDevInt(CNodes)]; 00033 int DstNId = CmtyV[Rnd.GetUniDevInt(CNodes)]; 00034 if(SrcNId>DstNId){Swap(SrcNId,DstNId);} 00035 if (SrcNId != DstNId && !NewEdgeSet.IsKey(TIntPr(SrcNId,DstNId))) { // is new edge 00036 NewEdgeSet.AddKey(TIntPr(SrcNId,DstNId)); 00037 Graph->AddEdge(SrcNId,DstNId); 00038 edge++; 00039 } 00040 } 00041 } 00042 00043 void TAGM::GetNodeMembership(THash<TInt,TIntV >& NIDComVH, const THash<TInt,TIntV>& CmtyVH) { 00044 for(int i=0;i<CmtyVH.Len();i++){ 00045 int CID = CmtyVH.GetKey(i); 00046 for(int j=0;j<CmtyVH[i].Len();j++) { 00047 int NID = CmtyVH[i][j]; 00048 NIDComVH.AddDat(NID).Add(CID); 00049 } 00050 } 00051 } 00052 00053 void TAGM::GetNodeMembership(THash<TInt,TIntV >& NIDComVH, const TVec<TIntV>& CmtyVV) { 00054 THash<TInt,TIntV> CmtyVH; 00055 for(int i=0;i<CmtyVV.Len();i++) { 00056 CmtyVH.AddDat(i,CmtyVV[i]); 00057 } 00058 GetNodeMembership(NIDComVH,CmtyVH); 00059 }