Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/networkx/algorithms/centrality/harmonic.py: 27%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

22 statements  

1"""Functions for computing the harmonic centrality of a graph.""" 

2 

3from functools import partial 

4 

5import networkx as nx 

6 

7__all__ = ["harmonic_centrality"] 

8 

9 

10@nx._dispatchable(edge_attrs="distance") 

11def harmonic_centrality(G, nbunch=None, distance=None, sources=None): 

12 r"""Compute harmonic centrality for nodes. 

13 

14 Harmonic centrality [1]_ of a node `u` is the sum of the reciprocal 

15 of the shortest path distances from all other nodes to `u` 

16 

17 .. math:: 

18 

19 C(u) = \sum_{v \neq u} \frac{1}{d(v, u)} 

20 

21 where `d(v, u)` is the shortest-path distance between `v` and `u`. 

22 

23 If `sources` is given as an argument, the returned harmonic centrality 

24 values are calculated as the sum of the reciprocals of the shortest 

25 path distances from the nodes specified in `sources` to `u` instead 

26 of from all nodes to `u`. 

27 

28 Notice that higher values indicate higher centrality. 

29 

30 Parameters 

31 ---------- 

32 G : graph 

33 A NetworkX graph 

34 

35 nbunch : container (default: all nodes in G) 

36 Container of nodes for which harmonic centrality values are calculated. 

37 

38 sources : container (default: all nodes in G) 

39 Container of nodes `v` over which reciprocal distances are computed. 

40 Nodes not in `G` are silently ignored. 

41 

42 distance : edge attribute key, optional (default=None) 

43 Use the specified edge attribute as the edge distance in shortest 

44 path calculations. If `None`, then each edge will have distance equal to 1. 

45 

46 Returns 

47 ------- 

48 nodes : dictionary 

49 Dictionary of nodes with harmonic centrality as the value. 

50 

51 See Also 

52 -------- 

53 betweenness_centrality, load_centrality, eigenvector_centrality, 

54 degree_centrality, closeness_centrality 

55 

56 Notes 

57 ----- 

58 If the 'distance' keyword is set to an edge attribute key then the 

59 shortest-path length will be computed using Dijkstra's algorithm with 

60 that edge attribute as the edge weight. 

61 

62 References 

63 ---------- 

64 .. [1] Boldi, Paolo, and Sebastiano Vigna. "Axioms for centrality." 

65 Internet Mathematics 10.3-4 (2014): 222-262. 

66 """ 

67 

68 nbunch = set(G.nbunch_iter(nbunch) if nbunch is not None else G.nodes) 

69 sources = set(G.nbunch_iter(sources) if sources is not None else G.nodes) 

70 

71 centrality = {u: 0 for u in nbunch} 

72 

73 transposed = False 

74 if len(nbunch) < len(sources): 

75 transposed = True 

76 nbunch, sources = sources, nbunch 

77 if nx.is_directed(G): 

78 G = nx.reverse(G, copy=False) 

79 

80 spl = partial(nx.shortest_path_length, G, weight=distance) 

81 for v in sources: 

82 dist = spl(v) 

83 for u, d_uv in dist.items(): 

84 # Ignore self-loops and edges with 0 weight 

85 if d_uv != 0 and u in nbunch: 

86 centrality[v if transposed else u] += 1 / d_uv 

87 

88 return centrality