6 if (Graph->GetNodes() > 1) {
7 return double(Graph->GetNI(NId).GetDeg())/
double(Graph->GetNodes()-1); }
12 const int NNodes = Graph->GetNodes();
13 NIdEigenH.
Gen(NNodes);
16 NIdEigenH.
AddDat(NI.GetId(), 1.0/NNodes);
20 for (
int iter = 0; iter < MaxIter; iter++) {
25 for (
int e = 0; e < NI.GetOutDeg(); e++) {
26 TmpV[j] += NIdEigenH.
GetDat(NI.GetOutNId(e)); }
31 for (
int i = 0; i < TmpV.
Len(); i++) {
32 sum += (TmpV[i]*TmpV[i]);
35 for (
int i = 0; i < TmpV.
Len(); i++) {
43 diff += fabs(NIdEigenH.
GetDat(NI.GetId())-TmpV[j]);
49 NIdEigenH.
AddDat(NI.GetId(), TmpV[j]);
63 deg = Graph->GetNI(NI.GetId()).GetDeg();
64 for (
int i=0; i<deg; i++) {
65 if (Group->IsNode(Graph->GetNI(NI.GetId()).GetNbrNId(i))==0)
66 NN.
AddDat(Graph->GetNI(NI.GetId()).GetNbrNId(i),NI.GetId());
69 return (
double)NN.
Len();
75 for (
int i = 0; i<GroupNodes.
Len(); i++) {
76 deg = Graph->GetNI(GroupNodes.
GetDat(i)).GetDeg();
77 for (
int j = 0; j < deg; j++) {
78 if (GroupNodes.
IsKey(Graph->GetNI(GroupNodes.
GetDat(i)).GetNbrNId(j))==0)
82 return (
double)NN.
Len();
91 GroupNodes1.
AddDat(NI.GetDat(),NI.GetDat());
96 for (
int j = 0; j < deg; j++){
102 return (
double)NN.
Len();
108 for (
int i=0; i<GroupNodes.
Len(); i++){
109 NDistH[i](Graph->GetNodes());
110 TSnap::GetShortPath<PUNGraph>(Graph, GroupNodes.
GetDat(i), NDistH[i],
true,
TInt::Mx);
113 int min, dist, sum=0, len=0;
114 for (PUNGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
115 if(NDistH[0].IsKey(NI.GetId()))
116 min = NDistH[0].
GetDat(NI.GetId());
119 for (
int j=1; j<GroupNodes.
Len(); j++){
120 if (NDistH[j].IsKey(NI.GetId()))
121 dist = NDistH[j].
GetDat(NI.GetId());
124 if ((dist < min && dist != -1) || (dist > min && min == -1))
134 if (len > 0) {
return sum/double(len); }
141 for(
int i=0; i<n; i++)
147 for(
int i=0; i<n; i++)
148 for(
int j=i; j<n; j++){
159 for(
int i=n; i>0; i--){
160 N *= (float)m/(
float)n;
171 if (Farness != 0.0) {
return 1.0/Farness; }
179 double gc = 0, gc0 = 0;
180 int addId = 0, addIdPrev = 0;
183 Nodes.
AddDat(NI.GetId(),NI.GetDeg());
191 if ((NI.GetDat() <= (int)gc0))
193 gc = NI.GetDat()-
Intersect(Graph->GetNI(NI.GetKey()),NNodes);
200 if (addId != addIdPrev){
202 GroupNodes.
AddDat(br,addId);
207 for (
int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
208 NNodes.
AddDat(Graph->GetNI(addId).GetNbrNId(i),0);
227 double gc = 0, gc0 = 0;
228 int addId = 0, addIdPrev = 0;
232 Nodes.
AddDat(NI.GetId(),NI.GetDeg());
239 if((NI.GetDat() < (int)gc0))
241 gc = NI.GetDat()-
Intersect(Graph->GetNI(NI.GetKey()),NNodes);
248 if (addId != addIdPrev){
250 GroupNodes.
AddDat(br,addId);
255 for (
int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
256 NNodes.
AddDat(Graph->GetNI(addId).GetNbrNId(i),0);
272 double gc = 0, gc0 = 0;
273 int addId = 0, addIdPrev=0;
276 Nodes.
AddDat(NI.GetId(),NI.GetDeg());
284 if((NI.GetDat() <= (int)gc0))
286 gc = NI.GetDat()-
Intersect(Graph->GetNI(NI.GetKey()),NNodes);
293 if (addId != addIdPrev) {
295 GroupNodes.
AddDat(br,addId);
304 for (
int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
305 TInt digi = Graph->GetNI(addId).GetNbrNId(i);
324 const int n = Graph->GetNodes();
325 int *NNodes =
new int[n];
328 double gc = 0, gc0 = 0;
329 int addId = 0, addIdPrev = 0;
332 Nodes.
AddDat(NI.GetId(),NI.GetDeg());
340 if((NI.GetDat() <= (int)gc0))
342 gc = NI.GetDat()-
Intersect(Graph->GetNI(NI.GetKey()),NNodes,NNodes_br);
349 if (addId != addIdPrev) {
351 GroupNodes.
AddDat(br,addId);
357 for (
int j=0; j<NNodes_br; j++)
358 if (NNodes[j] == nn){
364 NNodes[NNodes_br] = nn;
368 for (
int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
369 int nn = Graph->GetNI(addId).GetNbrNId(i);
371 for (
int j=0; j<NNodes_br; j++) {
372 if (NNodes[j] == nn){
378 NNodes[NNodes_br] = nn;
397 if (!Graph->IsFltAttrE(Attr))
return -1;
399 TFltV Weights = Graph->GetFltAttrVecE(Attr);
401 int mxid = Graph->GetMxNId();
402 TFltV OutWeights(mxid);
403 Graph->GetWeightOutEdgesV(OutWeights, Weights);
405 const int NNodes = Graph->GetNodes();
409 PRankH.
AddDat(NI.GetId(), 1.0/NNodes);
413 for (
int iter = 0; iter < MaxIter; iter++) {
415 for (
TNEANet::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
417 for (
int e = 0; e < NI.GetInDeg(); e++) {
418 const int InNId = NI.GetInNId(e);
419 const TFlt OutWeight = OutWeights[InNId];
420 int EId = Graph->GetEId(InNId, NI.GetId());
421 const TFlt Weight = Weights[Graph->GetFltKeyIdE(EId)];
423 TmpV[j] += PRankH.
GetDat(InNId) * Weight / OutWeight; }
428 double diff=0, sum=0, NewVal;
429 for (
int i = 0; i < TmpV.
Len(); i++) { sum += TmpV[i]; }
430 const double Leaked = (1.0-sum) /
double(NNodes);
431 for (
int i = 0; i < PRankH.
Len(); i++) {
432 NewVal = TmpV[i] + Leaked;
434 diff += fabs(NewVal-PRankH[i]);
437 if (diff < Eps) {
break; }
444 if (!Graph->IsFltAttrE(Attr))
return -1;
445 const int NNodes = Graph->GetNodes();
454 PRankH.
AddDat(NI.GetId(), 1.0/NNodes);
461 TFltV PRankV(MxId+1);
462 TFltV OutWeights(MxId+1);
464 TFltV Weights = Graph->GetFltAttrVecE(Attr);
466 #pragma omp parallel for schedule(dynamic,10000)
467 for (
int j = 0; j < NNodes; j++) {
470 OutWeights[Id] = Graph->GetWeightOutEdges(NI, Attr);
471 PRankV[Id] = 1/NNodes;
475 for (
int iter = 0; iter < MaxIter; iter++) {
477 #pragma omp parallel for schedule(dynamic,10000)
478 for (
int j = 0; j < NNodes; j++) {
481 for (
int e = 0; e < NI.
GetInDeg(); e++) {
484 const TFlt OutWeight = OutWeights[InNId];
486 int EId = Graph->GetEId(InNId, NI.
GetId());
487 const TFlt Weight = Weights[Graph->GetFltKeyIdE(EId)];
490 Tmp += PRankH.
GetDat(InNId) * Weight / OutWeight;
498 #pragma omp parallel for reduction(+:sum) schedule(dynamic,10000)
499 for (
int i = 0; i < TmpV.
Len(); i++) { sum += TmpV[i]; }
500 const double Leaked = (1.0-sum) /
double(NNodes);
503 #pragma omp parallel for reduction(+:diff) schedule(dynamic,10000)
504 for (
int i = 0; i < NNodes; i++) {
506 double NewVal = TmpV[i] + Leaked;
509 diff += fabs(NewVal-PRankV[Id]);
512 if (diff < Eps) {
break; }
515 #pragma omp parallel for schedule(dynamic,10000)
516 for (
int i = 0; i < NNodes; i++) {
518 PRankH[i] = PRankV[NI.
GetId()];
531 NodeList.
AddDat(NI.GetId(),NI.GetOutDeg());
536 int outdeg = Graph->
GetNI(NI.GetKey()).GetOutDeg();
537 int indeg = Graph->
GetNI(NI.GetKey()).GetInDeg();
539 if (outdeg>1 && indeg>0){
540 double val = (1-(1/(double)outdeg))/(
double)indeg;
541 for(
int i=0; i<(outdeg+indeg);i++){
542 int NId = Graph->
GetNI(NI.GetKey()).GetNbrNId(i);
543 if (Graph->
GetNI(NI.GetKey()).IsInNId(NId) ==
true){
560 NodeList.
AddDat(NI.GetId(),NI.GetOutDeg());
565 int outdeg = Graph->
GetNI(NI.GetKey()).GetOutDeg();
566 int indeg = Graph->
GetNI(NI.GetKey()).GetInDeg();
568 if (outdeg>1 && indeg>0){
569 double val = (1-(1/(double)outdeg))/(
double)indeg;
570 for(
int i=0; i<(outdeg+indeg);i++){
571 int NId = Graph->
GetNI(NI.GetKey()).GetNbrNId(i);
572 if (Graph->
GetNI(NI.GetKey()).IsInNId(NId) ==
true){
586 for (
int i=0; i<Node.
GetDeg(); i++)
603 for (
int i=0; i<Node.
GetDeg(); i++)
624 for (
int i=0; i<Node.
GetDeg(); i++)
627 for (
int j=0; j<NNodes_br; j++)
629 if (neig == NNodes[j])
638 for (
int j=0; j<NNodes_br; j++)
640 if (neig == NNodes[j])
652 for (
int i=0; i<Node.
GetDeg(); i++)
688 for (
int i = 0; i < Frontier.
Len(); i++) {
689 int NId = Frontier.
GetVal(i);
690 if (NIdDistH[NId] < minimum) {
691 minimum = NIdDistH[NId];
695 const int NId = Frontier.
GetVal(min_index);
696 Frontier.
Del(min_index);
704 NIdDistH.
Clr(
false); NIdDistH.
AddDat(SrcNId, 0);
705 frontier.
Add(SrcNId);
706 while (! frontier.
Empty()) {
708 const PNEANet::TObj::TNodeI NodeI = Graph->GetNI(NId);
709 for (
int v = 0; v < NodeI.GetOutDeg(); v++) {
710 int DstNId = NodeI.GetOutNId(v);
711 int EId = NodeI.GetOutEId(v);
713 if (! NIdDistH.
IsKey(DstNId)) {
714 NIdDistH.
AddDat(DstNId, NIdDistH.
GetDat(NId) + Attr[EId]);
715 frontier.
Add(DstNId);
717 if (NIdDistH[DstNId] > NIdDistH.
GetDat(NId) + Attr[EId]) {
718 NIdDistH[DstNId] = NIdDistH.
GetDat(NId) + Attr[EId];
735 if (NDistH.Len() > 1) {
736 double centr = sum/double(NDistH.Len()-1);
738 centr *= (Graph->GetNodes() - 1)/
double(NDistH.Len()-1);
747 if (Farness != 0.0) {
return 1.0/Farness; }
753 if (DoNodeCent) { NodeBtwH.
Clr(); }
754 if (DoEdgeCent) { EdgeBtwH.
Clr(); }
755 const int nodes = Graph->GetNodes();
762 for (PNEANet::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
764 NodeBtwH.
AddDat(NI.GetId(), 0); }
766 for (
int e = 0; e < NI.GetOutDeg(); e++) {
767 if (NI.GetId() < NI.GetOutNId(e)) {
768 EdgeBtwH.
AddDat(
TIntPr(NI.GetId(), NI.GetOutNId(e)), 0);
772 sigma.AddDat(NI.GetId(), 0);
775 delta.
AddDat(NI.GetId(), 0);
778 for (
int k=0; k < BtwNIdV.
Len(); k++) {
779 const PNEANet::TObj::TNodeI NI = Graph->GetNI(BtwNIdV[k]);
781 for (
int i = 0; i < sigma.Len(); i++) {
782 sigma[i]=0; d[i]=-1; delta[i]=0; P[i].
Clr(
false);
786 sigma.AddDat(NI.GetId(), 1);
789 while (! Q.
Empty()) {
790 const int v = Q.
Top(); Q.
Pop();
791 const PNEANet::TObj::TNodeI NI2 = Graph->GetNI(v);
793 const double VDat = d.
GetDat(v);
794 for (
int e = 0; e < NI2.GetOutDeg(); e++) {
795 const int w = NI2.GetOutNId(e);
796 const int eid = NI2.GetOutEId(e);
800 d.
AddDat(w, VDat+Attr[eid]);
803 if (d.
GetDat(w) == VDat+Attr[eid]) {
804 sigma.AddDat(w) += sigma.GetDat(v);
810 while (! S.
Empty()) {
811 const int w = S.
Top();
812 const double SigmaW = sigma.GetDat(w);
813 const double DeltaW = delta.
GetDat(w);
816 for (
int i = 0; i < NIdV.
Len(); i++) {
817 const int NId = NIdV[i];
818 const double c = (sigma.GetDat(NId)*1.0/SigmaW) * (1+DeltaW);
823 double factor = (IsDir) ? 1.0 : 2.0;
824 if (DoNodeCent && w != NI.GetId()) {
831 TIntV NIdV; Graph->GetNIdV(NIdV);
832 if (NodeFrac < 1.0) {
834 for (
int i =
int((1.0-NodeFrac)*NIdV.
Len()); i > 0; i--) {
843 TIntV NIdV; Graph->GetNIdV(NIdV);
844 if (NodeFrac < 1.0) {
846 for (
int i =
int((1.0-NodeFrac)*NIdV.
Len()); i > 0; i--) {
854 TIntV NIdV; Graph->GetNIdV(NIdV);
855 if (NodeFrac < 1.0) {
857 for (
int i =
int((1.0-NodeFrac)*NIdV.
Len()); i > 0; i--) {
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)
TIntH * AllCombinationsMN(int m, int n)
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
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)
void Del(const TSizeTy &ValN)
Removes the element at position ValN.
int GetWeightedShortestPath(const PNEANet Graph, const int &SrcNId, TIntFltH &NIdDistH, const TFltV &Attr)
static const T & Mx(const T &LVal, const T &RVal)
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
int GetWeightedPageRankMP(const PNEANet Graph, TIntFltH &PRankH, const TStr &Attr, const double &C, const double &Eps, const int &MaxIter)
int GetInNId(const int &EdgeN) const
Returns ID of EdgeN-th in-node (the node pointing to the current node).
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.
int findMinimum(TIntV &Frontier, TIntFltH &NIdDistH)
bool GetInt(const int &FldN, int &Val) const
If the field FldN is an integer its value is returned in Val and the function returns true...
void Clr(const bool &DoDel=false)
const TDat & GetDat(const TKey &Key) const
Node iterator. Only forward iteration (operator++) is supported.
TIntFltH EventImportance(const PNGraph &Graph, const int k)
Event importance.
bool Empty() const
Tests whether the vector is empty.
int GetDeg() const
Returns degree of the current node.
void DelKey(const TKey &Key)
int GetId() const
Returns ID of the current node.
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
void Gen(const int &ExpectVals)
TIntH MaxCPGreedyBetter(const PUNGraph &Graph, const int k)
Returns centrality Maximum k group.
int SearchStr(const TStr &Str, const int &BChN=0) const
double GetGroupFarnessCentr(const PUNGraph &Graph, const TIntH &GroupNodes)
Whitespace (space or tab) separated.
double GetGroupClosenessCentr(const PUNGraph &Graph, const TIntH &GroupNodes)
int GetInDeg() const
Returns in-degree of the current node.
double GetGroupDegreeCentr0(const PUNGraph &Graph, const TIntH &GroupNodes)
TIntH MaxCPGreedyBetter1(const PUNGraph &Graph, const int k)
Returns centrality Maximum k group.
int Intersect1(TUNGraph::TNodeI Node, TStr NNodes)
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
TIntFltH EventImportance1(const PNGraph &Graph, const int k)
double GetWeightedFarnessCentr(const PNEANet Graph, const int &NId, const bool &IsDir, const TFltV &Attr, const bool &Normalized)
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Node iterator. Only forward iteration (operator++) is supported.
TIntH MaxCPGreedyBetter2(const PUNGraph &Graph, const int k)
Returns centrality Maximum k group.
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
bool Next()
Loads next line from the input file.
void Push(const TVal &Val)
TIntH LoadNodeList(TStr InFNmNodes)
void Clr(const bool &DoDel=true)
int GetId() const
Returns ID of the current node.
bool IsKey(const TKey &Key) const
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
bool IsStrIn(const TStr &Str) const
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.
const TKey & GetKey(const int &KeyId) const
PUNGraph * AllGraphsWithNNodes(int n)
void SortByDat(const bool &Asc=true)