Skip to content

Commit

Permalink
Add edge betweenness centrality (#799)
Browse files Browse the repository at this point in the history
* Add edge betweenness centrality

* Fix MSRV

* Update pyfunction signatures

* Add tests

* Add test with stable graph

* Remove unnecessary import from doc test

* Address review comments

---------

Co-authored-by: Ivan Carvalho <8753214+IvanIsCoding@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 9, 2023
1 parent 8cfc849 commit 004dc16
Show file tree
Hide file tree
Showing 11 changed files with 694 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ the functions from the explicitly typed based on the data type.
rustworkx.digraph_spring_layout
rustworkx.digraph_num_shortest_paths_unweighted
rustworkx.digraph_betweenness_centrality
rustworkx.digraph_edge_betweenness_centrality
rustworkx.digraph_eigenvector_centrality
rustworkx.digraph_unweighted_average_shortest_path_length
rustworkx.digraph_bfs_search
Expand Down Expand Up @@ -374,6 +375,7 @@ typed API based on the data type.
rustworkx.graph_spring_layout
rustworkx.graph_num_shortest_paths_unweighted
rustworkx.graph_betweenness_centrality
rustworkx.graph_edge_betweenness_centrality
rustworkx.graph_eigenvector_centrality
rustworkx.graph_unweighted_average_shortest_path_length
rustworkx.graph_bfs_search
Expand Down Expand Up @@ -416,6 +418,7 @@ Custom Return Types
rustworkx.AllPairsPathMapping
rustworkx.AllPairsPathLengthMapping
rustworkx.CentralityMapping
rustworkx.EdgeCentralityMapping
rustworkx.Chains
rustworkx.NodeMap
rustworkx.ProductNodeMap
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
features:
- |
Added a new function, :func:`~rustworkx.edge_betweenness_centrality` to compute
edge betweenness centrality of all edges in a :class:`~rustworkx.PyGraph` or
:class:`~rustworkx.PyDiGraph` object. The algorithm used in this function is
based on:
Ulrik Brandes, On Variants of Shortest-Path Betweenness Centrality
and their Generic Computation. Social Networks 30(2):136-145, 2008.
Edge betweenness centrality of an edge :math:`e` is the sum of the
fraction of all-pairs shortest paths that pass through :math`e`
.. math::
c_B(e) =\sum_{s,t \in V} \frac{\sigma(s, t|e)}{\sigma(s, t)}
where :math:`V` is the set of nodes, :math:`\sigma(s, t)` is the
number of shortest :math:`(s, t)`-paths, and :math:`\sigma(s, t|e)` is
the number of those paths passing through edge :math:`e`.
For example, the following computes the edge betweenness centrality for all
edges in a 5x5 grid graph and uses the result to color the edges in a graph
visualization:
.. jupyter-execute::
import rustworkx
from rustworkx.visualization import mpl_draw
graph = rustworkx.generators.grid_graph(5, 5)
btw = rustworkx.edge_betweenness_centrality(graph)
# Color edges in graph visualization with edge betweenness centrality
colors = []
for i in graph.edge_indices():
colors.append(btw[i])
mpl_draw(graph, edge_color=colors)
- |
Added a new function to rustworkx-core ``edge_betweenness_centrality`` to
the ``rustworkx_core:centrality`` module which computes the edge betweenness
centrality of all edges in a given graph.
1 change: 1 addition & 0 deletions rustworkx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ petgraph = "0.6.3"
rayon = "1.6"
num-traits = "0.2"
priority-queue = "1.2"
rayon-cond = "0.2"

[dependencies.hashbrown]
version = "0.12"
Expand Down
Loading

0 comments on commit 004dc16

Please sign in to comment.