From 5d1602a5ab24ae7a79571160d53e7a22b143cf37 Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Tue, 25 Jun 2024 03:23:58 +0530 Subject: [PATCH 01/14] [centrality] changed centrality.hpp to betweennessCentrality.hpp --- include/metrics/{centrality.hpp => betweennessCentrality.hpp} | 0 src/metrics/centrality_driver.cpp | 3 +-- 2 files changed, 1 insertion(+), 2 deletions(-) rename include/metrics/{centrality.hpp => betweennessCentrality.hpp} (100%) diff --git a/include/metrics/centrality.hpp b/include/metrics/betweennessCentrality.hpp similarity index 100% rename from include/metrics/centrality.hpp rename to include/metrics/betweennessCentrality.hpp diff --git a/src/metrics/centrality_driver.cpp b/src/metrics/centrality_driver.cpp index d8c5f11377..93d7a9c1d5 100644 --- a/src/metrics/centrality_driver.cpp +++ b/src/metrics/centrality_driver.cpp @@ -33,8 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include #include -//TODO: Right now this is doing the floyd warshall, creating the centrality algorithm is pending -#include "metrics/centrality.hpp" +#include "metrics/betweennessCentrality.hpp" #include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_assert.hpp" From 78d66d87b822ac529503c34fdedc525bffa820a8 Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Fri, 28 Jun 2024 01:18:24 +0530 Subject: [PATCH 02/14] [centrality] name change to betweennessCentrality.hpp --- include/metrics/betweennessCentrality.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/metrics/betweennessCentrality.hpp b/include/metrics/betweennessCentrality.hpp index e4e59fddf2..149ca419fc 100644 --- a/include/metrics/betweennessCentrality.hpp +++ b/include/metrics/betweennessCentrality.hpp @@ -1,5 +1,5 @@ /*PGR-GNU***************************************************************** -File: centrality.hpp +File: betweennessCentrality.hpp Copyright (c) 2015 pgRouting developers Mail: project@pgrouting.org @@ -26,8 +26,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -#ifndef INCLUDE_METRICS_CENTRALITY_HPP_ -#define INCLUDE_METRICS_CENTRALITY_HPP_ +#ifndef INCLUDE_METRICS_BETWEENNESSCENTRALITY_HPP_ +#define INCLUDE_METRICS_BETWEENNESSCENTRALITY_HPP_ #pragma once #include @@ -131,4 +131,4 @@ class Pgr_metrics { } // namespace pgrouting -#endif // INCLUDE_METRICS_CENTRALITY_HPP_ +#endif // INCLUDE_METRICS_BETWEENNESSCENTRALITY_HPP_ From f05e95678e73f23ef779916a4b5958c00caf0b8c Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Fri, 28 Jun 2024 03:03:36 +0530 Subject: [PATCH 03/14] [centrality] made betweenessCentrality.hpp code better --- include/metrics/betweennessCentrality.hpp | 45 +++++++++-------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/include/metrics/betweennessCentrality.hpp b/include/metrics/betweennessCentrality.hpp index 149ca419fc..a4f4fd1beb 100644 --- a/include/metrics/betweennessCentrality.hpp +++ b/include/metrics/betweennessCentrality.hpp @@ -30,18 +30,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_METRICS_BETWEENNESSCENTRALITY_HPP_ #pragma once -#include #include -#include -#include +#include #include #include #include #include #include -#include -#include #include "c_types/iid_t_rt.h" #include "cpp_common/basePath_SSEC.hpp" @@ -85,46 +81,41 @@ class Pgr_metrics { const G &graph, size_t &result_tuple_count, IID_t_rt **postgres_rows ){ - std::map centrality_map; - std::vector centrality_score(boost::num_vertices(graph.graph)); + + std::vector 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::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 centrality_results, + const G &graph, + const std::vector 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++; } } }; From f27112b0ae04f2a8920fea41d2273d000a8a6c8b Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Sat, 29 Jun 2024 00:54:21 +0530 Subject: [PATCH 04/14] [centrality] minor changes to switch to betweennessCentrality --- include/drivers/metrics/centrality_driver.h | 8 ++++---- include/metrics/betweennessCentrality.hpp | 13 +++---------- sql/metrics/centrality.sql | 2 +- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/include/drivers/metrics/centrality_driver.h b/include/drivers/metrics/centrality_driver.h index a9a6f21034..93a50b952b 100644 --- a/include/drivers/metrics/centrality_driver.h +++ b/include/drivers/metrics/centrality_driver.h @@ -1,5 +1,5 @@ /*PGR-GNU***************************************************************** -File: floydWarshall_driver.h +File: betweennessCentrality_driver.h Generated with Template by: Copyright (c) 2015 pgRouting developers @@ -27,8 +27,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -#ifndef INCLUDE_DRIVERS_METRICS_CENTRALITY_DRIVER_H_ -#define INCLUDE_DRIVERS_METRICS_CENTRALITY_DRIVER_H_ +#ifndef INCLUDE_DRIVERS_METRICS_BETWEENNESSCENTRALITY_DRIVER_H_ +#define INCLUDE_DRIVERS_METRICS_BETWEENNESSCENTRALITY_DRIVER_H_ #pragma once /* for size-t */ @@ -61,4 +61,4 @@ pgr_do_centrality( } #endif -#endif // INCLUDE_DRIVERS_METRICS_CENTRALITY_DRIVER_H_ +#endif // INCLUDE_DRIVERS_METRICS_BETWEENNESSCENTRALITY_DRIVER_H_ diff --git a/include/metrics/betweennessCentrality.hpp b/include/metrics/betweennessCentrality.hpp index a4f4fd1beb..d5b29f7372 100644 --- a/include/metrics/betweennessCentrality.hpp +++ b/include/metrics/betweennessCentrality.hpp @@ -48,18 +48,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/pgr_alloc.hpp" namespace pgrouting { -template < class G > class Pgr_metrics; +template class Pgr_metrics; -// user's functions -template < class G > -void -pgr_centrality(G &graph, std::vector< IID_t_rt> &rows) { - Pgr_metrics< G > fn_centrality; - fn_centrality.centrality(graph, rows); -} // for postgres -template < class G > +template void pgr_centrality( G &graph, @@ -71,7 +64,7 @@ pgr_centrality( // template class -template < class G > +template class Pgr_metrics { public: using Graph = typename G::B_G; diff --git a/sql/metrics/centrality.sql b/sql/metrics/centrality.sql index 08307b906f..48b468de9c 100644 --- a/sql/metrics/centrality.sql +++ b/sql/metrics/centrality.sql @@ -31,7 +31,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. --v3.7 CREATE FUNCTION pgr_centrality( TEXT, -- edges_sql (required) - directed BOOLEAN DEFAULT true, + directed BOOLEAN DEFAULT false, OUT start_vid BIGINT, OUT end_vid BIGINT, From 812db24ed58d98c8049fa83b7a2f6bbaa5bc4a7c Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Sat, 29 Jun 2024 02:30:05 +0530 Subject: [PATCH 05/14] [centrality] minor changes to src files to switch to betweennessCentrality --- include/drivers/metrics/centrality_driver.h | 64 --------- src/metrics/CMakeLists.txt | 4 +- src/metrics/centrality.c | 143 -------------------- src/metrics/centrality_driver.cpp | 118 ---------------- 4 files changed, 2 insertions(+), 327 deletions(-) delete mode 100644 include/drivers/metrics/centrality_driver.h delete mode 100644 src/metrics/centrality.c delete mode 100644 src/metrics/centrality_driver.cpp diff --git a/include/drivers/metrics/centrality_driver.h b/include/drivers/metrics/centrality_driver.h deleted file mode 100644 index 93a50b952b..0000000000 --- a/include/drivers/metrics/centrality_driver.h +++ /dev/null @@ -1,64 +0,0 @@ -/*PGR-GNU***************************************************************** -File: betweennessCentrality_driver.h - -Generated with Template by: -Copyright (c) 2015 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2024 Arun Thakur -Mail: bedupako12mas at gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_DRIVERS_METRICS_BETWEENNESSCENTRALITY_DRIVER_H_ -#define INCLUDE_DRIVERS_METRICS_BETWEENNESSCENTRALITY_DRIVER_H_ -#pragma once - -/* for size-t */ -#ifdef __cplusplus -# include -using IID_t_rt = struct IID_t_rt; -#else -# include -#include -typedef struct IID_t_rt IID_t_rt; -#endif - - - -#ifdef __cplusplus -extern "C" { -#endif - -void -pgr_do_centrality( - char*, - bool, - - IID_t_rt**, - size_t*, - char**, - char**); - -#ifdef __cplusplus -} -#endif - -#endif // INCLUDE_DRIVERS_METRICS_BETWEENNESSCENTRALITY_DRIVER_H_ diff --git a/src/metrics/CMakeLists.txt b/src/metrics/CMakeLists.txt index 4d082e613a..c88a1dfd8e 100644 --- a/src/metrics/CMakeLists.txt +++ b/src/metrics/CMakeLists.txt @@ -1,4 +1,4 @@ ADD_LIBRARY(metrics OBJECT - centrality.c - centrality_driver.cpp + betweennessCentrality.c + betweennessCentrality_driver.cpp ) diff --git a/src/metrics/centrality.c b/src/metrics/centrality.c deleted file mode 100644 index 34705c8118..0000000000 --- a/src/metrics/centrality.c +++ /dev/null @@ -1,143 +0,0 @@ -/*PGR-GNU***************************************************************** -File: centrality.c - -Generated with Template by: -Copyright (c) 2015 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2024 Arun Thakur -Mail: bedupako12mas at gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include -#include "c_common/postgres_connection.h" - -#include "c_types/iid_t_rt.h" -#include "c_common/debug_macro.h" -#include "c_common/e_report.h" -#include "c_common/time_msg.h" - -#include "drivers/metrics/centrality_driver.h" - -PGDLLEXPORT Datum _pgr_centrality(PG_FUNCTION_ARGS); -PG_FUNCTION_INFO_V1(_pgr_centrality); - -static -void -process( - char* edges_sql, - bool directed, - IID_t_rt **result_tuples, - size_t *result_count) { - pgr_SPI_connect(); - char* log_msg = NULL; - char* notice_msg = NULL; - char* err_msg = NULL; - - clock_t start_t = clock(); - pgr_do_centrality( - edges_sql, - directed, - result_tuples, - result_count, - &log_msg, - &err_msg); - time_msg(" processing Centrality", start_t, clock()); - - if (err_msg && (*result_tuples)) { - pfree(*result_tuples); - (*result_tuples) = NULL; - (*result_count) = 0; - } - - pgr_global_report(log_msg, notice_msg, err_msg); - - - if (log_msg) pfree(log_msg); - if (notice_msg) pfree(notice_msg); - if (err_msg) pfree(err_msg); - pgr_SPI_finish(); -} - - -PGDLLEXPORT Datum -_pgr_centrality(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - TupleDesc tuple_desc; - - - IID_t_rt *result_tuples = NULL; - size_t result_count = 0; - - if (SRF_IS_FIRSTCALL()) { - MemoryContext oldcontext; - funcctx = SRF_FIRSTCALL_INIT(); - oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - - - process( - text_to_cstring(PG_GETARG_TEXT_P(0)), - PG_GETARG_BOOL(1), - &result_tuples, - &result_count); - - funcctx->max_calls = result_count; - funcctx->user_fctx = result_tuples; - if (get_call_result_type(fcinfo, NULL, &tuple_desc) - != TYPEFUNC_COMPOSITE) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("function returning record called in context " - "that cannot accept type record"))); - - funcctx->tuple_desc = tuple_desc; - MemoryContextSwitchTo(oldcontext); - } - - funcctx = SRF_PERCALL_SETUP(); - tuple_desc = funcctx->tuple_desc; - result_tuples = (IID_t_rt*) funcctx->user_fctx; - - if (funcctx->call_cntr < funcctx->max_calls) { - HeapTuple tuple; - Datum result; - Datum *values; - bool* nulls; - - values = palloc(3 * sizeof(Datum)); - nulls = palloc(3 * sizeof(bool)); - - // postgres starts counting from 1 - values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_vid); - nulls[0] = false; - values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].to_vid); - nulls[1] = false; - values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); - nulls[2] = false; - - tuple = heap_form_tuple(tuple_desc, values, nulls); - result = HeapTupleGetDatum(tuple); - SRF_RETURN_NEXT(funcctx, result); - } else { - SRF_RETURN_DONE(funcctx); - } -} - diff --git a/src/metrics/centrality_driver.cpp b/src/metrics/centrality_driver.cpp deleted file mode 100644 index 93d7a9c1d5..0000000000 --- a/src/metrics/centrality_driver.cpp +++ /dev/null @@ -1,118 +0,0 @@ -/*PGR-GNU***************************************************************** -File: centrality_driver.cpp - -Generated with Template by: -Copyright (c) 2015 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2024 Arun Thakur -Mail: bedupako12mas at gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "drivers/metrics/centrality_driver.h" - -#include -#include -#include -#include -#include "metrics/betweennessCentrality.hpp" -#include "cpp_common/pgdata_getters.hpp" - -#include "cpp_common/pgr_assert.hpp" - - -void -pgr_do_centrality( - char *edges_sql, - bool directed, - - IID_t_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; - - std::ostringstream log; - std::ostringstream err; - char *hint = nullptr; - - try { - pgassert(!(*log_msg)); - pgassert(!(*err_msg)); - pgassert(!(*return_tuples)); - pgassert(*return_count == 0); - - hint = edges_sql; - auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, true); - - if (edges.empty()) { - throw std::string("No edges found"); - } - hint = nullptr; - - if (directed) { - log << "Processing Directed graph\n"; - pgrouting::DirectedGraph digraph; - digraph.insert_edges(edges); - pgr_centrality(digraph, *return_count, return_tuples); - } else { - log << "Processing Undirected graph\n"; - pgrouting::UndirectedGraph undigraph; - undigraph.insert_edges(edges); - pgr_centrality(undigraph, *return_count, return_tuples); - } - - - if (*return_count == 0) { - err << "No result generated, report this error\n"; - *err_msg = pgr_msg(err.str().c_str()); - *return_tuples = NULL; - *return_count = 0; - return; - } - - *log_msg = log.str().empty()? - *log_msg : - pgr_msg(log.str().c_str()); - } catch (AssertFailedException &except) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << except.what(); - *err_msg = pgr_msg(err.str().c_str()); - *log_msg = pgr_msg(log.str().c_str()); - } catch (const std::string &ex) { - *err_msg = pgr_msg(ex.c_str()); - *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); - } catch (std::exception &except) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << except.what(); - *err_msg = pgr_msg(err.str().c_str()); - *log_msg = pgr_msg(log.str().c_str()); - } catch(...) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << "Caught unknown exception!"; - *err_msg = pgr_msg(err.str().c_str()); - *log_msg = pgr_msg(log.str().c_str()); - } -} From f8beb9cc2905dec7e9fd54b3925fa4cd7e300a6c Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Sat, 29 Jun 2024 02:36:12 +0530 Subject: [PATCH 06/14] [centrality] minor changes to sql --- sql/metrics/CMakeLists.txt | 4 +-- sql/metrics/_centrality.sql | 50 -------------------------------- sql/metrics/centrality.sql | 58 ------------------------------------- 3 files changed, 2 insertions(+), 110 deletions(-) delete mode 100644 sql/metrics/_centrality.sql delete mode 100644 sql/metrics/centrality.sql diff --git a/sql/metrics/CMakeLists.txt b/sql/metrics/CMakeLists.txt index 01fa5eed6c..c047b395e5 100644 --- a/sql/metrics/CMakeLists.txt +++ b/sql/metrics/CMakeLists.txt @@ -1,7 +1,7 @@ SET(LOCAL_FILES - _centrality.sql - centrality.sql + _betweennessCentrality.sql + betweennessCentrality.sql ) foreach (f ${LOCAL_FILES}) diff --git a/sql/metrics/_centrality.sql b/sql/metrics/_centrality.sql deleted file mode 100644 index e79a9ce4e5..0000000000 --- a/sql/metrics/_centrality.sql +++ /dev/null @@ -1,50 +0,0 @@ -/*PGR-GNU***************************************************************** - -File: _centrality.sql - -Template: -Copyright (c) 2015 pgRouting developers -Mail: project@pgrouting.org - -Function developer: -Copyright (c) 2024 Arun Thakur -bedupako12mas@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - ---------------------- --- pgr_centrality ---------------------- - ---v3.7 -CREATE FUNCTION _pgr_centrality( - edges_sql TEXT, - directed BOOLEAN, - - OUT start_vid BIGINT, - OUT end_vid BIGINT, - OUT agg_cost FLOAT) -RETURNS SETOF RECORD AS -'MODULE_PATHNAME' -LANGUAGE C VOLATILE STRICT; - --- COMMENTS - -COMMENT ON FUNCTION _pgr_centrality(TEXT, BOOLEAN) -IS 'pgRouting internal function'; diff --git a/sql/metrics/centrality.sql b/sql/metrics/centrality.sql deleted file mode 100644 index 48b468de9c..0000000000 --- a/sql/metrics/centrality.sql +++ /dev/null @@ -1,58 +0,0 @@ -/*PGR-GNU***************************************************************** - -File: centrality.sql - -Template: -Copyright (c) 2015 pgRouting developers -Mail: project@pgrouting.org - -Function developer: -Copyright (c) 2024 Arun Thakur -bedupako12mas@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - ---v3.7 -CREATE FUNCTION pgr_centrality( - TEXT, -- edges_sql (required) - directed BOOLEAN DEFAULT false, - - OUT start_vid BIGINT, - OUT end_vid BIGINT, - OUT agg_cost FLOAT) -RETURNS SETOF RECORD AS -$BODY$ - - SELECT start_vid, end_vid, agg_cost - FROM _pgr_centrality(_pgr_get_statement($1), $2); - -$BODY$ -LANGUAGE SQL VOLATILE STRICT; - --- COMMENTS - -COMMENT ON FUNCTION pgr_centrality(TEXT, BOOLEAN) -IS 'pgr_centrality -- Parameters: - - edges SQL with columns: source, target, cost [,reverse_cost]) -- Optional Parameters: - - directed := true -- Documentation: - - ${PROJECT_DOC_LINK}/pgr_centrality.html -'; From abe164af805df6aa6c2bce3358e097e5d01cb9b3 Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Sun, 30 Jun 2024 20:55:07 +0530 Subject: [PATCH 07/14] [centrality] workflow fixes --- sql/metrics/_betweennessCentrality.sql | 50 +++++++++ sql/metrics/betweennessCentrality.sql | 58 ++++++++++ src/betweennessCentrality_driver.cpp | 143 +++++++++++++++++++++++++ 3 files changed, 251 insertions(+) create mode 100644 sql/metrics/_betweennessCentrality.sql create mode 100644 sql/metrics/betweennessCentrality.sql create mode 100644 src/betweennessCentrality_driver.cpp diff --git a/sql/metrics/_betweennessCentrality.sql b/sql/metrics/_betweennessCentrality.sql new file mode 100644 index 0000000000..e79a9ce4e5 --- /dev/null +++ b/sql/metrics/_betweennessCentrality.sql @@ -0,0 +1,50 @@ +/*PGR-GNU***************************************************************** + +File: _centrality.sql + +Template: +Copyright (c) 2015 pgRouting developers +Mail: project@pgrouting.org + +Function developer: +Copyright (c) 2024 Arun Thakur +bedupako12mas@gmail.com + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +--------------------- +-- pgr_centrality +--------------------- + +--v3.7 +CREATE FUNCTION _pgr_centrality( + edges_sql TEXT, + directed BOOLEAN, + + OUT start_vid BIGINT, + OUT end_vid BIGINT, + OUT agg_cost FLOAT) +RETURNS SETOF RECORD AS +'MODULE_PATHNAME' +LANGUAGE C VOLATILE STRICT; + +-- COMMENTS + +COMMENT ON FUNCTION _pgr_centrality(TEXT, BOOLEAN) +IS 'pgRouting internal function'; diff --git a/sql/metrics/betweennessCentrality.sql b/sql/metrics/betweennessCentrality.sql new file mode 100644 index 0000000000..48b468de9c --- /dev/null +++ b/sql/metrics/betweennessCentrality.sql @@ -0,0 +1,58 @@ +/*PGR-GNU***************************************************************** + +File: centrality.sql + +Template: +Copyright (c) 2015 pgRouting developers +Mail: project@pgrouting.org + +Function developer: +Copyright (c) 2024 Arun Thakur +bedupako12mas@gmail.com + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +--v3.7 +CREATE FUNCTION pgr_centrality( + TEXT, -- edges_sql (required) + directed BOOLEAN DEFAULT false, + + OUT start_vid BIGINT, + OUT end_vid BIGINT, + OUT agg_cost FLOAT) +RETURNS SETOF RECORD AS +$BODY$ + + SELECT start_vid, end_vid, agg_cost + FROM _pgr_centrality(_pgr_get_statement($1), $2); + +$BODY$ +LANGUAGE SQL VOLATILE STRICT; + +-- COMMENTS + +COMMENT ON FUNCTION pgr_centrality(TEXT, BOOLEAN) +IS 'pgr_centrality +- Parameters: + - edges SQL with columns: source, target, cost [,reverse_cost]) +- Optional Parameters: + - directed := true +- Documentation: + - ${PROJECT_DOC_LINK}/pgr_centrality.html +'; diff --git a/src/betweennessCentrality_driver.cpp b/src/betweennessCentrality_driver.cpp new file mode 100644 index 0000000000..34705c8118 --- /dev/null +++ b/src/betweennessCentrality_driver.cpp @@ -0,0 +1,143 @@ +/*PGR-GNU***************************************************************** +File: centrality.c + +Generated with Template by: +Copyright (c) 2015 pgRouting developers +Mail: project@pgrouting.org + +Function's developer: +Copyright (c) 2024 Arun Thakur +Mail: bedupako12mas at gmail.com + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#include +#include "c_common/postgres_connection.h" + +#include "c_types/iid_t_rt.h" +#include "c_common/debug_macro.h" +#include "c_common/e_report.h" +#include "c_common/time_msg.h" + +#include "drivers/metrics/centrality_driver.h" + +PGDLLEXPORT Datum _pgr_centrality(PG_FUNCTION_ARGS); +PG_FUNCTION_INFO_V1(_pgr_centrality); + +static +void +process( + char* edges_sql, + bool directed, + IID_t_rt **result_tuples, + size_t *result_count) { + pgr_SPI_connect(); + char* log_msg = NULL; + char* notice_msg = NULL; + char* err_msg = NULL; + + clock_t start_t = clock(); + pgr_do_centrality( + edges_sql, + directed, + result_tuples, + result_count, + &log_msg, + &err_msg); + time_msg(" processing Centrality", start_t, clock()); + + if (err_msg && (*result_tuples)) { + pfree(*result_tuples); + (*result_tuples) = NULL; + (*result_count) = 0; + } + + pgr_global_report(log_msg, notice_msg, err_msg); + + + if (log_msg) pfree(log_msg); + if (notice_msg) pfree(notice_msg); + if (err_msg) pfree(err_msg); + pgr_SPI_finish(); +} + + +PGDLLEXPORT Datum +_pgr_centrality(PG_FUNCTION_ARGS) { + FuncCallContext *funcctx; + TupleDesc tuple_desc; + + + IID_t_rt *result_tuples = NULL; + size_t result_count = 0; + + if (SRF_IS_FIRSTCALL()) { + MemoryContext oldcontext; + funcctx = SRF_FIRSTCALL_INIT(); + oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); + + + process( + text_to_cstring(PG_GETARG_TEXT_P(0)), + PG_GETARG_BOOL(1), + &result_tuples, + &result_count); + + funcctx->max_calls = result_count; + funcctx->user_fctx = result_tuples; + if (get_call_result_type(fcinfo, NULL, &tuple_desc) + != TYPEFUNC_COMPOSITE) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("function returning record called in context " + "that cannot accept type record"))); + + funcctx->tuple_desc = tuple_desc; + MemoryContextSwitchTo(oldcontext); + } + + funcctx = SRF_PERCALL_SETUP(); + tuple_desc = funcctx->tuple_desc; + result_tuples = (IID_t_rt*) funcctx->user_fctx; + + if (funcctx->call_cntr < funcctx->max_calls) { + HeapTuple tuple; + Datum result; + Datum *values; + bool* nulls; + + values = palloc(3 * sizeof(Datum)); + nulls = palloc(3 * sizeof(bool)); + + // postgres starts counting from 1 + values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_vid); + nulls[0] = false; + values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].to_vid); + nulls[1] = false; + values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); + nulls[2] = false; + + tuple = heap_form_tuple(tuple_desc, values, nulls); + result = HeapTupleGetDatum(tuple); + SRF_RETURN_NEXT(funcctx, result); + } else { + SRF_RETURN_DONE(funcctx); + } +} + From f24861fbc0feaebad8258f56d70b6c8ef24e850e Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Sun, 30 Jun 2024 21:20:40 +0530 Subject: [PATCH 08/14] [centrality] reverting changes --- include/drivers/metrics/centrality_driver.h | 64 --------------------- src/metrics/centrality.c | 2 +- src/metrics/centrality_driver.cpp | 2 +- 3 files changed, 2 insertions(+), 66 deletions(-) delete mode 100644 include/drivers/metrics/centrality_driver.h diff --git a/include/drivers/metrics/centrality_driver.h b/include/drivers/metrics/centrality_driver.h deleted file mode 100644 index a9a6f21034..0000000000 --- a/include/drivers/metrics/centrality_driver.h +++ /dev/null @@ -1,64 +0,0 @@ -/*PGR-GNU***************************************************************** -File: floydWarshall_driver.h - -Generated with Template by: -Copyright (c) 2015 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2024 Arun Thakur -Mail: bedupako12mas at gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_DRIVERS_METRICS_CENTRALITY_DRIVER_H_ -#define INCLUDE_DRIVERS_METRICS_CENTRALITY_DRIVER_H_ -#pragma once - -/* for size-t */ -#ifdef __cplusplus -# include -using IID_t_rt = struct IID_t_rt; -#else -# include -#include -typedef struct IID_t_rt IID_t_rt; -#endif - - - -#ifdef __cplusplus -extern "C" { -#endif - -void -pgr_do_centrality( - char*, - bool, - - IID_t_rt**, - size_t*, - char**, - char**); - -#ifdef __cplusplus -} -#endif - -#endif // INCLUDE_DRIVERS_METRICS_CENTRALITY_DRIVER_H_ diff --git a/src/metrics/centrality.c b/src/metrics/centrality.c index 34705c8118..4875115983 100644 --- a/src/metrics/centrality.c +++ b/src/metrics/centrality.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "drivers/metrics/centrality_driver.h" +#include "drivers/metrics/betweennessCentrality_driver.h" PGDLLEXPORT Datum _pgr_centrality(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_centrality); diff --git a/src/metrics/centrality_driver.cpp b/src/metrics/centrality_driver.cpp index 93d7a9c1d5..666c7659ab 100644 --- a/src/metrics/centrality_driver.cpp +++ b/src/metrics/centrality_driver.cpp @@ -27,7 +27,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -#include "drivers/metrics/centrality_driver.h" +#include "drivers/metrics/betweennessCentrality_driver.h" #include #include From 9cc2d0e1229442f4059b54207393b0b45f7f399d Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Sun, 30 Jun 2024 21:36:49 +0530 Subject: [PATCH 09/14] [centrality] minor changes to revert changes --- ...river.h => betweennessCentrality_driver.h} | 0 src/metrics/centrality_driver.cpp | 118 ------------------ 2 files changed, 118 deletions(-) rename include/drivers/metrics/{centrality_driver.h => betweennessCentrality_driver.h} (100%) delete mode 100644 src/metrics/centrality_driver.cpp diff --git a/include/drivers/metrics/centrality_driver.h b/include/drivers/metrics/betweennessCentrality_driver.h similarity index 100% rename from include/drivers/metrics/centrality_driver.h rename to include/drivers/metrics/betweennessCentrality_driver.h diff --git a/src/metrics/centrality_driver.cpp b/src/metrics/centrality_driver.cpp deleted file mode 100644 index 666c7659ab..0000000000 --- a/src/metrics/centrality_driver.cpp +++ /dev/null @@ -1,118 +0,0 @@ -/*PGR-GNU***************************************************************** -File: centrality_driver.cpp - -Generated with Template by: -Copyright (c) 2015 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2024 Arun Thakur -Mail: bedupako12mas at gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "drivers/metrics/betweennessCentrality_driver.h" - -#include -#include -#include -#include -#include "metrics/betweennessCentrality.hpp" -#include "cpp_common/pgdata_getters.hpp" - -#include "cpp_common/pgr_assert.hpp" - - -void -pgr_do_centrality( - char *edges_sql, - bool directed, - - IID_t_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; - - std::ostringstream log; - std::ostringstream err; - char *hint = nullptr; - - try { - pgassert(!(*log_msg)); - pgassert(!(*err_msg)); - pgassert(!(*return_tuples)); - pgassert(*return_count == 0); - - hint = edges_sql; - auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, true); - - if (edges.empty()) { - throw std::string("No edges found"); - } - hint = nullptr; - - if (directed) { - log << "Processing Directed graph\n"; - pgrouting::DirectedGraph digraph; - digraph.insert_edges(edges); - pgr_centrality(digraph, *return_count, return_tuples); - } else { - log << "Processing Undirected graph\n"; - pgrouting::UndirectedGraph undigraph; - undigraph.insert_edges(edges); - pgr_centrality(undigraph, *return_count, return_tuples); - } - - - if (*return_count == 0) { - err << "No result generated, report this error\n"; - *err_msg = pgr_msg(err.str().c_str()); - *return_tuples = NULL; - *return_count = 0; - return; - } - - *log_msg = log.str().empty()? - *log_msg : - pgr_msg(log.str().c_str()); - } catch (AssertFailedException &except) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << except.what(); - *err_msg = pgr_msg(err.str().c_str()); - *log_msg = pgr_msg(log.str().c_str()); - } catch (const std::string &ex) { - *err_msg = pgr_msg(ex.c_str()); - *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); - } catch (std::exception &except) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << except.what(); - *err_msg = pgr_msg(err.str().c_str()); - *log_msg = pgr_msg(log.str().c_str()); - } catch(...) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << "Caught unknown exception!"; - *err_msg = pgr_msg(err.str().c_str()); - *log_msg = pgr_msg(log.str().c_str()); - } -} From 8b9a05f4ce09e2ac785f4f6adf08e003832365fd Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Sun, 30 Jun 2024 22:53:41 +0530 Subject: [PATCH 10/14] [centrality] switching file names to betweennessCentrality --- docqueries/metrics/CMakeLists.txt | 2 +- .../metrics/{centrality.pg => betweennessCentrality.pg} | 0 .../{centrality.result => betweennessCentrality.result} | 0 docqueries/metrics/test.conf | 2 +- include/drivers/metrics/betweennessCentrality_driver.h | 8 ++++---- 5 files changed, 6 insertions(+), 6 deletions(-) rename docqueries/metrics/{centrality.pg => betweennessCentrality.pg} (100%) rename docqueries/metrics/{centrality.result => betweennessCentrality.result} (100%) diff --git a/docqueries/metrics/CMakeLists.txt b/docqueries/metrics/CMakeLists.txt index 76682554e3..3e4f70e9c1 100644 --- a/docqueries/metrics/CMakeLists.txt +++ b/docqueries/metrics/CMakeLists.txt @@ -1,6 +1,6 @@ # Do not use extensions SET(LOCAL_FILES - centrality.pg + betweennessCentrality.pg ) foreach (f ${LOCAL_FILES}) diff --git a/docqueries/metrics/centrality.pg b/docqueries/metrics/betweennessCentrality.pg similarity index 100% rename from docqueries/metrics/centrality.pg rename to docqueries/metrics/betweennessCentrality.pg diff --git a/docqueries/metrics/centrality.result b/docqueries/metrics/betweennessCentrality.result similarity index 100% rename from docqueries/metrics/centrality.result rename to docqueries/metrics/betweennessCentrality.result diff --git a/docqueries/metrics/test.conf b/docqueries/metrics/test.conf index c9e35d3af9..c1462123db 100644 --- a/docqueries/metrics/test.conf +++ b/docqueries/metrics/test.conf @@ -3,7 +3,7 @@ %main::tests = ( 'any' => { 'files' => [qw( - centrality.pg + betweennessCentrality.pg )] }, # I don't know what this are for or how to use them. diff --git a/include/drivers/metrics/betweennessCentrality_driver.h b/include/drivers/metrics/betweennessCentrality_driver.h index a9a6f21034..93a50b952b 100644 --- a/include/drivers/metrics/betweennessCentrality_driver.h +++ b/include/drivers/metrics/betweennessCentrality_driver.h @@ -1,5 +1,5 @@ /*PGR-GNU***************************************************************** -File: floydWarshall_driver.h +File: betweennessCentrality_driver.h Generated with Template by: Copyright (c) 2015 pgRouting developers @@ -27,8 +27,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -#ifndef INCLUDE_DRIVERS_METRICS_CENTRALITY_DRIVER_H_ -#define INCLUDE_DRIVERS_METRICS_CENTRALITY_DRIVER_H_ +#ifndef INCLUDE_DRIVERS_METRICS_BETWEENNESSCENTRALITY_DRIVER_H_ +#define INCLUDE_DRIVERS_METRICS_BETWEENNESSCENTRALITY_DRIVER_H_ #pragma once /* for size-t */ @@ -61,4 +61,4 @@ pgr_do_centrality( } #endif -#endif // INCLUDE_DRIVERS_METRICS_CENTRALITY_DRIVER_H_ +#endif // INCLUDE_DRIVERS_METRICS_BETWEENNESSCENTRALITY_DRIVER_H_ From 6f78fb212f3fe78a5d788481326022f10b226a3a Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Mon, 1 Jul 2024 01:00:06 +0530 Subject: [PATCH 11/14] [centrality] fixed division by zero case for relative betweenness centrality --- include/metrics/betweennessCentrality.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/metrics/betweennessCentrality.hpp b/include/metrics/betweennessCentrality.hpp index d5b29f7372..573122c3f2 100644 --- a/include/metrics/betweennessCentrality.hpp +++ b/include/metrics/betweennessCentrality.hpp @@ -86,10 +86,12 @@ class Pgr_metrics { graph.graph, centrality_map ); - boost::brandes_betweenness_centrality( + if(boost::num_vertices(graph.graph) > 2){ + boost::relative_betweenness_centrality( graph.graph, centrality_map - ); + ); + } generate_results(graph, centrality, result_tuple_count, postgres_rows); } From e19134df09766569b763a019373daa63701dc53b Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Mon, 1 Jul 2024 01:49:40 +0530 Subject: [PATCH 12/14] [centrality] minor changes to switch to betweennessCentrality --- doc/metrics/pgr_betweennessCentrality.rst | 111 +++++++++++++++++++ include/metrics/betweennessCentrality.hpp | 2 +- src/metrics/betweennessCentrality_driver.cpp | 6 +- 3 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 doc/metrics/pgr_betweennessCentrality.rst diff --git a/doc/metrics/pgr_betweennessCentrality.rst b/doc/metrics/pgr_betweennessCentrality.rst new file mode 100644 index 0000000000..254a87d962 --- /dev/null +++ b/doc/metrics/pgr_betweennessCentrality.rst @@ -0,0 +1,111 @@ +.. + **************************************************************************** + pgRouting Manual + Copyright(c) pgRouting Contributors + + This documentation is licensed under a Creative Commons Attribution-Share + Alike 3.0 License: https://creativecommons.org/licenses/by-sa/3.0/ + **************************************************************************** + +| + + +``pgr_betweennessCentrality`` +=============================================================================== + +``pgr_betweennessCentrality`` - Returns the relative betweeness centrality of +all edges in a graph using Brandes Algorithm. + +.. figure:: images/boost-inside.jpeg + :target: https://www.boost.org/doc/libs/1_84_0/libs/graph/doc/betweenness_centrality.html + + Boost Graph Inside + +.. rubric:: Availability +.. TODO: Add availability + +Description +------------------------------------------------------------------------------- + +The Brandes Algorithm for utilises the sparse nature of graphs to evaluating the +betweenness centrality score of all edges/vertices. +We use Boost's implementation which runs in :math:`\Theta(VE)` for unweighted +graphs and :math:`Theta(VE + V(V+E)log(V))` for weighted graphs and uses +:math:`\Theta(VE)` space. + +Signatures +------------------------------------------------------------------------------- + +.. rubric:: Summary + +.. admonition:: \ \ + :class: signatures + + pgr_betweennessCentrality(`Edges SQL`_, [``directed``]) + + | Returns set of ```(seq, edge_id, betweenness_centrality)``` + | OR EMPTY SET + +.. TODO: Fix this when docqueries are made +:Example: For a directed subgraph with edges :math:`\{1, 2, 3, 4\}`. + +.. literalinclude:: floydWarshall.queries + :start-after: -- q1 + :end-before: -- q2 + +Parameters +------------------------------------------------------------------------------- + +.. include:: allpairs-family.rst + :start-after: edges_start + :end-before: edges_end + +Optional parameters +............................................................................... + +.. include:: dijkstra-family.rst + :start-after: dijkstra_optionals_start + :end-before: dijkstra_optionals_end + +Inner Queries +------------------------------------------------------------------------------- + +Edges SQL +............................................................................... + +.. include:: pgRouting-concepts.rst + :start-after: no_id_edges_sql_start + :end-before: no_id_edges_sql_end + +Result columns +------------------------------------------------------------------------------- + +.. list-table:: + :width: 81 + :widths: auto + :header-rows: 1 + + * - Column + - Type + - Description + * - ``seq`` + - ``INTEGER`` + - Sequential Value starting from ``1`` + * - ``edge_id`` + - ``BIGINT`` + - Identifier of the edge + * - ``centrality`` + - ``FLOAT`` + - relative betweenness centrality score of the edge (will be in range [0,1]) + +See Also +------------------------------------------------------------------------------- + +* Boost `centrality + `_ +* Queries uses the :doc:`sampledata` network. + +.. rubric:: Indices and tables + +* :ref:`genindex` +* :ref:`search` diff --git a/include/metrics/betweennessCentrality.hpp b/include/metrics/betweennessCentrality.hpp index 573122c3f2..d3f91391a5 100644 --- a/include/metrics/betweennessCentrality.hpp +++ b/include/metrics/betweennessCentrality.hpp @@ -54,7 +54,7 @@ template class Pgr_metrics; // for postgres template void -pgr_centrality( +pgr_betweennessCentrality( G &graph, size_t &result_tuple_count, IID_t_rt **postgres_rows) { diff --git a/src/metrics/betweennessCentrality_driver.cpp b/src/metrics/betweennessCentrality_driver.cpp index 666c7659ab..8400c2abac 100644 --- a/src/metrics/betweennessCentrality_driver.cpp +++ b/src/metrics/betweennessCentrality_driver.cpp @@ -1,5 +1,5 @@ /*PGR-GNU***************************************************************** -File: centrality_driver.cpp +File: betweennessCentrality_driver.cpp Generated with Template by: Copyright (c) 2015 pgRouting developers @@ -73,12 +73,12 @@ pgr_do_centrality( log << "Processing Directed graph\n"; pgrouting::DirectedGraph digraph; digraph.insert_edges(edges); - pgr_centrality(digraph, *return_count, return_tuples); + pgr_betweennessCentrality(digraph, *return_count, return_tuples); } else { log << "Processing Undirected graph\n"; pgrouting::UndirectedGraph undigraph; undigraph.insert_edges(edges); - pgr_centrality(undigraph, *return_count, return_tuples); + pgr_betweennessCentrality(undigraph, *return_count, return_tuples); } From c0a2cfd9457d856ecc8df0a1fd43ed831b4940ad Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Mon, 1 Jul 2024 05:46:06 +0530 Subject: [PATCH 13/14] [centrality] changed name to betweennesscentrality --- docqueries/metrics/betweennessCentrality.pg | 2 +- docqueries/metrics/betweennessCentrality.result | 2 +- .../drivers/metrics/betweennessCentrality_driver.h | 2 +- include/metrics/betweennessCentrality.hpp | 2 +- sql/metrics/_betweennessCentrality.sql | 14 +++++++------- sql/metrics/betweennessCentrality.sql | 12 ++++++------ src/metrics/betweennessCentrality.c | 8 ++++---- src/metrics/betweennessCentrality_driver.cpp | 6 +++--- 8 files changed, 24 insertions(+), 24 deletions(-) diff --git a/docqueries/metrics/betweennessCentrality.pg b/docqueries/metrics/betweennessCentrality.pg index ec5710a60f..bc8b322a35 100644 --- a/docqueries/metrics/betweennessCentrality.pg +++ b/docqueries/metrics/betweennessCentrality.pg @@ -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_centrality( +SELECT * FROM pgr_betweennesscentrality( 'SELECT id, source, target, cost, reverse_cost FROM edges where id < 5' ) ORDER BY start_vid, end_vid; diff --git a/docqueries/metrics/betweennessCentrality.result b/docqueries/metrics/betweennessCentrality.result index fd38df1ece..410bfd6f5e 100644 --- a/docqueries/metrics/betweennessCentrality.result +++ b/docqueries/metrics/betweennessCentrality.result @@ -3,7 +3,7 @@ BEGIN SET client_min_messages TO NOTICE; SET /* -- q1 */ -SELECT * FROM pgr_centrality( +SELECT * FROM pgr_betweennesscentrality( 'SELECT id, source, target, cost, reverse_cost FROM edges where id < 5' ) ORDER BY start_vid, end_vid; diff --git a/include/drivers/metrics/betweennessCentrality_driver.h b/include/drivers/metrics/betweennessCentrality_driver.h index 93a50b952b..2d849bbaa4 100644 --- a/include/drivers/metrics/betweennessCentrality_driver.h +++ b/include/drivers/metrics/betweennessCentrality_driver.h @@ -48,7 +48,7 @@ extern "C" { #endif void -pgr_do_centrality( +pgr_do_betweennesscentrality( char*, bool, diff --git a/include/metrics/betweennessCentrality.hpp b/include/metrics/betweennessCentrality.hpp index d3f91391a5..7af4e44e14 100644 --- a/include/metrics/betweennessCentrality.hpp +++ b/include/metrics/betweennessCentrality.hpp @@ -54,7 +54,7 @@ template class Pgr_metrics; // for postgres template void -pgr_betweennessCentrality( +pgr_betweennesscentrality( G &graph, size_t &result_tuple_count, IID_t_rt **postgres_rows) { diff --git a/sql/metrics/_betweennessCentrality.sql b/sql/metrics/_betweennessCentrality.sql index e79a9ce4e5..df9bac5778 100644 --- a/sql/metrics/_betweennessCentrality.sql +++ b/sql/metrics/_betweennessCentrality.sql @@ -1,6 +1,6 @@ /*PGR-GNU***************************************************************** -File: _centrality.sql +File: _betweennessCentrality.sql Template: Copyright (c) 2015 pgRouting developers @@ -28,17 +28,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ ---------------------- --- pgr_centrality ---------------------- +-------------------------------- +-- pgr_betweennessCentrality +-------------------------------- --v3.7 -CREATE FUNCTION _pgr_centrality( +CREATE FUNCTION _pgr_betweennesscentrality( edges_sql TEXT, directed BOOLEAN, OUT start_vid BIGINT, - OUT end_vid BIGINT, + OUT end_vid BIGINT, OUT agg_cost FLOAT) RETURNS SETOF RECORD AS 'MODULE_PATHNAME' @@ -46,5 +46,5 @@ LANGUAGE C VOLATILE STRICT; -- COMMENTS -COMMENT ON FUNCTION _pgr_centrality(TEXT, BOOLEAN) +COMMENT ON FUNCTION _pgr_betweennesscentrality(TEXT, BOOLEAN) IS 'pgRouting internal function'; diff --git a/sql/metrics/betweennessCentrality.sql b/sql/metrics/betweennessCentrality.sql index 48b468de9c..5be653625a 100644 --- a/sql/metrics/betweennessCentrality.sql +++ b/sql/metrics/betweennessCentrality.sql @@ -1,6 +1,6 @@ /*PGR-GNU***************************************************************** -File: centrality.sql +File: betweennessCentrality.sql Template: Copyright (c) 2015 pgRouting developers @@ -29,26 +29,26 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ --v3.7 -CREATE FUNCTION pgr_centrality( +CREATE FUNCTION pgr_betweennesscentrality( TEXT, -- edges_sql (required) directed BOOLEAN DEFAULT false, OUT start_vid BIGINT, - OUT end_vid BIGINT, + OUT end_vid BIGINT, OUT agg_cost FLOAT) RETURNS SETOF RECORD AS $BODY$ SELECT start_vid, end_vid, agg_cost - FROM _pgr_centrality(_pgr_get_statement($1), $2); + FROM _pgr_betweennesscentrality(_pgr_get_statement($1), $2); $BODY$ LANGUAGE SQL VOLATILE STRICT; -- COMMENTS -COMMENT ON FUNCTION pgr_centrality(TEXT, BOOLEAN) -IS 'pgr_centrality +COMMENT ON FUNCTION pgr_betweennesscentrality(TEXT, BOOLEAN) +IS 'pgr_betweennessCentrality - Parameters: - edges SQL with columns: source, target, cost [,reverse_cost]) - Optional Parameters: diff --git a/src/metrics/betweennessCentrality.c b/src/metrics/betweennessCentrality.c index 59f7ea5500..7d24584674 100644 --- a/src/metrics/betweennessCentrality.c +++ b/src/metrics/betweennessCentrality.c @@ -37,8 +37,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "drivers/metrics/betweennessCentrality_driver.h" -PGDLLEXPORT Datum _pgr_centrality(PG_FUNCTION_ARGS); -PG_FUNCTION_INFO_V1(_pgr_centrality); +PGDLLEXPORT Datum _pgr_betweennesscentrality(PG_FUNCTION_ARGS); +PG_FUNCTION_INFO_V1(_pgr_betweennesscentrality); static void @@ -53,7 +53,7 @@ process( char* err_msg = NULL; clock_t start_t = clock(); - pgr_do_centrality( + pgr_do_betweennesscentrality( edges_sql, directed, result_tuples, @@ -79,7 +79,7 @@ process( PGDLLEXPORT Datum -_pgr_centrality(PG_FUNCTION_ARGS) { +_pgr_betweennesscentrality(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; diff --git a/src/metrics/betweennessCentrality_driver.cpp b/src/metrics/betweennessCentrality_driver.cpp index 8400c2abac..792e9cbf0c 100644 --- a/src/metrics/betweennessCentrality_driver.cpp +++ b/src/metrics/betweennessCentrality_driver.cpp @@ -40,7 +40,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. void -pgr_do_centrality( +pgr_do_betweennesscentrality( char *edges_sql, bool directed, @@ -73,12 +73,12 @@ pgr_do_centrality( log << "Processing Directed graph\n"; pgrouting::DirectedGraph digraph; digraph.insert_edges(edges); - pgr_betweennessCentrality(digraph, *return_count, return_tuples); + pgr_betweennesscentrality(digraph, *return_count, return_tuples); } else { log << "Processing Undirected graph\n"; pgrouting::UndirectedGraph undigraph; undigraph.insert_edges(edges); - pgr_betweennessCentrality(undigraph, *return_count, return_tuples); + pgr_betweennesscentrality(undigraph, *return_count, return_tuples); } From 4a2551195b2efdd69a75759f1c3f882957ed96cc Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Mon, 1 Jul 2024 06:19:05 +0530 Subject: [PATCH 14/14] [centrality] changed sql and src code. Code compiles and correct output is present --- sql/metrics/_betweennessCentrality.sql | 5 ++--- sql/metrics/betweennessCentrality.sql | 7 +++---- src/metrics/betweennessCentrality.c | 8 +++----- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/sql/metrics/_betweennessCentrality.sql b/sql/metrics/_betweennessCentrality.sql index df9bac5778..a2c6bf26e4 100644 --- a/sql/metrics/_betweennessCentrality.sql +++ b/sql/metrics/_betweennessCentrality.sql @@ -37,9 +37,8 @@ CREATE FUNCTION _pgr_betweennesscentrality( edges_sql TEXT, directed BOOLEAN, - OUT start_vid BIGINT, - OUT end_vid BIGINT, - OUT agg_cost FLOAT) + OUT vid BIGINT, + OUT betweenness_centrality FLOAT) RETURNS SETOF RECORD AS 'MODULE_PATHNAME' LANGUAGE C VOLATILE STRICT; diff --git a/sql/metrics/betweennessCentrality.sql b/sql/metrics/betweennessCentrality.sql index 5be653625a..c5f3f893f9 100644 --- a/sql/metrics/betweennessCentrality.sql +++ b/sql/metrics/betweennessCentrality.sql @@ -33,13 +33,12 @@ CREATE FUNCTION pgr_betweennesscentrality( TEXT, -- edges_sql (required) directed BOOLEAN DEFAULT false, - OUT start_vid BIGINT, - OUT end_vid BIGINT, - OUT agg_cost FLOAT) + OUT vid BIGINT, + OUT betweenness_centrality FLOAT) RETURNS SETOF RECORD AS $BODY$ - SELECT start_vid, end_vid, agg_cost + SELECT vid, betweenness_centrality FROM _pgr_betweennesscentrality(_pgr_get_statement($1), $2); $BODY$ diff --git a/src/metrics/betweennessCentrality.c b/src/metrics/betweennessCentrality.c index 7d24584674..05ffdda9f4 100644 --- a/src/metrics/betweennessCentrality.c +++ b/src/metrics/betweennessCentrality.c @@ -122,16 +122,14 @@ _pgr_betweennesscentrality(PG_FUNCTION_ARGS) { Datum *values; bool* nulls; - values = palloc(3 * sizeof(Datum)); - nulls = palloc(3 * sizeof(bool)); + values = palloc(2 * sizeof(Datum)); + nulls = palloc(2 * sizeof(bool)); // postgres starts counting from 1 values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_vid); nulls[0] = false; - values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].to_vid); + values[1] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); nulls[1] = false; - values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); - nulls[2] = false; tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple);