All Distance Sketch  0.1
All distance sketch based algorithms
rank_calculator.h
1 #ifndef THIRD_PARTY_ALL_DISTANCE_SKETCH_ALL_DISTANCE_SKETCH_SKETCH_RANK_CALCULATOR_H_
2 #define THIRD_PARTY_ALL_DISTANCE_SKETCH_ALL_DISTANCE_SKETCH_SKETCH_RANK_CALCULATOR_H_
3 #include "../graph/graph.h"
4 #include "../common.h"
5 
6 
7 namespace all_distance_sketch {
8 
9 class RankCalculatorInterface {
10 public:
11  virtual ~RankCalculatorInterface() {};
12  virtual double CalculateNodeRank(int node_id) = 0;
13 
14  virtual double GetNodeInsertProb(int node_id, double threshold_prob) = 0;
15 };
16 
17 template <class T>
18 class DegreeRankCalculator : public RankCalculatorInterface {
19  public:
20  void InitDegreeRankCalculator(const graph::Graph<T>* graph) {
21  m_graph = graph;
22  // m_rd.reset(new std::random_device());
23  m_gen.reset(new boost::random::mt19937()); //(*m_rd)));
24  m_dis.reset(new boost::random::uniform_real_distribution<>(0, 1));
25  }
26 
27  double CalculateNodeRank(int node_id) {
28  int node_degree = m_graph->GetNI(node_id).GetDeg();
29  double degree_param = node_degree == 0 ? degree_param = 1 : (1 / static_cast<double>(node_degree));
30  m_dis.reset(new boost::random::uniform_real_distribution<>(0, degree_param));
31  return m_dis((*m_gen));
32  }
33 
34  double GetNodeInsertProb(int node_id, double threshold_prob) {
35  int node_degree = m_graph->GetNI(node_id).GetDeg();
36  double degree_param = node_degree == 0 ? degree_param = 1 : (1 / static_cast<double>(node_degree));
37  if (degree_param < threshold_prob) {
38  return 1;
39  }
40 
41  return threshold_prob / degree_param;
42  }
43  private:
44  const graph::Graph<T>* m_graph;
45  // std::unique_ptr<boost::random::random_device> m_rd;
46  std::unique_ptr<boost::random::mt19937 > m_gen;
47  std::unique_ptr<boost::random::uniform_real_distribution<> > m_dis;
48 };
49 
50 typedef boost::minstd_rand base_generator_type;
51 class UniformRankCalculator : public RankCalculatorInterface {
52  public:
53  void InitUniformRankCalculator(double start_range = 0, double end_range = 1) {
54  m_generator.reset(new base_generator_type(42u));
55  m_uni_dist.reset(new boost::uniform_real<>(start_range, end_range));
56  m_dis.reset(new boost::variate_generator<base_generator_type&, boost::uniform_real<> >((*m_generator), (*m_uni_dist)) );
57  }
58 
59  double CalculateNodeRank(int node_id) {
60  return (*m_dis)();
61  }
62 
63  double GetNodeInsertProb(int node_id, double threshold_prob) {
64  return threshold_prob;
65  }
66 
67  private:
68  std::unique_ptr<base_generator_type> m_generator;
69  std::unique_ptr<boost::uniform_real<> > m_uni_dist;
70  std::unique_ptr<boost::variate_generator<base_generator_type&, boost::uniform_real<> > > m_dis;
71 };
72 
73 } // namespace all_distance_sketch
74 #endif // THIRD_PARTY_ALL_DISTANCE_SKETCH_ALL_DISTANCE_SKETCH_SKETCH_RANK_CALCULATOR_H_
Definition: common.h:53