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
TTimeNet Class Reference

#include <timenet.h>

Inherits TNodeNet< TSecTm >.

Classes

class  TTmBucket
 

Public Types

typedef TNodeNet< TSecTmTNet
 
typedef TPt< TNodeNet< TSecTm > > PNet
 
typedef TVec< TTmBucketTTmBucketV
 
- Public Types inherited from TNodeNet< TSecTm >
typedef TSecTm TNodeDat
 
typedef TNodeNet< TSecTmTNet
 
typedef TPt< TNetPNet
 

Public Member Functions

 TTimeNet ()
 
 TTimeNet (const int &Nodes, const int &Edges)
 
 TTimeNet (const TTimeNet &TimeNet)
 
 TTimeNet (TSIn &SIn)
 
void Save (TSOut &SOut) const
 Saves the network to a (binary) stream SOut. More...
 
TTimeNetoperator= (const TTimeNet &TimeNet)
 
PTimeNet GetSubGraph (const TIntV &NIdV) const
 
PTimeNENet GetTimeNENet () const
 
void GetNIdByTm (TIntV &NIdV) const
 
void GetTmBuckets (const TTmUnit &GroupBy, TTmBucketV &TmBucketV) const
 
void GetNodeBuckets (const int NodesPerBucket, TTimeNet::TTmBucketV &TmBucketV) const
 
PGStatVec TimeGrowth (const TTmUnit &TmUnit, const TFSet &TakeStat, const TSecTm &StartTm) const
 
void PlotEffDiam (const TStr &FNmPref, const TStr &Desc, const TTmUnit &GroupBy, const TSecTm &StartTm, const int &NDiamRuns=10, const bool &OnlyWcc=false, const bool &AlsoRewire=false) const
 
void PlotMissingPast (const TStr &FNmPref, const TStr &Desc, const TTmUnit &GroupBy, const TSecTm &DelPreTmEdges, const TSecTm &PostTmDiam) const
 
void PlotCCfOverTm (const TStr &FNmPref, TStr Desc, const TTmUnit &TmUnit, const int &NodesBucket=-1) const
 
void PlotMedianDegOverTm (const TStr &FNmPref, const TTmUnit &TmUnit, const int &NodesPerBucket=-1) const
 
- Public Member Functions inherited from TNodeNet< TSecTm >
const TNode & GetNode (const int &NId) const
 Returns node element for the node of ID NId in the network. More...
 
 TNodeNet ()
 
 TNodeNet (const int &Nodes, const int &Edges)
 Constructor that reserves enough memory for a network of Nodes nodes and Edges edges. More...
 
 TNodeNet (const TNodeNet &NodeNet)
 
 TNodeNet (TSIn &SIn)
 Constructor that loads the network from a (binary) stream SIn. More...
 
virtual ~TNodeNet ()
 
bool HasFlag (const TGraphFlag &Flag) const
 Allows for run-time checking the type of the network (see the TGraphFlag for flags). More...
 
TNodeNetoperator= (const TNodeNet &NodeNet)
 
int GetNodes () const
 Returns the number of nodes in the network. More...
 
int AddNode (int NId=-1)
 Adds a node of ID NId to the network. More...
 
int AddNode (int NId, const TSecTm &NodeDat)
 Adds a node of ID NId and node data NodeDat to the network. More...
 
int AddNode (const TNodeI &NodeI)
 Adds a node NodeI and its node data to the network. More...
 
void DelNode (const int &NId)
 Deletes node of ID NId from the network. More...
 
void DelNode (const TNode &NodeI)
 Deletes node of ID NodeI.GetId() from the network. More...
 
bool IsNode (const int &NId) const
 Tests whether ID NId is a node. More...
 
TNodeI BegNI () const
 Returns an iterator referring to the first node in the network. More...
 
TNodeI EndNI () const
 Returns an iterator referring to the past-the-end node in the network. More...
 
TNodeI GetNI (const int &NId) const
 Returns an iterator referring to the node of ID NId in the network. More...
 
void SetNDat (const int &NId, const TSecTm &NodeDat)
 Sets node data for the node of ID NId in the network. More...
 
TSecTmGetNDat (const int &NId)
 Returns node data for the node of ID NId in the network. More...
 
const TSecTmGetNDat (const int &NId) const
 Returns node data for the node of ID NId in the network. More...
 
int GetMxNId () const
 Returns an ID that is larger than any node ID in the network. More...
 
int GetEdges () const
 Returns the number of edges in the network. More...
 
int AddEdge (const int &SrcNId, const int &DstNId)
 Adds an edge from node IDs SrcNId to node DstNId to the network. More...
 
int AddEdge (const TEdgeI &EdgeI)
 Adds an edge from EdgeI.GetSrcNId() to EdgeI.GetDstNId() to the network. More...
 
void DelEdge (const int &SrcNId, const int &DstNId, const bool &IsDir=true)
 Deletes an edge from node IDs SrcNId to DstNId from the network. More...
 
bool IsEdge (const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
 Tests whether an edge from node IDs SrcNId to DstNId exists in the network. More...
 
TEdgeI BegEI () const
 Returns an iterator referring to the first edge in the network. More...
 
TEdgeI EndEI () const
 Returns an iterator referring to the past-the-end edge in the network. More...
 
TEdgeI GetEI (const int &EId) const
 Not supported/implemented! More...
 
TEdgeI GetEI (const int &SrcNId, const int &DstNId) const
 Returns an iterator referring to edge (SrcNId, DstNId) in the network. More...
 
int GetRndNId (TRnd &Rnd=TInt::Rnd)
 Returns an ID of a random node in the network. More...
 
TNodeI GetRndNI (TRnd &Rnd=TInt::Rnd)
 Returns an interator referring to a random node in the network. More...
 
void GetNIdV (TIntV &NIdV) const
 Gets a vector IDs of all nodes in the network. More...
 
bool Empty () const
 Tests whether the network is empty (has zero nodes). More...
 
void Clr (const bool &DoDel=true, const bool &ResetDat=true)
 Deletes all nodes and edges from the network. More...
 
void Reserve (const int &Nodes, const int &Edges)
 Reserves memory for a network of Nodes nodes and Edges edges. More...
 
void SortNIdById (const bool &Asc=true)
 Sorts nodes by node IDs. More...
 
void SortNIdByDat (const bool &Asc=true)
 Sorts nodes by node data. More...
 
void Defrag (const bool &OnlyNodeLinks=false)
 Defragments the network. More...
 
bool IsOk (const bool &ThrowExcept=true) const
 Checks the network data structure for internal consistency. More...
 

Static Public Member Functions

static PTimeNet New ()
 
static PTimeNet New (const int &Nodes, const int &Edges)
 
static PTimeNet Load (TSIn &SIn)
 
static PTimeNet LoadBipartite (const TStr &InFNm)
 
static PTimeNet LoadArxiv (const TStr &PaperFNm, const TStr &CiteFNm)
 
static PTimeNet LoadPatents (const TStr &PatentFNm, const TStr &CiteFNm)
 
static PTimeNet LoadAmazon (const TStr &StlFNm)
 
- Static Public Member Functions inherited from TNodeNet< TSecTm >
static PNet New ()
 Static constructor that returns a pointer to the network. Call: TPt <TNodeNet<TNodeData> > Net = TNodeNet<TNodeData>::New(). More...
 
static PNet Load (TSIn &SIn)
 Static constructor that loads the network from a stream SIn and returns a pointer to it. More...
 

Friends

class TPt< TTimeNet >
 

Additional Inherited Members

- Protected Member Functions inherited from TNodeNet< TSecTm >
TNode & GetNode (const int &NId)
 
- Protected Attributes inherited from TNodeNet< TSecTm >
TCRef CRef
 
TInt MxNId
 
THash< TInt, TNode > NodeH
 

Detailed Description

Definition at line 16 of file timenet.h.

Member Typedef Documentation

Definition at line 19 of file timenet.h.

Definition at line 18 of file timenet.h.

Definition at line 32 of file timenet.h.

Constructor & Destructor Documentation

TTimeNet::TTimeNet ( )
inline

Definition at line 34 of file timenet.h.

34 { }
TTimeNet::TTimeNet ( const int &  Nodes,
const int &  Edges 
)
inline

Definition at line 35 of file timenet.h.

35 : TNet(Nodes, Edges) { }
TNodeNet< TSecTm > TNet
Definition: timenet.h:18
TTimeNet::TTimeNet ( const TTimeNet TimeNet)
inline

Definition at line 36 of file timenet.h.

36 : TNet(TimeNet) { }
TNodeNet< TSecTm > TNet
Definition: timenet.h:18
TTimeNet::TTimeNet ( TSIn SIn)
inline

Definition at line 37 of file timenet.h.

37 : TNet(SIn) { }
TNodeNet< TSecTm > TNet
Definition: timenet.h:18

Member Function Documentation

void TTimeNet::GetNIdByTm ( TIntV NIdV) const

Definition at line 48 of file timenet.cpp.

48  {
49  TVec<TKeyDat<TSecTm, TInt> > TmToNIdV(GetNodes(), 0);
50  for (TNodeI NodeI = BegNI(); NodeI < EndNI(); NodeI++) {
51  TmToNIdV.Add(TKeyDat<TSecTm, TInt>(NodeI.GetDat(), NodeI.GetId())); }
52  TmToNIdV.Sort();
53  NIdV.Gen(GetNodes(), 0);
54  for (int i = 0; i < TmToNIdV.Len(); i++) {
55  NIdV.Add(TmToNIdV[i].Dat); }
56 }
Definition: ds.h:336
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:183
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:157
TNodeI BegNI() const
Returns an iterator referring to the first node in the network.
Definition: network.h:181
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:486
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:420
void TTimeNet::GetNodeBuckets ( const int  NodesPerBucket,
TTimeNet::TTmBucketV TmBucketV 
) const

Definition at line 77 of file timenet.cpp.

77  {
78  TIntV NIdV;
79  GetNIdByTm(NIdV);
80  TmBucketV.Gen(NIdV.Len() / NodesPerBucket + 1, 0);
81  for (int i = 0; i < NIdV.Len(); i++) {
82  const int b = i/NodesPerBucket;
83  if (TmBucketV.Len() <= b) { TmBucketV.Add(TTimeNet::TTmBucket(TSecTm(b))); }
84  TmBucketV[b].NIdV.Add(NIdV[i]);
85  }
86 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
Definition: tm.h:81
void GetNIdByTm(TIntV &NIdV) const
Definition: timenet.cpp:48
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:486
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
PTimeNet TTimeNet::GetSubGraph ( const TIntV NIdV) const

Definition at line 10 of file timenet.cpp.

10  {
11  PTimeNet NewNetPt = TTimeNet::New();
12  TTimeNet& NewNet = *NewNetPt;
13  NewNet.Reserve(NIdV.Len(), -1);
14  int node, edge;
15  TNodeI NI;
16  for (node = 0; node < NIdV.Len(); node++) {
17  NewNet.AddNode(NIdV[node], GetNDat(NIdV[node])); // also copy the node data
18  }
19  for (node = 0; node < NIdV.Len(); node++) {
20  NI = GetNI(NIdV[node]);
21  const int SrcNId = NI.GetId();
22  for (edge = 0; edge < NI.GetOutDeg(); edge++) {
23  const int OutNId = NI.GetOutNId(edge);
24  if (NewNet.IsNode(OutNId)) {
25  NewNet.AddEdge(SrcNId, OutNId); }
26  }
27  }
28  NewNet.Defrag();
29  return NewNetPt;
30 }
static PTimeNet New()
Definition: timenet.h:39
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
TSecTm & GetNDat(const int &NId)
Returns node data for the node of ID NId in the network.
Definition: network.h:191
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:179
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the network.
Definition: network.h:185
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the network.
Definition: network.h:331
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.h:273
Definition: bd.h:196
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the network.
Definition: network.h:373
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a network of Nodes nodes and Edges edges.
Definition: network.h:240
PTimeNENet TTimeNet::GetTimeNENet ( ) const

Definition at line 32 of file timenet.cpp.

32  {
33  TIntV NIdV; GetNIdByTm(NIdV);
35  for (int i = 0; i < NIdV.Len(); i++) {
36  const int Src = NIdV[i];
37  const TTimeNet::TNodeI NI = GetNI(Src);
38  const TSecTm SrcTm = NI.GetDat();
39  if (! OutNet->IsNode(Src)) { OutNet->AddNode(Src, SrcTm); }
40  for (int e = 0; e < NI.GetOutDeg(); e++) {
41  if (! OutNet->IsNode(NI.GetOutNId(e))) { OutNet->AddNode(NI.GetOutNId(e), SrcTm); }
42  OutNet->AddEdge(Src, NI.GetOutNId(e), -1, SrcTm);
43  }
44  }
45  return OutNet;
46 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
int GetEdges() const
Returns the number of edges in the network.
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:157
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the network.
Definition: network.h:185
static PTimeNENet New()
Definition: timenet.h:86
Definition: tm.h:81
void GetNIdByTm(TIntV &NIdV) const
Definition: timenet.cpp:48
Definition: bd.h:196
void TTimeNet::GetTmBuckets ( const TTmUnit GroupBy,
TTmBucketV TmBucketV 
) const

Definition at line 59 of file timenet.cpp.

59  {
60  THash<TInt, TIntV> TmIdToNIdVH;
61  for (TNodeI NodeI = BegNI(); NodeI < EndNI(); NodeI++) {
62  const int TmId = NodeI().Round(TmUnit);
63  if (! TmIdToNIdVH.IsKey(TmId)) TmIdToNIdVH.AddKey(TmId);
64  TmIdToNIdVH.GetDat(TmId).Add(NodeI.GetId());
65  }
66  TVec<TPair<TInt, TIntV> > TmIdNIdVV;
67  TmIdToNIdVH.GetKeyDatPrV(TmIdNIdVV);
68  TmIdNIdVV.Sort();
69  TmBucketV.Gen(TmIdNIdVV.Len());
70  for (int i = 0; i < TmIdNIdVV.Len(); i++) {
71  TTmBucket& Bucket = TmBucketV[i];
72  Bucket.BegTm = TmIdNIdVV[i].Val1;
73  Bucket.NIdV = TmIdNIdVV[i].Val2;
74  }
75 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:183
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
TNodeI BegNI() const
Returns an iterator referring to the first node in the network.
Definition: network.h:181
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1218
int AddKey(const TKey &Key)
Definition: hash.h:327
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
Definition: hash.h:454
bool IsKey(const TKey &Key) const
Definition: hash.h:216
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:420
static PTimeNet TTimeNet::Load ( TSIn SIn)
inlinestatic

Definition at line 41 of file timenet.h.

41 { return new TTimeNet(SIn); }
TTimeNet()
Definition: timenet.h:34
PTimeNet TTimeNet::LoadAmazon ( const TStr StlFNm)
static

Definition at line 561 of file timenet.cpp.

561  {
562  PTimeNet TimeNetPt = TTimeNet::New();
563  TTimeNet& TimeNet = *TimeNetPt;
564  TimeNet.Reserve(3953993, -1);
565  printf("Amazon Share-the-Love...\n");
566  char line [2024], MonthStr[4];
567  int NLines=0;
568  TStrV ColV;
569  FILE *F = fopen(StlFNm.CStr(), "rt");
570  while (! feof(F)) {
571  memset(line, 0, 2024);
572  fgets(line, 2024, F);
573  if (strlen(line) == 0) break;
574  TStr(line).SplitOnAllCh(',', ColV);
575  const int SrcNId = ColV[0].GetInt();
576  const int DstNId = ColV[1].GetInt();
577  // time data
578  TStr TmStr = ColV[2]; // time-format: 29JAN02:21:55:23
579  int Year = TmStr.GetSubStr(5, 6).GetInt();
580  if (Year < 10) { Year += 2000; } else { Year += 1900; }
581  MonthStr[0]=toupper(TmStr[2]); MonthStr[1]=tolower(TmStr[3]);
582  MonthStr[2]=tolower(TmStr[4]); MonthStr[3]=0;
583  const int Month = TTmInfo::GetMonthN(MonthStr, lUs);
584  const int Day = TmStr.GetSubStr(0, 1).GetInt();
585  const int Hour = TmStr.GetSubStr(8, 9).GetInt();
586  const int Min = TmStr.GetSubStr(11, 12).GetInt();
587  const int Sec = TmStr.GetSubStr(14, 15).GetInt();
588  // add nodes and links
589  if (! TimeNet.IsNode(SrcNId)) { TimeNet.AddNode(SrcNId, TSecTm(Year, Month, Day, Hour, Min, Sec)); }
590  if (! TimeNet.IsNode(DstNId)) { TimeNet.AddNode(DstNId, TSecTm(Year, Month, Day, Hour, Min, Sec)); }
591  if (! TimeNet.IsEdge(SrcNId, DstNId)) { TimeNet.AddEdge(SrcNId, DstNId); }
592  if (++NLines % 100000 == 0) printf("\r %dk", NLines/1000);
593  }
594  fclose(F);
595  printf("\r %d lines read\n", NLines);
596  printf("Graph: nodes: %d edges: %d\n", TimeNet.GetNodes(), TimeNet.GetEdges());
597  TimeNet.Defrag(true);
598  return TimeNetPt;
599 }
int GetInt() const
Definition: dt.h:579
static PTimeNet New()
Definition: timenet.h:39
int GetEdges() const
Returns the number of edges in the network.
Definition: network.h:323
TStr GetSubStr(const int &BChN, const int &EChN) const
Definition: dt.cpp:811
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:157
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:179
Definition: bd.h:63
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the network.
Definition: network.h:331
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the network.
Definition: network.h:351
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.h:273
Definition: tm.h:81
Definition: dt.h:412
void SplitOnAllCh(const char &SplitCh, TStrV &StrV, const bool &SkipEmpty=true) const
Definition: dt.cpp:926
static int GetMonthN(const TStr &MonthNm, const TLoc &Loc=lUs)
Definition: tm.cpp:39
Definition: bd.h:196
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the network.
Definition: network.h:373
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a network of Nodes nodes and Edges edges.
Definition: network.h:240
char * CStr()
Definition: dt.h:476
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:420
PTimeNet TTimeNet::LoadArxiv ( const TStr PaperFNm,
const TStr CiteFNm 
)
static

Definition at line 381 of file timenet.cpp.

381  {
382  TExeTm ExeTm;
383  PTimeNet TimeNetPt = TTimeNet::New();
384  TTimeNet& TimeNet = *TimeNetPt;
385  printf("Arxiv citation graph (paper publication year)...\n");
386  // load time data (file hep-ph-slacdates)
387  char Line [1024];
388  FILE *PprF = fopen(PaperFNm.CStr(), "rt");
389  TStr StrId, StrTime;
390  TStrV StrV, StrTimeV;
391  int N = 0, DuplicateNode = 0;
392  while (! feof(PprF)) {
393  Line[0] = 0;
394  fgets(Line, 1024, PprF);
395  if (strlen(Line) == 0 || Line[0] == '#') continue;
396  Line[strlen(Line)-1] = 0; // delete trailing '\n'
397  TStr(Line).SplitOnWs(StrV); IAssert(StrV.Len() == 2);
398  StrId = StrV[0]; StrTime = StrV[1]; IAssert(!StrId.Empty() && !StrTime.Empty());
399  StrTime.SplitOnAllCh('-', StrTimeV); IAssert(StrTimeV.Len() == 3);
400  const int NodeId = StrId.GetInt();
401  if (! TimeNet.IsNode(NodeId)) {
402  const int Year = StrTimeV[0].GetInt();
403  const int Month = StrTimeV[1].GetInt();
404  const int Day = StrTimeV[2].GetInt();
405  TimeNet.AddNode(NodeId, TSecTm(Year, Month, Day));
406  } else { DuplicateNode++; }
407  if (++N % 10000 == 0) printf("\r %dk", N/1000);
408  }
409  printf("\r %d nodes read. %d duplicate nodes. %s\n", N, DuplicateNode, ExeTm.GetTmStr());
410  fclose(PprF);
411  // load citations (file hep-ph-citations)
412  int NewSrcIds=0, NewDstIds=0, DupLinks=0, NewCits=0;
413  FILE *CiteF = fopen(CiteFNm.CStr(), "rt");
414  N = 0; ExeTm.Tick();
415  printf("Loading Arxiv citations...\n");
416  TIntPrV EdgeV;
417  THash<TInt, TSecTm> NIdToTimeH;
418  while (! feof(CiteF)) {
419  Line[0] = 0;
420  fgets(Line, 1024, CiteF);
421  if (strlen(Line) == 0 || Line[0] == '#') continue;
422  Line[strlen(Line)-1] = 0; // delete trailing '\n'
423  TStr(Line).SplitOnWs(StrV); IAssert(StrV.Len() == 2);
424  const int SrcNId = StrV[0].GetInt();
425  const int DstNId = StrV[1].GetInt();
426  EdgeV.Add(TIntPr(SrcNId, DstNId));
427  // infer time of destination node -- earliest paper that cites the node (paper)
428  if (! TimeNet.IsNode(DstNId) && TimeNet.IsNode(SrcNId)) {
429  const TSecTm& SrcTm = TimeNet.GetNDat(SrcNId);
430  if (! NIdToTimeH.IsKey(DstNId)) {
431  NIdToTimeH.AddDat(DstNId, SrcTm);
432  NewDstIds++;
433  }
434  else if (NIdToTimeH.GetDat(DstNId) < SrcTm) {
435  NIdToTimeH.GetDat(DstNId) = SrcTm; }
436  }
437  if (++N % 10000 == 0) printf("\r %dk", N/1000);
438  }
439  fclose(CiteF);
440  // add infeered time nodes (nodes which are cited by papers with known time)
441  for (int i = 0; i < NIdToTimeH.Len(); i++) {
442  TimeNet.AddNode(NIdToTimeH.GetKey(i), NIdToTimeH[i]);
443  }
444  // add links
445  for (int i = 0; i < EdgeV.Len(); i++) {
446  const int SrcNId = EdgeV[i].Val1;
447  const int DstNId = EdgeV[i].Val2;
448  if (TimeNet.IsNode(SrcNId) && TimeNet.IsNode(DstNId)) {
449  if (! TimeNet.IsEdge(SrcNId, DstNId)) { TimeNet.AddEdge(SrcNId, DstNId); }
450  else { DupLinks++; }
451  } else {
452  if (! TimeNet.IsNode(SrcNId)) {
453  NewSrcIds++;
454  if (! TimeNet.IsNode(DstNId)) { NewCits++; }
455  }
456  }
457  }
458  printf("\r %d citations read. %s\n", N, ExeTm.GetTmStr());
459  printf("Graph: nodes: %d edges: %d\n", TimeNet.GetNodes(), TimeNet.GetEdges());
460  printf("Removing 0-degree nodes: %d nodes\n", TSnap::CntDegNodes(TimeNetPt, 0));
461  TIntV RmNIdV;
462  for (TTimeNet::TNodeI ni = TimeNet.BegNI(); ni < TimeNet.EndNI(); ni++) {
463  if (ni.GetDeg() == 0) { RmNIdV.Add(ni.GetId()); }
464  }
465  for (int i = 0; i < RmNIdV.Len(); i++) {
466  TimeNet.DelNode(RmNIdV[i]);
467  }
468  TimeNet.Defrag(true);
469  printf("\nFinal graph: nodes: %d edges: %d\n", TimeNet.GetNodes(), TimeNet.GetEdges());
470  printf(" Duplicate citations : %d\n", DupLinks);
471  printf(" Nodes without time which are cited : %d (add them to graph, use time of the earliest source node)\n", NewDstIds);
472  printf(" Citations between unknown time nodes : %d\n", NewCits);
473  printf(" Nodes without time which make citations: %d (do not add them into the graph)\n", NewSrcIds);
474  return TimeNetPt;
475 }
#define IAssert(Cond)
Definition: bd.h:262
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
Definition: tm.h:354
static PTimeNet New()
Definition: timenet.h:39
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:183
int GetEdges() const
Returns the number of edges in the network.
Definition: network.h:323
TNodeData & GetNDat(const int &NId)
Returns node data for the node of ID NId in the network.
Definition: network.h:191
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:157
TNodeI BegNI() const
Returns an iterator referring to the first node in the network.
Definition: network.h:181
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:179
const char * GetTmStr() const
Definition: tm.h:369
void DelNode(const int &NId)
Deletes node of ID NId from the network.
Definition: network.h:297
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the network.
Definition: network.h:331
void Tick()
Definition: tm.h:363
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the network.
Definition: network.h:351
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.h:273
Definition: tm.h:81
Definition: dt.h:412
Definition: hash.h:88
Definition: bd.h:196
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the network.
Definition: network.h:373
void SplitOnWs(TStrV &StrV) const
Definition: dt.cpp:972
char * CStr()
Definition: dt.h:476
int CntDegNodes(const PGraph &Graph, const int &NodeDeg)
Returns the number of nodes with degree NodeDeg.
Definition: alg.h:105
bool IsKey(const TKey &Key) const
Definition: hash.h:216
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:420
PTimeNet TTimeNet::LoadBipartite ( const TStr InFNm)
static

Definition at line 344 of file timenet.cpp.

344  {
345  PTimeNet TimeNetPt = TTimeNet::New();
346  TTimeNet& TimeNet = *TimeNetPt;
347  PSs Ss = TSs::LoadTxt(ssfTabSep, InFNm.CStr());
348  TIntH Set1IdH; // paper ids
349  TStrV StrTimeV;
350  for (int y = 0; y < Ss->GetYLen(); y++) {
351  if (Ss->At(0, y)[0] == '#') continue; // skip comments
352  if (Ss->GetXLen(y) < 3) continue; // there must be at least one author
353  const int& SrcId = Ss->At(0, y).GetInt();
354  IAssert(! Set1IdH.IsKey(SrcId));
355  IAssert(! TimeNet.IsNode(SrcId));
356  Set1IdH.AddKey(SrcId);
357  Ss->At(1, y).SplitOnAllCh('-', StrTimeV);
358  const int Year = StrTimeV[0].GetInt();
359  const int Month = StrTimeV[1].GetInt();
360  const int Day = StrTimeV[2].GetInt();
361  const TSecTm NodeTm(Year, Month, Day);
362  TimeNet.AddNode(SrcId, NodeTm);
363  for (int dst = 2; dst < Ss->GetXLen(y); dst++) {
364  const int DstId = Ss->At(dst, y).GetInt();
365  IAssert(! Set1IdH.IsKey(DstId));
366  if (! TimeNet.IsNode(DstId)) { TimeNet.AddNode(DstId, NodeTm); }
367  else { TimeNet.GetNDat(DstId) = TMath::Mn(NodeTm, TimeNet.GetNDat(DstId)); }
368  if (! TimeNet.IsEdge(SrcId, DstId)) { TimeNet.AddEdge(SrcId, DstId); }
369  }
370  }
371  TimeNet.Defrag();
372  printf("Bipartate graph: nodes: %d edges: %d\n", TimeNet.GetNodes(), TimeNet.GetEdges());
373  printf(" Bipartate sets: %d nodes --> %d nodes\n", TSnap::CntInDegNodes(TimeNetPt, 0),
374  TSnap::CntOutDegNodes(TimeNetPt, 0));
375  return TimeNetPt;
376 }
#define IAssert(Cond)
Definition: bd.h:262
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
static PSs LoadTxt(const TSsFmt &SsFmt, const TStr &FNm, const PNotify &Notify=NULL, const bool &IsExcelEoln=true, const int &MxY=-1, const TIntV &AllowedColNV=TIntV(), const bool &IsQStr=true)
Definition: ss.cpp:100
static PTimeNet New()
Definition: timenet.h:39
int GetEdges() const
Returns the number of edges in the network.
Definition: network.h:323
TNodeData & GetNDat(const int &NId)
Returns node data for the node of ID NId in the network.
Definition: network.h:191
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:157
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:179
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the network.
Definition: network.h:331
Tab separated.
Definition: ss.h:6
int CntInDegNodes(const PGraph &Graph, const int &NodeInDeg)
Returns the number of nodes with in-degree NodeInDeg.
Definition: alg.h:87
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the network.
Definition: network.h:351
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.h:273
Definition: tm.h:81
Definition: bd.h:196
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the network.
Definition: network.h:373
char * CStr()
Definition: dt.h:476
int CntOutDegNodes(const PGraph &Graph, const int &NodeOutDeg)
Returns the number of nodes with out-degree NodeOutDeg.
Definition: alg.h:96
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:420
PTimeNet TTimeNet::LoadPatents ( const TStr PatentFNm,
const TStr CiteFNm 
)
static

Definition at line 478 of file timenet.cpp.

478  {
479  int N = 0;
480  TExeTm ExeTm;
481  PTimeNet TimeNetPt = TTimeNet::New();
482  TTimeNet& TimeNet = *TimeNetPt;
483  TimeNet.Reserve(4000000, 160000000);
484  printf("parsing patent data (patent grant year)...\n");
485  // load time data (file pat63_99.txt)
486  const int& PatIdCol = 0;
487  const int& GYearCol = 1;
488  TStrV ColV;
489  char Line [1024];
490  FILE *PatF = fopen(PatentFNm.CStr(), "rt");
491  fgets(Line, 1024, PatF); // skip 1st line
492  while (! feof(PatF)) {
493  Line[0] = 0;
494  fgets(Line, 1024, PatF);
495  if (strlen(Line) == 0) break;
496  TStr(Line).SplitOnAllCh(',', ColV, false);
497  IAssert(ColV.Len() == 23);
498  const int PatentId = ColV[PatIdCol].GetInt();
499  const int GrantYear = ColV[GYearCol].GetInt();
500  IAssert(! TimeNet.IsNode(PatentId));
501  TimeNet.AddNode(PatentId, TSecTm(GrantYear)); // pretend year is a second
502  if (++N % 100000 == 0) printf("\r %dk", N/1000);
503  }
504  printf("\r %d patents read. %s\n", N, ExeTm.GetTmStr());
505  fclose(PatF);
506  // load citations (file cite75_99.txt)
507  printf("\nLoading patent citations...\n");
508  int NewSrcIds=0, NewDstIds=0, DupLinks=0, NewCits=0;
509  N = 0; ExeTm.Tick();
510  TStr SrcId, DstId;
511  FILE *CiteF = fopen(CiteFNm.CStr(), "rt");
512  fgets(Line, 1024, CiteF); // skip 1st line
513  while (! feof(CiteF)) {
514  Line[0] = 0;
515  fgets(Line, 1024, CiteF);
516  if (strlen(Line) == 0) break;
517  Line[strlen(Line)-1] = 0; // delete trailing '\n'
518  TStr(Line).SplitOnCh(SrcId, ',', DstId);
519  const int SrcNId = SrcId.GetInt();
520  const int DstNId = DstId.GetInt();
521  if (! TimeNet.IsNode(SrcNId) && ! TimeNet.IsNode(DstNId)) {
522  //TimeNet.AddNode(SrcNId, TSecTm(1, 1, 1)); NewSrcIds++;
523  //TimeNet.AddNode(DstNId, TSecTm(1, 1, 1)); NewDstIds++;
524  NewCits++;
525  continue;
526  }
527  else if (TimeNet.IsNode(SrcNId) && ! TimeNet.IsNode(DstNId)) {
528  TimeNet.AddNode(DstNId, TimeNet.GetNDat(SrcNId)); NewDstIds++;
529  }
530  else if (! TimeNet.IsNode(SrcNId) && TimeNet.IsNode(DstNId)) {
531  TimeNet.AddNode(SrcNId, TimeNet.GetNDat(DstNId)); NewSrcIds++;
532  }
533  if (! TimeNet.IsEdge(SrcNId, DstNId)) {
534  TimeNet.AddEdge(SrcNId, DstNId);
535  } else { DupLinks++; }
536  if (++N % 100000 == 0) printf("\r %dk", N/1000);
537  }
538  fclose(CiteF);
539  printf("\r %d citations read. %s\n\n", N, ExeTm.GetTmStr());
540  printf("Graph: nodes: %d edges: %d\n", TimeNet.GetNodes(), TimeNet.GetEdges());
541  printf("Removing 0-degree nodes: %d nodes\n", TSnap::CntDegNodes(TimeNetPt, 0));
542  TIntV RmNIdV;
543  for (TTimeNet::TNodeI ni = TimeNet.BegNI(); ni < TimeNet.EndNI(); ni++) {
544  if (ni.GetDeg() == 0) { RmNIdV.Add(ni.GetId()); }
545  }
546  for (int i = 0; i < RmNIdV.Len(); i++) {
547  TimeNet.DelNode(RmNIdV[i]);
548  }
549  TimeNet.Defrag(true);
550  printf("\nFinal graph: nodes: %d edges: %d\n", TimeNet.GetNodes(), TimeNet.GetEdges());
551  printf(" Duplicate citations : %d\n", DupLinks);
552  printf(" Citations between unknown time nodes : %d\n", NewCits);
553  printf(" Nodes without time which make citations: %d\n", NewSrcIds);
554  printf(" Nodes without time which are cited : %d\n", NewDstIds);
555  return TimeNetPt;
556 }
#define IAssert(Cond)
Definition: bd.h:262
int GetInt() const
Definition: dt.h:579
Definition: tm.h:354
static PTimeNet New()
Definition: timenet.h:39
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:183
int GetEdges() const
Returns the number of edges in the network.
Definition: network.h:323
TNodeData & GetNDat(const int &NId)
Returns node data for the node of ID NId in the network.
Definition: network.h:191
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:157
TNodeI BegNI() const
Returns an iterator referring to the first node in the network.
Definition: network.h:181
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:179
void SplitOnCh(TStr &LStr, const char &SplitCh, TStr &RStr) const
Definition: dt.cpp:901
const char * GetTmStr() const
Definition: tm.h:369
void DelNode(const int &NId)
Deletes node of ID NId from the network.
Definition: network.h:297
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the network.
Definition: network.h:331
void Tick()
Definition: tm.h:363
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the network.
Definition: network.h:351
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.h:273
Definition: tm.h:81
Definition: dt.h:412
void SplitOnAllCh(const char &SplitCh, TStrV &StrV, const bool &SkipEmpty=true) const
Definition: dt.cpp:926
Definition: bd.h:196
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the network.
Definition: network.h:373
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a network of Nodes nodes and Edges edges.
Definition: network.h:240
char * CStr()
Definition: dt.h:476
int CntDegNodes(const PGraph &Graph, const int &NodeDeg)
Returns the number of nodes with degree NodeDeg.
Definition: alg.h:105
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:420
static PTimeNet TTimeNet::New ( )
inlinestatic

Definition at line 39 of file timenet.h.

39 { return new TTimeNet(); }
TTimeNet()
Definition: timenet.h:34
static PTimeNet TTimeNet::New ( const int &  Nodes,
const int &  Edges 
)
inlinestatic

Definition at line 40 of file timenet.h.

40 { return new TTimeNet(Nodes, Edges); }
TTimeNet()
Definition: timenet.h:34
TTimeNet & TTimeNet::operator= ( const TTimeNet TimeNet)

Definition at line 3 of file timenet.cpp.

3  {
4  if (this != &TimeNet) {
5  TNet::operator=(TimeNet);
6  }
7  return *this;
8 }
TNodeNet & operator=(const TNodeNet &NodeNet)
Definition: network.h:153
void TTimeNet::PlotCCfOverTm ( const TStr FNmPref,
TStr  Desc,
const TTmUnit TmUnit,
const int &  NodesBucket = -1 
) const

Definition at line 253 of file timenet.cpp.

253  {
254  if (Desc.Empty()) { Desc = FNmPref; }
255  TTimeNet::TTmBucketV TmBucketV;
256  TStr XLbl;
257  if (TmUnit == tmuNodes) {
258  XLbl = "Number of nodes (time)";
259  IAssert(NodesBucket > 0);
260  GetNodeBuckets(NodesBucket, TmBucketV); }
261  else {
262  XLbl = TStr::Fmt("Time (%s)", TTmInfo::GetTmUnitStr(TmUnit).CStr());
263  GetTmBuckets(TmUnit, TmBucketV);
264  }
265  TIntV NodeIdV;
266  TFltPrV DegToCCfV, CcfV, OpClV, OpV;
267  TVec<TTuple<TFlt, 4> > OpenClsV;
268  TTuple<TFlt, 4> Tuple;
269  TExeTm ExeTm;
270  int XVal = 0;
271  printf("Clustering coefficient over time:\n %d edges, %d edges per bucket, %d buckets \n", GetEdges(), 100000, TmBucketV.Len());
272  PUNGraph UNGraph = TSnap::ConvertGraph<PUNGraph>(PTimeNet((TTimeNet*)this));
273  for (int t = 0; t < TmBucketV.Len(); t++) {
274  printf("\r %d/%d: ", t+1, TmBucketV.Len());
275  NodeIdV.AddV(TmBucketV[t].NIdV); // edges up to time T
276  int64 Open=0, Close=0;
277  const PUNGraph Graph = TSnap::GetSubGraph(UNGraph, NodeIdV);
278  const double CCf = TSnap::GetClustCf(Graph, DegToCCfV, Open, Close);
279  if (TmUnit == tmuNodes) { XVal = Graph->GetNodes(); }
280  else { XVal = TmBucketV[t].BegTm.GetInUnits(TmUnit); }
281  CcfV.Add(TFltPr(XVal, CCf));
282  OpClV.Add(TFltPr(XVal, Open+Close==0?0:(double)Close/(Open+Close)));
283  OpV.Add(TFltPr(XVal, Open==0?0:(double)Close/Open));
284  Tuple[0]=Graph->GetNodes();
285  Tuple[1]=Graph->GetEdges();
286  Tuple[2]=(double)Close; Tuple[3]=(double)Open;
287  OpenClsV.Add(Tuple);
288  printf(" %s", ExeTm.GetStr());
289  TGnuPlot::PlotValV(DegToCCfV, TStr::Fmt("ccfAt%02dtm.%s", t+1, FNmPref.CStr()),
290  TStr::Fmt("%s. At time %d. Clustering Coefficient. G(%d,%d)", Desc.CStr(), t+1, Graph->GetNodes(), Graph->GetEdges()),
291  "Degree", "Clustering coefficient", gpsLog10XY, false);
292  }
293  TGnuPlot::PlotValV(CcfV, "ccfOverTm."+FNmPref, Desc+". Average Clustering Coefficient", XLbl, "Average clustering coefficient", gpsAuto, false);
294  TGnuPlot::PlotValV(OpClV, "ClsOpnTr1."+FNmPref, Desc+". Close/(Open+Closed) triads", XLbl, "Close / (Open+Closed) triads", gpsAuto, false);
295  TGnuPlot::PlotValV(OpV, "ClsOpnTr2."+FNmPref, Desc+". Close/Open triads", XLbl, "Close / Open triads", gpsAuto, false);
296  TGnuPlot::SaveTs(OpenClsV, "ClsOpnTr."+FNmPref+".tab", TStr::Fmt("#%s\n#Nodes\tEdges\tClosed\tOpenTriads", Desc.CStr()));
297  printf("\n");
298 }
#define IAssert(Cond)
Definition: bd.h:262
Definition: tm.h:354
Definition: ds.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
int GetEdges() const
Returns the number of edges in the network.
void GetTmBuckets(const TTmUnit &GroupBy, TTmBucketV &TmBucketV) const
Definition: timenet.cpp:59
Definition: gnuplot.h:7
static void SaveTs(const TIntKdV &KdV, const TStr &FNm, const TStr &HeadLn=TStr())
Definition: gnuplot.cpp:713
PUNGraph GetSubGraph(const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumb...
Definition: subgraph.cpp:7
Definition: tm.h:14
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void GetNodeBuckets(const int NodesPerBucket, TTimeNet::TTmBucketV &TmBucketV) const
Definition: timenet.cpp:77
static TStr GetTmUnitStr(const TTmUnit &TmUnit)
Definition: tm.cpp:108
long long int64
Definition: bd.h:27
Definition: dt.h:412
bool Empty() const
Definition: dt.h:488
static void PlotValV(const TVec< TPair< TVal1, TVal2 > > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:363
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
double GetClustCf(const PGraph &Graph, int SampleNodes=-1)
Computes the average clustering coefficient as defined in Watts and Strogatz, Collective dynamics of ...
Definition: triad.h:100
Definition: bd.h:196
TPt< TTimeNet > PTimeNet
Definition: timenet.h:8
const char * GetStr() const
Definition: tm.h:367
char * CStr()
Definition: dt.h:476
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:420
TSizeTy AddV(const TVec< TVal, TSizeTy > &ValV)
Adds the elements of the vector ValV to the to end of the vector.
Definition: ds.h:1020
void TTimeNet::PlotEffDiam ( const TStr FNmPref,
const TStr Desc,
const TTmUnit GroupBy,
const TSecTm StartTm,
const int &  NDiamRuns = 10,
const bool &  OnlyWcc = false,
const bool &  AlsoRewire = false 
) const

Definition at line 106 of file timenet.cpp.

107  {
108  const TStr WccStr = OnlyWcc ? "WCC " : TStr::GetNullStr();
109  TTmBucketV TmBucketV;
110  GetTmBuckets(TmUnit, TmBucketV);
111  TIntV NodeIdV;
112  TExeTm ExeTm, Run1Tm;
113  TFltTrV TmDiamV, NdsDiamV;
114  TFltTrV RwTmDiamV, RwNdsDiamV;
115  for (int t = 0; t < TmBucketV.Len(); t++) {
116  NodeIdV.AddV(TmBucketV[t].NIdV); // nodes up to time T
117  printf("\n*** %d/%d] at %s (%d nodes)\n", t+1, TmBucketV.Len(),
118  TmBucketV[t].BegTm.GetStr(TmUnit).CStr(), NodeIdV.Len()); ExeTm.Tick();
119  if (TmBucketV[t].BegTm < StartTm) continue;
120  //PUNGraph PreGraph = GetSubUNGraph(NodeIdV, true);
121  PUNGraph PreGraph = TSnap::ConvertSubGraph<PUNGraph>(PTimeNet((TTimeNet*)this), NodeIdV);
122  { TMom Mom;
123  for (int r = 0; r < NDiamRuns; r++) {
124  printf("%d...", r+1); Run1Tm.Tick();
125  const double EffDiam = TSnap::GetAnfEffDiam(OnlyWcc ? TSnap::GetMxWcc(PreGraph) : PreGraph);
126  Mom.Add(EffDiam); printf("[%s]\r", Run1Tm.GetTmStr());
127  }
128  Mom.Def();
129  TmDiamV.Add(TFltTr((int)TmBucketV[t].BegTm.GetInUnits(TmUnit), Mom.GetMean(), Mom.GetSDev()));
130  NdsDiamV.Add(TFltTr(PreGraph->GetNodes(), Mom.GetMean(), Mom.GetSDev()));
131  NdsDiamV.Sort();
132  printf(" [%s] \n", ExeTm.GetTmStr()); }
133  if (AlsoRewire) {
134  //PUNGraph RwGraph = TGGen::GenRndDegSeqS(PreGraph, 50, TInt::Rnd); // edge switching model
135  TIntV DegSeqV(PreGraph->GetNodes(), 0);
136  for (TUNGraph::TNodeI NI = PreGraph->BegNI(); NI < PreGraph->EndNI(); NI++) { DegSeqV.Add(NI.GetDeg()); }
137  PUNGraph RwGraph = TSnap::GenConfModel(DegSeqV, TInt::Rnd);
138  printf("Configuration model: (%d, %d) --> (%d, %d)\n", PreGraph->GetNodes(), PreGraph->GetEdges(), RwGraph->GetNodes(), RwGraph->GetEdges());
139  TMom Mom;
140  for (int r = 0; r < NDiamRuns; r++) {
141  printf(" diam run %d...", r+1); Run1Tm.Tick();
142  const double EffDiam = TSnap::GetAnfEffDiam(OnlyWcc ? TSnap::GetMxWcc(PreGraph):PreGraph);
143  Mom.Add(EffDiam); printf(" current run [%s]\n", Run1Tm.GetTmStr());
144  }
145  Mom.Def();
146  RwTmDiamV.Add(TFltTr((int)TmBucketV[t].BegTm.GetInUnits(TmUnit), Mom.GetMean(), Mom.GetSDev()));
147  RwNdsDiamV.Add(TFltTr(PreGraph->GetNodes(), Mom.GetMean(), Mom.GetSDev()));
148  RwNdsDiamV.Sort();
149  printf("done with diameter. Total time [%s] \n", ExeTm.GetTmStr());
150  }
151  // plot
152  { TGnuPlot GnuPlot("diamEff-T."+FNmPref, TStr::Fmt("%s. G(%d, %d)", Desc.CStr(), GetNodes(), GetEdges()));
153  GnuPlot.SetXYLabel(TStr::Fmt("TIME [%s]", TTmInfo::GetTmUnitStr(TmUnit).CStr()), WccStr+"Effective Diameter");
154  GnuPlot.AddErrBar(TmDiamV, "True", "");
155  if (! RwTmDiamV.Empty()) { GnuPlot.AddErrBar(RwTmDiamV, "Rewired", "");}
156  GnuPlot.SavePng(); }
157  { TGnuPlot GnuPlot("diamEff-N."+FNmPref, TStr::Fmt("%s. G(%d, %d)", Desc.CStr(), GetNodes(), GetEdges()));
158  GnuPlot.SetXYLabel("NODES", WccStr+"Effective Diameter");
159  GnuPlot.AddErrBar(NdsDiamV, "True", "");
160  if (! RwNdsDiamV.Empty()) { GnuPlot.AddErrBar(RwNdsDiamV, "Rewired", "");}
161  GnuPlot.SavePng(); }
162  }
163 }
Definition: tm.h:354
PGraph GetMxWcc(const PGraph &Graph)
Returns a graph representing the largest weakly connected component on an input Graph.
Definition: cncom.h:452
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:63
double GetAnfEffDiam(const PGraph &Graph, const bool &IsDir, const double &Percentile, const int &NApprox)
Definition: anf.h:217
int GetEdges() const
Returns the number of edges in the network.
void GetTmBuckets(const TTmUnit &GroupBy, TTmBucketV &TmBucketV) const
Definition: timenet.cpp:59
Definition: xmath.h:129
TVec< TTmBucket > TTmBucketV
Definition: timenet.h:32
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:157
static TRnd Rnd
Definition: dt.h:1051
double GetSDev() const
Definition: xmath.h:242
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:530
const char * GetTmStr() const
Definition: tm.h:369
void Add(const TFlt &Val, const TFlt &Wgt=1)
Definition: xmath.h:217
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1218
TTriple< TFlt, TFlt, TFlt > TFltTr
Definition: ds.h:175
PUNGraph GenConfModel(const TIntV &DegSeqV, TRnd &Rnd)
Generates a random undirect graph with a given degree sequence.
Definition: ggen.cpp:119
static TStr GetNullStr()
Definition: dt.cpp:1626
void Tick()
Definition: tm.h:363
static TStr GetTmUnitStr(const TTmUnit &TmUnit)
Definition: tm.cpp:108
double GetMean() const
Definition: xmath.h:240
Definition: dt.h:412
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Definition: bd.h:196
TPt< TTimeNet > PTimeNet
Definition: timenet.h:8
char * CStr()
Definition: dt.h:476
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
void Def()
Definition: xmath.cpp:339
TSizeTy AddV(const TVec< TVal, TSizeTy > &ValV)
Adds the elements of the vector ValV to the to end of the vector.
Definition: ds.h:1020
void TTimeNet::PlotMedianDegOverTm ( const TStr FNmPref,
const TTmUnit TmUnit,
const int &  NodesPerBucket = -1 
) const

Definition at line 300 of file timenet.cpp.

300  {
301  TTimeNet::TTmBucketV TmBucketV;
302  TStr XLbl;
303  if (TmUnit == tmuNodes) {
304  XLbl = "Number of nodes (time)"; IAssert(NodesPerBucket > 0);
305  GetNodeBuckets(NodesPerBucket, TmBucketV); }
306  else {
307  XLbl = TStr::Fmt("Time (%s)", TTmInfo::GetTmUnitStr(TmUnit).CStr());
308  GetTmBuckets(TmUnit, TmBucketV); }
309  printf("\n\n%s\nMedian degree over time:\n %d edges, %d edges per bucket, %d buckets \n", FNmPref.CStr(), GetEdges(), NodesPerBucket, TmBucketV.Len());
310  TFltPrV MedDegV, MedInDegV, MedOutDegV;
311  TIntV NodeIdV;
312  int XVal;
313  PUNGraph UNGraph = TSnap::ConvertGraph<PUNGraph>(PTimeNet((TTimeNet*)this));
314  PNGraph NGraph = TSnap::ConvertGraph<PNGraph>(PTimeNet((TTimeNet*)this));
315  FILE *F = fopen(("gStat-"+FNmPref+".tab").CStr(), "wt");
316  fprintf(F, "UndirNodes\tUndirEdges\tUndirNonZNodes\tMedianDeg\tMeanDeg\tDirNodes\tDirEdges\tDirNonzNodes\tMedInDeg\tMedOutDeg\tMeanInDeg\tMeanOutDeg\n");
317  for (int t = 0; t < TmBucketV.Len(); t++) {
318  printf("\r %d/%d: ", t+1, TmBucketV.Len());
319  NodeIdV.AddV(TmBucketV[t].NIdV); // edges up to time T
320  if (TmUnit == tmuNodes) { XVal = NodeIdV.Len(); }
321  else { XVal = TmBucketV[t].BegTm.GetInUnits(TmUnit); }
322  // un graph
323  { const PUNGraph Graph = TSnap::GetSubGraph(UNGraph, NodeIdV); TMom Mom;
324  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { if (NI.GetOutDeg()>0) { Mom.Add(NI.GetOutDeg());} }
325  Mom.Def(); MedDegV.Add(TFltPr(XVal, Mom.GetMedian()));
326  fprintf(F, "%d\t%d\t%d\t%f\t%f", Graph->GetNodes(), Graph->GetEdges(), TSnap::CntNonZNodes(Graph), (float)Mom.GetMedian(), (float)Mom.GetMean()); }
327  // directed graph
328  { const PNGraph Graph = TSnap::GetSubGraph<PNGraph>(NGraph, NodeIdV); TMom MomOut, MomIn;
329  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
330  if (NI.GetOutDeg()>0) { MomOut.Add(NI.GetOutDeg()); }
331  if (NI.GetInDeg()>0) { MomIn.Add(NI.GetInDeg()); } }
332  MomOut.Def(); MedOutDegV.Add(TFltPr(XVal, MomOut.GetMedian()));
333  MomIn.Def(); MedInDegV.Add(TFltPr(XVal, MomIn.GetMedian()));
334  fprintf(F, "\t%d\t%d\t%d\t%f\t%f\t%f\t%f\n", Graph->GetNodes(), Graph->GetEdges(), (int)TSnap::CntNonZNodes(Graph), (float)MomIn.GetMedian(), (float)MomOut.GetMedian(), (float)MomIn.GetMean(), (float)MomOut.GetMean()); }
335  }
336  fclose(F);
337  TGnuPlot::PlotValV(MedDegV, "medDeg."+FNmPref, FNmPref+" Median degree", TTmInfo::GetTmUnitStr(TmUnit), "Median degree");
338  TGnuPlot::PlotValV(MedOutDegV, "medOutDeg."+FNmPref, FNmPref+" Median OUT degree", TTmInfo::GetTmUnitStr(TmUnit), "Median OUT degree");
339  TGnuPlot::PlotValV(MedInDegV, "medInDeg."+FNmPref, FNmPref+" Median IN degree", TTmInfo::GetTmUnitStr(TmUnit), "Median IN degree");
340 }
#define IAssert(Cond)
Definition: bd.h:262
double GetMedian() const
Definition: xmath.h:244
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:63
int GetEdges() const
Returns the number of edges in the network.
void GetTmBuckets(const TTmUnit &GroupBy, TTmBucketV &TmBucketV) const
Definition: timenet.cpp:59
Definition: xmath.h:129
void Add(const TFlt &Val, const TFlt &Wgt=1)
Definition: xmath.h:217
int CntNonZNodes(const PGraph &Graph)
Returns the number of nodes with degree greater than 0.
Definition: alg.h:114
PUNGraph GetSubGraph(const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumb...
Definition: subgraph.cpp:7
Definition: tm.h:14
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void GetNodeBuckets(const int NodesPerBucket, TTimeNet::TTmBucketV &TmBucketV) const
Definition: timenet.cpp:77
static TStr GetTmUnitStr(const TTmUnit &TmUnit)
Definition: tm.cpp:108
double GetMean() const
Definition: xmath.h:240
Definition: dt.h:412
static void PlotValV(const TVec< TPair< TVal1, TVal2 > > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:363
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:327
Definition: bd.h:196
TPt< TTimeNet > PTimeNet
Definition: timenet.h:8
char * CStr()
Definition: dt.h:476
void Def()
Definition: xmath.cpp:339
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:420
TSizeTy AddV(const TVec< TVal, TSizeTy > &ValV)
Adds the elements of the vector ValV to the to end of the vector.
Definition: ds.h:1020
void TTimeNet::PlotMissingPast ( const TStr FNmPref,
const TStr Desc,
const TTmUnit GroupBy,
const TSecTm DelPreTmEdges,
const TSecTm PostTmDiam 
) const

Definition at line 169 of file timenet.cpp.

170  {
171  printf("\nGrowth over time: degree distribution, Growth Power Law, Diameter.\n %s group by %s.\n",
172  FNmPref.CStr(), TTmInfo::GetTmUnitStr(TmUnit).CStr());
173  printf(" Delete out-edges of pre time %s nodes.\n Take subgraph of post year %s subgraph.\n\n",
174  DelPreTmEdges.GetStr().CStr(), PostTmDiam.GetStr().CStr());
175  const int NDiamRuns = 10;
176  const int NSamples = 100;
177  //PUNGraph FullGraph = GetUNGraph();
178  PUNGraph FullGraph = TSnap::ConvertGraph<PUNGraph>(PTimeNet((TTimeNet*)this));
179  // delete past
180  if (DelPreTmEdges.IsDef()) {
181  int NDelNodes = 0, NDelEdges = 0;
182  printf("Deleting pre-%s node out-links\n", DelPreTmEdges.GetStr().CStr());
183  for (TNodeI NodeI = BegNI(); NodeI < EndNI(); NodeI++) {
184  if (NodeI() < DelPreTmEdges) {
185  const int NodeId = NodeI.GetId();
186  for (int edge = 0; edge < NodeI.GetOutDeg(); edge++) {
187  FullGraph->DelEdge(NodeId, NodeI.GetOutNId(edge)); }
188  NDelEdges += NodeI.GetOutDeg(); NDelNodes++;
189  }
190  }
191  printf(" Deleted %d nodes out-edges (%d edges total).\n", NDelNodes, NDelEdges);
192  FullGraph->Defrag(true);
193  }
194  PGStatVec GrowthStat = TGStatVec::New(TmUnit);
195  TFltV PreDiamSDev, PreEffDiamSDev, WccDiamSDev, WccEffDiamSDev;
196  TIntV NodeIdV;
197  TExeTm ExeTm;
198  TTmBucketV TmBucketV;
199  GetTmBuckets(TmUnit, TmBucketV);
200  for (int t = 0; t < TmBucketV.Len(); t++) {
201  printf("\nGraph: %s (%d / %d) [%s]\n", TmBucketV[t].BegTm.GetTmStr().CStr(),
202  t+1, TmBucketV.Len(), TExeTm::GetCurTm());
203  // up-to-year subgraph
204  NodeIdV.AddV(TmBucketV[t].NIdV); // nodes up to time T
205  if (TmBucketV[t].BegTm < PostTmDiam) { continue; }
206  const PUNGraph PreGraph = TSnap::GetSubGraph(FullGraph, NodeIdV, true);
207  const PUNGraph WccGraph = TSnap::GetMxWcc(PreGraph);
208  TIntV PostYearNIdV, WccPostYearNIdV;
209  for (TUNGraph::TNodeI NI = PreGraph->BegNI(); NI < PreGraph->EndNI(); NI++) {
210  if (GetNDat(NI.GetId()) >= PostTmDiam) {
211  PostYearNIdV.Add(NI.GetId());
212  if (WccGraph->IsNode(NI.GetId())) { WccPostYearNIdV.Add(NI.GetId()); }
213  }
214  }
215  TMom PreDiamMom, PreEffDiamMom, WccDiamMom, WccEffDiamMom;
216  // diameter of PostYearDiam subgraph using whole graph (all edges)
217  int FullDiam; double EffDiam;
218  for (int r = 0; r < NDiamRuns; r++) {
219  if (! PostYearNIdV.Empty()) {
220  TSnap::GetBfsEffDiam(PreGraph, NSamples, PostYearNIdV, false, EffDiam, FullDiam);
221  PreDiamMom.Add(FullDiam); PreEffDiamMom.Add(EffDiam);
222  }
223  if (! WccPostYearNIdV.Empty()) {
224  TSnap::GetBfsEffDiam(WccGraph, NSamples, WccPostYearNIdV, false, EffDiam, FullDiam);
225  WccDiamMom.Add(FullDiam); WccEffDiamMom.Add(EffDiam);
226  }
227  printf(" diam: %d [%s] \r", r+1, ExeTm.GetTmStr()); ExeTm.Tick();
228  }
229  PreDiamMom.Def(); PreEffDiamMom.Def();
230  WccDiamMom.Def(); WccEffDiamMom.Def();
231  // save stat
232  PGStat GraphStatPt = GrowthStat->Add(TmBucketV[t].BegTm);
233  TGStat& GS = *GraphStatPt;
234  GS.TakeBasicStat(PreGraph, false);
235  GS.TakeBasicStat(WccGraph, true);
236  GS.SetVal(gsvFullDiam, PreDiamMom.GetMean()); // mean
237  GS.SetVal(gsvEffDiam, PreEffDiamMom.GetMean());
238  GS.SetVal(gsvFullWccDiam, WccDiamMom.GetMean());
239  GS.SetVal(gsvEffWccDiam, WccEffDiamMom.GetMean());
240  GS.SetVal(gsvFullDiamDev, PreDiamMom.GetSDev()); // variance
241  GS.SetVal(gsvEffDiamDev, PreEffDiamMom.GetSDev());
242  GS.SetVal(gsvFullWccDiamDev, WccDiamMom.GetSDev());
243  GS.SetVal(gsvEffWccDiamDev, WccEffDiamMom.GetSDev());
244  { TFOut FOut("growth."+FNmPref+".gStatVec"); GrowthStat->Save(FOut); }
245  GrowthStat->SaveTxt(FNmPref, TStr::Fmt("%s. MISSING PAST DIAMETER\nDelPreEdges\t%s\nPostYearDiam\t%s\n",
246  Desc.CStr(), DelPreTmEdges.GetStr().CStr(), PostTmDiam.GetStr().CStr()));
247  }
248  // diameter plots
249  //GrowthStat->PlotDiam(FNmPref, Desc + TStr::Fmt(" MISSING PAST. DelPre:%d PostYear:%d.",
250  // DelPreEdges, PostYearDiam));*/
251 }
static char * GetCurTm()
Definition: tm.h:373
Definition: tm.h:354
bool IsDef() const
Definition: tm.h:123
double GetBfsEffDiam(const PGraph &Graph, const int &NTestNodes, const bool &IsDir=false)
Returns the (approximation of the) Effective Diameter (90-th percentile of the distribution of shorte...
Definition: bfsdfs.h:278
PGraph GetMxWcc(const PGraph &Graph)
Returns a graph representing the largest weakly connected component on an input Graph.
Definition: cncom.h:452
Definition: fl.h:319
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:63
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:183
void GetTmBuckets(const TTmUnit &GroupBy, TTmBucketV &TmBucketV) const
Definition: timenet.cpp:59
Definition: xmath.h:129
TVec< TTmBucket > TTmBucketV
Definition: timenet.h:32
TSecTm & GetNDat(const int &NId)
Returns node data for the node of ID NId in the network.
Definition: network.h:191
TNodeI BegNI() const
Returns an iterator referring to the first node in the network.
Definition: network.h:181
double GetSDev() const
Definition: xmath.h:242
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:530
const char * GetTmStr() const
Definition: tm.h:369
void Add(const TFlt &Val, const TFlt &Wgt=1)
Definition: xmath.h:217
Statistics of a Graph Snapshot.
Definition: gstat.h:36
PUNGraph GetSubGraph(const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumb...
Definition: subgraph.cpp:7
void Tick()
Definition: tm.h:363
void SetVal(const TGStatVal &StatVal, const double &Val)
Definition: gstat.cpp:88
static TStr GetTmUnitStr(const TTmUnit &TmUnit)
Definition: tm.cpp:108
double GetMean() const
Definition: xmath.h:240
void Save(TSOut &SOut) const
Definition: xmlser.h:16
TStr GetStr(const TLoc &Loc=lUs) const
Definition: tm.cpp:457
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Definition: bd.h:196
TPt< TTimeNet > PTimeNet
Definition: timenet.h:8
char * CStr()
Definition: dt.h:476
static PGStatVec New(const TTmUnit &_TmUnit=tmu1Sec)
Definition: gstat.cpp:426
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
void Def()
Definition: xmath.cpp:339
TSizeTy AddV(const TVec< TVal, TSizeTy > &ValV)
Adds the elements of the vector ValV to the to end of the vector.
Definition: ds.h:1020
void TakeBasicStat(const PGraph &Graph, const bool &IsMxWcc=false)
Definition: gstat.h:257
void TTimeNet::Save ( TSOut SOut) const
inlinevirtual

Saves the network to a (binary) stream SOut.

Reimplemented from TNodeNet< TSecTm >.

Definition at line 38 of file timenet.h.

38 { TNet::Save(SOut); }
virtual void Save(TSOut &SOut) const
Saves the network to a (binary) stream SOut.
Definition: network.h:146
PGStatVec TTimeNet::TimeGrowth ( const TTmUnit TmUnit,
const TFSet TakeStat,
const TSecTm StartTm 
) const

Definition at line 88 of file timenet.cpp.

88  {
89  PGStatVec GrowthStat = new TGStatVec(TmUnit, TakeStat);
90  TTmBucketV TmBucketV;
91  GetTmBuckets(TmUnit, TmBucketV);
92  TIntV NodeIdV;
93  TExeTm ExeTm;
94  for (int t = 0; t < TmBucketV.Len(); t++) {
95  NodeIdV.AddV(TmBucketV[t].NIdV); // nodes up to time T
96  printf("\n=== %d/%d] %s (%d nodes)\n", t+1, TmBucketV.Len(),
97  TmBucketV[t].BegTm.GetStr().CStr(), NodeIdV.Len()); ExeTm.Tick();
98  if (TmBucketV[t].BegTm < StartTm) continue;
99  //PNGraph PreGraph = GetSubGraph(NodeIdV, true); // renumber nodes
100  PNGraph PreGraph = TSnap::ConvertSubGraph<PNGraph>(PTimeNet((TTimeNet*)this), NodeIdV); // don't renumber nodes
101  GrowthStat->Add(PreGraph, TmBucketV[t].BegTm);
102  }
103  return GrowthStat;
104 }
Definition: tm.h:354
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
void GetTmBuckets(const TTmUnit &GroupBy, TTmBucketV &TmBucketV) const
Definition: timenet.cpp:59
TVec< TTmBucket > TTmBucketV
Definition: timenet.h:32
Graph Statistics Sequence.
Definition: gstat.h:155
void Tick()
Definition: tm.h:363
Definition: bd.h:196
TPt< TTimeNet > PTimeNet
Definition: timenet.h:8
TSizeTy AddV(const TVec< TVal, TSizeTy > &ValV)
Adds the elements of the vector ValV to the to end of the vector.
Definition: ds.h:1020

Friends And Related Function Documentation

friend class TPt< TTimeNet >
friend

Definition at line 65 of file timenet.h.


The documentation for this class was generated from the following files: