From 7f46b55210aeea65cb0679098c829f6ad1ebb05a Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Mon, 29 Jul 2024 22:26:35 +0530 Subject: [PATCH 1/3] [centrality] fixing mistake of copying floydWarshall edge cases --- .../betweennessCentrality/edge_cases.pg | 268 +++++++++++++++++- 1 file changed, 253 insertions(+), 15 deletions(-) diff --git a/pgtap/metrics/betweennessCentrality/edge_cases.pg b/pgtap/metrics/betweennessCentrality/edge_cases.pg index bcfc6c9594..672556d0aa 100644 --- a/pgtap/metrics/betweennessCentrality/edge_cases.pg +++ b/pgtap/metrics/betweennessCentrality/edge_cases.pg @@ -20,25 +20,263 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. BEGIN; UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost); -SELECT plan(15); +SELECT CASE WHEN NOT min_version('3.7.0') THEN plan(1) ELSE plan(24) END; -SELECT test_agg_cost('pgr_floydWarshall', true); -SELECT test_agg_cost('pgr_floydWarshall', false); -SELECT allPairs_test_flags('pgr_floydwarshall'); +CREATE OR REPLACE FUNCTION edge_cases() +RETURNS SETOF TEXT AS +$BODY$ +BEGIN -PREPARE q2 AS -SELECT * FROM pgr_floydWarshall( - 'SELECT source, target, cost, reverse_cost FROM edges' -); +IF NOT min_version('3.7.0') THEN + RETURN QUERY + SELECT skip(1, 'Function is new on 3.7.0'); + RETURN; +END IF; -PREPARE q3 AS -SELECT * FROM pgr_floydWarshall( - 'SELECT source, target, cost FROM edges', - true -); +/* Implicit Test Cases */ -SELECT lives_ok('q2', 'SHOULD WORK: without id with flag'); -SELECT lives_ok('q3', 'SHOULD WORK: without id, with flag'); +PREPARE idless5_q AS + SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges WHERE id < 5' + ) ORDER BY vid; + +PREPARE idless5_r AS + SELECT * FROM (VALUES + (5::BIGINT , 0::FLOAT), + (6 , 0.5), + (7 , 0), + (10 , 0.25), + (15 , 0)) + AS t(vid, centrality); + +RETURN QUERY +SELECT lives_ok('idless5_q'); + +RETURN QUERY +SELECT results_eq('idless5_q', 'idless5_r'); + + +PREPARE idless4_q AS + SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges WHERE id < 4' + ) ORDER BY vid; + +PREPARE idless4_r AS + SELECT * FROM (VALUES + (5::BIGINT , 0::FLOAT), + (6 , 0.3333333333333333), + (10 , 0.3333333333333333), + (15 , 0)) + AS t(vid, centrality); + +RETURN QUERY +SELECT lives_ok('idless4_q'); + +RETURN QUERY +SELECT results_eq('idless4_q', 'idless4_r'); + + +PREPARE idless3_q AS + SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges WHERE id < 3' + ) ORDER BY vid; + +PREPARE idless3_r AS + SELECT * FROM (VALUES + (5::BIGINT , 0::FLOAT), + (6 , 0.5), + (10 , 0) + ) AS t(vid, centrality); + +RETURN QUERY +SELECT lives_ok('idless3_q'); + +RETURN QUERY +SELECT results_eq('idless3_q', 'idless3_r'); + +PREPARE idless2_q AS + SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges WHERE id < 2' + ) ORDER BY vid; + +PREPARE idless2_r AS + SELECT * FROM (VALUES + (5::BIGINT , 0::FLOAT), + (6 , 0) + ) AS t(vid, centrality); + +RETURN QUERY +SELECT lives_ok('idless2_q'); + +RETURN QUERY +SELECT results_eq('idless2_q', 'idless2_r'); + +/* Explicit Undirected Cases */ + +PREPARE idless5ud_q AS + SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges WHERE id < 5', directed => false ) ORDER BY vid; + +PREPARE idless5ud_r AS + SELECT * FROM (VALUES + (5::BIGINT , 0::FLOAT), + (6 , 0.8333333333333333), + (7 , 0), + (10 , 0.5), + (15 , 0)) + AS t(vid, centrality); + +RETURN QUERY +SELECT lives_ok('idless5ud_q'); + +RETURN QUERY +SELECT results_eq('idless5ud_q', 'idless5ud_r'); + +PREPARE idless4ud_q AS + SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges WHERE id < 4', directed => false ) ORDER BY vid; + +PREPARE idless4ud_r AS + SELECT * FROM (VALUES + (5::BIGINT , 0::FLOAT), + (6 , 0.6666666666666666), + (10 , 0.6666666666666666), (15 , 0)) + AS t(vid, centrality); + +RETURN QUERY +SELECT lives_ok('idless4ud_q'); + +RETURN QUERY +SELECT results_eq('idless4ud_q', 'idless4ud_r'); + + +PREPARE idless3ud_q AS + SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges WHERE id < 3', directed => false ) ORDER BY vid; + +PREPARE idless3ud_r AS + SELECT * FROM (VALUES + (5::BIGINT , 0::FLOAT), + (6 , 1), + (10 , 0)) + AS t(vid, centrality); + +RETURN QUERY +SELECT lives_ok('idless3ud_q'); + +RETURN QUERY +SELECT results_eq('idless3ud_q', 'idless3ud_r'); + +PREPARE idless2ud_q AS + SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges WHERE id < 2', directed => false + ) ORDER BY vid; + +PREPARE idless2ud_r AS + SELECT * FROM (VALUES + (5::BIGINT , 0::FLOAT), + (6 , 0)) + AS t(vid, centrality); + +RETURN QUERY +SELECT lives_ok('idless2ud_q'); + +RETURN QUERY +SELECT results_eq('idless2ud_q', 'idless2ud_r'); + +/* Explicit Directed Cases */ + + +PREPARE idless5d_q AS + SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges WHERE id < 5', directed => true ) ORDER BY vid; + +PREPARE idless5d_r AS + SELECT * FROM (VALUES + (5::BIGINT , 0::FLOAT), + (6 , 0.5), + (7 , 0), + (10 , 0.25), + (15 , 0)) + AS t(vid, centrality); + +RETURN QUERY +SELECT lives_ok('idless5d_q'); + +RETURN QUERY +SELECT results_eq('idless5d_q', 'idless5d_r'); + + +PREPARE idless4d_q AS + SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges WHERE id < 4', directed => true ) ORDER BY vid; + +PREPARE idless4d_r AS + SELECT * FROM (VALUES + (5::BIGINT , 0::FLOAT), + (6 , 0.3333333333333333), + (10 , 0.3333333333333333), + (15 , 0)) + AS t(vid, centrality); + +RETURN QUERY +SELECT lives_ok('idless4d_q'); + +RETURN QUERY +SELECT results_eq('idless4d_q', 'idless4d_r'); + + +PREPARE idless3d_q AS + SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges WHERE id < 3', directed => true ) ORDER BY vid; + +PREPARE idless3d_r AS + SELECT * FROM (VALUES + (5::BIGINT , 0::FLOAT), + (6 , 0.5), + (10 , 0) + ) AS t(vid, centrality); + +RETURN QUERY +SELECT lives_ok('idless3d_q'); + +RETURN QUERY +SELECT results_eq('idless3d_q', 'idless3d_r'); + +PREPARE idless2d_q AS + SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges WHERE id < 2', directed => true + ) ORDER BY vid; + +PREPARE idless2d_r AS + SELECT * FROM (VALUES + (5::BIGINT , 0::FLOAT), + (6 , 0) + ) AS t(vid, centrality); + +RETURN QUERY +SELECT lives_ok('idless2d_q'); + +RETURN QUERY +SELECT results_eq('idless2d_q', 'idless2d_r'); + +END; +$BODY$ +LANGUAGE plpgsql; + +SELECT edge_cases(); SELECT finish(); ROLLBACK; From 6486142ecc415ecb79e883627948b819f26d5e4c Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Tue, 30 Jul 2024 00:00:54 +0530 Subject: [PATCH 2/3] [centrality] to ingore errors of other directories --- include/metrics/betweennessCentrality.hpp | 2 +- tools/scripts/code_checker.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/metrics/betweennessCentrality.hpp b/include/metrics/betweennessCentrality.hpp index a48df4b027..df493b4c31 100644 --- a/include/metrics/betweennessCentrality.hpp +++ b/include/metrics/betweennessCentrality.hpp @@ -45,7 +45,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/pgr_base_graph.hpp" #include "cpp_common/interruption.hpp" -// TODO(vicky) don't keep it here +// TODO(arun) don't keep it here #include "cpp_common/pgr_alloc.hpp" namespace pgrouting { diff --git a/tools/scripts/code_checker.sh b/tools/scripts/code_checker.sh index 8a705e55a1..5c145abcca 100755 --- a/tools/scripts/code_checker.sh +++ b/tools/scripts/code_checker.sh @@ -48,11 +48,11 @@ if test -z "$DIRECTORY"; then echo "--------------------" echo "------ *.c ------" echo "--------------------" - code_linter/cpplint/cpplint.py --extensions=c --linelength=120 --filter=-readability/casting src/*/*.c + code_linter/cpplint/cpplint.py --extensions=c --linelength=120 --filter=-readability/casting,-whitespace/newline src/*/*.c echo "--------------------" echo "------ *.cpp ------" echo "--------------------" - code_linter/cpplint/cpplint.py --filter=-runtime/references,-whitespace/indent_namespace --linelength=120 src/*/*.cpp + code_linter/cpplint/cpplint.py --filter=-runtime/references,-whitespace/indent_namespace,-whitespace/newline --linelength=120 src/*/*.cpp echo "--------------------" echo "------ HEADERS ------" echo "--------------------" From a333230fdf0567b7bb60a5bbab48dbb7423ba02e Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Mon, 5 Aug 2024 20:09:56 +0530 Subject: [PATCH 3/3] [centrality] fixed all linting errors in driver code --- include/metrics/betweennessCentrality.hpp | 96 +++++++++++------------ 1 file changed, 46 insertions(+), 50 deletions(-) diff --git a/include/metrics/betweennessCentrality.hpp b/include/metrics/betweennessCentrality.hpp index df493b4c31..5ad6207950 100644 --- a/include/metrics/betweennessCentrality.hpp +++ b/include/metrics/betweennessCentrality.hpp @@ -68,59 +68,55 @@ pgr_betweennesscentrality( template class Pgr_metrics { public: - using Graph = typename G::B_G; + using Graph = typename G::B_G; using Vertex = typename G::V; - typedef typename boost::graph_traits::directed_category directed_category; - - void betweennessCentrality ( - const G &graph, - size_t &result_tuple_count, - IID_t_rt **postgres_rows ){ - - 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, - centrality_map - - ); - if(boost::num_vertices(graph.graph) > 2) { - boost::relative_betweenness_centrality( - graph.graph, - centrality_map - ); - } - - generate_results(graph, centrality, result_tuple_count, postgres_rows); - } + typedef typename boost::graph_traits::directed_category directed_category; + + void betweennessCentrality( + const G &graph, + size_t &result_tuple_count, + IID_t_rt **postgres_rows ) { + 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, + centrality_map); + + if (boost::num_vertices(graph.graph) > 2) { + boost::relative_betweenness_centrality( + graph.graph, + centrality_map); + } + + generate_results(graph, centrality, result_tuple_count, postgres_rows); + } private: - void generate_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(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 = centrality_results[v_i]; - if(std::is_same::value) { - (*postgres_rows)[seq].cost = centrality_results[v_i]/2.0; - } - seq++; - } - } + void generate_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 (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 = centrality_results[v_i]; + if (std::is_same::value) { + (*postgres_rows)[seq].cost = centrality_results[v_i]/2.0; + } + seq++; + } + } }; } // namespace pgrouting