39 template <
class PGraph>
double GetFarnessCentr(
const PGraph& Graph,
const int& NId,
const bool& IsDir,
const bool& Normalized =
true);
41 template <
class PGraph>
double GetFarnessCentrMP(
const PGraph& Graph,
const int& NId,
const bool& IsDir,
const bool& Normalized =
true);
49 template <
class PGraph>
double GetClosenessCentr(
const PGraph& Graph,
const int& NId,
const bool& IsDir,
const bool& Normalized =
true);
50 template <
class PGraph>
double GetClosenessCentrMP(
const PGraph& Graph,
const int& NId,
const bool& IsDir,
const bool& Normalized =
true);
56 template <
class PGraph>
int GetNodeEcc(
const PGraph& Graph,
const int& NId,
const bool& IsDir=
false);
61 template<
class PGraph>
void GetBetweennessCentr(
const PGraph& Graph,
TIntFltH& NIdBtwH,
const bool& IsDir=
false,
const double& NodeFrac=1.0);
69 template<
class PGraph>
void GetBetweennessCentr(
const PGraph& Graph,
TIntPrFltH& EdgeBtwH,
const bool& IsDir=
false,
const double& NodeFrac=1.0);
98 template<
class PGraph>
void GetPageRank(
const PGraph& Graph,
TIntFltH& PRankH,
const double& C=0.85,
const double& Eps=1e-4,
const int& MaxIter=100);
99 template<
class PGraph>
void GetPageRank_v1(
const PGraph& Graph,
TIntFltH& PRankH,
const double& C=0.85,
const double& Eps=1e-4,
const int& MaxIter=100);
101 template<
class PGraph>
void GetPageRankMP(
const PGraph& Graph,
TIntFltH& PRankH,
const double& C=0.85,
const double& Eps=1e-4,
const int& MaxIter=100);
112 template<
class PGraph>
void GetHits(
const PGraph& Graph,
TIntFltH& NIdHubH,
TIntFltH& NIdAuthH,
const int& MaxIter=20);
114 template<
class PGraph>
void GetHitsMP(
const PGraph& Graph,
TIntFltH& NIdHubH,
TIntFltH& NIdAuthH,
const int& MaxIter=20);
122 template <
class PGraph>
123 double GetFarnessCentr(
const PGraph& Graph,
const int& NId,
const bool& IsDir,
const bool& Normalized) {
124 TIntH NDistH(Graph->GetNodes());
125 TSnap::GetShortPath<PGraph>(Graph, NId, NDistH, IsDir,
TInt::Mx);
131 if (NDistH.Len() > 1) {
132 double centr = sum/double(NDistH.Len()-1);
134 centr *= (Graph->GetNodes() - 1)/
double(NDistH.Len()-1);
141 template <
class PGraph>
142 double GetFarnessCentrMP(
const PGraph& Graph,
const int& NId,
const bool& IsDir,
const bool& Normalized) {
143 TIntH NDistH(Graph->GetNodes());
144 TSnap::GetShortPath<PGraph>(Graph, NId, NDistH, IsDir,
TInt::Mx);
150 if (NDistH.Len() > 1) {
151 double centr = sum/double(NDistH.Len()-1);
153 centr *= (Graph->GetNodes() - 1)/
double(NDistH.Len()-1);
160 template <
class PGraph>
161 double GetClosenessCentr(
const PGraph& Graph,
const int& NId,
const bool& IsDir,
const bool& Normalized) {
162 const double Farness = GetFarnessCentr<PGraph> (Graph, NId, IsDir, Normalized);
163 if (Farness != 0.0) {
return 1.0/Farness; }
168 template <
class PGraph>
169 double GetClosenessCentrMP(
const PGraph& Graph,
const int& NId,
const bool& IsDir,
const bool& Normalized) {
170 const double Farness = GetFarnessCentrMP<PGraph> (Graph, NId, IsDir, Normalized);
171 if (Farness != 0.0) {
return 1.0/Farness; }
176 template <
class PGraph>
177 int GetNodeEcc(
const PGraph& Graph,
const int& NId,
const bool& IsDir) {
188 if (Dist > NodeEcc) {
199 template<
class PGraph>
201 const int NNodes = Graph->GetNodes();
204 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
205 PRankH.
AddDat(NI.GetId(), 1.0/NNodes);
209 for (
int iter = 0; iter < MaxIter; iter++) {
211 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
213 for (
int e = 0; e < NI.GetInDeg(); e++) {
214 const int InNId = NI.GetInNId(e);
215 const int OutDeg = Graph->GetNI(InNId).GetOutDeg();
217 TmpV[j] += PRankH.
GetDat(InNId) / OutDeg; }
222 double diff=0, sum=0, NewVal;
223 for (
int i = 0; i < TmpV.
Len(); i++) { sum += TmpV[i]; }
224 const double Leaked = (1.0-sum) /
double(NNodes);
225 for (
int i = 0; i < PRankH.
Len(); i++) {
226 NewVal = TmpV[i] + Leaked;
228 diff += fabs(NewVal-PRankH[i]);
231 if (diff < Eps) {
break; }
239 template<
class PGraph>
240 void GetPageRank(
const PGraph& Graph,
TIntFltH& PRankH,
const double& C,
const double& Eps,
const int& MaxIter) {
241 const int NNodes = Graph->GetNodes();
245 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
247 PRankH.
AddDat(NI.GetId(), 1.0/NNodes);
254 TFltV PRankV(MxId+1);
255 TIntV OutDegV(MxId+1);
257 for (
int j = 0; j < NNodes; j++) {
258 typename PGraph::TObj::TNodeI NI = NV[j];
260 PRankV[Id] = 1.0/NNodes;
261 OutDegV[Id] = NI.GetOutDeg();
266 for (
int iter = 0; iter < MaxIter; iter++) {
267 for (
int j = 0; j < NNodes; j++) {
268 typename PGraph::TObj::TNodeI NI = NV[j];
270 for (
int e = 0; e < NI.GetInDeg(); e++) {
271 const int InNId = NI.GetInNId(e);
272 const int OutDeg = OutDegV[InNId];
274 Tmp += PRankV[InNId] / OutDeg;
280 for (
int i = 0; i < TmpV.
Len(); i++) { sum += TmpV[i]; }
281 const double Leaked = (1.0-sum) /
double(NNodes);
284 for (
int i = 0; i < NNodes; i++) {
285 typename PGraph::TObj::TNodeI NI = NV[i];
286 double NewVal = TmpV[i] + Leaked;
288 diff += fabs(NewVal-PRankV[Id]);
291 if (diff < Eps) {
break; }
294 for (
int i = 0; i < NNodes; i++) {
295 typename PGraph::TObj::TNodeI NI = NV[i];
296 PRankH[i] = PRankV[NI.GetId()];
305 template<
class PGraph>
307 const int NNodes = Graph->GetNodes();
312 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
314 PRankH.
AddDat(NI.GetId(), 1.0/NNodes);
321 TFltV PRankV(MxId+1);
322 TIntV OutDegV(MxId+1);
324 #pragma omp parallel for schedule(dynamic,10000)
325 for (
int j = 0; j < NNodes; j++) {
326 typename PGraph::TObj::TNodeI NI = NV[j];
328 PRankV[Id] = 1.0/NNodes;
329 OutDegV[Id] = NI.GetOutDeg();
333 for (
int iter = 0; iter < MaxIter; iter++) {
334 #pragma omp parallel for schedule(dynamic,10000)
335 for (
int j = 0; j < NNodes; j++) {
336 typename PGraph::TObj::TNodeI NI = NV[j];
338 for (
int e = 0; e < NI.GetInDeg(); e++) {
339 const int InNId = NI.GetInNId(e);
340 const int OutDeg = OutDegV[InNId];
342 Tmp += PRankV[InNId] / OutDeg;
349 #pragma omp parallel for reduction(+:sum) schedule(dynamic,10000)
350 for (
int i = 0; i < TmpV.
Len(); i++) { sum += TmpV[i]; }
351 const double Leaked = (1.0-sum) /
double(NNodes);
354 #pragma omp parallel for reduction(+:diff) schedule(dynamic,10000)
355 for (
int i = 0; i < NNodes; i++) {
356 double NewVal = TmpV[i] + Leaked;
357 int Id = NV[i].GetId();
358 diff += fabs(NewVal-PRankV[Id]);
361 if (diff < Eps) {
break; }
364 #pragma omp parallel for schedule(dynamic,10000)
365 for (
int i = 0; i < NNodes; i++) {
366 typename PGraph::TObj::TNodeI NI = NV[i];
367 PRankH[i] = PRankV[NI.GetId()];
373 template<
class PGraph>
375 if (DoNodeCent) { NodeBtwH.
Clr(); }
376 if (DoEdgeCent) { EdgeBtwH.
Clr(); }
377 const int nodes = Graph->GetNodes();
382 TIntH sigma(nodes), d(nodes);
384 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
386 NodeBtwH.
AddDat(NI.GetId(), 0); }
388 for (
int e = 0; e < NI.GetOutDeg(); e++) {
389 if (NI.GetId() < NI.GetOutNId(e)) {
390 EdgeBtwH.
AddDat(
TIntPr(NI.GetId(), NI.GetOutNId(e)), 0);
394 sigma.AddDat(NI.GetId(), 0);
397 delta.
AddDat(NI.GetId(), 0);
400 for (
int k=0; k < BtwNIdV.
Len(); k++) {
401 const typename PGraph::TObj::TNodeI NI = Graph->GetNI(BtwNIdV[k]);
403 for (
int i = 0; i < sigma.Len(); i++) {
404 sigma[i]=0; d[i]=-1; delta[i]=0; P[i].
Clr(
false);
408 sigma.AddDat(NI.GetId(), 1);
411 while (! Q.
Empty()) {
412 const int v = Q.
Top(); Q.
Pop();
413 const typename PGraph::TObj::TNodeI NI2 = Graph->GetNI(v);
415 const int VDat = d.
GetDat(v);
416 for (
int e = 0; e < NI2.GetOutDeg(); e++) {
417 const int w = NI2.GetOutNId(e);
423 if (d.
GetDat(w) == VDat+1) {
424 sigma.AddDat(w) += sigma.GetDat(v);
429 while (! S.
Empty()) {
430 const int w = S.
Top();
431 const double SigmaW = sigma.GetDat(w);
432 const double DeltaW = delta.
GetDat(w);
435 for (
int i = 0; i < NIdV.
Len(); i++) {
436 const int NId = NIdV[i];
437 const double c = (sigma.GetDat(NId)*1.0/SigmaW) * (1+DeltaW);
442 double factor = (IsDir) ? 1.0 : 2.0;
443 if (DoNodeCent && w != NI.GetId()) {
449 template<
class PGraph>
452 TIntV NIdV; Graph->GetNIdV(NIdV);
453 if (NodeFrac < 1.0) {
455 for (
int i =
int((1.0-NodeFrac)*NIdV.
Len()); i > 0; i--) {
458 GetBetweennessCentr<PGraph> (Graph, NIdV, NodeBtwH, IsDir,
true, EdgeBtwH,
false);
461 template<
class PGraph>
464 TIntV NIdV; Graph->GetNIdV(NIdV);
465 if (NodeFrac < 1.0) {
467 for (
int i =
int((1.0-NodeFrac)*NIdV.
Len()); i > 0; i--) {
470 GetBetweennessCentr<PGraph> (Graph, NIdV, NodeBtwH, IsDir,
false, EdgeBtwH,
true);
473 template<
class PGraph>
475 TIntV NIdV; Graph->GetNIdV(NIdV);
476 if (NodeFrac < 1.0) {
478 for (
int i =
int((1.0-NodeFrac)*NIdV.
Len()); i > 0; i--) {
481 GetBetweennessCentr<PGraph> (Graph, NIdV, NodeBtwH, IsDir,
true, EdgeBtwH,
true);
484 template<
class PGraph>
486 const int NNodes = Graph->GetNodes();
488 NIdAuthH.
Gen(NNodes);
489 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
490 NIdHubH.
AddDat(NI.GetId(), 1.0);
491 NIdAuthH.
AddDat(NI.GetId(), 1.0);
494 for (
int iter = 0; iter < MaxIter; iter++) {
497 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
498 double& Auth = NIdAuthH.
GetDat(NI.GetId()).Val;
500 for (
int e = 0; e < NI.GetInDeg(); e++) {
501 Auth += NIdHubH.
GetDat(NI.GetInNId(e)); }
505 for (
int i = 0; i < NIdAuthH.
Len(); i++) { NIdAuthH[i] /= Norm; }
507 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
508 double& Hub = NIdHubH.
GetDat(NI.GetId()).Val;
510 for (
int e = 0; e < NI.GetOutDeg(); e++) {
511 Hub += NIdAuthH.
GetDat(NI.GetOutNId(e)); }
515 for (
int i = 0; i < NIdHubH.
Len(); i++) { NIdHubH[i] /= Norm; }
519 for (
int i = 0; i < NIdHubH.
Len(); i++) { Norm +=
TMath::Sqr(NIdHubH[i]); }
521 for (
int i = 0; i < NIdHubH.
Len(); i++) { NIdHubH[i] /= Norm; }
523 for (
int i = 0; i < NIdAuthH.
Len(); i++) { Norm +=
TMath::Sqr(NIdAuthH[i]); }
525 for (
int i = 0; i < NIdAuthH.
Len(); i++) { NIdAuthH[i] /= Norm; }
529 template<
class PGraph>
531 const int NNodes = Graph->GetNodes();
534 NIdAuthH.
Gen(NNodes);
535 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
537 NIdHubH.
AddDat(NI.GetId(), 1.0);
538 NIdAuthH.
AddDat(NI.GetId(), 1.0);
541 for (
int iter = 0; iter < MaxIter; iter++) {
544 #pragma omp parallel for reduction(+:Norm) schedule(dynamic,1000)
545 for (
int i = 0; i < NNodes; i++) {
546 typename PGraph::TObj::TNodeI NI = Graph->GetNI(NV[i]);
547 double& Auth = NIdAuthH.
GetDat(NI.GetId()).Val;
549 for (
int e = 0; e < NI.GetInDeg(); e++) {
550 Auth += NIdHubH.
GetDat(NI.GetInNId(e)); }
551 Norm = Norm + Auth*Auth;
554 for (
int i = 0; i < NIdAuthH.
Len(); i++) { NIdAuthH[i] /= Norm; }
556 #pragma omp parallel for reduction(+:Norm) schedule(dynamic,1000)
557 for (
int i = 0; i < NNodes; i++) {
558 typename PGraph::TObj::TNodeI NI = Graph->GetNI(NV[i]);
559 double& Hub = NIdHubH.
GetDat(NI.GetId()).Val;
561 for (
int e = 0; e < NI.GetOutDeg(); e++) {
562 Hub += NIdAuthH.
GetDat(NI.GetOutNId(e)); }
563 Norm = Norm + Hub*Hub;
566 for (
int i = 0; i < NIdHubH.
Len(); i++) { NIdHubH[i] /= Norm; }
570 for (
int i = 0; i < NIdHubH.
Len(); i++) { Norm +=
TMath::Sqr(NIdHubH[i]); }
572 for (
int i = 0; i < NIdHubH.
Len(); i++) { NIdHubH[i] /= Norm; }
574 for (
int i = 0; i < NIdAuthH.
Len(); i++) { Norm +=
TMath::Sqr(NIdAuthH[i]); }
576 for (
int i = 0; i < NIdAuthH.
Len(); i++) { NIdAuthH[i] /= Norm; }
double GetGroupDegreeCentr(const PUNGraph &Graph, const PUNGraph &Group)
static const T & Mn(const T &LVal, const T &RVal)
TPair< TInt, TInt > TIntPr
double GetDegreeCentr(const PUNGraph &Graph, const int &NId)
int Intersect(TUNGraph::TNodeI Node, TIntH NNodes)
Intersect.
int GetWeightedPageRank(const PNEANet Graph, TIntFltH &PRankH, const TStr &Attr, const double &C, const double &Eps, const int &MaxIter)
Weighted PageRank (TODO: Use template)
int GetWeightedShortestPath(const PNEANet Graph, const int &SrcNId, TIntFltH &NIdDistH, const TFltV &Attr)
static const T & Mx(const T &LVal, const T &RVal)
double GetFarnessCentr(const PGraph &Graph, const int &NId, const bool &IsDir, const bool &Normalized=true)
int GetWeightedPageRankMP(const PNEANet Graph, TIntFltH &PRankH, const TStr &Attr, const double &C, const double &Eps, const int &MaxIter)
void GetPageRank(const PGraph &Graph, TIntFltH &PRankH, const double &C=0.85, const double &Eps=1e-4, const int &MaxIter=100)
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...
TSizeTy Len() const
Returns the number of elements in the vector.
void GetWeightedBetweennessCentr(const PNEANet Graph, const TIntV &BtwNIdV, TIntFltH &NodeBtwH, const bool &IsDir, const bool &DoNodeCent, TIntPrFltH &EdgeBtwH, const bool &DoEdgeCent, const TFltV &Attr)
Computes (approximate) weighted Beetweenness Centrality of all nodes and all edges of the network...
double GetWeightedClosenessCentr(const PNEANet Graph, const int &NId, const bool &IsDir, const TFltV &Attr, const bool &Normalized)
Node iterator. Only forward iteration (operator++) is supported.
double GetFarnessCentrMP(const PGraph &Graph, const int &NId, const bool &IsDir, const bool &Normalized=true)
void GetHitsMP(const PGraph &Graph, TIntFltH &NIdHubH, TIntFltH &NIdAuthH, const int &MaxIter=20)
void GetBetweennessCentr(const PGraph &Graph, TIntFltH &NIdBtwH, const bool &IsDir=false, const double &NodeFrac=1.0)
void Clr(const bool &DoDel=false)
const TDat & GetDat(const TKey &Key) const
TIntFltH EventImportance(const PNGraph &Graph, const int k)
Event importance.
static double Sqr(const double &x)
void GetPageRankMP(const PGraph &Graph, TIntFltH &PRankH, const double &C=0.85, const double &Eps=1e-4, const int &MaxIter=100)
void Gen(const int &ExpectVals)
TIntH MaxCPGreedyBetter(const PUNGraph &Graph, const int k)
Returns centrality Maximum k group.
double GetGroupClosenessCentr(const PUNGraph &Graph, const TIntH &GroupNodes)
int GetNodeEcc(const PGraph &Graph, const int &NId, const bool &IsDir=false)
void GetHits(const PGraph &Graph, TIntFltH &NIdHubH, TIntFltH &NIdAuthH, const int &MaxIter=20)
TIntH MaxCPGreedyBetter1(const PUNGraph &Graph, const int k)
Returns centrality Maximum k group.
int Intersect1(TUNGraph::TNodeI Node, TStr NNodes)
void GetPageRank_v1(const PGraph &Graph, TIntFltH &PRankH, const double &C=0.85, const double &Eps=1e-4, const int &MaxIter=100)
double GetWeightedFarnessCentr(const PNEANet Graph, const int &NId, const bool &IsDir, const TFltV &Attr, const bool &Normalized)
double GetClosenessCentrMP(const PGraph &Graph, const int &NId, const bool &IsDir, const bool &Normalized=true)
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
TIntH MaxCPGreedyBetter2(const PUNGraph &Graph, const int k)
Returns centrality Maximum k group.
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
void Push(const TVal &Val)
TIntH LoadNodeList(TStr InFNmNodes)
void Clr(const bool &DoDel=true)
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
void DelLast()
Removes the last element of the vector.
void GetEigenVectorCentr(const PUNGraph &Graph, TIntFltH &NIdEigenH, const double &Eps, const int &MaxIter)
TDat & AddDat(const TKey &Key)
TIntH MaxCPGreedyBetter3(const PUNGraph &Graph, const int k)
Returns centrality Maximum k group.
double GetClosenessCentr(const PGraph &Graph, const int &NId, const bool &IsDir, const bool &Normalized=true)