harmonic.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """Functions for computing the harmonic centrality of a graph."""
  2. from functools import partial
  3. import networkx as nx
  4. __all__ = ["harmonic_centrality"]
  5. @nx._dispatchable(edge_attrs="distance")
  6. def harmonic_centrality(G, nbunch=None, distance=None, sources=None):
  7. r"""Compute harmonic centrality for nodes.
  8. Harmonic centrality [1]_ of a node `u` is the sum of the reciprocal
  9. of the shortest path distances from all other nodes to `u`
  10. .. math::
  11. C(u) = \sum_{v \neq u} \frac{1}{d(v, u)}
  12. where `d(v, u)` is the shortest-path distance between `v` and `u`.
  13. If `sources` is given as an argument, the returned harmonic centrality
  14. values are calculated as the sum of the reciprocals of the shortest
  15. path distances from the nodes specified in `sources` to `u` instead
  16. of from all nodes to `u`.
  17. Notice that higher values indicate higher centrality.
  18. Parameters
  19. ----------
  20. G : graph
  21. A NetworkX graph
  22. nbunch : container (default: all nodes in G)
  23. Container of nodes for which harmonic centrality values are calculated.
  24. sources : container (default: all nodes in G)
  25. Container of nodes `v` over which reciprocal distances are computed.
  26. Nodes not in `G` are silently ignored.
  27. distance : edge attribute key, optional (default=None)
  28. Use the specified edge attribute as the edge distance in shortest
  29. path calculations. If `None`, then each edge will have distance equal to 1.
  30. Returns
  31. -------
  32. nodes : dictionary
  33. Dictionary of nodes with harmonic centrality as the value.
  34. See Also
  35. --------
  36. betweenness_centrality, load_centrality, eigenvector_centrality,
  37. degree_centrality, closeness_centrality
  38. Notes
  39. -----
  40. If the 'distance' keyword is set to an edge attribute key then the
  41. shortest-path length will be computed using Dijkstra's algorithm with
  42. that edge attribute as the edge weight.
  43. References
  44. ----------
  45. .. [1] Boldi, Paolo, and Sebastiano Vigna. "Axioms for centrality."
  46. Internet Mathematics 10.3-4 (2014): 222-262.
  47. """
  48. nbunch = set(G.nbunch_iter(nbunch) if nbunch is not None else G.nodes)
  49. sources = set(G.nbunch_iter(sources) if sources is not None else G.nodes)
  50. centrality = {u: 0 for u in nbunch}
  51. transposed = False
  52. if len(nbunch) < len(sources):
  53. transposed = True
  54. nbunch, sources = sources, nbunch
  55. if nx.is_directed(G):
  56. G = nx.reverse(G, copy=False)
  57. spl = partial(nx.shortest_path_length, G, weight=distance)
  58. for v in sources:
  59. dist = spl(v)
  60. for u, d_uv in dist.items():
  61. # Ignore self-loops and edges with 0 weight
  62. if d_uv != 0 and u in nbunch:
  63. centrality[v if transposed else u] += 1 / d_uv
  64. return centrality