All Distance Sketch  0.1
All distance sketch based algorithms
t_skim_reverse_rank.h
Go to the documentation of this file.
1 #ifndef SRC_ALGORITHMS_T_SKIM_REVERSE_RANK_H_
2 #define SRC_ALGORITHMS_T_SKIM_REVERSE_RANK_H_
3 
4 #include "t_skim.h"
5 
6 
10 namespace all_distance_sketch {
11 
14 template<class Z>
15 class TSkimReverseRankCallBacks {
16  public:
17  void SetWantedNodes(const std::unordered_map<int, bool>& nodes) {
18  wanted_nodes_ = nodes;
19  }
20  void InitTSkimReverseRankCallBacks(int T) {
21  T_ = T;
22  num_nodes_visited_ = 0;
23  wanted_nodes_.clear();
24  delta_.clear();
25  visited_nodes_including_self_.clear();
26  }
27 
28  void PrepareForIteration() {
29  num_nodes_visited_ = 0;
30  visited_nodes_including_self_.clear();
31  }
32 
33  inline void Started(int source_node_id, graph::Graph<Z>* graph) {
34  LOG_M(DEBUG3, "Source node id=" << source_node_id);
35  source_node_id_ = source_node_id;
36  graph_ = graph;
37  }
38 
39  inline void NodePopedFromHeap(int poped_node_id, const RankData& heap_value) {
40  if (wanted_nodes_.size() != 0 && wanted_nodes_.count(poped_node_id) == 0) {
41  return;
42  }
43  LOG_M(DEBUG3, "poped node=" << poped_node_id);
44  if (T_ >= heap_value.rank) {
45  LOG_M(DEBUG3, "Inserted node to visited nodes");
46  visited_nodes_including_self_.push_back(poped_node_id);
47  }
48  ++num_nodes_visited_;
49  return;
50  }
51 
52  inline bool ShouldPrune(int visited_node_id, const RankData& rank_data) {
53  LOG_M(DEBUG3, " node id=" << visited_node_id <<
54  " Should prune? " << (rank_data.rank >= T_) <<
55  " current rank =" << rank_data.rank <<
56  " Distance =" << rank_data.distance <<
57  " T = " << T_ <<
58  " Deg= " << graph_->GetNI(visited_node_id).GetOutDeg());
59 
60  if (rank_data.rank > T_) {
61  return true;
62  }
63 
64  if (delta_.count(visited_node_id) == 0 || delta_[visited_node_id] > rank_data.rank ) {
65  delta_[visited_node_id] = rank_data.rank;
66  }
67 
68  if (delta_[visited_node_id] < rank_data.rank) {
69  return true;
70  }
71 
72  return false;
73  }
74 
75  inline bool ShouldStop() {
76  return false;
77  }
78 
79  inline void RelaxedPath(int node_id) { }
80 
81  inline std::vector<int>& get_visited_nodes() {
82  return visited_nodes_including_self_;
83  }
84 
85  std::unordered_map<int, bool> wanted_nodes_;
86  std::unordered_map<int, double> delta_;
87  int source_node_id_;
88  int T_;
89  std::vector<int> visited_nodes_including_self_;
90  int num_nodes_visited_;
91  graph::Graph<Z>* graph_;
92 };
93 
99 template <class Z>
100 class TSkimReverseRank : public TSkimBase<Z> {
101 public:
112  void InitTSkim(int T,
113  int K_all_distance_sketch,
114  int min_influence_for_seed,
115  Cover * cover,
116  graph::Graph<Z>* graph) {
117  K_all_distance_sketch_ = K_all_distance_sketch;
118  graph_sketch_ = NULL;
119  TSkimBase<Z>::InitTSkimBase(T, min_influence_for_seed, cover, graph);
120  }
125  void set_graph_sketch(GraphSketch* graph_sketch) {
126  graph_sketch_ = graph_sketch;
127  }
128 
131  int AddSeed(int seed, std::unordered_map<int, int>* influence_change) {
132  LOG_M(DEBUG3, "seed node = " << seed);
133  std::vector<int> ranking;
134  reverse_rank_call_backs_.PrepareForIteration();
135  CalculateReverseRank<Z, TSkimReverseRankCallBacks<Z> > (seed,
136  &graph_transpose_,
137  graph_sketch_,
138  &ranking,
139  &reverse_rank_call_backs_);
140  LOG_M(DEBUG3, "Adding seed node= " << seed << " Cover size=" << reverse_rank_call_backs_.get_visited_nodes().size() <<
141  " Cover size=" << this->cover_->Size());
142  return TSkimBase<Z>::UpdateCover(seed, influence_change, reverse_rank_call_backs_.get_visited_nodes());
143  }
144 
145  const std::vector<int>& CalculateVisitedNodes(int source_node_id) {
146  NodeIdRandomIdData source_node_details(source_node_id, graph_sketch_->GetNodeRandomId(source_node_id));
147  NodeSketch * source_sketch = graph_sketch_->GetNodeSketch(source_node_details);
148  double distance = source_sketch->GetDistanceCoverNeighborhood(this->T_);
149  typename Z::TNode source(source_node_id);
150  LOG_M(DEBUG3, "Running from node=" << source_node_id << " T=" << this->T_ << " Distance=" << distance);
151  call_backs_.InitTSkimDijkstraCallBacksDistancePrune(distance);
152  PrunedDijkstra< Z, TSkimDijkstraCallBacksDistancePrune<Z> > (source,
153  TSkimBase<Z>::graph_,
154  &call_backs_,
155  &param_);
156  return call_backs_.get_visited_nodes();
157  }
160  int Run() {
161  TSkimBase<Z>::PreRunInit();
162  GraphSketch local_graph_sketch;
163  this->graph_->Transpose(&graph_transpose_);
164  if (graph_sketch_ == NULL) {
165  graph_sketch_ = &local_graph_sketch;
166  graph_sketch_->InitGraphSketch(K_all_distance_sketch_, this->graph_->GetMxNId());
167  CalculateGraphSketch<Z>(&graph_transpose_, graph_sketch_);
168  }
169  reverse_rank_call_backs_.InitTSkimReverseRankCallBacks(this->T_);
170  reverse_rank_call_backs_.SetWantedNodes(this->wanted_nodes_);
171  call_backs_.SetWantedNodes(this->rankees_nodes_);
172  return TSkimBase<Z>::Run(false);
173  }
174 
175 private:
176  int K_all_distance_sketch_;
177  graph::Graph<Z> graph_transpose_;
178  GraphSketch* graph_sketch_;
179  TSkimReverseRankCallBacks<Z> reverse_rank_call_backs_;
180  DijkstraParams param_;
181  TSkimDijkstraCallBacksDistancePrune<Z> call_backs_;
182 };
183 
184 } // namespace all_distance_sketch
185 
186 #endif // SRC_ALGORITHMS_T_SKIM_REVERSE_RANK_H_
void InitGraphSketch(unsigned int K, int max_node_id, const std::vector< int > *nodes_id=NULL)
Init the class.
Definition: graph_sketch.h:28
Single node sketch.
Definition: node_sketch.h:204
Definition: common.h:53
Infuelnce maximization based on reverse ranks.
Definition: t_skim_reverse_rank.h:100
void set_graph_sketch(GraphSketch *graph_sketch)
Sets the graph sketch and prevents the algorithm from performing the calculation Calculating the grap...
Definition: t_skim_reverse_rank.h:125
Contains all TSkim influence maximization algorithms.
void InitTSkim(int T, int K_all_distance_sketch, int min_influence_for_seed, Cover *cover, graph::Graph< Z > *graph)
Initialize the class.
Definition: t_skim_reverse_rank.h:112
Data structure for the graph sketch.
Definition: graph_sketch.h:17
Cover extracted by the influence maximization algorithms.
Definition: t_skim.h:34