SNAP Library 2.3, User 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
TSnap::TSnapDetail::TCNMQMatrix Class Reference

Classes

struct  TCmtyDat
 

Public Member Functions

 TCNMQMatrix (const PUNGraph &Graph)
 
void Init (const PUNGraph &Graph)
 
TFltIntIntTr FindMxQEdge ()
 
bool MergeBestQ ()
 

Static Public Member Functions

static double CmtyCMN (const PUNGraph &Graph, TCnComV &CmtyV)
 

Private Attributes

THash< TInt, TCmtyDatCmtyQH
 
THeap< TFltIntIntTrMxQHeap
 
TUnionFind CmtyIdUF
 
double Q
 

Detailed Description

Clauset-Newman-Moore community detection method. At every step two communities that contribute maximum positive value to global modularity are merged. See: Finding community structure in very large networks, A. Clauset, M.E.J. Newman, C. Moore, 2004

Definition at line 192 of file cmty.cpp.

Constructor & Destructor Documentation

TSnap::TSnapDetail::TCNMQMatrix::TCNMQMatrix ( const PUNGraph Graph)
inline

Definition at line 217 of file cmty.cpp.

217  : CmtyQH(Graph->GetNodes()),
218  MxQHeap(Graph->GetNodes()), CmtyIdUF(Graph->GetNodes()) { Init(Graph); }
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:162
THeap< TFltIntIntTr > MxQHeap
Definition: cmty.cpp:213
void Init(const PUNGraph &Graph)
Definition: cmty.cpp:219
THash< TInt, TCmtyDat > CmtyQH
Definition: cmty.cpp:212

Member Function Documentation

static double TSnap::TSnapDetail::TCNMQMatrix::CmtyCMN ( const PUNGraph Graph,
TCnComV CmtyV 
)
inlinestatic

Definition at line 285 of file cmty.cpp.

285  {
286  TCNMQMatrix QMatrix(Graph);
287  // maximize modularity
288  while (QMatrix.MergeBestQ()) { }
289  // reconstruct communities
290  THash<TInt, TIntV> IdCmtyH;
291  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
292  IdCmtyH.AddDat(QMatrix.CmtyIdUF.Find(NI.GetId())).Add(NI.GetId());
293  }
294  CmtyV.Gen(IdCmtyH.Len());
295  for (int j = 0; j < IdCmtyH.Len(); j++) {
296  CmtyV[j].NIdV.Swap(IdCmtyH[j]);
297  }
298  return QMatrix.Q;
299  }
TCNMQMatrix(const PUNGraph &Graph)
Definition: cmty.cpp:217
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:63
void Swap(TVec< TVal, TSizeTy > &Vec)
Swaps the contents of the vector with Vec.
Definition: ds.h:1011
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:203
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:486
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:201
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
TFltIntIntTr TSnap::TSnapDetail::TCNMQMatrix::FindMxQEdge ( )
inline

Definition at line 238 of file cmty.cpp.

238  {
239  while (true) {
240  if (MxQHeap.Empty()) { break; }
241  const TFltIntIntTr TopQ = MxQHeap.PopHeap();
242  if (! CmtyQH.IsKey(TopQ.Val2) || ! CmtyQH.IsKey(TopQ.Val3)) { continue; }
243  if (TopQ.Val1!=CmtyQH.GetDat(TopQ.Val2).GetMxQ() && TopQ.Val1!=CmtyQH.GetDat(TopQ.Val3).GetMxQ()) { continue; }
244  return TopQ;
245  }
246  return TFltIntIntTr(-1, -1, -1);
247  }
Definition: ds.h:129
TVal1 Val1
Definition: ds.h:131
TVal2 Val2
Definition: ds.h:132
THeap< TFltIntIntTr > MxQHeap
Definition: cmty.cpp:213
TTriple< TFlt, TInt, TInt > TFltIntIntTr
Definition: ds.h:176
TVal3 Val3
Definition: ds.h:133
THash< TInt, TCmtyDat > CmtyQH
Definition: cmty.cpp:212
void TSnap::TSnapDetail::TCNMQMatrix::Init ( const PUNGraph Graph)
inline

Definition at line 219 of file cmty.cpp.

219  {
220  const double M = 0.5/Graph->GetEdges(); // 1/2m
221  Q = 0.0;
222  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
223  CmtyIdUF.Add(NI.GetId());
224  const int OutDeg = NI.GetOutDeg();
225  if (OutDeg == 0) { continue; }
226  TCmtyDat& Dat = CmtyQH.AddDat(NI.GetId(), TCmtyDat(M * OutDeg, OutDeg));
227  for (int e = 0; e < NI.GetOutDeg(); e++) {
228  const int DstNId = NI.GetOutNId(e);
229  const double DstMod = 2 * M * (1.0 - OutDeg * Graph->GetNI(DstNId).GetOutDeg() * M);
230  Dat.AddQ(DstNId, DstMod);
231  }
232  Q += -1.0*TMath::Sqr(OutDeg*M);
233  if (NI.GetId() < Dat.GetMxQNId()) {
234  MxQHeap.Add(TFltIntIntTr(Dat.GetMxQ(), NI.GetId(), Dat.GetMxQNId())); }
235  }
236  MxQHeap.MakeHeap();
237  }
int Add(const int &Key)
Adds an element Key to the structure.
Definition: gbase.h:238
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:74
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:63
static double Sqr(const double &x)
Definition: xmath.h:12
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:86
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:205
THeap< TFltIntIntTr > MxQHeap
Definition: cmty.cpp:213
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:203
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:201
TTriple< TFlt, TInt, TInt > TFltIntIntTr
Definition: ds.h:176
THash< TInt, TCmtyDat > CmtyQH
Definition: cmty.cpp:212
bool TSnap::TSnapDetail::TCNMQMatrix::MergeBestQ ( )
inline

Definition at line 248 of file cmty.cpp.

248  {
249  const TFltIntIntTr TopQ = FindMxQEdge();
250  if (TopQ.Val1 <= 0.0) { return false; }
251  // joint communities
252  const int I = TopQ.Val3;
253  const int J = TopQ.Val2;
254  CmtyIdUF.Union(I, J); // join
255  Q += TopQ.Val1;
256  TCmtyDat& DatJ = CmtyQH.GetDat(J);
257  { TCmtyDat& DatI = CmtyQH.GetDat(I);
258  DatI.DelLink(J); DatJ.DelLink(I);
259  for (int i = -1; DatJ.NIdQH.FNextKeyId(i); ) {
260  const int K = DatJ.NIdQH.GetKey(i);
261  TCmtyDat& DatK = CmtyQH.GetDat(K);
262  double NewQ = DatJ.NIdQH[i];
263  if (DatI.NIdQH.IsKey(K)) { NewQ = NewQ+DatI.NIdQH.GetDat(K); DatK.DelLink(I); } // K connected to I and J
264  else { NewQ = NewQ-2*DatI.DegFrac*DatK.DegFrac; } // K connected to J not I
265  DatJ.AddQ(K, NewQ);
266  DatK.AddQ(J, NewQ);
267  MxQHeap.PushHeap(TFltIntIntTr(NewQ, TMath::Mn(J,K), TMath::Mx(J,K)));
268  }
269  for (int i = -1; DatI.NIdQH.FNextKeyId(i); ) {
270  const int K = DatI.NIdQH.GetKey(i);
271  if (! DatJ.NIdQH.IsKey(K)) { // K connected to I not J
272  TCmtyDat& DatK = CmtyQH.GetDat(K);
273  const double NewQ = DatI.NIdQH[i]-2*DatJ.DegFrac*DatK.DegFrac;
274  DatJ.AddQ(K, NewQ);
275  DatK.DelLink(I);
276  DatK.AddQ(J, NewQ);
277  MxQHeap.PushHeap(TFltIntIntTr(NewQ, TMath::Mn(J,K), TMath::Mx(J,K)));
278  }
279  }
280  DatJ.DegFrac += DatI.DegFrac; }
281  if (DatJ.NIdQH.Empty()) { CmtyQH.DelKey(J); } // isolated community (done)
282  CmtyQH.DelKey(I);
283  return true;
284  }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
void Union(const int &Key1, const int &Key2)
Merges sets with elements Key1 and Key2.
Definition: gbase.cpp:40
TFltIntIntTr FindMxQEdge()
Definition: cmty.cpp:238
Definition: ds.h:129
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
TVal1 Val1
Definition: ds.h:131
TVal2 Val2
Definition: ds.h:132
THeap< TFltIntIntTr > MxQHeap
Definition: cmty.cpp:213
TTriple< TFlt, TInt, TInt > TFltIntIntTr
Definition: ds.h:176
TVal3 Val3
Definition: ds.h:133
THash< TInt, TCmtyDat > CmtyQH
Definition: cmty.cpp:212

Member Data Documentation

TUnionFind TSnap::TSnapDetail::TCNMQMatrix::CmtyIdUF
private

Definition at line 214 of file cmty.cpp.

THash<TInt, TCmtyDat> TSnap::TSnapDetail::TCNMQMatrix::CmtyQH
private

Definition at line 212 of file cmty.cpp.

THeap<TFltIntIntTr> TSnap::TSnapDetail::TCNMQMatrix::MxQHeap
private

Definition at line 213 of file cmty.cpp.

double TSnap::TSnapDetail::TCNMQMatrix::Q
private

Definition at line 215 of file cmty.cpp.


The documentation for this class was generated from the following file: