From 4933a19f5180eeee27b4969373275e24a7bd6efd Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Tue, 2 Jul 2024 22:08:36 +0530 Subject: [PATCH 1/5] [centrality] adding simple docqueries --- docqueries/metrics/betweennessCentrality.pg | 4 +-- .../metrics/betweennessCentrality.result | 27 +++++++------------ include/metrics/betweennessCentrality.hpp | 12 ++++----- 3 files changed, 16 insertions(+), 27 deletions(-) diff --git a/docqueries/metrics/betweennessCentrality.pg b/docqueries/metrics/betweennessCentrality.pg index bc8b322a35..82430f5ca9 100644 --- a/docqueries/metrics/betweennessCentrality.pg +++ b/docqueries/metrics/betweennessCentrality.pg @@ -3,6 +3,6 @@ /* -- q1 */ SELECT * FROM pgr_betweennesscentrality( 'SELECT id, source, target, cost, reverse_cost - FROM edges where id < 5' -) ORDER BY start_vid, end_vid; + FROM edges where id IN (6,7)', +) ORDER BY vid; /* -- q2 */ diff --git a/docqueries/metrics/betweennessCentrality.result b/docqueries/metrics/betweennessCentrality.result index 410bfd6f5e..0b6be29253 100644 --- a/docqueries/metrics/betweennessCentrality.result +++ b/docqueries/metrics/betweennessCentrality.result @@ -5,24 +5,15 @@ SET /* -- q1 */ SELECT * FROM pgr_betweennesscentrality( 'SELECT id, source, target, cost, reverse_cost - FROM edges where id < 5' -) ORDER BY start_vid, end_vid; - start_vid | end_vid | agg_cost ------------+---------+---------- - 5 | 6 | 1 - 5 | 7 | 2 - 6 | 5 | 1 - 6 | 7 | 1 - 7 | 5 | 2 - 7 | 6 | 1 - 10 | 5 | 2 - 10 | 6 | 1 - 10 | 7 | 2 - 15 | 5 | 3 - 15 | 6 | 2 - 15 | 7 | 3 - 15 | 10 | 1 -(13 rows) + FROM edges where id IN (6,7)', directed => false +) ORDER BY vid; + + vid | betweenness_centrality +-----+------------------------ + 1 | 0 + 3 | 1 + 7 | 0 +(3 rows) /* -- q2 */ ROLLBACK; diff --git a/include/metrics/betweennessCentrality.hpp b/include/metrics/betweennessCentrality.hpp index 7af4e44e14..89c4db98e5 100644 --- a/include/metrics/betweennessCentrality.hpp +++ b/include/metrics/betweennessCentrality.hpp @@ -69,7 +69,7 @@ class Pgr_metrics { public: using Graph = typename G::B_G; using Vertex = typename G::V; - + void betweennessCentrality ( const G &graph, size_t &result_tuple_count, @@ -86,12 +86,10 @@ class Pgr_metrics { graph.graph, centrality_map ); - if(boost::num_vertices(graph.graph) > 2){ - boost::relative_betweenness_centrality( - graph.graph, - centrality_map - ); - } + boost::relative_betweenness_centrality( + graph.graph, + centrality_map + ); generate_results(graph, centrality, result_tuple_count, postgres_rows); } From b5f77bcffb95556deaacb118454ed747d37c0f3e Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Thu, 4 Jul 2024 00:51:33 +0530 Subject: [PATCH 2/5] [centrality] fixed the directed graph case and changed default value to directed --- include/metrics/betweennessCentrality.hpp | 19 +++++++++++++------ sql/metrics/betweennessCentrality.sql | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/include/metrics/betweennessCentrality.hpp b/include/metrics/betweennessCentrality.hpp index 89c4db98e5..3f27c2d65c 100644 --- a/include/metrics/betweennessCentrality.hpp +++ b/include/metrics/betweennessCentrality.hpp @@ -32,6 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include #include #include @@ -69,7 +70,8 @@ class Pgr_metrics { public: 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, @@ -86,11 +88,13 @@ class Pgr_metrics { graph.graph, centrality_map ); - boost::relative_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); } @@ -108,6 +112,9 @@ class Pgr_metrics { (*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++; } } diff --git a/sql/metrics/betweennessCentrality.sql b/sql/metrics/betweennessCentrality.sql index c5f3f893f9..bf52dd4c71 100644 --- a/sql/metrics/betweennessCentrality.sql +++ b/sql/metrics/betweennessCentrality.sql @@ -31,7 +31,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. --v3.7 CREATE FUNCTION pgr_betweennesscentrality( TEXT, -- edges_sql (required) - directed BOOLEAN DEFAULT false, + directed BOOLEAN DEFAULT true, OUT vid BIGINT, OUT betweenness_centrality FLOAT) From cbbc9cff59d5cb8d22919e8eb1517b93c07a1d37 Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Fri, 5 Jul 2024 03:01:35 +0530 Subject: [PATCH 3/5] [centrality] finished docqueries with implicit(directed) and explicit(directed and undirected) cases --- doc/metrics/CMakeLists.txt | 3 +- doc/metrics/metrics-family.rst | 268 ------------------ doc/metrics/pgr_centrality.rst | 111 -------- docqueries/metrics/betweennessCentrality.pg | 65 ++++- .../metrics/betweennessCentrality.result | 145 +++++++++- .../metrics/betweennessCentrality_driver.h | 2 +- sql/metrics/_betweennessCentrality.sql | 4 +- sql/metrics/betweennessCentrality.sql | 8 +- src/metrics/betweennessCentrality.c | 2 +- src/metrics/betweennessCentrality_driver.cpp | 2 +- 10 files changed, 207 insertions(+), 403 deletions(-) delete mode 100644 doc/metrics/metrics-family.rst delete mode 100644 doc/metrics/pgr_centrality.rst diff --git a/doc/metrics/CMakeLists.txt b/doc/metrics/CMakeLists.txt index c58ca7d742..a03d8aee3d 100644 --- a/doc/metrics/CMakeLists.txt +++ b/doc/metrics/CMakeLists.txt @@ -1,7 +1,6 @@ SET(LOCAL_FILES - metrics-family.rst - pgr_centrality.rst + pgr_betweennessCentrality.rst ) foreach (f ${LOCAL_FILES}) diff --git a/doc/metrics/metrics-family.rst b/doc/metrics/metrics-family.rst deleted file mode 100644 index f467feac2f..0000000000 --- a/doc/metrics/metrics-family.rst +++ /dev/null @@ -1,268 +0,0 @@ -.. - **************************************************************************** - 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/ - **************************************************************************** - -| - - - -All Pairs - Family of Functions -=============================================================================== - -The following functions work on all vertices pair combinations - -.. index from here - -* :doc:`pgr_floydWarshall` - Floyd-Warshall's algorithm. -* :doc:`pgr_johnson` - Johnson's algorithm - -.. index to here - -.. toctree:: - :hidden: - - pgr_floydWarshall - pgr_johnson - -Introduction -------------------------------------------------------------------------------- - -.. characteristics_start - -The main characteristics are: - -- It does not return a path. -- Returns the sum of the costs of the shortest path for each pair of nodes in - the graph. -- Process is done only on edges with positive costs. -- Boost returns a :math:`V \times V` matrix, where the infinity values. - Represent the distance between vertices for which there is no path. - - - We return only the non infinity values in form of a set of `(start_vid, - end_vid, agg_cost)`. - -- Let be the case the values returned are stored in a table, so the unique index - would be the pair: `(start_vid, end_vid)`. - -- For the undirected graph, the results are symmetric. - - - The `agg_cost` of `(u, v)` is the same as for `(v, u)`. - -- When `start_vid` = `end_vid`, the `agg_cost` = 0. - -- **Recommended, use a bounding box of no more than 3500 edges.** - -.. characteristics_end - -Parameters -------------------------------------------------------------------------------- - -.. edges_start - -.. list-table:: - :width: 81 - :widths: auto - :header-rows: 1 - - * - Parameter - - Type - - Default - - Description - * - `Edges SQL`_ - - ``TEXT`` - - - - `Edges SQL`_ as described below. - -.. 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 -------------------------------------------------------------------------------- - -.. include:: pgRouting-concepts.rst - :start-after: return_cost_start - :end-before: return_cost_end - - -Performance ------------------------------------------------------------------------------- - -The following tests: - - non server computer - - with AMD 64 CPU - - 4G memory - - trusty - - posgreSQL version 9.3 - -Data -......................................... - -The following data was used - -.. parsed-literal:: - - BBOX="-122.8,45.4,-122.5,45.6" - wget --progress=dot:mega -O "sampledata.osm" \ - "https://www.overpass-api.de/api/xapi?*[bbox=${BBOX}][@meta]" - - -Data processing was done with osm2pgrouting-alpha - -.. parsed-literal:: - - createdb portland - psql -c "create extension postgis" portland - psql -c "create extension pgrouting" portland - osm2pgrouting -f sampledata.osm -d portland -s 0 - - - - -Results -......................................... - -:Test: One - -This test is not with a bounding box -The density of the passed graph is extremely low. -For each 30 tests were executed to get the average -The tested query is: - -.. parsed-literal:: - - SELECT count(*) FROM pgr_floydWarshall( - 'SELECT gid as id, source, target, cost, reverse_cost - FROM ways where id <= '); - - SELECT count(*) FROM pgr_johnson( - 'SELECT gid as id, source, target, cost, reverse_cost - FROM ways where id <= '); - -The results of this tests are presented as: - -:SIZE: is the number of edges given as input. -:EDGES: is the total number of records in the query. -:DENSITY: is the density of the data :math:`\dfrac{E}{V \times (V-1)}`. -:OUT ROWS: is the number of records returned by the queries. -:Floyd-Warshall: is the average execution time in seconds of pgr_floydWarshall. -:Johnson: is the average execution time in seconds of pgr_johnson. - - -====== ====== ========== ======== ============== ============= - SIZE EDGES DENSITY OUT ROWS Floyd-Warshall Johnson -====== ====== ========== ======== ============== ============= - 500 500 0.18E-7 1346 0.14 0.13 - 1000 1000 0.36E-7 2655 0.23 0.18 - 1500 1500 0.55E-7 4110 0.37 0.34 - 2000 2000 0.73E-7 5676 0.56 0.37 - 2500 2500 0.89E-7 7177 0.84 0.51 - 3000 3000 1.07E-7 8778 1.28 0.68 - 3500 3500 1.24E-7 10526 2.08 0.95 - 4000 4000 1.41E-7 12484 3.16 1.24 - 4500 4500 1.58E-7 14354 4.49 1.47 - 5000 5000 1.76E-7 16503 6.05 1.78 - 5500 5500 1.93E-7 18623 7.53 2.03 - 6000 6000 2.11E-7 20710 8.47 2.37 - 6500 6500 2.28E-7 22752 9.99 2.68 - 7000 7000 2.46E-7 24687 11.82 3.12 - 7500 7500 2.64E-7 26861 13.94 3.60 - 8000 8000 2.83E-7 29050 15.61 4.09 - 8500 8500 3.01E-7 31693 17.43 4.63 - 9000 9000 3.17E-7 33879 19.19 5.34 - 9500 9500 3.35E-7 36287 20.77 6.24 - 10000 10000 3.52E-7 38491 23.26 6.51 -====== ====== ========== ======== ============== ============= - - -:Test: Two - -This test is with a bounding box -The density of the passed graph higher than of the Test One. -For each 30 tests were executed to get the average -The tested edge query is: - -.. parsed-literal:: - - WITH - buffer AS ( - SELECT ST_Buffer(ST_Centroid(ST_Extent(the_geom)), SIZE) AS geom - FROM ways), - bbox AS ( - SELECT ST_Envelope(ST_Extent(geom)) as box FROM buffer) - SELECT gid as id, source, target, cost, reverse_cost - FROM ways where the_geom && (SELECT box from bbox); - -The tested queries - -.. parsed-literal:: - - SELECT count(*) FROM pgr_floydWarshall() - SELECT count(*) FROM pgr_johnson() - -The results of this tests are presented as: - -:SIZE: is the size of the bounding box. -:EDGES: is the total number of records in the query. -:DENSITY: is the density of the data :math:`\dfrac{E}{V \times (V-1)}`. -:OUT ROWS: is the number of records returned by the queries. -:Floyd-Warshall: is the average execution time in seconds of pgr_floydWarshall. -:Johnson: is the average execution time in seconds of pgr_johnson. - - -====== ===== ======== ======== ============== ============= - SIZE EDGES DENSITY OUT ROWS Floyd-Warshall Johnson -====== ===== ======== ======== ============== ============= - 0.001 44 0.0608 1197 0.10 0.10 - 0.002 99 0.0251 4330 0.10 0.10 - 0.003 223 0.0122 18849 0.12 0.12 - 0.004 358 0.0085 71834 0.16 0.16 - 0.005 470 0.0070 116290 0.22 0.19 - 0.006 639 0.0055 207030 0.37 0.27 - 0.007 843 0.0043 346930 0.64 0.38 - 0.008 996 0.0037 469936 0.90 0.49 - 0.009 1146 0.0032 613135 1.26 0.62 - 0.010 1360 0.0027 849304 1.87 0.82 - 0.011 1573 0.0024 1147101 2.65 1.04 - 0.012 1789 0.0021 1483629 3.72 1.35 - 0.013 1975 0.0019 1846897 4.86 1.68 - 0.014 2281 0.0017 2438298 7.08 2.28 - 0.015 2588 0.0015 3156007 10.28 2.80 - 0.016 2958 0.0013 4090618 14.67 3.76 - 0.017 3247 0.0012 4868919 18.12 4.48 -====== ===== ======== ======== ============== ============= - - -See Also -......................................... - -* :doc:`pgr_johnson` -* :doc:`pgr_floydWarshall` -* Boost `floyd-Warshall - `__ - -.. rubric:: Indices and tables - -* :ref:`genindex` -* :ref:`search` - diff --git a/doc/metrics/pgr_centrality.rst b/doc/metrics/pgr_centrality.rst deleted file mode 100644 index ae5ab13b52..0000000000 --- a/doc/metrics/pgr_centrality.rst +++ /dev/null @@ -1,111 +0,0 @@ -.. - **************************************************************************** - 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_centrality`` -=============================================================================== - -``pgr_centrality`` - 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_centrality(`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/docqueries/metrics/betweennessCentrality.pg b/docqueries/metrics/betweennessCentrality.pg index 82430f5ca9..4b90742f48 100644 --- a/docqueries/metrics/betweennessCentrality.pg +++ b/docqueries/metrics/betweennessCentrality.pg @@ -1,8 +1,65 @@ -- CopyRight(c) pgRouting developers -- Creative Commons Attribution-Share Alike 3.0 License : https://creativecommons.org/licenses/by-sa/3.0/ -/* -- q1 */ -SELECT * FROM pgr_betweennesscentrality( + +/* Implicit Cases (directed) */ +SELECT * FROM pgr_betweennessCentrality( 'SELECT id, source, target, cost, reverse_cost - FROM edges where id IN (6,7)', + FROM edges where id < 2' +) ORDER BY vid; + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 3' +) ORDER BY vid; + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 4' +) ORDER BY vid; + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 5' +) ORDER BY vid; + +/* Explicit Cases (undirected) */ +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 2', directed => false +) ORDER BY vid; + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 3', directed => false +) ORDER BY vid; + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 4', directed => false +) ORDER BY vid; + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 5', directed => false +) ORDER BY vid; + +/* Explicit Cases (directed) */ +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 2', directed => true +) ORDER BY vid; + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 3', directed => true +) ORDER BY vid; + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 4', directed => true +) ORDER BY vid; + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 5', directed => true ) ORDER BY vid; -/* -- q2 */ diff --git a/docqueries/metrics/betweennessCentrality.result b/docqueries/metrics/betweennessCentrality.result index 0b6be29253..27950e4a2f 100644 --- a/docqueries/metrics/betweennessCentrality.result +++ b/docqueries/metrics/betweennessCentrality.result @@ -2,19 +2,146 @@ BEGIN; BEGIN SET client_min_messages TO NOTICE; SET -/* -- q1 */ -SELECT * FROM pgr_betweennesscentrality( +/* Implicit Cases (directed) */ +SELECT * FROM pgr_betweennessCentrality( 'SELECT id, source, target, cost, reverse_cost - FROM edges where id IN (6,7)', directed => false + FROM edges where id < 2' ) ORDER BY vid; + vid | centrality +-----+------------ + 5 | 0 + 6 | 0 +(2 rows) - vid | betweenness_centrality ------+------------------------ - 1 | 0 - 3 | 1 - 7 | 0 +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 3' +) ORDER BY vid; + vid | centrality +-----+------------ + 5 | 0 + 6 | 0.5 + 10 | 0 +(3 rows) + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 4' +) ORDER BY vid; + vid | centrality +-----+-------------------- + 5 | 0 + 6 | 0.3333333333333333 + 10 | 0.3333333333333333 + 15 | 0 +(4 rows) + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 5' +) ORDER BY vid; + vid | centrality +-----+------------ + 5 | 0 + 6 | 0.5 + 7 | 0 + 10 | 0.25 + 15 | 0 +(5 rows) + +/* Explicit Cases (undirected) */ +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 2', directed => false +) ORDER BY vid; + vid | centrality +-----+------------ + 5 | 0 + 6 | 0 +(2 rows) + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 3', directed => false +) ORDER BY vid; + vid | centrality +-----+------------ + 5 | 0 + 6 | 1 + 10 | 0 +(3 rows) + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 4', directed => false +) ORDER BY vid; + vid | centrality +-----+-------------------- + 5 | 0 + 6 | 0.6666666666666666 + 10 | 0.6666666666666666 + 15 | 0 +(4 rows) + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 5', directed => false +) ORDER BY vid; + vid | centrality +-----+-------------------- + 5 | 0 + 6 | 0.8333333333333333 + 7 | 0 + 10 | 0.5 + 15 | 0 +(5 rows) + +/* Explicit Cases (directed) */ +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 2', directed => true +) ORDER BY vid; + vid | centrality +-----+------------ + 5 | 0 + 6 | 0 +(2 rows) + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 3', directed => true +) ORDER BY vid; + vid | centrality +-----+------------ + 5 | 0 + 6 | 0.5 + 10 | 0 (3 rows) -/* -- q2 */ +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 4', directed => true +) ORDER BY vid; + vid | centrality +-----+-------------------- + 5 | 0 + 6 | 0.3333333333333333 + 10 | 0.3333333333333333 + 15 | 0 +(4 rows) + +SELECT * FROM pgr_betweennessCentrality( + 'SELECT id, source, target, cost, reverse_cost + FROM edges where id < 5', directed => true +) ORDER BY vid; + vid | centrality +-----+------------ + 5 | 0 + 6 | 0.5 + 7 | 0 + 10 | 0.25 + 15 | 0 +(5 rows) + ROLLBACK; ROLLBACK diff --git a/include/drivers/metrics/betweennessCentrality_driver.h b/include/drivers/metrics/betweennessCentrality_driver.h index 2d849bbaa4..29d2a2e978 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_betweennesscentrality( +pgr_do_betweennessCentrality( char*, bool, diff --git a/sql/metrics/_betweennessCentrality.sql b/sql/metrics/_betweennessCentrality.sql index a2c6bf26e4..d68d849b6c 100644 --- a/sql/metrics/_betweennessCentrality.sql +++ b/sql/metrics/_betweennessCentrality.sql @@ -33,12 +33,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -------------------------------- --v3.7 -CREATE FUNCTION _pgr_betweennesscentrality( +CREATE FUNCTION _pgr_betweennessCentrality( edges_sql TEXT, directed BOOLEAN, OUT vid BIGINT, - OUT betweenness_centrality FLOAT) + OUT 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 bf52dd4c71..597cb2a7a8 100644 --- a/sql/metrics/betweennessCentrality.sql +++ b/sql/metrics/betweennessCentrality.sql @@ -29,16 +29,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ --v3.7 -CREATE FUNCTION pgr_betweennesscentrality( +CREATE FUNCTION pgr_betweennessCentrality( TEXT, -- edges_sql (required) directed BOOLEAN DEFAULT true, OUT vid BIGINT, - OUT betweenness_centrality FLOAT) + OUT centrality FLOAT) RETURNS SETOF RECORD AS $BODY$ - SELECT vid, betweenness_centrality + SELECT vid, centrality FROM _pgr_betweennesscentrality(_pgr_get_statement($1), $2); $BODY$ @@ -46,7 +46,7 @@ LANGUAGE SQL VOLATILE STRICT; -- COMMENTS -COMMENT ON FUNCTION pgr_betweennesscentrality(TEXT, BOOLEAN) +COMMENT ON FUNCTION pgr_betweennessCentrality(TEXT, BOOLEAN) IS 'pgr_betweennessCentrality - Parameters: - edges SQL with columns: source, target, cost [,reverse_cost]) diff --git a/src/metrics/betweennessCentrality.c b/src/metrics/betweennessCentrality.c index 05ffdda9f4..d50d02ae49 100644 --- a/src/metrics/betweennessCentrality.c +++ b/src/metrics/betweennessCentrality.c @@ -53,7 +53,7 @@ process( char* err_msg = NULL; clock_t start_t = clock(); - pgr_do_betweennesscentrality( + pgr_do_betweennessCentrality( edges_sql, directed, result_tuples, diff --git a/src/metrics/betweennessCentrality_driver.cpp b/src/metrics/betweennessCentrality_driver.cpp index 792e9cbf0c..6c6a1aa1ce 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_betweennesscentrality( +pgr_do_betweennessCentrality( char *edges_sql, bool directed, From 8e88f29ec6e952d7d45d176b6bd0ef48516e99c8 Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Fri, 5 Jul 2024 04:07:34 +0530 Subject: [PATCH 4/5] [centrality] minor change to camelCase --- sql/metrics/_betweennessCentrality.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/metrics/_betweennessCentrality.sql b/sql/metrics/_betweennessCentrality.sql index d68d849b6c..e705cdac3a 100644 --- a/sql/metrics/_betweennessCentrality.sql +++ b/sql/metrics/_betweennessCentrality.sql @@ -45,5 +45,5 @@ LANGUAGE C VOLATILE STRICT; -- COMMENTS -COMMENT ON FUNCTION _pgr_betweennesscentrality(TEXT, BOOLEAN) +COMMENT ON FUNCTION _pgr_betweennessCentrality(TEXT, BOOLEAN) IS 'pgRouting internal function'; From ea879dee3b779d6b8ebff910e52dfaadbb0b0795 Mon Sep 17 00:00:00 2001 From: Arun Thakur Date: Mon, 8 Jul 2024 20:25:32 +0530 Subject: [PATCH 5/5] [centrality] made the docs and added pgr_betweennessCentrality to experimental functions --- configuration.conf | 2 +- doc/metrics/pgr_betweennessCentrality.rst | 14 ++++++-------- doc/src/experimental.rst | 2 ++ docqueries/metrics/CMakeLists.txt | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/configuration.conf b/configuration.conf index 353634d61b..1f63c98998 100644 --- a/configuration.conf +++ b/configuration.conf @@ -46,7 +46,7 @@ planar | Y | Y | Y dominator | Y | Y | Y ordering | Y | Y | Y circuits | Y | Y | Y -metrics | Y | Y | N +metrics | Y | Y | Y #---------------------- # SQL only directories #---------------------- diff --git a/doc/metrics/pgr_betweennessCentrality.rst b/doc/metrics/pgr_betweennessCentrality.rst index 254a87d962..8d18983f69 100644 --- a/doc/metrics/pgr_betweennessCentrality.rst +++ b/doc/metrics/pgr_betweennessCentrality.rst @@ -14,7 +14,7 @@ =============================================================================== ``pgr_betweennessCentrality`` - Returns the relative betweeness centrality of -all edges in a graph using Brandes Algorithm. +all vertices 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 @@ -29,9 +29,7 @@ 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. +We use Boost's implementation which runs in :math:`\Theta(VE)` time and uses :math:`\Theta(VE)` space. Signatures ------------------------------------------------------------------------------- @@ -43,7 +41,7 @@ Signatures pgr_betweennessCentrality(`Edges SQL`_, [``directed``]) - | Returns set of ```(seq, edge_id, betweenness_centrality)``` + | Returns set of ```(seq, vid, centrality)``` | OR EMPTY SET .. TODO: Fix this when docqueries are made @@ -91,12 +89,12 @@ Result columns * - ``seq`` - ``INTEGER`` - Sequential Value starting from ``1`` - * - ``edge_id`` + * - ``vid`` - ``BIGINT`` - - Identifier of the edge + - Identifier of the vertex * - ``centrality`` - ``FLOAT`` - - relative betweenness centrality score of the edge (will be in range [0,1]) + - relative betweenness centrality score of the vertex (will be in range [0,1]) See Also ------------------------------------------------------------------------------- diff --git a/doc/src/experimental.rst b/doc/src/experimental.rst index 7277a28067..5f81a1dc24 100644 --- a/doc/src/experimental.rst +++ b/doc/src/experimental.rst @@ -121,6 +121,7 @@ Experimental Functions - :doc:`pgr_transitiveClosure` - :doc:`pgr_lengauerTarjanDominatorTree` - :doc:`pgr_hawickCircuits` +- :doc:`pgr_betweennessCentrality` .. toctree:: :hidden: @@ -136,6 +137,7 @@ Experimental Functions pgr_transitiveClosure pgr_lengauerTarjanDominatorTree pgr_hawickCircuits + pgr_betweennessCentrality See Also diff --git a/docqueries/metrics/CMakeLists.txt b/docqueries/metrics/CMakeLists.txt index 3e4f70e9c1..27f1bdae57 100644 --- a/docqueries/metrics/CMakeLists.txt +++ b/docqueries/metrics/CMakeLists.txt @@ -1,6 +1,6 @@ # Do not use extensions SET(LOCAL_FILES - betweennessCentrality.pg + betweennessCentrality ) foreach (f ${LOCAL_FILES})