SNAP Library 2.2, Developer Reference
2014-03-11 19:15:55
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
00001 namespace TSnap { 00002 00004 // Modularity 00007 template<typename PGraph> double GetModularity(const PGraph& G, const TIntV& NIdV, int GEdges=-1); 00010 template<typename PGraph> double GetModularity(const PGraph& G, const TCnComV& CmtyV, int GEdges=-1); 00014 template<typename PGraph> void GetEdgesInOut(const PGraph& Graph, const TIntV& NIdV, int& EdgesInX, int& EdgesOutX); 00015 00018 double CommunityGirvanNewman(PUNGraph& Graph, TCnComV& CmtyV); 00019 00023 double CommunityCNM(const PUNGraph& Graph, TCnComV& CmtyV); 00024 00027 double Infomap(PUNGraph& Graph, TCnComV& CmtyV); 00028 00029 namespace TSnapDetail { 00031 void CmtyGirvanNewmanStep(PUNGraph& Graph, TIntV& Cmty1, TIntV& Cmty2); 00032 } 00033 00035 // Implementation 00036 template<typename PGraph> 00037 double GetModularity(const PGraph& Graph, const TIntV& NIdV, int GEdges) { 00038 if (GEdges == -1) { GEdges = Graph->GetEdges(); } 00039 double EdgesIn = 0.0, EEdgesIn = 0.0; // EdgesIn=2*number of edges inside the cluster, EEdgesIn=expected edges inside 00040 TIntSet NIdSet(NIdV.Len()); 00041 for (int e = 0; e < NIdV.Len(); e++) { // edges inside 00042 NIdSet.AddKey(NIdV[e]); } 00043 for (int e1 = 0; e1 < NIdV.Len(); e1++) { 00044 typename PGraph::TObj::TNodeI NI = Graph->GetNI(NIdV[e1]); 00045 EEdgesIn += NI.GetOutDeg(); 00046 for (int i = 0; i < NI.GetOutDeg(); i++) { 00047 if (NIdSet.IsKey(NI.GetOutNId(i))) { EdgesIn += 1; } 00048 } 00049 } 00050 EEdgesIn = EEdgesIn*EEdgesIn/(2.0*GEdges); 00051 if ((EdgesIn - EEdgesIn) == 0) { return 0; } 00052 else { return (EdgesIn - EEdgesIn) / (2.0*GEdges); } // modularity 00053 } 00054 00055 template<typename PGraph> 00056 double GetModularity(const PGraph& G, const TCnComV& CmtyV, int GEdges) { 00057 if (GEdges == -1) { GEdges = G->GetEdges(); } 00058 double Modularity = 0; 00059 for (int c = 0; c < CmtyV.Len(); c++) { 00060 Modularity += GetModularity(G, CmtyV[c](), GEdges); 00061 } 00062 return Modularity; 00063 } 00064 00065 template<typename PGraph> 00066 void GetEdgesInOut(const PGraph& Graph, const TIntV& NIdV, int& EdgesIn, int& EdgesOut) { 00067 EdgesIn=0; 00068 EdgesOut=0; 00069 TIntSet NIdSet(NIdV.Len()); 00070 for (int e = 0; e < NIdV.Len(); e++) { 00071 NIdSet.AddKey(NIdV[e]); } 00072 for (int e = 0; e < NIdV.Len(); e++) { 00073 typename PGraph::TObj::TNodeI NI = Graph->GetNI(NIdV[e]); 00074 for (int i = 0; i < NI.GetOutDeg(); i++) { 00075 if (NIdSet.IsKey(NI.GetOutNId(i))) { EdgesIn += 1; } 00076 else { EdgesOut += 1; } 00077 } 00078 } 00079 EdgesIn /= 2; 00080 } 00081 00082 }; // namespace TSnap