Skip to content

Commit

Permalink
[centrality] made betweenessCentrality.hpp code better
Browse files Browse the repository at this point in the history
  • Loading branch information
bedupako12mas committed Jun 27, 2024
1 parent 78d66d8 commit f05e956
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions include/metrics/betweennessCentrality.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#define INCLUDE_METRICS_BETWEENNESSCENTRALITY_HPP_
#pragma once

#include <deque>
#include <vector>
#include <set>
#include <limits>
#include <map>

#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/betweenness_centrality.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/johnson_all_pairs_shortest.hpp>
#include <boost/graph/floyd_warshall_shortest.hpp>

#include "c_types/iid_t_rt.h"
#include "cpp_common/basePath_SSEC.hpp"
Expand Down Expand Up @@ -85,46 +81,41 @@ class Pgr_metrics {
const G &graph,
size_t &result_tuple_count,
IID_t_rt **postgres_rows ){
std::map<int64_t, double> centrality_map;
std::vector<double> centrality_score(boost::num_vertices(graph.graph));

std::vector<double> centrality(boost::num_vertices(graph.graph), 0.0);
auto centrality_map = boost::make_iterator_property_map(centrality.begin(),
boost::get(boost::vertex_index, graph.graph)
);

/* abort in case of an interruption occurs (e.g. the query is being cancelled) */
CHECK_FOR_INTERRUPTS();
boost::brandes_betweenness_centrality(
graph.graph,
boost::centrality_map(
boost::make_iterator_property_map(
centrality_score.begin(),
boost::get(boost::vertex_index, graph.graph)
)
)
centrality_map
);
boost::brandes_betweenness_centrality(
graph.graph,
centrality_map
);

typename boost::graph_traits<Graph>::vertex_iterator vi, vi_end;
for(boost::tie(vi, vi_end) = boost::vertices(graph.graph); vi != vi_end; ++vi) {
int64_t id = graph.graph[*vi].id;
centrality_map[id] = centrality_score[boost::get(boost::vertex_index, graph.graph, *vi)];
}

generate_results(centrality_map, result_tuple_count, postgres_rows);

generate_results(graph, centrality, result_tuple_count, postgres_rows);
}

private:
void generate_results(
const std::map<int64_t, double> centrality_results,
const G &graph,
const std::vector<double> centrality_results,
size_t &result_tuple_count,
IID_t_rt **postgres_rows) const {
result_tuple_count = centrality_results.size();
*postgres_rows = pgr_alloc(result_tuple_count, (*postgres_rows));


size_t seq = 0;
for(auto results : centrality_results) {
(*postgres_rows)[seq].from_vid = results.first;
for(typename G::V v_i = 0; v_i < graph.num_vertices(); ++v_i) {
(*postgres_rows)[seq].from_vid = graph[v_i].id;
(*postgres_rows)[seq].to_vid = 0;
(*postgres_rows)[seq].cost = results.second;
seq++;
(*postgres_rows)[seq].cost = centrality_results[v_i];
seq++;
}
}
};
Expand Down

0 comments on commit f05e956

Please sign in to comment.