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
centr.cpp
Go to the documentation of this file.
1 namespace TSnap {
2 
4 // Node centrality measures
5 double GetDegreeCentr(const PUNGraph& Graph, const int& NId) {
6  if (Graph->GetNodes() > 1) {
7  return double(Graph->GetNI(NId).GetDeg())/double(Graph->GetNodes()-1); }
8  else { return 0.0; }
9 }
10 
11 double GetFarnessCentr(const PUNGraph& Graph, const int& NId) {
12  TIntH NDistH(Graph->GetNodes());
13  TSnap::GetShortPath<PUNGraph>(Graph, NId, NDistH, true, TInt::Mx);
14  double sum = 0;
15  for (TIntH::TIter I = NDistH.BegI(); I < NDistH.EndI(); I++) {
16  sum += I->Dat();
17  }
18  if (NDistH.Len() > 1) { return sum/double(NDistH.Len()-1); }
19  else { return 0.0; }
20 }
21 
22 double GetClosenessCentr(const PUNGraph& Graph, const int& NId) {
23  const double Farness = GetFarnessCentr(Graph, NId);
24  if (Farness != 0.0) { return 1.0/Farness; }
25  else { return 0.0; }
26 }
27 
28 void GetBetweennessCentr(const PUNGraph& Graph, const TIntV& BtwNIdV, TIntFltH& NodeBtwH, const bool& DoNodeCent, TIntPrFltH& EdgeBtwH, const bool& DoEdgeCent) {
29  if (DoNodeCent) { NodeBtwH.Clr(); }
30  if (DoEdgeCent) { EdgeBtwH.Clr(); }
31  const int nodes = Graph->GetNodes();
32  TIntS S(nodes);
33  TIntQ Q(nodes);
34  TIntIntVH P(nodes); // one vector for every node
35  TIntFltH delta(nodes);
36  TIntH sigma(nodes), d(nodes);
37  // init
38  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
39  if (DoNodeCent) {
40  NodeBtwH.AddDat(NI.GetId(), 0); }
41  if (DoEdgeCent) {
42  for (int e = 0; e < NI.GetOutDeg(); e++) {
43  if (NI.GetId() < NI.GetOutNId(e)) {
44  EdgeBtwH.AddDat(TIntPr(NI.GetId(), NI.GetOutNId(e)), 0); }
45  }
46  }
47  sigma.AddDat(NI.GetId(), 0);
48  d.AddDat(NI.GetId(), -1);
49  P.AddDat(NI.GetId(), TIntV());
50  delta.AddDat(NI.GetId(), 0);
51  }
52  // calc betweeness
53  for (int k=0; k < BtwNIdV.Len(); k++) {
54  const TUNGraph::TNodeI NI = Graph->GetNI(BtwNIdV[k]);
55  // reset
56  for (int i = 0; i < sigma.Len(); i++) {
57  sigma[i]=0; d[i]=-1; delta[i]=0; P[i].Clr(false);
58  }
59  S.Clr(false);
60  Q.Clr(false);
61  sigma.AddDat(NI.GetId(), 1);
62  d.AddDat(NI.GetId(), 0);
63  Q.Push(NI.GetId());
64  while (! Q.Empty()) {
65  const int v = Q.Top(); Q.Pop();
66  const TUNGraph::TNodeI NI2 = Graph->GetNI(v);
67  S.Push(v);
68  const int VDat = d.GetDat(v);
69  for (int e = 0; e < NI2.GetOutDeg(); e++) {
70  const int w = NI2.GetOutNId(e);
71  if (d.GetDat(w) < 0) { // find w for the first time
72  Q.Push(w);
73  d.AddDat(w, VDat+1);
74  }
75  //shortest path to w via v ?
76  if (d.GetDat(w) == VDat+1) {
77  sigma.AddDat(w) += sigma.GetDat(v);
78  P.GetDat(w).Add(v);
79  }
80  }
81  }
82  while (! S.Empty()) {
83  const int w = S.Top();
84  const double SigmaW = sigma.GetDat(w);
85  const double DeltaW = delta.GetDat(w);
86  const TIntV NIdV = P.GetDat(w);
87  S.Pop();
88  for (int i = 0; i < NIdV.Len(); i++) {
89  const int nid = NIdV[i];
90  const double c = (sigma.GetDat(nid)*1.0/SigmaW) * (1+DeltaW);
91  delta.AddDat(nid) += c;
92  if (DoEdgeCent) {
93  EdgeBtwH.AddDat(TIntPr(TMath::Mn(nid, w), TMath::Mx(nid, w))) += c; }
94  }
95  if (DoNodeCent && w != NI.GetId()) {
96  NodeBtwH.AddDat(w) += delta.GetDat(w)/2.0; }
97  }
98  }
99 }
100 
101 // Approximate beetweenness centrality scores. The implementation scales to large networks.
102 // NodeFrac ... calculate Beetweenness Centrality for a fraction of nodes. Gives approximate.
103 void GetBetweennessCentr(const PUNGraph& Graph, TIntFltH& NodeBtwH, const double& NodeFrac) {
104  TIntPrFltH EdgeBtwH;
105  TIntV NIdV; Graph->GetNIdV(NIdV);
106  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
107  NIdV.Shuffle(TInt::Rnd);
108  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
109  NIdV.DelLast(); }
110  }
111  GetBetweennessCentr(Graph, NIdV, NodeBtwH, true, EdgeBtwH, false);
112 }
113 
114 void GetBetweennessCentr(const PUNGraph& Graph, TIntPrFltH& EdgeBtwH, const double& NodeFrac) {
115  TIntFltH NodeBtwH;
116  TIntV NIdV; Graph->GetNIdV(NIdV);
117  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
118  NIdV.Shuffle(TInt::Rnd);
119  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
120  NIdV.DelLast(); }
121  }
122  GetBetweennessCentr(Graph, NIdV, NodeBtwH, false, EdgeBtwH, true);
123 }
124 
125 void GetBetweennessCentr(const PUNGraph& Graph, TIntFltH& NodeBtwH, TIntPrFltH& EdgeBtwH, const double& NodeFrac) {
126  TIntV NIdV; Graph->GetNIdV(NIdV);
127  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
128  NIdV.Shuffle(TInt::Rnd);
129  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
130  NIdV.DelLast(); }
131  }
132  GetBetweennessCentr(Graph, NIdV, NodeBtwH, true, EdgeBtwH, true);
133 }
134 
135 void GetEigenVectorCentr(const PUNGraph& Graph, TIntFltH& NIdEigenH, const double& Eps, const int& MaxIter) {
136  const int NNodes = Graph->GetNodes();
137  NIdEigenH.Gen(NNodes);
138  // initialize vector values
139  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
140  NIdEigenH.AddDat(NI.GetId(), 1.0/NNodes);
141  IAssert(NI.GetId() == NIdEigenH.GetKey(NIdEigenH.Len()-1));
142  }
143  TFltV TmpV(NNodes);
144  for (int iter = 0; iter < MaxIter; iter++) {
145  int j = 0;
146  // add neighbor values
147  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
148  TmpV[j] = 0;
149  for (int e = 0; e < NI.GetOutDeg(); e++) {
150  TmpV[j] += NIdEigenH.GetDat(NI.GetOutNId(e)); }
151  }
152 
153  // normalize
154  double sum = 0;
155  for (int i = 0; i < TmpV.Len(); i++) {
156  sum += (TmpV[i]*TmpV[i]);
157  }
158  sum = sqrt(sum);
159  for (int i = 0; i < TmpV.Len(); i++) {
160  TmpV[i] /= sum;
161  }
162 
163  // compute difference
164  double diff = 0.0;
165  j = 0;
166  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
167  diff += fabs(NIdEigenH.GetDat(NI.GetId())-TmpV[j]);
168  }
169 
170  // set new values
171  j = 0;
172  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
173  NIdEigenH.AddDat(NI.GetId(), TmpV[j]);
174  }
175 
176  if (diff < Eps) {
177  break;
178  }
179  }
180 }
181 
182 }; // namespace TSnap
#define IAssert(Cond)
Definition: bd.h:262
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
bool Empty()
Definition: ds.h:3468
double GetDegreeCentr(const PUNGraph &Graph, const int &NId)
Definition: centr.cpp:5
bool Empty() const
Definition: ds.h:3523
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
TVal & Top()
Definition: ds.h:3472
static const int Mx
Definition: dt.h:1046
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
void GetBetweennessCentr(const PUNGraph &Graph, const TIntV &BtwNIdV, TIntFltH &NodeBtwH, const bool &DoNodeCent, TIntPrFltH &EdgeBtwH, const bool &DoEdgeCent)
Definition: centr.cpp:28
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:63
void Clr(const bool &DoDel=false)
Definition: ds.h:3469
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
static TRnd Rnd
Definition: dt.h:1050
void Pop()
Definition: ds.h:3527
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:86
void Gen(const int &ExpectVals)
Definition: hash.h:180
double GetClosenessCentr(const PUNGraph &Graph, const int &NId)
Definition: centr.cpp:22
const TVal & Top() const
Definition: ds.h:3525
void Push()
Definition: ds.h:3474
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:96
void Pop()
Definition: ds.h:3476
Definition: hash.h:88
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1235
double GetFarnessCentr(const PUNGraph &Graph, const int &NId)
Definition: centr.cpp:11
TVec< TInt > TIntV
Definition: ds.h:2473
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:315
Definition: bd.h:196
void Push(const TVal &Val)
Definition: ds.h:3530
void Clr(const bool &DoDel=true)
Definition: ds.h:3512
int GetId() const
Returns ID of the current node.
Definition: graph.h:80
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
void DelLast()
Removes the last element of the vector.
Definition: ds.h:609
int Len() const
Definition: hash.h:186
void GetEigenVectorCentr(const PUNGraph &Graph, TIntFltH &NIdEigenH, const double &Eps, const int &MaxIter)
Definition: centr.cpp:135
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
THKeyDat * EndI
Definition: hash.h:45
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210