reaching.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. """Functions for computing reaching centrality of a node or a graph."""
  2. import networkx as nx
  3. from networkx.utils import pairwise
  4. __all__ = ["global_reaching_centrality", "local_reaching_centrality"]
  5. def _average_weight(G, path, weight=None):
  6. """Returns the average weight of an edge in a weighted path.
  7. Parameters
  8. ----------
  9. G : graph
  10. A networkx graph.
  11. path: list
  12. A list of vertices that define the path.
  13. weight : None or string, optional (default=None)
  14. If None, edge weights are ignored. Then the average weight of an edge
  15. is assumed to be the multiplicative inverse of the length of the path.
  16. Otherwise holds the name of the edge attribute used as weight.
  17. """
  18. path_length = len(path) - 1
  19. if path_length <= 0:
  20. return 0
  21. if weight is None:
  22. return 1 / path_length
  23. total_weight = sum(G.edges[i, j][weight] for i, j in pairwise(path))
  24. return total_weight / path_length
  25. @nx._dispatchable(edge_attrs="weight")
  26. def global_reaching_centrality(G, weight=None, normalized=True):
  27. """Returns the global reaching centrality of a directed graph.
  28. The *global reaching centrality* of a weighted directed graph is the
  29. average over all nodes of the difference between the local reaching
  30. centrality of the node and the greatest local reaching centrality of
  31. any node in the graph [1]_. For more information on the local
  32. reaching centrality, see :func:`local_reaching_centrality`.
  33. Informally, the local reaching centrality is the proportion of the
  34. graph that is reachable from the neighbors of the node.
  35. Parameters
  36. ----------
  37. G : DiGraph
  38. A networkx DiGraph.
  39. weight : None or string, optional (default=None)
  40. Attribute to use for edge weights. If ``None``, each edge weight
  41. is assumed to be one. A higher weight implies a stronger
  42. connection between nodes and a *shorter* path length.
  43. normalized : bool, optional (default=True)
  44. Whether to normalize the edge weights by the total sum of edge
  45. weights.
  46. Returns
  47. -------
  48. h : float
  49. The global reaching centrality of the graph.
  50. Examples
  51. --------
  52. >>> G = nx.DiGraph()
  53. >>> G.add_edge(1, 2)
  54. >>> G.add_edge(1, 3)
  55. >>> nx.global_reaching_centrality(G)
  56. 1.0
  57. >>> G.add_edge(3, 2)
  58. >>> nx.global_reaching_centrality(G)
  59. 0.75
  60. See also
  61. --------
  62. local_reaching_centrality
  63. References
  64. ----------
  65. .. [1] Mones, Enys, Lilla Vicsek, and Tamás Vicsek.
  66. "Hierarchy Measure for Complex Networks."
  67. *PLoS ONE* 7.3 (2012): e33799.
  68. https://doi.org/10.1371/journal.pone.0033799
  69. """
  70. if nx.is_negatively_weighted(G, weight=weight):
  71. raise nx.NetworkXError("edge weights must be positive")
  72. total_weight = G.size(weight=weight)
  73. if total_weight <= 0:
  74. raise nx.NetworkXError("Size of G must be positive")
  75. # If provided, weights must be interpreted as connection strength
  76. # (so higher weights are more likely to be chosen). However, the
  77. # shortest path algorithms in NetworkX assume the provided "weight"
  78. # is actually a distance (so edges with higher weight are less
  79. # likely to be chosen). Therefore we need to invert the weights when
  80. # computing shortest paths.
  81. #
  82. # If weight is None, we leave it as-is so that the shortest path
  83. # algorithm can use a faster, unweighted algorithm.
  84. if weight is not None:
  85. def as_distance(u, v, d):
  86. return total_weight / d.get(weight, 1)
  87. shortest_paths = dict(nx.shortest_path(G, weight=as_distance))
  88. else:
  89. shortest_paths = dict(nx.shortest_path(G))
  90. centrality = local_reaching_centrality
  91. # TODO This can be trivially parallelized.
  92. lrc = [
  93. centrality(G, node, paths=paths, weight=weight, normalized=normalized)
  94. for node, paths in shortest_paths.items()
  95. ]
  96. max_lrc = max(lrc)
  97. return sum(max_lrc - c for c in lrc) / (len(G) - 1)
  98. @nx._dispatchable(edge_attrs="weight")
  99. def local_reaching_centrality(G, v, paths=None, weight=None, normalized=True):
  100. """Returns the local reaching centrality of a node in a directed
  101. graph.
  102. The *local reaching centrality* of a node in a directed graph is the
  103. proportion of other nodes reachable from that node [1]_.
  104. Parameters
  105. ----------
  106. G : DiGraph
  107. A NetworkX DiGraph.
  108. v : node
  109. A node in the directed graph `G`.
  110. paths : dictionary (default=None)
  111. If this is not `None` it must be a dictionary representation
  112. of single-source shortest paths, as computed by, for example,
  113. :func:`networkx.shortest_path` with source node `v`. Use this
  114. keyword argument if you intend to invoke this function many
  115. times but don't want the paths to be recomputed each time.
  116. weight : None or string, optional (default=None)
  117. Attribute to use for edge weights. If `None`, each edge weight
  118. is assumed to be one. A higher weight implies a stronger
  119. connection between nodes and a *shorter* path length.
  120. normalized : bool, optional (default=True)
  121. Whether to normalize the edge weights by the total sum of edge
  122. weights.
  123. Returns
  124. -------
  125. h : float
  126. The local reaching centrality of the node ``v`` in the graph
  127. ``G``.
  128. Examples
  129. --------
  130. >>> G = nx.DiGraph()
  131. >>> G.add_edges_from([(1, 2), (1, 3)])
  132. >>> nx.local_reaching_centrality(G, 3)
  133. 0.0
  134. >>> G.add_edge(3, 2)
  135. >>> nx.local_reaching_centrality(G, 3)
  136. 0.5
  137. See also
  138. --------
  139. global_reaching_centrality
  140. References
  141. ----------
  142. .. [1] Mones, Enys, Lilla Vicsek, and Tamás Vicsek.
  143. "Hierarchy Measure for Complex Networks."
  144. *PLoS ONE* 7.3 (2012): e33799.
  145. https://doi.org/10.1371/journal.pone.0033799
  146. """
  147. # Corner case: graph with single node containing a self-loop
  148. if (total_weight := G.size(weight=weight)) > 0 and len(G) == 1:
  149. raise nx.NetworkXError(
  150. "local_reaching_centrality of a single node with self-loop not well-defined"
  151. )
  152. if paths is None:
  153. if nx.is_negatively_weighted(G, weight=weight):
  154. raise nx.NetworkXError("edge weights must be positive")
  155. if total_weight <= 0:
  156. raise nx.NetworkXError("Size of G must be positive")
  157. if weight is not None:
  158. # Interpret weights as lengths.
  159. def as_distance(u, v, d):
  160. return total_weight / d.get(weight, 1)
  161. paths = nx.shortest_path(G, source=v, weight=as_distance)
  162. else:
  163. paths = nx.shortest_path(G, source=v)
  164. # If the graph is unweighted, simply return the proportion of nodes
  165. # reachable from the source node ``v``.
  166. if weight is None and G.is_directed():
  167. return (len(paths) - 1) / (len(G) - 1)
  168. if normalized and weight is not None:
  169. norm = G.size(weight=weight) / G.size()
  170. else:
  171. norm = 1
  172. # TODO This can be trivially parallelized.
  173. avgw = (_average_weight(G, path, weight=weight) for path in paths.values())
  174. sum_avg_weight = sum(avgw) / norm
  175. return sum_avg_weight / (len(G) - 1)