-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation of closeness_centrality #593
Changes from 5 commits
6e819b5
46044a6
1a23946
1de4da1
05de1ac
c8745d6
d8fc186
29516fb
5fad410
6082d68
55debb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
features: | ||
- | | ||
Added a new function, :func:`~retworkx.closeness_centrality` to compute | ||
closeness centrality of all nodes in a :class:`~retworkx.PyGraph` or | ||
:class:`~retworkx.PyDiGraph` object. | ||
|
||
The closeness centrality of a node :math:`u` is the reciprocal of the | ||
average shortest path distance to :math:`u` over all :math:`n-1` reachable | ||
nodes. | ||
|
||
.. 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`. | ||
|
||
Wasserman and Faust propose an improved formula for graphs with more than | ||
one connected component. The result is "a ratio of the fraction of actors | ||
in the group who are reachable, to the average distance" from the reachable | ||
actors. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This paragraph looks identical to what's in the NetworkX documentation for this option: https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.centrality.closeness_centrality.html It might be better to put this in your own words instead of copying exactly what NetworkX has in the documentation or provide a link to the original text as a comment to cite the original source of this text (same with the docstrings). |
||
|
||
.. math:: | ||
|
||
C_{WF}(u) = \frac{n-1}{N-1} \frac{n - 1}{\sum_{v=1}^{n-1} d(v, u)}, | ||
|
||
where :math:`N` is the number of nodes in the graph. By default, this is | ||
enabled. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// 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; | ||
mtreinish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use petgraph::Graph; | ||
use retworkx_core::centrality::closeness_centrality; | ||
use retworkx_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); | ||
} | ||
|
||
#[test] | ||
fn test_weighted_closeness() { | ||
georgios-ts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let mut g = Graph::new(); | ||
let s = g.add_node(0); | ||
let u = g.add_node(0); | ||
let x = g.add_node(0); | ||
let v = g.add_node(0); | ||
let y = g.add_node(0); | ||
g.add_edge(s, u, 10.); | ||
g.add_edge(s, x, 5.); | ||
g.add_edge(u, v, 1.); | ||
g.add_edge(u, x, 2.); | ||
g.add_edge(v, y, 1.); | ||
g.add_edge(x, u, 3.); | ||
g.add_edge(x, v, 5.); | ||
g.add_edge(x, y, 2.); | ||
g.add_edge(y, s, 7.); | ||
g.add_edge(y, v, 6.); | ||
let c = closeness_centrality(&g, true); | ||
println!("{:?}", c); | ||
georgios-ts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_eq!(0, 0) | ||
georgios-ts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a link to the paper for this? If so it'd be good to include a link to it. (also in the docstrings)