SNAP Library 6.0, Developer Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
Go to the documentation of this file.
1 #ifndef snap_cascnetinf_h
2 #define snap_cascnetinf_h
4 #include "Snap.h"
6 // Hit info (timestamp, candidate parent) about a node in a cascade
7 class THitInfo {
8 public:
10  TFlt Tm;
11 public:
12  THitInfo(const int& NodeId=-1, const double& HitTime=0) : NId(NodeId), Parent(-1), Tm(HitTime) { }
13  THitInfo(TSIn& SIn) : NId(SIn), Parent(SIn), Tm(SIn) { }
14  void Save(TSOut& SOut) const { NId.Save(SOut); Parent.Save(SOut); Tm.Save(SOut); }
15  bool operator < (const THitInfo& Hit) const {
16  return Tm < Hit.Tm; }
17 };
19 // Cascade
20 class TCascade {
21 public:
24  TInt Model;
25 public:
26  TCascade() : NIdHitH(), CurProb(0), Alpha(1.0), Eps(1e-64), Model(0) { }
27  TCascade(const double &alpha) : NIdHitH(), CurProb(0), Alpha(alpha), Eps(1e-64), Model(0) { }
28  TCascade(const double &alpha, const int &model) : NIdHitH(), CurProb(0), Alpha(alpha), Eps(1e-64), Model(model) { }
29  TCascade(const double &alpha, const double &eps) : NIdHitH(), CurProb(0), Alpha(alpha), Eps(eps), Model(0) { }
30  TCascade(const double &alpha, const int &model, const double &eps) : NIdHitH(), CurProb(0), Alpha(alpha), Eps(eps), Model(model) { }
31  TCascade(TSIn& SIn) : NIdHitH(SIn), CurProb(SIn), Alpha(SIn) { }
32  void Save(TSOut& SOut) const { NIdHitH.Save(SOut); CurProb.Save(SOut); Alpha.Save(SOut); }
33  void Clr() { NIdHitH.Clr(); CurProb = 0; Alpha = 1.0; }
34  int Len() const { return NIdHitH.Len(); }
35  int GetNode(const int& i) const { return NIdHitH.GetKey(i); }
36  int GetParent(const int NId) const { return NIdHitH.GetDat(NId).Parent; }
37  double GetAlpha() const { return Alpha; }
38  double GetTm(const int& NId) const { return NIdHitH.GetDat(NId).Tm; }
39  void Add(const int& NId, const double& HitTm) { NIdHitH.AddDat(NId, THitInfo(NId, HitTm)); }
40  void Del(const int& NId) { NIdHitH.DelKey(NId); }
41  bool IsNode(const int& NId) const { return NIdHitH.IsKey(NId); }
42  void Sort() { NIdHitH.SortByDat(true); }
43  double TransProb(const int& NId1, const int& NId2) const;
44  double GetProb(const PNGraph& G);
45  void InitProb();
46  double UpdateProb(const int& N1, const int& N2, const bool& UpdateProb=false);
47 };
49 // Node info (name and number of cascades)
50 class TNodeInfo {
51 public:
52  TStr Name;
53  TInt Vol;
54 public:
55  TNodeInfo() { }
56  TNodeInfo(const TStr& NodeNm, const int& Volume) : Name(NodeNm), Vol(Volume) { }
57  TNodeInfo(TSIn& SIn) : Name(SIn), Vol(SIn) { }
58  void Save(TSOut& SOut) const { Name.Save(SOut); Vol.Save(SOut); }
59 };
61 // Edge info (name and number of cascades)
62 class TEdgeInfo {
63 public:
65  TFlt MarginalGain, MarginalBound, MedianTimeDiff, AverageTimeDiff; // we can skip MarginalBound for efficiency if not explicitly required
66 public:
67  TEdgeInfo() { }
68  TEdgeInfo(const int& v,
69  const double& mg,
70  const double& mb,
71  const double& mt,
72  const double& at) : Vol(v), MarginalGain(mg), MarginalBound(mb), MedianTimeDiff(mt), AverageTimeDiff(at) { }
73  TEdgeInfo(const int& v,
74  const double& mg,
75  const double& mt,
76  const double& at) : Vol(v), MarginalGain(mg), MarginalBound(0), MedianTimeDiff(mt), AverageTimeDiff(at) { }
77  TEdgeInfo(TSIn& SIn) : Vol(SIn), MarginalGain(SIn), MarginalBound(SIn), MedianTimeDiff(SIn), AverageTimeDiff(SIn) { }
78  void Save(TSOut& SOut) const { Vol.Save(SOut); SOut.Save(MarginalGain); SOut.Save(MarginalBound); SOut.Save(MedianTimeDiff); SOut.Save(AverageTimeDiff); } //
79 };
81 // NETINF algorithm class
82 class TNetInfBs {
83 public:
89  THash<TIntPr, TIntV> CascPerEdge; // To implement localized update
96 public:
97  TNetInfBs( ) { BoundOn = false; CompareGroundTruth=false; }
98  TNetInfBs(bool bo, bool cgt) { BoundOn=bo; CompareGroundTruth=cgt; }
99  TNetInfBs(TSIn& SIn) : CascV(SIn), NodeNmH(SIn) { }
100  void Save(TSOut& SOut) const { CascV.Save(SOut); NodeNmH.Save(SOut); }
102  void LoadCascadesTxt(TSIn& SIn, const int& Model, const double& alpha);
103  void LoadGroundTruthTxt(TSIn& SIn);
105  void AddGroundTruth(PNGraph& gt) { GroundTruth = gt; }
107  void AddCasc(const TStr& CascStr, const int& Model=0, const double& alpha=1.0);
108  void AddCasc(const TCascade& Cascade) { CascV.Add(Cascade); }
109  void GenCascade(TCascade& C, const int& TModel, const double &window, TIntPrIntH& EdgesUsed, const double& delta,
110  const double& std_waiting_time=0, const double& std_beta=0);
111  TCascade & GetCasc(int c) { return CascV[c]; }
112  int GetCascs() { return CascV.Len(); }
114  int GetNodes() { return Graph->GetNodes(); }
115  void AddNodeNm(const int& NId, const TNodeInfo& Info) { NodeNmH.AddDat(NId, Info); }
116  TStr GetNodeNm(const int& NId) const { return NodeNmH.GetDat(NId).Name; }
117  TNodeInfo GetNodeInfo(const int& NId) const { return NodeNmH.GetDat(NId); }
118  bool IsNodeNm(const int& NId) const { return NodeNmH.IsKey(NId); }
120  void Init();
121  double GetAllCascProb(const int& EdgeN1, const int& EdgeN2);
122  TIntPr GetBestEdge(double& CurProb, double& LastGain, bool& msort, int &attempts);
123  double GetBound(const TIntPr& Edge, double& CurProb);
124  void GreedyOpt(const int& MxEdges);
126  void SavePajek(const TStr& OutFNm);
127  void SavePlaneTextNet(const TStr& OutFNm);
128  void SaveEdgeInfo(const TStr& OutFNm);
129  void SaveObjInfo(const TStr& OutFNm);
131  void SaveGroundTruth(const TStr& OutFNm);
132  void SaveCascades(const TStr& OutFNm);
133 };
135 #endif
TFlt MarginalGain
Definition: cascnetinf.h:65
void AddNodeNm(const int &NId, const TNodeInfo &Info)
Definition: cascnetinf.h:115
TIntPrFltH Alphas
Definition: cascnetinf.h:94
void Save(TSOut &SOut) const
Definition: cascnetinf.h:78
void AddGroundTruth(PNGraph &gt)
Definition: cascnetinf.h:105
void Save(TSOut &SOut) const
Definition: cascnetinf.h:32
TFlt Eps
Definition: cascnetinf.h:23
THitInfo(const int &NodeId=-1, const double &HitTime=0)
Definition: cascnetinf.h:12
TEdgeInfo(TSIn &SIn)
Definition: cascnetinf.h:77
TInt NId
Definition: cascdynetinf.h:70
TFlt CurProb
Definition: cascnetinf.h:23
PNGraph Graph
Definition: cascnetinf.h:90
TCascade(TSIn &SIn)
Definition: cascnetinf.h:31
TEdgeInfo(const int &v, const double &mg, const double &mt, const double &at)
Definition: cascnetinf.h:73
void Save(TSOut &SOut) const
Definition: dt.h:1153
TFlt MedianTimeDiff
Definition: cascnetinf.h:65
int Len() const
Definition: cascnetinf.h:34
TNetInfBs(bool bo, bool cgt)
Definition: cascnetinf.h:98
double GetProb(const PNGraph &G)
Definition: cascnetinf.cpp:17
void LoadCascadesTxt(TSIn &SIn, const int &Model, const double &alpha)
Definition: cascnetinf.cpp:62
int GetParent(const int NId) const
Definition: cascnetinf.h:36
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
double TransProb(const int &NId1, const int &NId2) const
Definition: cascnetinf.cpp:4
TEdgeInfo(const int &v, const double &mg, const double &mb, const double &mt, const double &at)
Definition: cascnetinf.h:68
void Save(TSOut &SOut) const
Definition: hash.h:183
TFlt MarginalBound
Definition: cascnetinf.h:65
Definition: cascdynetinf.h:7
TNodeInfo(const TStr &NodeNm, const int &Volume)
Definition: cascnetinf.h:56
TNodeInfo(TSIn &SIn)
Definition: cascnetinf.h:57
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:503
void SavePlaneTextNet(const TStr &OutFNm)
Definition: cascnetinf.cpp:455
bool BoundOn
Definition: cascnetinf.h:91
THash< TInt, THitInfo > NIdHitH
Definition: cascdynetinf.h:87
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
void SaveObjInfo(const TStr &OutFNm)
Definition: cascnetinf.cpp:485
bool operator<(const THitInfo &Hit) const
Definition: cascdynetinf.h:79
double UpdateProb(const int &N1, const int &N2, const bool &UpdateProb=false)
Definition: cascnetinf.cpp:47
THash< TInt, TNodeInfo > NodeNmH
Definition: cascnetinf.h:85
Definition: dt.h:1386
void Save(TSOut &SOut) const
Definition: cascnetinf.h:100
THash< TIntPr, TIntV > CascPerEdge
Definition: cascnetinf.h:89
Definition: fl.h:58
void Save(TSOut &SOut) const
Definition: ds.h:954
TCascade & GetCasc(int c)
Definition: cascnetinf.h:111
void SaveEdgeInfo(const TStr &OutFNm)
Definition: cascnetinf.cpp:470
void Init()
Definition: cascnetinf.cpp:216
TInt Model
Definition: cascdynetinf.h:88
bool CompareGroundTruth
Definition: cascnetinf.h:91
double GetTm(const int &NId) const
Definition: cascnetinf.h:38
TFlt Alpha
Definition: cascnetinf.h:23
void AddCasc(const TCascade &Cascade)
Definition: cascnetinf.h:108
THitInfo(TSIn &SIn)
Definition: cascnetinf.h:13
void DelKey(const TKey &Key)
Definition: hash.h:404
TVec< TPair< TFlt, TIntPr > > EdgeGainV
Definition: cascnetinf.h:87
TCascade(const double &alpha, const int &model)
Definition: cascnetinf.h:28
void GreedyOpt(const int &MxEdges)
Definition: cascnetinf.cpp:374
int GetNodes()
Definition: cascnetinf.h:114
TStr GetNodeNm(const int &NId) const
Definition: cascnetinf.h:116
void InitProb()
Definition: cascnetinf.cpp:40
void Save(TSOut &SOut, const bool &IsSmall=false) const
Definition: dt.h:440
TInt Parent
Definition: cascnetinf.h:9
bool IsNodeNm(const int &NId) const
Definition: cascnetinf.h:118
void SaveCascades(const TStr &OutFNm)
Definition: cascnetinf.cpp:524
void Del(const int &NId)
Definition: cascnetinf.h:40
void Save(TSOut &SOut) const
Definition: cascnetinf.h:14
void SavePajek(const TStr &OutFNm)
Definition: cascnetinf.cpp:439
THash< TIntPr, TEdgeInfo > EdgeInfoH
Definition: cascnetinf.h:86
Definition: fl.h:128
void Clr()
Definition: cascnetinf.h:33
void Save(const bool &Bool)
Definition: fl.h:173
Definition: cascnetinf.h:26
TNodeInfo GetNodeInfo(const int &NId) const
Definition: cascnetinf.h:117
TFlt AverageTimeDiff
Definition: cascnetinf.h:65
void GenCascade(TCascade &C, const int &TModel, const double &window, TIntPrIntH &EdgesUsed, const double &delta, const double &std_waiting_time=0, const double &std_beta=0)
Definition: cascnetinf.cpp:113
Definition: dt.h:1137
double GetAlpha() const
Definition: cascnetinf.h:37
PNGraph GroundTruth
Definition: cascnetinf.h:90
Definition: ds.h:32
TVec< TCascade > CascV
Definition: cascnetinf.h:84
void AddCasc(const TStr &CascStr, const int &Model=0, const double &alpha=1.0)
Definition: cascnetinf.cpp:98
Definition: dt.h:412
TIntPrFltH Betas
Definition: cascnetinf.h:94
TCascade(const double &alpha)
Definition: cascnetinf.h:27
void Add(const int &NId, const double &HitTm)
Definition: cascnetinf.h:39
TNetInfBs(TSIn &SIn)
Definition: cascnetinf.h:99
double GetAllCascProb(const int &EdgeN1, const int &EdgeN2)
Definition: cascnetinf.cpp:255
TCascade(const double &alpha, const double &eps)
Definition: cascnetinf.h:29
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
Definition: bd.h:196
void LoadGroundTruthTxt(TSIn &SIn)
Definition: cascnetinf.cpp:75
TInt Vol
Definition: cascnetinf.h:64
int GetNode(const int &i) const
Definition: cascnetinf.h:35
void Save(TSOut &SOut) const
Definition: cascnetinf.h:58
bool IsKey(const TKey &Key) const
Definition: hash.h:258
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
int Len() const
Definition: hash.h:228
TFltPrV PrecisionRecall
Definition: cascnetinf.h:92
void Sort()
Definition: cascnetinf.h:42
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
double GetBound(const TIntPr &Edge, double &CurProb)
Definition: cascnetinf.cpp:356
void Save(TSOut &SOut) const
Definition: dt.h:1402
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
bool IsNode(const int &NId) const
Definition: cascnetinf.h:41
void SaveGroundTruth(const TStr &OutFNm)
Definition: cascnetinf.cpp:502
int GetCascs()
Definition: cascnetinf.h:112
TCascade(const double &alpha, const int &model, const double &eps)
Definition: cascnetinf.h:30
void SortByDat(const bool &Asc=true)
Definition: hash.h:292
TIntPr GetBestEdge(double &CurProb, double &LastGain, bool &msort, int &attempts)
Definition: cascnetinf.cpp:271