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++) {
769 EdgeBtwH.
AddDat(
TIntPr(NI.GetId(), NI.GetOutNId(e)), 0);
772 if (NI.GetId() < NI.GetOutNId(e)) {
773 EdgeBtwH.
AddDat(
TIntPr(NI.GetId(), NI.GetOutNId(e)), 0);
779 for (
int e = 0; e < NI.GetInDeg(); e++) {
780 if (NI.GetId() < NI.GetInNId(e) &&
781 !Graph->IsEdge(NI.GetId(), NI.GetInNId(e))) {
787 sigma.AddDat(NI.GetId(), 0);
790 delta.
AddDat(NI.GetId(), 0);
793 for (
int k=0; k < BtwNIdV.
Len(); k++) {
794 const PNEANet::TObj::TNodeI NI = Graph->GetNI(BtwNIdV[k]);
796 for (
int i = 0; i < sigma.Len(); i++) {
797 sigma[i]=0; d[i]=-1; delta[i]=0; P[i].
Clr(
false);
801 sigma.AddDat(NI.GetId(), 1);
804 while (! Q.
Empty()) {
805 const int v = Q.
Top(); Q.
Pop();
806 const PNEANet::TObj::TNodeI NI2 = Graph->GetNI(v);
808 const double VDat = d.
GetDat(v);
810 for (
int e = 0; e < NI2.GetOutDeg(); e++) {
811 const int w = NI2.GetOutNId(e);
812 const int eid = NI2.GetOutEId(e);
816 d.
AddDat(w, VDat+Attr[eid]);
819 if (d.
GetDat(w) == VDat+Attr[eid]) {
820 sigma.AddDat(w) += sigma.GetDat(v);
826 for (
int e = 0; e < NI2.GetInDeg(); e++) {
827 const int w = NI2.GetInNId(e);
829 if (Graph->IsEdge(NI2.GetId(), w)) {
832 const int eid = NI2.GetInEId(e);
836 d.
AddDat(w, VDat+Attr[eid]);
839 if (d.
GetDat(w) == VDat+Attr[eid]) {
840 sigma.AddDat(w) += sigma.GetDat(v);
847 while (! S.
Empty()) {
848 const int w = S.
Top();
849 const double SigmaW = sigma.GetDat(w);
850 const double DeltaW = delta.
GetDat(w);
853 for (
int i = 0; i < NIdV.
Len(); i++) {
854 const int NId = NIdV[i];
855 const double c = (sigma.GetDat(NId)*1.0/SigmaW) * (1+DeltaW);
865 if (DoNodeCent && w != NI.GetId()) {
872 TIntV NIdV; Graph->GetNIdV(NIdV);
873 if (NodeFrac < 1.0) {
875 for (
int i =
int((1.0-NodeFrac)*NIdV.
Len()); i > 0; i--) {
884 TIntV NIdV; Graph->GetNIdV(NIdV);
885 if (NodeFrac < 1.0) {
887 for (
int i =
int((1.0-NodeFrac)*NIdV.
Len()); i > 0; i--) {
896 TIntV NIdV; Graph->GetNIdV(NIdV);
897 if (NodeFrac < 1.0) {
899 for (
int i =
int((1.0-NodeFrac)*NIdV.
Len()); i > 0; i--) {
910 const double& C = 0.85,
const double& Eps = 1e-4,
const int& MaxIter = 100) {
920 const int& MaxIter = 20) {
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.
Node iterator. Only forward iteration (operator++) is supported.
int findMinimum(TIntV &Frontier, TIntFltH &NIdDistH)
Iterator over a vector of tables.
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.
void MapHits(const TVec< PGraph > &GraphSeq, TVec< PTable > &TableSeq, TTableContext *Context, const int &MaxIter)
Gets sequence of Hits tables from given GraphSeq into TableSeq.
int GetDeg() const
Returns degree of the current node.
void DelKey(const TKey &Key)
int GetId() const
Returns ID of the current node.
TTableIterator GetMapPageRank(const TVec< PNEANet > &GraphSeq, TTableContext *Context, const double &C=0.85, const double &Eps=1e-4, const int &MaxIter=100)
Gets sequence of PageRank tables from given GraphSeq.
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
TTableIterator GetMapHitsIterator(const TVec< PNEANet > &GraphSeq, TTableContext *Context, const int &MaxIter=20)
Gets sequence of Hits tables from given GraphSeq.
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.
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)
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)
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)
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
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.
TIntFltH EventImportance1(const PNGraph &Graph, const int k)
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)