-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of closeness_centrality (#593)
* Initial implementation of closeness_centrality * fix typo * add tests * apply the suggestions. * add a releasenote and improve Doc * apply the suggestions * supports rust version 1.48.0 * improve variable name * Fix up docstrings and release note * Fix rustworkx-core tests --------- Co-authored-by: Matthew Treinish <[email protected]>
- Loading branch information
Showing
12 changed files
with
431 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
releasenotes/notes/closeness-centrality-459c5c7e35cb2e63.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
features: | ||
- | | ||
Added a new function, :func:`~.closeness_centrality` to compute the | ||
closeness centrality of all nodes in a :class:`~.PyGraph` or | ||
:class:`~.PyDiGraph` object. | ||
The closeness centrality of a node :math:`u` is defined as the the | ||
reciprocal of the average shortest path distance to :math:`u` over all | ||
:math:`n-1` reachable nodes. In it's general form this can be expressed as: | ||
.. math:: | ||
C(u) = \frac{n - 1}{\sum_{v=1}^{n-1} d(v, u)}, | ||
where :math:`d(v, u)` is the shortest-path distance between :math:`v` and | ||
:math:`u`, and :math:`n` is the number of nodes that can reach :math:`u`. | ||
For example, to visualize the closeness centrality of a graph: | ||
.. jupyter-execute:: | ||
import matplotlib.pyplot as plt | ||
import rustworkx as rx | ||
from rustworkx.visualization import mpl_draw | ||
graph = rx.generators.hexagonal_lattice_graph(4, 4) | ||
centrality = rx.closeness_centrality(graph) | ||
# Generate a color list | ||
colors = [] | ||
for node in graph.node_indices(): | ||
colors.append(centrality[node]) | ||
# Generate a visualization with a colorbar | ||
plt.rcParams['figure.figsize'] = [15, 10] | ||
ax = plt.gca() | ||
sm = plt.cm.ScalarMappable(norm=plt.Normalize( | ||
vmin=min(centrality.values()), | ||
vmax=max(centrality.values()) | ||
)) | ||
plt.colorbar(sm, ax=ax) | ||
plt.title("Closeness Centrality of a 4 x 4 Hexagonal Lattice Graph") | ||
mpl_draw(graph, node_color=colors, ax=ax) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use petgraph::visit::Reversed; | ||
use rustworkx_core::centrality::closeness_centrality; | ||
use rustworkx_core::petgraph::graph::{DiGraph, UnGraph}; | ||
|
||
#[test] | ||
fn test_simple() { | ||
let g = UnGraph::<i32, ()>::from_edges(&[(1, 2), (2, 3), (3, 4), (1, 4)]); | ||
let c = closeness_centrality(&g, true); | ||
assert_eq!( | ||
vec![ | ||
Some(0.0), | ||
Some(0.5625), | ||
Some(0.5625), | ||
Some(0.5625), | ||
Some(0.5625) | ||
], | ||
c | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_wf_improved() { | ||
let g = UnGraph::<i32, ()>::from_edges(&[(0, 1), (1, 2), (2, 3), (4, 5), (5, 6)]); | ||
let c = closeness_centrality(&g, true); | ||
assert_eq!( | ||
vec![ | ||
Some(1.0 / 4.0), | ||
Some(3.0 / 8.0), | ||
Some(3.0 / 8.0), | ||
Some(1.0 / 4.0), | ||
Some(2.0 / 9.0), | ||
Some(1.0 / 3.0), | ||
Some(2.0 / 9.0) | ||
], | ||
c | ||
); | ||
let cwf = closeness_centrality(&g, false); | ||
assert_eq!( | ||
vec![ | ||
Some(1.0 / 2.0), | ||
Some(3.0 / 4.0), | ||
Some(3.0 / 4.0), | ||
Some(1.0 / 2.0), | ||
Some(2.0 / 3.0), | ||
Some(1.0), | ||
Some(2.0 / 3.0) | ||
], | ||
cwf | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_digraph() { | ||
let g = DiGraph::<i32, ()>::from_edges(&[(0, 1), (1, 2)]); | ||
let c = closeness_centrality(&g, true); | ||
assert_eq!(vec![Some(0.), Some(1. / 2.), Some(2. / 3.)], c); | ||
|
||
let cr = closeness_centrality(Reversed(&g), true); | ||
assert_eq!(vec![Some(2. / 3.), Some(1. / 2.), Some(0.)], cr); | ||
} | ||
|
||
#[test] | ||
fn test_k5() { | ||
let g = UnGraph::<i32, ()>::from_edges(&[ | ||
(0, 1), | ||
(0, 2), | ||
(0, 3), | ||
(0, 4), | ||
(1, 2), | ||
(1, 3), | ||
(1, 4), | ||
(2, 3), | ||
(2, 4), | ||
(3, 4), | ||
]); | ||
let c = closeness_centrality(&g, true); | ||
assert_eq!( | ||
vec![Some(1.0), Some(1.0), Some(1.0), Some(1.0), Some(1.0)], | ||
c | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_path() { | ||
let g = UnGraph::<i32, ()>::from_edges(&[(0, 1), (1, 2)]); | ||
let c = closeness_centrality(&g, true); | ||
assert_eq!(vec![Some(2.0 / 3.0), Some(1.0), Some(2.0 / 3.0)], c); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.