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
centr.h
Go to the documentation of this file.
1 namespace TSnap {
2 
4 // Node centrality measures (See: http://en.wikipedia.org/wiki/Centrality)
5 
8 double GetDegreeCentr(const PUNGraph& Graph, const int& NId);
11 double GetFarnessCentr(const PUNGraph& Graph, const int& NId);
14 double GetClosenessCentr(const PUNGraph& Graph, const int& NId);
17 template <class PGraph> int GetNodeEcc(const PGraph& Graph, const int& NId, const bool& IsDir=false);
18 
22 void GetBetweennessCentr(const PUNGraph& Graph, TIntFltH& NIdBtwH, const double& NodeFrac=1.0);
26 void GetBetweennessCentr(const PUNGraph& Graph, TIntPrFltH& EdgeBtwH, const double& NodeFrac=1.0);
31 void GetBetweennessCentr(const PUNGraph& Graph, TIntFltH& NIdBtwH, TIntPrFltH& EdgeBtwH, const double& NodeFrac=1.0);
37 void GetBetweennessCentr(const PUNGraph& Graph, const TIntV& BtwNIdV, TIntFltH& NodeBtwH, const bool& DoNodeCent, TIntPrFltH& EdgeBtwH, const bool& DoEdgeCent);
38 
41 void GetEigenVectorCentr(const PUNGraph& Graph, TIntFltH& NIdEigenH, const double& Eps=1e-4, const int& MaxIter=100);
42 
45 template<class PGraph> void GetPageRank(const PGraph& Graph, TIntFltH& PRankH, const double& C=0.85, const double& Eps=1e-4, const int& MaxIter=100);
48 template<class PGraph> void GetHits(const PGraph& Graph, TIntFltH& NIdHubH, TIntFltH& NIdAuthH, const int& MaxIter=20);
49 
51 // Implementation
52 template <class PGraph>
53 int GetNodeEcc(const PGraph& Graph, const int& NId, const bool& IsDir) {
54  int NodeEcc;
55  int Dist;
56  TBreathFS<PGraph> BFS(Graph);
57  // get shortest paths to all the nodes
58  BFS.DoBfs(NId, true, ! IsDir, -1, TInt::Mx);
59 
60  NodeEcc = 0;
61  // find the largest value
62  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
63  Dist = BFS.NIdDistH[i];
64  if (Dist > NodeEcc) {
65  NodeEcc = Dist;
66  }
67  }
68  return NodeEcc;
69 }
70 
71 // Page Rank -- there are two different implementations (uncomment the desired 2 lines):
72 // Berkhin -- (the correct way) see Algorithm 1 of P. Berkhin, A Survey on PageRank Computing, Internet Mathematics, 2005
73 // iGraph -- iGraph implementation(which treats leaked PageRank in a funny way)
74 template<class PGraph>
75 void GetPageRank(const PGraph& Graph, TIntFltH& PRankH, const double& C, const double& Eps, const int& MaxIter) {
76  const int NNodes = Graph->GetNodes();
77  //const double OneOver = 1.0/double(NNodes);
78  PRankH.Gen(NNodes);
79  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
80  PRankH.AddDat(NI.GetId(), 1.0/NNodes);
81  //IAssert(NI.GetId() == PRankH.GetKey(PRankH.Len()-1));
82  }
83  TFltV TmpV(NNodes);
84  for (int iter = 0; iter < MaxIter; iter++) {
85  int j = 0;
86  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
87  TmpV[j] = 0;
88  for (int e = 0; e < NI.GetInDeg(); e++) {
89  const int InNId = NI.GetInNId(e);
90  const int OutDeg = Graph->GetNI(InNId).GetOutDeg();
91  if (OutDeg > 0) {
92  TmpV[j] += PRankH.GetDat(InNId) / OutDeg; }
93  }
94  TmpV[j] = C*TmpV[j]; // Berkhin (the correct way of doing it)
95  //TmpV[j] = C*TmpV[j] + (1.0-C)*OneOver; // iGraph
96  }
97  double diff=0, sum=0, NewVal;
98  for (int i = 0; i < TmpV.Len(); i++) { sum += TmpV[i]; }
99  const double Leaked = (1.0-sum) / double(NNodes);
100  for (int i = 0; i < PRankH.Len(); i++) { // re-instert leaked PageRank
101  NewVal = TmpV[i] + Leaked; // Berkhin
102  //NewVal = TmpV[i] / sum; // iGraph
103  diff += fabs(NewVal-PRankH[i]);
104  PRankH[i] = NewVal;
105  }
106  if (diff < Eps) { break; }
107  }
108 }
109 
110 template<class PGraph>
111 void GetHits(const PGraph& Graph, TIntFltH& NIdHubH, TIntFltH& NIdAuthH, const int& MaxIter) {
112  const int NNodes = Graph->GetNodes();
113  NIdHubH.Gen(NNodes);
114  NIdAuthH.Gen(NNodes);
115  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
116  NIdHubH.AddDat(NI.GetId(), 1.0);
117  NIdAuthH.AddDat(NI.GetId(), 1.0);
118  }
119  double Norm=0;
120  for (int iter = 0; iter < MaxIter; iter++) {
121  // update authority scores
122  Norm = 0;
123  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
124  double& Auth = NIdAuthH.GetDat(NI.GetId()).Val;
125  Auth = 0;
126  for (int e = 0; e < NI.GetInDeg(); e++) {
127  Auth += NIdHubH.GetDat(NI.GetInNId(e)); }
128  Norm += Auth*Auth;
129  }
130  Norm = sqrt(Norm);
131  for (int i = 0; i < NIdAuthH.Len(); i++) { NIdAuthH[i] /= Norm; }
132  // update hub scores
133  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
134  double& Hub = NIdHubH.GetDat(NI.GetId()).Val;
135  Hub = 0;
136  for (int e = 0; e < NI.GetOutDeg(); e++) {
137  Hub += NIdAuthH.GetDat(NI.GetOutNId(e)); }
138  Norm += Hub*Hub;
139  }
140  Norm = sqrt(Norm);
141  for (int i = 0; i < NIdHubH.Len(); i++) { NIdHubH[i] /= Norm; }
142  }
143  // make sure Hub and Authority scores normalize to L2 norm 1
144  Norm = 0.0;
145  for (int i = 0; i < NIdHubH.Len(); i++) { Norm += TMath::Sqr(NIdHubH[i]); }
146  Norm = sqrt(Norm);
147  for (int i = 0; i < NIdHubH.Len(); i++) { NIdHubH[i] /= Norm; }
148  Norm = 0.0;
149  for (int i = 0; i < NIdAuthH.Len(); i++) { Norm += TMath::Sqr(NIdAuthH[i]); }
150  Norm = sqrt(Norm);
151  for (int i = 0; i < NIdAuthH.Len(); i++) { NIdAuthH[i] /= Norm; }
152 }
153 
154 }; // namespace TSnap
double GetDegreeCentr(const PUNGraph &Graph, const int &NId)
Definition: centr.cpp:5
static const int Mx
Definition: dt.h:1046
void GetPageRank(const PGraph &Graph, TIntFltH &PRankH, const double &C=0.85, const double &Eps=1e-4, const int &MaxIter=100)
Definition: centr.h:75
int DoBfs(const int &StartNode, const bool &FollowOut, const bool &FollowIn, const int &TargetNId=-1, const int &MxDist=TInt::Mx)
Performs BFS from node id StartNode for at maps MxDist steps by only following in-links (parameter Fo...
Definition: bfsdfs.h:108
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
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
static double Sqr(const double &x)
Definition: xmath.h:12
void Gen(const int &ExpectVals)
Definition: hash.h:180
double GetClosenessCentr(const PUNGraph &Graph, const int &NId)
Definition: centr.cpp:22
int GetNodeEcc(const PGraph &Graph, const int &NId, const bool &IsDir=false)
Definition: centr.h:53
void GetHits(const PGraph &Graph, TIntFltH &NIdHubH, TIntFltH &NIdAuthH, const int &MaxIter=20)
Definition: centr.h:111
double GetFarnessCentr(const PUNGraph &Graph, const int &NId)
Definition: centr.cpp:11
TIntH NIdDistH
Definition: bfsdfs.h:79
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