1 #ifndef SRC_ALGORITHMS_REVERSE_RANK_H_
2 #define SRC_ALGORITHMS_REVERSE_RANK_H_
5 #include "../sketch/graph_sketch.h"
6 #include "../proto/ranking.pb.h"
15 typedef proto::NodeRanksGpb NodeRanksGpb;
16 typedef proto::NodeRankGpb NodeRankGpb;
38 std::vector<int> * ranking);
60 template <
class T,
class CallBacks>
64 std::vector<int> * ranking,
65 CallBacks* call_backs);
70 const std::vector<int>& ranking,
71 NodeRanksGpb* node_ranks) {
72 node_ranks->set_source_node_id(source_node_id);
73 node_ranks->set_max_node_id(ranking.size());
74 for (
int node_id=0; node_id < ranking.size(); node_id++) {
78 NodeRankGpb* node_rank = node_ranks->add_node_ranks();
79 node_rank->set_node_id(node_id);
80 node_rank->set_node_rank(ranking[node_id]);
84 static void LoadRankingFromGpb(std::vector<int>* ranking,
85 const NodeRanksGpb& node_ranks) {
87 ranking->resize(node_ranks.max_node_id());
88 for (
int i=0; i < node_ranks.node_ranks_size(); i++) {
89 int node_id = node_ranks.node_ranks(i).node_id();
90 int node_ranking = node_ranks.node_ranks(i).node_rank();
91 (*ranking)[node_id] = node_ranking;
93 int source_node_id = node_ranks.source_node_id();
94 (*ranking)[source_node_id] = 0;
104 graph::EdgeWeight distance_from_source_to_targer);
108 RankData(
int _node_id,
int _rank, graph::EdgeWeight _distance) :
109 node_id(_node_id), rank(_rank), distance(_distance) {}
110 RankData() : node_id(-1), rank(-1), distance(constants::
UNREACHABLE) { }
113 graph::EdgeWeight distance;
116 struct compareRankNode {
117 bool operator()(
const RankData& n1,
const RankData& n2)
const {
119 if (n1.rank < n2.rank) {
122 if (n1.rank > n2.rank) {
125 if (n1.distance < n2.distance) {
128 if (n1.distance > n2.distance) {
131 if (n1.node_id < n2.node_id) {
143 graph::EdgeWeight distance_from_source_to_targer) {
144 if (source == target) {
145 LOG_M(DEBUG4,
" Source node is equal to target");
148 NodeIdRandomIdData source_node_details(source, graph_sketch->
GetNodeRandomId(source));
149 NodeSketch * sourceNodeAds = graph_sketch->GetNodeSketch(source_node_details);
150 int reverse_rank = sourceNodeAds->GetSizeNeighborhoodUpToDistance(distance_from_source_to_targer);
151 LOG_M(DEBUG3,
" Source=" << source <<
152 " node=" << target <<
153 " distance= " << distance_from_source_to_targer <<
154 " reverse_rank=" << reverse_rank <<
155 " Node degree=" << graph->GetNI(source).GetOutDeg());
165 graph::EdgeWeight distance_from_source_to_target) {
166 if (source == target) {
167 LOG_M(DEBUG4,
" Source node is equal to target");
170 NodeIdRandomIdData source_node_details(source, graph_sketch->
GetNodeRandomId(source));
171 NodeSketch * sourceNodeAds = graph_sketch->GetNodeSketch(source_node_details);
172 int distance_index = sourceNodeAds->GetNeighborhoodDistanceIndex(distance_from_source_to_target);
173 if (distance_index == -1) {
176 int n_size_up = sourceNodeAds->GetNeighborhoodDistanceByIndex(distance_index);
177 int n_size_down = sourceNodeAds->GetNeighborhoodDistanceByIndex(distance_index - 1);
178 LOG_M(DEBUG3,
" Source=" << source <<
179 " node=" << target <<
180 " distance= " << distance_from_source_to_target <<
181 " n_size_up=" << n_size_up <<
182 " n_size_down=" << n_size_down <<
183 " index=" << distance_index);
185 double reverse_rank =
static_cast<double>(n_size_up + n_size_down + 1) / static_cast<double>(2);
191 class DefaultReverseRankCallBacks {
193 inline void Started(
int source_node_id,
graph::Graph<T>* graph) {
return; }
195 inline void NodePopedFromHeap(
int poped_node_id,
const RankData& heap_value) {
return; }
197 inline bool ShouldPrune(
int visited_node_id,
const RankData& rank_data) {
return false; }
199 inline bool ShouldStop() {
return false; }
201 inline void RelaxedPath(
int node_id) { }
209 std::vector<int> * ranking) {
210 DefaultReverseRankCallBacks<T> default_reverse_rank_call_backs;
214 template <
class T,
class CallBacks>
218 std::vector<int> * ranking,
219 CallBacks* call_backs) {
220 int max_node_id = graph->GetMxNId();
223 (*ranking)[source_node_id] = 0;
224 TBitSet poped; poped.resize(max_node_id);
225 TBitSet touced; touced.resize(max_node_id);
226 std::vector<graph::EdgeWeight> min_distance;
228 min_distance[source_node_id] = 0;
229 std::set< RankData, compareRankNode > heap;
230 touced[source_node_id] =
true;
231 heap.insert(RankData(source_node_id, (*ranking)[source_node_id], 0));
233 LOG_M(DEBUG3,
"Starting Reverse rank from node=" << source_node_id);
234 call_backs->Started(source_node_id, graph);
235 double rank_last = 0;
236 while (!heap.empty()) {
238 int visited_node_id = heap.begin()->node_id;
239 auto node_data = *heap.begin();
240 graph::EdgeWeight distance_from_source_to_visited_node = node_data.distance;
241 (*ranking)[visited_node_id] = node_data.rank;
242 call_backs->NodePopedFromHeap(visited_node_id, node_data);
243 LOG_M(DEBUG3,
"Poped node=" << visited_node_id <<
" Distance=" << node_data.distance <<
244 " Rank=" << node_data.rank);
245 assert(rank_last <= node_data.rank);
246 rank_last = node_data.rank;
248 heap.erase(heap.begin());
249 poped[visited_node_id] =
true;
250 if (call_backs->ShouldPrune(visited_node_id, node_data)) {
254 if (call_backs->ShouldStop()) {
259 graph::TEdgesWeights * nodeWeights = graph->GetNodeWeights(visited_node_id);
260 typename T::TNodeI neighbors = graph->GetNI(visited_node_id);
261 for (
int i = 0 ; i < neighbors.GetOutDeg(); i++) {
262 int id_of_neighbor_of_visited_node = neighbors.GetOutNId(i);
263 LOG_M(DEBUG3,
"neighbors node=" << id_of_neighbor_of_visited_node);
264 if (poped[id_of_neighbor_of_visited_node]) {
267 std::pair<bool, graph::EdgeWeight> edge_u_v = graph->GetEdgeWeight(visited_node_id, id_of_neighbor_of_visited_node);
268 graph::EdgeWeight distance_through_u = distance_from_source_to_visited_node + edge_u_v.second;
269 LOG_M(DEBUG3,
"edge weight between " <<
270 " visited_node_id = " << visited_node_id <<
271 " id_of_neighbor_of_visited_node = " << id_of_neighbor_of_visited_node <<
272 " edge weight = " << (*nodeWeights)[i].get_edge_weight() <<
273 " Distance before relax=" << distance_from_source_to_visited_node <<
274 " distance through visited_node_id=" << distance_through_u);
276 if (touced[id_of_neighbor_of_visited_node] ==
false || distance_through_u < min_distance[id_of_neighbor_of_visited_node]) {
277 int rankOfSourceNode = EstimateReverseRankUpperBound<T>(graph,
279 id_of_neighbor_of_visited_node,
283 if (touced[id_of_neighbor_of_visited_node] ==
false) {
284 LOG_M(DEBUG3,
"Inserting node " << id_of_neighbor_of_visited_node <<
285 " to heap with distance " << distance_through_u);
286 heap.insert(RankData(id_of_neighbor_of_visited_node, rankOfSourceNode, distance_through_u));
288 LOG_M(DEBUG3,
"Decreasing distance of node " << id_of_neighbor_of_visited_node <<
289 " from " << min_distance[id_of_neighbor_of_visited_node] <<
290 " to " << distance_through_u);
291 heap.erase(RankData(id_of_neighbor_of_visited_node, (*ranking)[id_of_neighbor_of_visited_node], min_distance[id_of_neighbor_of_visited_node]));
292 heap.insert(RankData(id_of_neighbor_of_visited_node, rankOfSourceNode, distance_through_u));
294 LOG_M(DEBUG5,
"Updating rank of node " << id_of_neighbor_of_visited_node <<
295 " rank=" << rankOfSourceNode <<
296 " old rank=" << (*ranking)[id_of_neighbor_of_visited_node]);
297 touced[id_of_neighbor_of_visited_node] =
true;
298 min_distance[id_of_neighbor_of_visited_node] = distance_through_u;
299 (*ranking)[id_of_neighbor_of_visited_node] = rankOfSourceNode;
311 #endif // SRC_ALGORITHMS_REVERSE_RANK_H_
Single node sketch.
Definition: node_sketch.h:204
static void SaveRankingToGpb(int source_node_id, const std::vector< int > &ranking, NodeRanksGpb *node_ranks)
saves the ranking to Gpb
Definition: reverse_rank.h:69
Graph data structure Thin wrapper over SNAP graph.
Definition: graph.h:107
RandomId GetNodeRandomId(const int &node_id)
returns specific node random id
Definition: graph_sketch.h:371
static void CalculateReverseRank(int source, graph::Graph< T > *graph, GraphSketch *graph_sketch, std::vector< int > *ranking)
Calculates the reverse ranks of a single node.
const float UNREACHABLE
Definition: common.h:62
Data structure for the graph sketch.
Definition: graph_sketch.h:17