SNAP Library 2.3, Developer Reference  2014-06-16 11:58:46
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cmty.h
Go to the documentation of this file.
1 namespace TSnap {
2 
4 // Modularity
7 template<typename PGraph> double GetModularity(const PGraph& G, const TIntV& NIdV, int GEdges=-1);
10 template<typename PGraph> double GetModularity(const PGraph& G, const TCnComV& CmtyV, int GEdges=-1);
14 template<typename PGraph> void GetEdgesInOut(const PGraph& Graph, const TIntV& NIdV, int& EdgesInX, int& EdgesOutX);
15 
18 double CommunityGirvanNewman(PUNGraph& Graph, TCnComV& CmtyV);
19 
23 double CommunityCNM(const PUNGraph& Graph, TCnComV& CmtyV);
24 
27 double Infomap(PUNGraph& Graph, TCnComV& CmtyV);
28 
29 namespace TSnapDetail {
31 void CmtyGirvanNewmanStep(PUNGraph& Graph, TIntV& Cmty1, TIntV& Cmty2);
32 }
33 
35 // Implementation
36 template<typename PGraph>
37 double GetModularity(const PGraph& Graph, const TIntV& NIdV, int GEdges) {
38  if (GEdges == -1) { GEdges = Graph->GetEdges(); }
39  double EdgesIn = 0.0, EEdgesIn = 0.0; // EdgesIn=2*number of edges inside the cluster, EEdgesIn=expected edges inside
40  TIntSet NIdSet(NIdV.Len());
41  for (int e = 0; e < NIdV.Len(); e++) { // edges inside
42  NIdSet.AddKey(NIdV[e]); }
43  for (int e1 = 0; e1 < NIdV.Len(); e1++) {
44  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NIdV[e1]);
45  EEdgesIn += NI.GetOutDeg();
46  for (int i = 0; i < NI.GetOutDeg(); i++) {
47  if (NIdSet.IsKey(NI.GetOutNId(i))) { EdgesIn += 1; }
48  }
49  }
50  EEdgesIn = EEdgesIn*EEdgesIn/(2.0*GEdges);
51  if ((EdgesIn - EEdgesIn) == 0) { return 0; }
52  else { return (EdgesIn - EEdgesIn) / (2.0*GEdges); } // modularity
53 }
54 
55 template<typename PGraph>
56 double GetModularity(const PGraph& G, const TCnComV& CmtyV, int GEdges) {
57  if (GEdges == -1) { GEdges = G->GetEdges(); }
58  double Modularity = 0;
59  for (int c = 0; c < CmtyV.Len(); c++) {
60  Modularity += GetModularity(G, CmtyV[c](), GEdges);
61  }
62  return Modularity;
63 }
64 
65 template<typename PGraph>
66 void GetEdgesInOut(const PGraph& Graph, const TIntV& NIdV, int& EdgesIn, int& EdgesOut) {
67  EdgesIn=0;
68  EdgesOut=0;
69  TIntSet NIdSet(NIdV.Len());
70  for (int e = 0; e < NIdV.Len(); e++) {
71  NIdSet.AddKey(NIdV[e]); }
72  for (int e = 0; e < NIdV.Len(); e++) {
73  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NIdV[e]);
74  for (int i = 0; i < NI.GetOutDeg(); i++) {
75  if (NIdSet.IsKey(NI.GetOutNId(i))) { EdgesIn += 1; }
76  else { EdgesOut += 1; }
77  }
78  }
79  EdgesIn /= 2;
80 }
81 
82 }; // namespace TSnap
double CommunityGirvanNewman(PUNGraph &Graph, TCnComV &CmtyV)
Definition: cmty.cpp:98
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
void GetEdgesInOut(const PGraph &Graph, const TIntV &NIdV, int &EdgesInX, int &EdgesOutX)
Definition: cmty.h:66
int AddKey(const TKey &Key)
Definition: shash.h:1248
double GetModularity(const PGraph &G, const TIntV &NIdV, int GEdges=-1)
Definition: cmty.h:37
double CommunityCNM(const PUNGraph &Graph, TCnComV &CmtyV)
Definition: cmty.cpp:304
double Infomap(PUNGraph &Graph, TCnComV &CmtyV)
Definition: cmty.cpp:123
void CmtyGirvanNewmanStep(PUNGraph &Graph, TIntV &Cmty1, TIntV &Cmty2)
A single step of Girvan-Newman clustering procedure.
Definition: cmty.cpp:14