8 #ifndef snap_core_randwalk_h
9 #define snap_core_randwalk_h
20 gettimeofday(&t, NULL);
21 return t.tv_sec + t.tv_usec / 1.0e6;
31 template <
class PGraph>
35 float ForwardSecondsRMaxRatio,
38 float& ResultMaxResidual) {
41 nodesByResidual.
Insert(TargetNId, 1.0f);
42 while (!nodesByResidual.
IsEmpty() &&
45 int vId = nodesByResidual.
PopMax();
46 ResultEstimates(vId) = ResultEstimates(vId) + JumpProb * vPriority;
49 for (
int i = 0; i < v.
GetInDeg(); i++) {
52 float residualChange = (1.0 - JumpProb) * vPriority / u.
GetOutDeg();
66 template <
class PGraph>
68 int locationId = StartNIdV.
GetRndVal(Rnd);
87 template <
class PGraph>
90 const TIntV& StartNIdV,
92 double MinProbability = -1.0,
93 double RelativeError = 0.1,
94 bool provableRelativeError =
false,
95 bool PrintTimeForTuning =
false) {
96 if (MinProbability <= 0.0) {
97 MinProbability = 1.0 / Graph->GetNodes();
100 float kChernoffConstant = provableRelativeError ? 12 * exp((
double) 1) * log(2 / 1.0e-9) : 0.07;
101 float kSecondsPerWalk = 4.0e-7;
102 float WalkCountRMaxRatio = kChernoffConstant / (RelativeError * RelativeError) / MinProbability;
103 float ForwardSecondsRMaxRatio = kSecondsPerWalk * WalkCountRMaxRatio;
115 double Estimate = 0.0;
117 for (
int i = 0; i < StartNIdV.
Len(); i++) {
121 int RandomWalkCount =
static_cast<int>(WalkCountRMaxRatio * MaxResidual);
123 for (
int i = 0; i < RandomWalkCount; i++) {
128 if (PrintTimeForTuning) printf(
"forwardTime reverseTime %g %g\n", forwardTime, reverseTime);
134 template <
class PGraph>
139 double minProbability = -1.0,
140 double relativeError = 0.1,
141 bool proveRelativeError =
false,
142 bool PrintTimeForTuning =
false) {
144 minProbability, relativeError, proveRelativeError, PrintTimeForTuning);
void ApproxContributionsBalanced(const PGraph &Graph, double JumpProb, int TargetNId, float ForwardSecondsRMaxRatio, TIntFltH &ResultEstimates, TIntFltH &ResultResiduals, float &ResultMaxResidual)
float GetPriority(const TVal &X)
void GetPriorities(THash< TVal, TFlt > &Result)
double GetRndWalkRestartBidirectional(const PGraph &Graph, double JumpProb, int StartNId, int TargetNId, double minProbability=-1.0, double relativeError=0.1, bool proveRelativeError=false, bool PrintTimeForTuning=false)
TSizeTy Len() const
Returns the number of elements in the vector.
TDat GetDatWithDefault(const TKey &Key, TDat DefaultValue)
int SamplePersonalizedPageRank(const PGraph &Graph, double JumpProb, const TIntV &StartNIdV, TRnd &Rnd)
const TVal & GetRndVal(TRnd &Rnd=TInt::Rnd) const
Returns a reference to a random element in the vector.
void SetPriority(const TVal &X, float NewPriority)
int GetOutDeg() const
Returns out-degree of the current node.
void Insert(const TVal &X, float Priority)
Node iterator. Only forward iteration (operator++) is supported.
int GetUniDevInt(const int &Range=0)
static TVec< TInt, TSizeTy > GetV(const TInt &Val1)
Returns a vector on element Val1.
int GetInDeg() const
Returns in-degree of the current node.
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
double GetPersonalizedPageRankBidirectional(const PGraph &Graph, double JumpProb, const TIntV &StartNIdV, int TargetNId, double MinProbability=-1.0, double RelativeError=0.1, bool provableRelativeError=false, bool PrintTimeForTuning=false)
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Vector is a sequence TVal objects representing an array that can change in size.