Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/centrality/harmonic.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 """Functions for computing the harmonic centrality of a graph.""" | |
2 from functools import partial | |
3 | |
4 import networkx as nx | |
5 | |
6 __all__ = ["harmonic_centrality"] | |
7 | |
8 | |
9 def harmonic_centrality(G, nbunch=None, distance=None): | |
10 r"""Compute harmonic centrality for nodes. | |
11 | |
12 Harmonic centrality [1]_ of a node `u` is the sum of the reciprocal | |
13 of the shortest path distances from all other nodes to `u` | |
14 | |
15 .. math:: | |
16 | |
17 C(u) = \sum_{v \neq u} \frac{1}{d(v, u)} | |
18 | |
19 where `d(v, u)` is the shortest-path distance between `v` and `u`. | |
20 | |
21 Notice that higher values indicate higher centrality. | |
22 | |
23 Parameters | |
24 ---------- | |
25 G : graph | |
26 A NetworkX graph | |
27 | |
28 nbunch : container | |
29 Container of nodes. If provided harmonic centrality will be computed | |
30 only over the nodes in nbunch. | |
31 | |
32 distance : edge attribute key, optional (default=None) | |
33 Use the specified edge attribute as the edge distance in shortest | |
34 path calculations. If `None`, then each edge will have distance equal to 1. | |
35 | |
36 Returns | |
37 ------- | |
38 nodes : dictionary | |
39 Dictionary of nodes with harmonic centrality as the value. | |
40 | |
41 See Also | |
42 -------- | |
43 betweenness_centrality, load_centrality, eigenvector_centrality, | |
44 degree_centrality, closeness_centrality | |
45 | |
46 Notes | |
47 ----- | |
48 If the 'distance' keyword is set to an edge attribute key then the | |
49 shortest-path length will be computed using Dijkstra's algorithm with | |
50 that edge attribute as the edge weight. | |
51 | |
52 References | |
53 ---------- | |
54 .. [1] Boldi, Paolo, and Sebastiano Vigna. "Axioms for centrality." | |
55 Internet Mathematics 10.3-4 (2014): 222-262. | |
56 """ | |
57 if G.is_directed(): | |
58 G = G.reverse() | |
59 spl = partial(nx.shortest_path_length, G, weight=distance) | |
60 return { | |
61 u: sum(1 / d if d > 0 else 0 for v, d in spl(source=u).items()) | |
62 for u in G.nbunch_iter(nbunch) | |
63 } |