SNAP Library 2.4, User Reference  2015-05-11 19:40:56
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.cpp
Go to the documentation of this file.
1 namespace TSnap {
2 
4 // Node centrality measures
5 double GetDegreeCentr(const PUNGraph& Graph, const int& NId) {
6  if (Graph->GetNodes() > 1) {
7  return double(Graph->GetNI(NId).GetDeg())/double(Graph->GetNodes()-1); }
8  else { return 0.0; }
9 }
10 
11 double GetFarnessCentr(const PUNGraph& Graph, const int& NId) {
12  TIntH NDistH(Graph->GetNodes());
13  TSnap::GetShortPath<PUNGraph>(Graph, NId, NDistH, true, TInt::Mx);
14  double sum = 0;
15  for (TIntH::TIter I = NDistH.BegI(); I < NDistH.EndI(); I++) {
16  sum += I->Dat();
17  }
18  if (NDistH.Len() > 1) { return sum/double(NDistH.Len()-1); }
19  else { return 0.0; }
20 }
21 
22 double GetClosenessCentr(const PUNGraph& Graph, const int& NId) {
23  const double Farness = GetFarnessCentr(Graph, NId);
24  if (Farness != 0.0) { return 1.0/Farness; }
25  else { return 0.0; }
26 }
27 
28 void GetBetweennessCentr(const PUNGraph& Graph, const TIntV& BtwNIdV, TIntFltH& NodeBtwH, const bool& DoNodeCent, TIntPrFltH& EdgeBtwH, const bool& DoEdgeCent) {
29  if (DoNodeCent) { NodeBtwH.Clr(); }
30  if (DoEdgeCent) { EdgeBtwH.Clr(); }
31  const int nodes = Graph->GetNodes();
32  TIntS S(nodes);
33  TIntQ Q(nodes);
34  TIntIntVH P(nodes); // one vector for every node
35  TIntFltH delta(nodes);
36  TIntH sigma(nodes), d(nodes);
37  // init
38  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
39  if (DoNodeCent) {
40  NodeBtwH.AddDat(NI.GetId(), 0); }
41  if (DoEdgeCent) {
42  for (int e = 0; e < NI.GetOutDeg(); e++) {
43  if (NI.GetId() < NI.GetOutNId(e)) {
44  EdgeBtwH.AddDat(TIntPr(NI.GetId(), NI.GetOutNId(e)), 0); }
45  }
46  }
47  sigma.AddDat(NI.GetId(), 0);
48  d.AddDat(NI.GetId(), -1);
49  P.AddDat(NI.GetId(), TIntV());
50  delta.AddDat(NI.GetId(), 0);
51  }
52  // calc betweeness
53  for (int k=0; k < BtwNIdV.Len(); k++) {
54  const TUNGraph::TNodeI NI = Graph->GetNI(BtwNIdV[k]);
55  // reset
56  for (int i = 0; i < sigma.Len(); i++) {
57  sigma[i]=0; d[i]=-1; delta[i]=0; P[i].Clr(false);
58  }
59  S.Clr(false);
60  Q.Clr(false);
61  sigma.AddDat(NI.GetId(), 1);
62  d.AddDat(NI.GetId(), 0);
63  Q.Push(NI.GetId());
64  while (! Q.Empty()) {
65  const int v = Q.Top(); Q.Pop();
66  const TUNGraph::TNodeI NI2 = Graph->GetNI(v);
67  S.Push(v);
68  const int VDat = d.GetDat(v);
69  for (int e = 0; e < NI2.GetOutDeg(); e++) {
70  const int w = NI2.GetOutNId(e);
71  if (d.GetDat(w) < 0) { // find w for the first time
72  Q.Push(w);
73  d.AddDat(w, VDat+1);
74  }
75  //shortest path to w via v ?
76  if (d.GetDat(w) == VDat+1) {
77  sigma.AddDat(w) += sigma.GetDat(v);
78  P.GetDat(w).Add(v);
79  }
80  }
81  }
82  while (! S.Empty()) {
83  const int w = S.Top();
84  const double SigmaW = sigma.GetDat(w);
85  const double DeltaW = delta.GetDat(w);
86  const TIntV NIdV = P.GetDat(w);
87  S.Pop();
88  for (int i = 0; i < NIdV.Len(); i++) {
89  const int nid = NIdV[i];
90  const double c = (sigma.GetDat(nid)*1.0/SigmaW) * (1+DeltaW);
91  delta.AddDat(nid) += c;
92  if (DoEdgeCent) {
93  EdgeBtwH.AddDat(TIntPr(TMath::Mn(nid, w), TMath::Mx(nid, w))) += c; }
94  }
95  if (DoNodeCent && w != NI.GetId()) {
96  NodeBtwH.AddDat(w) += delta.GetDat(w)/2.0; }
97  }
98  }
99 }
100 
101 // Approximate beetweenness centrality scores. The implementation scales to large networks.
102 // NodeFrac ... calculate Beetweenness Centrality for a fraction of nodes. Gives approximate.
103 void GetBetweennessCentr(const PUNGraph& Graph, TIntFltH& NodeBtwH, const double& NodeFrac) {
104  TIntPrFltH EdgeBtwH;
105  TIntV NIdV; Graph->GetNIdV(NIdV);
106  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
107  NIdV.Shuffle(TInt::Rnd);
108  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
109  NIdV.DelLast(); }
110  }
111  GetBetweennessCentr(Graph, NIdV, NodeBtwH, true, EdgeBtwH, false);
112 }
113 
114 void GetBetweennessCentr(const PUNGraph& Graph, TIntPrFltH& EdgeBtwH, const double& NodeFrac) {
115  TIntFltH NodeBtwH;
116  TIntV NIdV; Graph->GetNIdV(NIdV);
117  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
118  NIdV.Shuffle(TInt::Rnd);
119  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
120  NIdV.DelLast(); }
121  }
122  GetBetweennessCentr(Graph, NIdV, NodeBtwH, false, EdgeBtwH, true);
123 }
124 
125 void GetBetweennessCentr(const PUNGraph& Graph, TIntFltH& NodeBtwH, TIntPrFltH& EdgeBtwH, const double& NodeFrac) {
126  TIntV NIdV; Graph->GetNIdV(NIdV);
127  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
128  NIdV.Shuffle(TInt::Rnd);
129  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
130  NIdV.DelLast(); }
131  }
132  GetBetweennessCentr(Graph, NIdV, NodeBtwH, true, EdgeBtwH, true);
133 }
134 
135 void GetEigenVectorCentr(const PUNGraph& Graph, TIntFltH& NIdEigenH, const double& Eps, const int& MaxIter) {
136  const int NNodes = Graph->GetNodes();
137  NIdEigenH.Gen(NNodes);
138  // initialize vector values
139  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
140  NIdEigenH.AddDat(NI.GetId(), 1.0/NNodes);
141  IAssert(NI.GetId() == NIdEigenH.GetKey(NIdEigenH.Len()-1));
142  }
143  TFltV TmpV(NNodes);
144  for (int iter = 0; iter < MaxIter; iter++) {
145  int j = 0;
146  // add neighbor values
147  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
148  TmpV[j] = 0;
149  for (int e = 0; e < NI.GetOutDeg(); e++) {
150  TmpV[j] += NIdEigenH.GetDat(NI.GetOutNId(e)); }
151  }
152 
153  // normalize
154  double sum = 0;
155  for (int i = 0; i < TmpV.Len(); i++) {
156  sum += (TmpV[i]*TmpV[i]);
157  }
158  sum = sqrt(sum);
159  for (int i = 0; i < TmpV.Len(); i++) {
160  TmpV[i] /= sum;
161  }
162 
163  // compute difference
164  double diff = 0.0;
165  j = 0;
166  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
167  diff += fabs(NIdEigenH.GetDat(NI.GetId())-TmpV[j]);
168  }
169 
170  // set new values
171  j = 0;
172  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
173  NIdEigenH.AddDat(NI.GetId(), TmpV[j]);
174  }
175 
176  if (diff < Eps) {
177  break;
178  }
179  }
180 }
181 
182 // Group centrality measures
183 double GetGroupDegreeCentr(const PUNGraph& Graph, const PUNGraph& Group) {
184  int deg;
185  TIntH NN;
186  for (TUNGraph::TNodeI NI = Group->BegNI(); NI < Group->EndNI(); NI++) {
187  deg = Graph->GetNI(NI.GetId()).GetDeg();
188  for (int i=0; i<deg; i++) {
189  if (Group->IsNode(Graph->GetNI(NI.GetId()).GetNbrNId(i))==0)
190  NN.AddDat(Graph->GetNI(NI.GetId()).GetNbrNId(i),NI.GetId());
191  }
192  }
193  return (double)NN.Len();
194 }
195 
196 double GetGroupDegreeCentr0(const PUNGraph& Graph, const TIntH& GroupNodes) {
197  int deg;
198  TIntH NN;
199  for (int i = 0; i<GroupNodes.Len(); i++) {
200  deg = Graph->GetNI(GroupNodes.GetDat(i)).GetDeg();
201  for (int j = 0; j < deg; j++) {
202  if (GroupNodes.IsKey(Graph->GetNI(GroupNodes.GetDat(i)).GetNbrNId(j))==0)
203  NN.AddDat(Graph->GetNI(GroupNodes.GetDat(i)).GetNbrNId(j),GroupNodes.GetDat(i));
204  }
205  }
206  return (double)NN.Len();
207 }
208 
209 double GetGroupDegreeCentr(const PUNGraph& Graph, const TIntH& GroupNodes) {
210  int deg;
211  TIntH NN;
212  TIntH GroupNodes1;
213 
214  for (THashKeyDatI<TInt,TInt> NI = GroupNodes.BegI(); NI < GroupNodes.EndI(); NI++)
215  GroupNodes1.AddDat(NI.GetDat(),NI.GetDat());
216 
217  for (THashKeyDatI<TInt,TInt> NI = GroupNodes1.BegI(); NI < GroupNodes1.EndI(); NI++){
218  TUNGraph::TNodeI node = Graph->GetNI(NI.GetKey());
219  deg = node.GetDeg();
220  for (int j = 0; j < deg; j++){
221  if (GroupNodes1.IsKey(node.GetNbrNId(j))==0 && NN.IsKey(node.GetNbrNId(j))==0)
222  NN.AddDat(node.GetNbrNId(j),NI.GetKey());
223  }
224  }
225 
226  return (double)NN.Len();
227 }
228 
229 double GetGroupFarnessCentr(const PUNGraph& Graph, const TIntH& GroupNodes) {
230  TIntH* NDistH = new TIntH[GroupNodes.Len()];
231 
232  for (int i=0; i<GroupNodes.Len(); i++){
233  NDistH[i](Graph->GetNodes());
234  TSnap::GetShortPath<PUNGraph>(Graph, GroupNodes.GetDat(i), NDistH[i], true, TInt::Mx);
235  }
236 
237  int min, dist, sum=0, len=0;
238  for (PUNGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
239  if(NDistH[0].IsKey(NI.GetId()))
240  min = NDistH[0].GetDat(NI.GetId());
241  else
242  min = -1;
243  for (int j=1; j<GroupNodes.Len(); j++){
244  if (NDistH[j].IsKey(NI.GetId()))
245  dist = NDistH[j].GetDat(NI.GetId());
246  else
247  dist = -1;
248  if ((dist < min && dist != -1) || (dist > min && min == -1))
249  min = dist;
250  }
251  if (min>0){
252  sum += min;
253  len++;
254  }
255 
256  }
257 
258  if (len > 0) { return sum/double(len); }
259  else { return 0.0; }
260 }
261 
263  PUNGraph* g = new PUNGraph[(((n*n)-n)/2)+1];
264  PUNGraph g0;
265  for(int i=0; i<n; i++)
266  g0->AddNode(i);
267 
268  g[0] = g0;
269  int br=1;
270 
271  for(int i=0; i<n; i++)
272  for(int j=i; j<n; j++){
273  g0->AddEdge(i,j);
274  g[br] = g0;
275  br++;
276  }
277 
278  return g;
279 }
280 
281 TIntH *AllCombinationsMN(int m, int n){
282  float N = 1;
283  for(int i=n; i>0; i--){
284  N *= (float)m/(float)n;
285  m--;
286  n--;
287  }
288 
289  TIntH* C = new TIntH[(int)N];
290  return C;
291 }
292 
293 double GetGroupClosenessCentr(const PUNGraph& Graph, const TIntH& GroupNodes) {
294  const double Farness = GetGroupFarnessCentr(Graph, GroupNodes);
295  if (Farness != 0.0) { return 1.0/Farness; }
296  else { return 0.0; }
297 }
298 
299 TIntH MaxCPGreedyBetter(const PUNGraph& Graph, const int k) {
300  TIntH GroupNodes; // buildup cpntainer of group nodes
301  TIntH NNodes; // container of neighbouring nodes
302  TIntH Nodes; // nodes sorted by vd
303  double gc = 0, gc0 = 0;
304  int addId = 0, addIdPrev = 0;
305 
306  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
307  Nodes.AddDat(NI.GetId(),NI.GetDeg());
308  }
309 
310  Nodes.SortByDat(false);
311 
312  int br = 0;
313  while (br < k) {
314  for (THashKeyDatI<TInt,TInt> NI = Nodes.BegI(); NI < Nodes.EndI(); NI++) {
315  if ((NI.GetDat() <= (int)gc0))
316  break;
317  gc = NI.GetDat()-Intersect(Graph->GetNI(NI.GetKey()),NNodes);
318  if (gc>gc0) {
319  gc0 = gc;
320  addId = NI.GetKey();
321  }
322  }
323 
324  if (addId != addIdPrev){
325 
326  GroupNodes.AddDat(br,addId);
327  br++;
328  gc0=0;
329 
330  NNodes.AddDat(addId,0);
331  for (int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
332  NNodes.AddDat(Graph->GetNI(addId).GetNbrNId(i),0);
333  }
334  addIdPrev = addId;
335  Nodes.DelKey(addId);
336  } else {
337  br = k;
338  }
339  printf("%i,",br);
340  }
341 
342  // gcFinal = GetGroupDegreeCentr(Graph, GroupNodes);
343  return GroupNodes;
344 }
345 
346 // this is the variation of the first version that doesent stop after finding the optimal K
347 TIntH MaxCPGreedyBetter1(const PUNGraph& Graph, const int k) {
348  TIntH GroupNodes;
349  TIntH NNodes;
350  TIntH Nodes;
351  double gc = 0, gc0 = 0;
352  int addId = 0, addIdPrev = 0;
353 
354  // put nodes in the container and sort them by vertex degree
355  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
356  Nodes.AddDat(NI.GetId(),NI.GetDeg());
357  }
358  Nodes.SortByDat(false);
359 
360  int br = 0;
361  while (br < k) {
362  for (THashKeyDatI<TInt,TInt> NI = Nodes.BegI(); NI < Nodes.EndI(); NI++){
363  if((NI.GetDat() < (int)gc0))
364  break;
365  gc = NI.GetDat()-Intersect(Graph->GetNI(NI.GetKey()),NNodes);
366  if (gc>gc0) {
367  gc0 = gc;
368  addId = NI.GetKey();
369  }
370  }
371 
372  if (addId != addIdPrev){
373 
374  GroupNodes.AddDat(br,addId);
375  br++;
376  gc0=-10000000;
377 
378  NNodes.AddDat(addId,0);
379  for (int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
380  NNodes.AddDat(Graph->GetNI(addId).GetNbrNId(i),0);
381  }
382  addIdPrev = addId;
383  Nodes.DelKey(addId);
384  }
385  }
386 
387  // gcFinal = GetGroupDegreeCentr(Graph, GroupNodes);
388  return GroupNodes;
389 }
390 
391 // version with string type of container of group nodes - Fail (it is slower)
392 TIntH MaxCPGreedyBetter2(const PUNGraph& Graph, const int k) {
393  TIntH GroupNodes; // buildup cpntainer of group nodes
394  TStr NNodes; // container of neighbouring nodes
395  TIntH Nodes; // nodes sorted by vd
396  double gc = 0, gc0 = 0;
397  int addId = 0, addIdPrev=0;
398 
399  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
400  Nodes.AddDat(NI.GetId(),NI.GetDeg());
401  }
402 
403  Nodes.SortByDat(false);
404 
405  int br=0;
406  while (br < k) {
407  for (THashKeyDatI<TInt,TInt> NI = Nodes.BegI(); NI < Nodes.EndI(); NI++){
408  if((NI.GetDat() <= (int)gc0))
409  break;
410  gc = NI.GetDat()-Intersect(Graph->GetNI(NI.GetKey()),NNodes);
411  if (gc>gc0) {
412  gc0 = gc;
413  addId = NI.GetKey();
414  }
415  }
416 
417  if (addId != addIdPrev) {
418 
419  GroupNodes.AddDat(br,addId);
420  br++;
421  gc0=0;
422 
423  TInt digi = addId;
424  TStr buf = digi.GetStr();
425 
426  NNodes += " "+buf;
427 
428  for (int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
429  TInt digi = Graph->GetNI(addId).GetNbrNId(i);
430  TStr buf = digi.GetStr();
431  NNodes += " "+buf;
432  }
433  addIdPrev = addId;
434  Nodes.DelKey(addId);
435  } else {
436  br = k;
437  }
438  printf("%i,",br);
439  }
440 
441  // gcFinal = GetGroupDegreeCentr(Graph, GroupNodes);
442  return GroupNodes;
443 }
444 
445 // version with int array - the fastest
446 TIntH MaxCPGreedyBetter3(const PUNGraph& Graph, const int k) {
447  TIntH GroupNodes; // buildup cpntainer of group nodes
448  const int n = Graph->GetNodes();
449  int *NNodes = new int[n]; // container of neighbouring nodes
450  int NNodes_br = 0;
451  TIntH Nodes; // nodes sorted by vd
452  double gc = 0, gc0 = 0;
453  int addId = 0, addIdPrev = 0;
454 
455  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
456  Nodes.AddDat(NI.GetId(),NI.GetDeg());
457  }
458 
459  Nodes.SortByDat(false);
460 
461  int br = 0;
462  while (br < k) {
463  for (THashKeyDatI<TInt,TInt> NI = Nodes.BegI(); NI < Nodes.EndI(); NI++){
464  if((NI.GetDat() <= (int)gc0))
465  break;
466  gc = NI.GetDat()-Intersect(Graph->GetNI(NI.GetKey()),NNodes,NNodes_br);
467  if (gc>gc0){
468  gc0 = gc;
469  addId = NI.GetKey();
470  }
471  }
472 
473  if (addId != addIdPrev) {
474 
475  GroupNodes.AddDat(br,addId);
476  br++;
477  gc0=0;
478 
479  int nn = addId;
480  bool nnnew = true;
481  for (int j=0; j<NNodes_br; j++)
482  if (NNodes[j] == nn){
483  nnnew = false;
484  j = NNodes_br;
485  }
486 
487  if (nnnew){
488  NNodes[NNodes_br] = nn;
489  NNodes_br++;
490  }
491 
492  for (int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
493  int nn = Graph->GetNI(addId).GetNbrNId(i);
494  bool nnnew = true;
495  for (int j=0; j<NNodes_br; j++) {
496  if (NNodes[j] == nn){
497  nnnew = false;
498  j = NNodes_br;
499  }
500  }
501  if (nnnew){
502  NNodes[NNodes_br] = nn;
503  NNodes_br++;
504  }
505  }
506  addIdPrev = addId;
507  Nodes.DelKey(addId);
508  } else {
509  br = k;
510  }
511  printf("%i,",br);
512  }
513 
514  delete NNodes;
515  // gcFinal = GetGroupDegreeCentr(Graph, GroupNodes);
516  return GroupNodes;
517 }
518 
519 //Event importance
520 TIntFltH EventImportance(const PNGraph& Graph, const int k) {
521  TIntFltH NodeList; // values for nodese
522 
523  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
524  NodeList.AddDat(NI.GetId(),NI.GetOutDeg());
525  }
526 
527 
528  for (THashKeyDatI<TInt,TFlt> NI = NodeList.BegI(); NI < NodeList.EndI(); NI++){
529  int outdeg = Graph->GetNI(NI.GetKey()).GetOutDeg();
530  int indeg = Graph->GetNI(NI.GetKey()).GetInDeg();
531 
532  if (outdeg>1 && indeg>0){
533  double val = (1-(1/(double)outdeg))/(double)indeg;
534  for(int i=0; i<(outdeg+indeg);i++){
535  int nid = Graph->GetNI(NI.GetKey()).GetNbrNId(i);
536  if (Graph->GetNI(NI.GetKey()).IsInNId(nid) == true){
537  NodeList.AddDat(nid,NodeList.GetDat(nid)+val);
538  }
539 
540  }
541  }
542 
543  }
544 
545  return NodeList;
546 }
547 
548 //Event importance 1
549 TIntFltH EventImportance1 (const PNGraph& Graph, const int k) {
550  TIntFltH NodeList; // values for nodese
551 
552  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
553  NodeList.AddDat(NI.GetId(),NI.GetOutDeg());
554  }
555 
556 
557  for (THashKeyDatI<TInt,TFlt> NI = NodeList.BegI(); NI < NodeList.EndI(); NI++){
558  int outdeg = Graph->GetNI(NI.GetKey()).GetOutDeg();
559  int indeg = Graph->GetNI(NI.GetKey()).GetInDeg();
560 
561  if (outdeg>1 && indeg>0){
562  double val = (1-(1/(double)outdeg))/(double)indeg;
563  for(int i=0; i<(outdeg+indeg);i++){
564  int nid = Graph->GetNI(NI.GetKey()).GetNbrNId(i);
565  if (Graph->GetNI(NI.GetKey()).IsInNId(nid) == true){
566  NodeList.AddDat(nid,NodeList.GetDat(nid)+val);
567  }
568 
569  }
570  }
571 
572  }
573 
574  return NodeList;
575 }
576 
577 int Intersect(TUNGraph::TNodeI Node, TIntH NNodes){
578  int br=0;
579  for (int i=0; i<Node.GetDeg(); i++)
580  {
581  if (NNodes.IsKey(Node.GetNbrNId(i)))
582  br++;
583  }
584  if (NNodes.IsKey(Node.GetId()))
585  br++;
586 
587  return br;
588 }
589 
590 int Intersect(TUNGraph::TNodeI Node, TStr NNodes){
591  int br=0;
592 
593  TInt digi = -1;
594  TStr buf = "";
595 
596  for (int i=0; i<Node.GetDeg(); i++)
597  {
598  digi = Node.GetNbrNId(i);
599  TStr buf = digi.GetStr();
600 
601  if (NNodes.IsStrIn(buf.CStr()))
602  br++;
603  }
604 
605  digi = Node.GetId();
606  buf = digi.GetStr();
607 
608  if (NNodes.IsStrIn(buf.CStr()))
609  br++;
610 
611  return br;
612 }
613 
614 int Intersect(TUNGraph::TNodeI Node, int *NNodes, int NNodes_br){
615  int br = 0;
616  int neig;
617  for (int i=0; i<Node.GetDeg(); i++)
618  {
619  neig = Node.GetNbrNId(i);
620  for (int j=0; j<NNodes_br; j++)
621  {
622  if (neig == NNodes[j])
623  {
624  br++;
625  j = NNodes_br;
626  }
627  }
628  }
629 
630  neig = Node.GetId();
631  for (int j=0; j<NNodes_br; j++)
632  {
633  if (neig == NNodes[j])
634  {
635  br++;
636  j = NNodes_br;
637  }
638  }
639 
640  return br;
641 }
642 
643 int Intersect1(TUNGraph::TNodeI Node, TStr NNodes){
644  int br=0;
645  for (int i=0; i<Node.GetDeg(); i++)
646  {
647  TInt digi = Node.GetNbrNId(i);
648  TStr buf = "";
649  buf = digi.GetStr();
650 
651  if (NNodes.SearchStr(buf.CStr())!=-1)
652  br++;
653  }
654 
655  TInt digi = Node.GetId();
656  TStr buf = digi.GetStr();
657 
658  if (NNodes.SearchStr(buf.CStr())!=-1)
659  br++;
660 
661  return br;
662 }
663 
664 TIntH LoadNodeList(TStr InFNmNodes){
665  TSsParser Ss(InFNmNodes, ssfWhiteSep, true, true, true);
666  TIntIntH Nodes;
667  int br = 0, NId;
668  while (Ss.Next()) {
669  if (Ss.GetInt(0, NId)) {
670  Nodes.AddDat(br,NId);
671  br++;
672  }
673  }
674  return Nodes;
675 }
676 
677 
678 }; // namespace TSnap
double GetGroupDegreeCentr(const PUNGraph &Graph, const PUNGraph &Group)
Definition: centr.cpp:183
#define IAssert(Cond)
Definition: bd.h:262
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
bool Empty()
Definition: ds.h:2477
TStr GetStr() const
Definition: dt.h:1105
double GetDegreeCentr(const PUNGraph &Graph, const int &NId)
Definition: centr.cpp:5
TIntH * AllCombinationsMN(int m, int n)
Definition: centr.cpp:281
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:463
int Intersect(TUNGraph::TNodeI Node, TIntH NNodes)
Intersect.
Definition: centr.cpp:577
bool Empty() const
Definition: ds.h:2532
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:467
TVal & Top()
Definition: ds.h:2481
static const int Mx
Definition: dt.h:1047
TIter BegI() const
Definition: hash.h:171
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
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:63
Definition: ss.h:72
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...
Definition: ss.cpp:443
void Clr(const bool &DoDel=false)
Definition: ds.h:2478
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
TIntFltH EventImportance(const PNGraph &Graph, const int k)
Event importance.
Definition: centr.cpp:520
TIter EndI() const
Definition: hash.h:176
static TRnd Rnd
Definition: dt.h:1051
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:85
void Pop()
Definition: ds.h:2536
void DelKey(const TKey &Key)
Definition: hash.h:358
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:89
void Gen(const int &ExpectVals)
Definition: hash.h:180
TIntH MaxCPGreedyBetter(const PUNGraph &Graph, const int k)
Returns centrality Maximum k group.
Definition: centr.cpp:299
int SearchStr(const TStr &Str, const int &BChN=0) const
Definition: dt.cpp:1065
double GetGroupFarnessCentr(const PUNGraph &Graph, const TIntH &GroupNodes)
Definition: centr.cpp:229
Whitespace (space or tab) separated.
Definition: ss.h:11
double GetGroupClosenessCentr(const PUNGraph &Graph, const TIntH &GroupNodes)
Definition: centr.cpp:293
double GetClosenessCentr(const PUNGraph &Graph, const int &NId)
Definition: centr.cpp:22
double GetGroupDegreeCentr0(const PUNGraph &Graph, const TIntH &GroupNodes)
Definition: centr.cpp:196
const TVal & Top() const
Definition: ds.h:2534
TIntH MaxCPGreedyBetter1(const PUNGraph &Graph, const int k)
Returns centrality Maximum k group.
Definition: centr.cpp:347
Definition: dt.h:1042
int Intersect1(TUNGraph::TNodeI Node, TStr NNodes)
Definition: centr.cpp:643
void Push()
Definition: ds.h:2483
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:465
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:99
void Pop()
Definition: ds.h:2485
Definition: dt.h:412
TIntFltH EventImportance1(const PNGraph &Graph, const int k)
Definition: centr.cpp:549
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1235
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:327
TIntH MaxCPGreedyBetter2(const PUNGraph &Graph, const int k)
Returns centrality Maximum k group.
Definition: centr.cpp:392
double GetFarnessCentr(const PUNGraph &Graph, const int &NId)
Definition: centr.cpp:11
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:104
TVec< TInt > TIntV
Definition: ds.h:1482
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:315
bool Next()
Loads next line from the input file.
Definition: ss.cpp:410
Definition: bd.h:196
void Push(const TVal &Val)
Definition: ds.h:2539
TIntH LoadNodeList(TStr InFNmNodes)
Definition: centr.cpp:664
void Clr(const bool &DoDel=true)
Definition: ds.h:2521
char * CStr()
Definition: dt.h:476
int GetId() const
Returns ID of the current node.
Definition: graph.h:83
bool IsKey(const TKey &Key) const
Definition: hash.h:216
bool IsStrIn(const TStr &Str) const
Definition: dt.h:555
void DelLast()
Removes the last element of the vector.
Definition: ds.h:609
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
TIntH MaxCPGreedyBetter3(const PUNGraph &Graph, const int k)
Returns centrality Maximum k group.
Definition: centr.cpp:446
THKeyDat * EndI
Definition: hash.h:45
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
PUNGraph * AllGraphsWithNNodes(int n)
Definition: centr.cpp:262
#define min(a, b)
Definition: bd.h:346
void SortByDat(const bool &Asc=true)
Definition: hash.h:246