39 template <
class PGraph>
double GetFarnessCentr(
const PGraph& Graph,
const int& NId,
const bool& Normalized=
true,
const bool& IsDir=
false);
41 template <
class PGraph>
double GetFarnessCentrMP(
const PGraph& Graph,
const int& NId,
const bool& Normalized=
true,
const bool& IsDir=
false);
49 template <
class PGraph>
double GetClosenessCentr(
const PGraph& Graph,
const int& NId,
const bool& Normalized=
true,
const bool& IsDir=
false);
50 template <
class PGraph>
double GetClosenessCentrMP(
const PGraph& Graph,
const int& NId,
const bool& Normalized=
true,
const bool& IsDir=
false);
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 double& NodeFrac=1.0,
const bool& IsDir=
false);
69 template<
class PGraph>
void GetBetweennessCentr(
const PGraph& Graph,
TIntPrFltH& EdgeBtwH,
const double& NodeFrac=1.0,
const bool& IsDir=
false);
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& Normalized,
const bool& IsDir) {
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& Normalized,
const bool& IsDir) {
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& Normalized,
const bool& IsDir) {
162 const double Farness = GetFarnessCentr<PGraph> (Graph, NId, Normalized, IsDir);
163 if (Farness != 0.0) {
return 1.0/Farness; }
168 template <
class PGraph>
169 double GetClosenessCentrMP(
const PGraph& Graph,
const int& NId,
const bool& Normalized,
const bool& IsDir) {
170 const double Farness = GetFarnessCentrMP<PGraph> (Graph, NId, Normalized, IsDir);
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++) {
391 EdgeBtwH.
AddDat(
TIntPr(NI.GetId(), NI.GetOutNId(e)), 0);
394 if (NI.GetId() < NI.GetOutNId(e)) {
395 EdgeBtwH.
AddDat(
TIntPr(NI.GetId(), NI.GetOutNId(e)), 0);
401 for (
int e = 0; e < NI.GetInDeg(); e++) {
402 if (NI.GetId() < NI.GetInNId(e) &&
403 !Graph->IsEdge(NI.GetId(), NI.GetInNId(e))) {
409 sigma.AddDat(NI.GetId(), 0);
412 delta.
AddDat(NI.GetId(), 0);
415 for (
int k=0; k < BtwNIdV.
Len(); k++) {
416 const typename PGraph::TObj::TNodeI NI = Graph->GetNI(BtwNIdV[k]);
418 for (
int i = 0; i < sigma.Len(); i++) {
419 sigma[i]=0; d[i]=-1; delta[i]=0; P[i].
Clr(
false);
423 sigma.AddDat(NI.GetId(), 1);
426 while (! Q.
Empty()) {
427 const int v = Q.
Top(); Q.
Pop();
428 const typename PGraph::TObj::TNodeI NI2 = Graph->GetNI(v);
430 const int VDat = d.
GetDat(v);
432 for (
int e = 0; e < NI2.GetOutDeg(); e++) {
433 const int w = NI2.GetOutNId(e);
439 if (d.
GetDat(w) == VDat+1) {
440 sigma.AddDat(w) += sigma.GetDat(v);
446 for (
int e = 0; e < NI2.GetInDeg(); e++) {
447 const int w = NI2.GetInNId(e);
449 if (Graph->IsEdge(NI2.GetId(), w)) {
457 if (d.
GetDat(w) == VDat+1) {
458 sigma.AddDat(w) += sigma.GetDat(v);
464 while (! S.
Empty()) {
465 const int w = S.
Top();
466 const double SigmaW = sigma.GetDat(w);
467 const double DeltaW = delta.
GetDat(w);
470 for (
int i = 0; i < NIdV.
Len(); i++) {
471 const int NId = NIdV[i];
472 const double c = (sigma.GetDat(NId)*1.0/SigmaW) * (1+DeltaW);
482 if (DoNodeCent && w != NI.GetId()) {
488 template<
class PGraph>
491 TIntV NIdV; Graph->GetNIdV(NIdV);
492 if (NodeFrac < 1.0) {
494 for (
int i =
int((1.0-NodeFrac)*NIdV.
Len()); i > 0; i--) {
497 GetBetweennessCentr<PGraph> (Graph, NIdV, NodeBtwH,
true, EdgeBtwH,
false, IsDir);
500 template<
class PGraph>
503 TIntV NIdV; Graph->GetNIdV(NIdV);
504 if (NodeFrac < 1.0) {
506 for (
int i =
int((1.0-NodeFrac)*NIdV.
Len()); i > 0; i--) {
509 GetBetweennessCentr<PGraph> (Graph, NIdV, NodeBtwH,
false, EdgeBtwH,
true, IsDir);
512 template<
class PGraph>
514 TIntV NIdV; Graph->GetNIdV(NIdV);
515 if (NodeFrac < 1.0) {
517 for (
int i =
int((1.0-NodeFrac)*NIdV.
Len()); i > 0; i--) {
520 GetBetweennessCentr<PGraph> (Graph, NIdV, NodeBtwH,
true, EdgeBtwH,
true, IsDir);
523 template<
class PGraph>
525 const int NNodes = Graph->GetNodes();
527 NIdAuthH.
Gen(NNodes);
528 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
529 NIdHubH.
AddDat(NI.GetId(), 1.0);
530 NIdAuthH.
AddDat(NI.GetId(), 1.0);
533 for (
int iter = 0; iter < MaxIter; iter++) {
536 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
537 double& Auth = NIdAuthH.
GetDat(NI.GetId()).Val;
539 for (
int e = 0; e < NI.GetInDeg(); e++) {
540 Auth += NIdHubH.
GetDat(NI.GetInNId(e)); }
544 for (
int i = 0; i < NIdAuthH.
Len(); i++) { NIdAuthH[i] /= Norm; }
546 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
547 double& Hub = NIdHubH.
GetDat(NI.GetId()).Val;
549 for (
int e = 0; e < NI.GetOutDeg(); e++) {
550 Hub += NIdAuthH.
GetDat(NI.GetOutNId(e)); }
554 for (
int i = 0; i < NIdHubH.
Len(); i++) { NIdHubH[i] /= Norm; }
558 for (
int i = 0; i < NIdHubH.
Len(); i++) { Norm +=
TMath::Sqr(NIdHubH[i]); }
560 for (
int i = 0; i < NIdHubH.
Len(); i++) { NIdHubH[i] /= Norm; }
562 for (
int i = 0; i < NIdAuthH.
Len(); i++) { Norm +=
TMath::Sqr(NIdAuthH[i]); }
564 for (
int i = 0; i < NIdAuthH.
Len(); i++) { NIdAuthH[i] /= Norm; }
568 template<
class PGraph>
570 const int NNodes = Graph->GetNodes();
573 NIdAuthH.
Gen(NNodes);
574 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
576 NIdHubH.
AddDat(NI.GetId(), 1.0);
577 NIdAuthH.
AddDat(NI.GetId(), 1.0);
580 for (
int iter = 0; iter < MaxIter; iter++) {
583 #pragma omp parallel for reduction(+:Norm) schedule(dynamic,1000)
584 for (
int i = 0; i < NNodes; i++) {
585 typename PGraph::TObj::TNodeI NI = Graph->GetNI(NV[i]);
586 double& Auth = NIdAuthH.
GetDat(NI.GetId()).Val;
588 for (
int e = 0; e < NI.GetInDeg(); e++) {
589 Auth += NIdHubH.
GetDat(NI.GetInNId(e)); }
590 Norm = Norm + Auth*Auth;
593 for (
int i = 0; i < NIdAuthH.
Len(); i++) { NIdAuthH[i] /= Norm; }
595 #pragma omp parallel for reduction(+:Norm) schedule(dynamic,1000)
596 for (
int i = 0; i < NNodes; i++) {
597 typename PGraph::TObj::TNodeI NI = Graph->GetNI(NV[i]);
598 double& Hub = NIdHubH.
GetDat(NI.GetId()).Val;
600 for (
int e = 0; e < NI.GetOutDeg(); e++) {
601 Hub += NIdAuthH.
GetDat(NI.GetOutNId(e)); }
602 Norm = Norm + Hub*Hub;
605 for (
int i = 0; i < NIdHubH.
Len(); i++) { NIdHubH[i] /= Norm; }
609 for (
int i = 0; i < NIdHubH.
Len(); i++) { Norm +=
TMath::Sqr(NIdHubH[i]); }
611 for (
int i = 0; i < NIdHubH.
Len(); i++) { NIdHubH[i] /= Norm; }
613 for (
int i = 0; i < NIdAuthH.
Len(); i++) { Norm +=
TMath::Sqr(NIdAuthH[i]); }
615 for (
int i = 0; i < NIdAuthH.
Len(); i++) { NIdAuthH[i] /= Norm; }
620 template <
class PGraph>
623 const double& C,
const double& Eps,
const int& MaxIter) {
624 int NumGraphs = GraphSeq.
Len();
625 TableSeq.
Reserve(NumGraphs, NumGraphs);
627 for (
TInt i = 0; i < NumGraphs; i++) {
635 template <
class PGraph>
638 const int& MaxIter) {
639 int NumGraphs = GraphSeq.
Len();
640 TableSeq.
Reserve(NumGraphs, NumGraphs);
642 for (
TInt i = 0; i < NumGraphs; i++) {
645 GetHits(GraphSeq[i], HubH, AuthH, MaxIter);
648 PTable HitsT = HubT->Join(
"NodeId", AuthT,
"NodeId");
649 HitsT->Rename(
"1.NodeId",
"NodeId");
650 HitsT->Rename(
"1.Hub",
"Hub");
651 HitsT->Rename(
"2.Authority",
"Authority");
656 HitsT->ProjectInPlace(V);
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 GetFarnessCentrMP(const PGraph &Graph, const int &NId, const bool &Normalized=true, const bool &IsDir=false)
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.
Node iterator. Only forward iteration (operator++) is supported.
void GetBetweennessCentr(const PGraph &Graph, TIntFltH &NIdBtwH, const double &NodeFrac=1.0, const bool &IsDir=false)
void GetHitsMP(const PGraph &Graph, TIntFltH &NIdHubH, TIntFltH &NIdAuthH, const int &MaxIter=20)
void Clr(const bool &DoDel=false)
const TDat & GetDat(const TKey &Key) const
TIntFltH EventImportance(const PNGraph &Graph, const int k)
Event importance.
static PTable TableFromHashMap(const THash< TInt, TInt > &H, const TStr &Col1, const TStr &Col2, TTableContext *Context, const TBool IsStrKeys=false)
Builds table from hash table of int->int.
static double Sqr(const double &x)
double GetFarnessCentr(const PGraph &Graph, const int &NId, const bool &Normalized=true, const bool &IsDir=false)
void MapHits(const TVec< PGraph > &GraphSeq, TVec< PTable > &TableSeq, TTableContext *Context, const int &MaxIter)
Gets sequence of Hits tables from given GraphSeq into TableSeq.
void GetPageRankMP(const PGraph &Graph, TIntFltH &PRankH, const double &C=0.85, const double &Eps=1e-4, const int &MaxIter=100)
double GetClosenessCentrMP(const PGraph &Graph, const int &NId, const bool &Normalized=true, const bool &IsDir=false)
void Gen(const int &ExpectVals)
void GetWeightedBetweennessCentr(const PNEANet Graph, const TIntV &BtwNIdV, TIntFltH &NodeBtwH, const bool &DoNodeCent, TIntPrFltH &EdgeBtwH, const bool &DoEdgeCent, const TFltV &Attr, const bool &IsDir)
Computes (approximate) weighted Beetweenness Centrality of all nodes and all edges of the network...
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)
double GetWeightedFarnessCentr(const PNEANet Graph, const int &NId, const TFltV &Attr, const bool &Normalized, const bool &IsDir)
double GetWeightedClosenessCentr(const PNEANet Graph, const int &NId, const TFltV &Attr, const bool &Normalized, const bool &IsDir)
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.
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
int Intersect1(TUNGraph::TNodeI Node, TStr NNodes)
void MapPageRank(const TVec< PGraph > &GraphSeq, TVec< PTable > &TableSeq, TTableContext *Context, const double &C, const double &Eps, const int &MaxIter)
Gets sequence of PageRank tables from given GraphSeq into TableSeq.
void GetPageRank_v1(const PGraph &Graph, TIntFltH &PRankH, const double &C=0.85, const double &Eps=1e-4, const int &MaxIter=100)
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 Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
void Clr(const bool &DoDel=true)
double GetClosenessCentr(const PGraph &Graph, const int &NId, const bool &Normalized=true, const bool &IsDir=false)
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.