Skip to content

Commit

Permalink
Merge pull request #390 from bedupako12mas/centrality-week5
Browse files Browse the repository at this point in the history
Centrality Week-5
  • Loading branch information
bedupako12mas authored Jul 1, 2024
2 parents ff67a34 + 4a25511 commit 1ff6bf8
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 81 deletions.
111 changes: 111 additions & 0 deletions doc/metrics/pgr_betweennessCentrality.rst
Original file line number Diff line number Diff line change
@@ -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
<https://www.boost.org/doc/libs/1_84_0/libs/graph/doc/betweenness_centrality.html>`_
* Queries uses the :doc:`sampledata` network.

.. rubric:: Indices and tables

* :ref:`genindex`
* :ref:`search`
2 changes: 1 addition & 1 deletion docqueries/metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Do not use extensions
SET(LOCAL_FILES
centrality.pg
betweennessCentrality.pg
)

foreach (f ${LOCAL_FILES})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- CopyRight(c) pgRouting developers
-- Creative Commons Attribution-Share Alike 3.0 License : https://creativecommons.org/licenses/by-sa/3.0/
/* -- q1 */
SELECT * FROM pgr_centrality(
SELECT * FROM pgr_betweennesscentrality(
'SELECT id, source, target, cost, reverse_cost
FROM edges where id < 5'
) ORDER BY start_vid, end_vid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ BEGIN
SET client_min_messages TO NOTICE;
SET
/* -- q1 */
SELECT * FROM pgr_centrality(
SELECT * FROM pgr_betweennesscentrality(
'SELECT id, source, target, cost, reverse_cost
FROM edges where id < 5'
) ORDER BY start_vid, end_vid;
Expand Down
2 changes: 1 addition & 1 deletion docqueries/metrics/test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*PGR-GNU*****************************************************************
File: floydWarshall_driver.h
File: betweennessCentrality_driver.h
Generated with Template by:
Copyright (c) 2015 pgRouting developers
Expand Down Expand Up @@ -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 */
Expand All @@ -48,7 +48,7 @@ extern "C" {
#endif

void
pgr_do_centrality(
pgr_do_betweennesscentrality(
char*,
bool,

Expand All @@ -61,4 +61,4 @@ pgr_do_centrality(
}
#endif

#endif // INCLUDE_DRIVERS_METRICS_CENTRALITY_DRIVER_H_
#endif // INCLUDE_DRIVERS_METRICS_BETWEENNESSCENTRALITY_DRIVER_H_
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*PGR-GNU*****************************************************************
File: centrality.hpp
File: betweennessCentrality.hpp
Copyright (c) 2015 pgRouting developers
Mail: project@pgrouting.org
Expand All @@ -26,22 +26,18 @@ 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 <deque>
#include <vector>
#include <set>
#include <limits>
#include <map>

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

#include "c_types/iid_t_rt.h"
#include "cpp_common/basePath_SSEC.hpp"
Expand All @@ -52,20 +48,13 @@ 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 G> 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 <class G>
void
pgr_centrality(
pgr_betweennesscentrality(
G &graph,
size_t &result_tuple_count,
IID_t_rt **postgres_rows) {
Expand All @@ -75,7 +64,7 @@ pgr_centrality(


// template class
template < class G >
template <class G>
class Pgr_metrics {
public:
using Graph = typename G::B_G;
Expand All @@ -85,50 +74,47 @@ class Pgr_metrics {
const G &graph,
size_t &result_tuple_count,
IID_t_rt **postgres_rows ){
std::map<int64_t, double> centrality_map;
std::vector<double> centrality_score(boost::num_vertices(graph.graph));

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

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

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

generate_results(centrality_map, result_tuple_count, postgres_rows);

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

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


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

} // namespace pgrouting

#endif // INCLUDE_METRICS_CENTRALITY_HPP_
#endif // INCLUDE_METRICS_BETWEENNESSCENTRALITY_HPP_
4 changes: 2 additions & 2 deletions sql/metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

SET(LOCAL_FILES
_centrality.sql
centrality.sql
_betweennessCentrality.sql
betweennessCentrality.sql
)

foreach (f ${LOCAL_FILES})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*PGR-GNU*****************************************************************
File: _centrality.sql
File: _betweennessCentrality.sql
Template:
Copyright (c) 2015 pgRouting developers
Expand Down Expand Up @@ -28,23 +28,22 @@ 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 agg_cost FLOAT)
OUT vid BIGINT,
OUT betweenness_centrality FLOAT)
RETURNS SETOF RECORD AS
'MODULE_PATHNAME'
LANGUAGE C VOLATILE STRICT;

-- COMMENTS

COMMENT ON FUNCTION _pgr_centrality(TEXT, BOOLEAN)
COMMENT ON FUNCTION _pgr_betweennesscentrality(TEXT, BOOLEAN)
IS 'pgRouting internal function';
Loading

0 comments on commit 1ff6bf8

Please sign in to comment.