Skip to content

Commit

Permalink
Merge pull request #388 from bedupako12mas/centrality-week4
Browse files Browse the repository at this point in the history
Centrality Week 4
  • Loading branch information
bedupako12mas authored Jun 24, 2024
2 parents ab240ab + 6e1eaff commit ff67a34
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 137 deletions.
3 changes: 1 addition & 2 deletions docqueries/metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Do not use extensions
SET(LOCAL_FILES
doc-floydWarshall
doc-johnson
centrality.pg
)

foreach (f ${LOCAL_FILES})
Expand Down
2 changes: 1 addition & 1 deletion docqueries/metrics/centrality.pg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- CopyRight(c) pgRouting developers
-- Creative Commons Attribution-Share Alike 3.0 License : https://creativecommons.org/licenses/by-sa/3.0/
/* -- q1 */
SELECT * FROM pgr_floydWarshall(
SELECT * FROM pgr_centrality(
'SELECT id, source, target, cost, reverse_cost
FROM edges where id < 5'
) ORDER BY start_vid, end_vid;
Expand Down
2 changes: 1 addition & 1 deletion docqueries/metrics/centrality.result
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ BEGIN
SET client_min_messages TO NOTICE;
SET
/* -- q1 */
SELECT * FROM pgr_floydWarshall(
SELECT * FROM pgr_centrality(
'SELECT id, source, target, cost, reverse_cost
FROM edges where id < 5'
) ORDER BY start_vid, end_vid;
Expand Down
12 changes: 2 additions & 10 deletions docqueries/metrics/test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@

%main::tests = (
'any' => {
'comment' => 'All pairs tests.',
'data' => [''],
'tests' => [qw(
doc-johnson
doc-floydWarshall
)],

'documentation' => [qw(
doc-johnson
doc-floydWarshall
'files' => [qw(
centrality.pg
)]
},
# I don't know what this are for or how to use them.
Expand Down
169 changes: 50 additions & 119 deletions include/metrics/centrality.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#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>

Expand Down Expand Up @@ -68,134 +70,63 @@ pgr_centrality(
size_t &result_tuple_count,
IID_t_rt **postgres_rows) {
Pgr_metrics< G > fn_centrality;
fn_centrality.centrality(graph, result_tuple_count, postgres_rows);
fn_centrality.betweennessCentrality(graph, result_tuple_count, postgres_rows);
}


// template class
template < class G >
class Pgr_metrics {
public:
void centrality(
G &graph,
size_t &result_tuple_count,
IID_t_rt **postgres_rows) {
std::vector< std::vector<double>> matrix;
make_matrix(graph.num_vertices(), matrix);
inf_plus<double> combine;

/* abort in case of an interruption occurs (e.g. the query is being cancelled) */
CHECK_FOR_INTERRUPTS();

boost::floyd_warshall_all_pairs_shortest_paths(
graph.graph,
matrix,
weight_map(get(&pgrouting::Basic_edge::cost, graph.graph)).
distance_combine(combine).
distance_inf((std::numeric_limits<double>::max)()).
distance_zero(0));

make_result(graph, matrix, result_tuple_count, postgres_rows);
}

void centrality(
G &graph,
std::vector< IID_t_rt> &rows) {
std::vector< std::vector<double>> matrix;
make_matrix(graph.num_vertices(), matrix);
inf_plus<double> combine;

/* abort in case of an interruption occurs (e.g. the query is being cancelled) */
CHECK_FOR_INTERRUPTS();

boost::floyd_warshall_all_pairs_shortest_paths(
graph.graph,
matrix,
weight_map(get(&pgrouting::Basic_edge::cost, graph.graph)).
distance_combine(combine).
distance_inf((std::numeric_limits<double>::max)()).
distance_zero(0));

make_result(graph, matrix, rows);
}
using Graph = typename G::B_G;
using Vertex = typename G::V;

void betweennessCentrality (
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));

/* 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)
)
)
);

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);

}

private:
void make_matrix(
size_t v_size,
std::vector< std::vector<double>> &matrix) const {
// TODO(vicky) in one step
matrix.resize(v_size);
for (size_t i=0; i < v_size; i++)
matrix[i].resize(v_size);
}

void make_result(
const G &graph,
const std::vector< std::vector<double> > &matrix,
size_t &result_tuple_count,
IID_t_rt **postgres_rows) const {
result_tuple_count = count_rows(graph, matrix);
*postgres_rows = pgr_alloc(result_tuple_count, (*postgres_rows));


size_t seq = 0;
for (typename G::V v_i = 0; v_i < graph.num_vertices(); v_i++) {
for (typename G::V v_j = 0; v_j < graph.num_vertices(); v_j++) {
if (v_i == v_j) continue;
if (matrix[v_i][v_j] != (std::numeric_limits<double>::max)()) {
(*postgres_rows)[seq].from_vid = graph[v_i].id;
(*postgres_rows)[seq].to_vid = graph[v_j].id;
(*postgres_rows)[seq].cost = matrix[v_i][v_j];
seq++;
} // if
} // for j
} // for i
}


size_t count_rows(
const G &graph,
const std::vector< std::vector<double> > &matrix) const {
size_t result_tuple_count = 0;
for (size_t i = 0; i < graph.num_vertices(); i++) {
for (size_t j = 0; j < graph.num_vertices(); j++) {
if (i == j) continue;
if (matrix[i][j] != (std::numeric_limits<double>::max)()) {
result_tuple_count++;
} // if
} // for j
} // for i
return result_tuple_count;
}

void make_result(
G &graph,
std::vector< std::vector<double> > &matrix,
std::vector< IID_t_rt> &rows) {
size_t count = count_rows(graph, matrix);
rows.resize(count);
size_t seq = 0;

for (typename G::V v_i = 0; v_i < graph.num_vertices(); v_i++) {
for (typename G::V v_j = 0; v_j < graph.num_vertices(); v_j++) {
if (matrix[v_i][v_j] != (std::numeric_limits<double>::max)()) {
rows[seq] =
{graph[v_i].id, graph[v_j].id, matrix[v_i][v_j]};
seq++;
} // if
} // for j
} // for i
}

template <typename T>
struct inf_plus {
T operator()(const T& a, const T& b) const {
T inf = (std::numeric_limits<T>::max)();
if (a == inf || b == inf)
return inf;
return a + b;
}
};
void generate_results(
const std::map<int64_t, 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;
(*postgres_rows)[seq].to_vid = 0;
(*postgres_rows)[seq].cost = results.second;
seq++;
}
}
};

} // namespace pgrouting
Expand Down
7 changes: 3 additions & 4 deletions src/metrics/centrality_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <string>
//TODO: Right now this is doing the floyd warshall, creating the centrality algorithm is pending
#include "metrics/centrality.hpp"
#include "allpairs/pgr_allpairs.hpp"
#include "cpp_common/pgdata_getters.hpp"

#include "cpp_common/pgr_assert.hpp"
Expand Down Expand Up @@ -75,12 +74,12 @@ pgr_do_centrality(
log << "Processing Directed graph\n";
pgrouting::DirectedGraph digraph;
digraph.insert_edges(edges);
pgr_floydWarshall(digraph, *return_count, return_tuples);
pgr_centrality(digraph, *return_count, return_tuples);
} else {
log << "Processing Undirected graph\n";
pgrouting::UndirectedGraph undigraph;
undigraph.insert_edges(edges);
pgr_floydWarshall(undigraph, *return_count, return_tuples);
pgr_centrality(undigraph, *return_count, return_tuples);
}


Expand Down Expand Up @@ -111,7 +110,7 @@ pgr_do_centrality(
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
} catch(...) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << "Caught unknown exception!";
*err_msg = pgr_msg(err.str().c_str());
Expand Down

0 comments on commit ff67a34

Please sign in to comment.