load.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. """Load centrality."""
  2. from operator import itemgetter
  3. import networkx as nx
  4. __all__ = ["load_centrality", "edge_load_centrality"]
  5. def newman_betweenness_centrality(G, v=None, cutoff=None, normalized=True, weight=None):
  6. """Compute load centrality for nodes.
  7. The load centrality of a node is the fraction of all shortest
  8. paths that pass through that node.
  9. Parameters
  10. ----------
  11. G : graph
  12. A networkx graph.
  13. normalized : bool, optional (default=True)
  14. If True the betweenness values are normalized by b=b/(n-1)(n-2) where
  15. n is the number of nodes in G.
  16. weight : None or string, optional (default=None)
  17. If None, edge weights are ignored.
  18. Otherwise holds the name of the edge attribute used as weight.
  19. The weight of an edge is treated as the length or distance between the two sides.
  20. cutoff : bool, optional (default=None)
  21. If specified, only consider paths of length <= cutoff.
  22. Returns
  23. -------
  24. nodes : dictionary
  25. Dictionary of nodes with centrality as the value.
  26. See Also
  27. --------
  28. betweenness_centrality
  29. Notes
  30. -----
  31. Load centrality is slightly different than betweenness. It was originally
  32. introduced by [2]_. For this load algorithm see [1]_.
  33. References
  34. ----------
  35. .. [1] Mark E. J. Newman:
  36. Scientific collaboration networks. II.
  37. Shortest paths, weighted networks, and centrality.
  38. Physical Review E 64, 016132, 2001.
  39. http://journals.aps.org/pre/abstract/10.1103/PhysRevE.64.016132
  40. .. [2] Kwang-Il Goh, Byungnam Kahng and Doochul Kim
  41. Universal behavior of Load Distribution in Scale-Free Networks.
  42. Physical Review Letters 87(27):1–4, 2001.
  43. https://doi.org/10.1103/PhysRevLett.87.278701
  44. """
  45. if v is not None: # only one node
  46. betweenness = 0.0
  47. for source in G:
  48. ubetween = _node_betweenness(G, source, cutoff, False, weight)
  49. betweenness += ubetween[v] if v in ubetween else 0
  50. if normalized:
  51. order = G.order()
  52. if order <= 2:
  53. return betweenness # no normalization b=0 for all nodes
  54. betweenness *= 1.0 / ((order - 1) * (order - 2))
  55. else:
  56. betweenness = {}.fromkeys(G, 0.0)
  57. for source in betweenness:
  58. ubetween = _node_betweenness(G, source, cutoff, False, weight)
  59. for vk in ubetween:
  60. betweenness[vk] += ubetween[vk]
  61. if normalized:
  62. order = G.order()
  63. if order <= 2:
  64. return betweenness # no normalization b=0 for all nodes
  65. scale = 1.0 / ((order - 1) * (order - 2))
  66. for v in betweenness:
  67. betweenness[v] *= scale
  68. return betweenness # all nodes
  69. def _node_betweenness(G, source, cutoff=False, normalized=True, weight=None):
  70. """Node betweenness_centrality helper:
  71. See betweenness_centrality for what you probably want.
  72. This actually computes "load" and not betweenness.
  73. See https://networkx.lanl.gov/ticket/103
  74. This calculates the load of each node for paths from a single source.
  75. (The fraction of number of shortests paths from source that go
  76. through each node.)
  77. To get the load for a node you need to do all-pairs shortest paths.
  78. If weight is not None then use Dijkstra for finding shortest paths.
  79. """
  80. # get the predecessor and path length data
  81. if weight is None:
  82. (pred, length) = nx.predecessor(G, source, cutoff=cutoff, return_seen=True)
  83. else:
  84. (pred, length) = nx.dijkstra_predecessor_and_distance(G, source, cutoff, weight)
  85. # order the nodes by path length
  86. onodes = [(l, vert) for (vert, l) in length.items()]
  87. onodes.sort()
  88. onodes[:] = [vert for (l, vert) in onodes if l > 0]
  89. # initialize betweenness
  90. between = {}.fromkeys(length, 1.0)
  91. while onodes:
  92. v = onodes.pop()
  93. if v in pred:
  94. num_paths = len(pred[v]) # Discount betweenness if more than
  95. for x in pred[v]: # one shortest path.
  96. if x == source: # stop if hit source because all remaining v
  97. break # also have pred[v]==[source]
  98. between[x] += between[v] / num_paths
  99. # remove source
  100. for v in between:
  101. between[v] -= 1
  102. # rescale to be between 0 and 1
  103. if normalized:
  104. l = len(between)
  105. if l > 2:
  106. # scale by 1/the number of possible paths
  107. scale = 1 / ((l - 1) * (l - 2))
  108. for v in between:
  109. between[v] *= scale
  110. return between
  111. load_centrality = newman_betweenness_centrality
  112. def edge_load_centrality(G, cutoff=False):
  113. """Compute edge load.
  114. WARNING: This concept of edge load has not been analysed
  115. or discussed outside of NetworkX that we know of.
  116. It is based loosely on load_centrality in the sense that
  117. it counts the number of shortest paths which cross each edge.
  118. This function is for demonstration and testing purposes.
  119. Parameters
  120. ----------
  121. G : graph
  122. A networkx graph
  123. cutoff : bool, optional (default=False)
  124. If specified, only consider paths of length <= cutoff.
  125. Returns
  126. -------
  127. A dict keyed by edge 2-tuple to the number of shortest paths
  128. which use that edge. Where more than one path is shortest
  129. the count is divided equally among paths.
  130. """
  131. betweenness = {}
  132. for u, v in G.edges():
  133. betweenness[(u, v)] = 0.0
  134. betweenness[(v, u)] = 0.0
  135. for source in G:
  136. ubetween = _edge_betweenness(G, source, cutoff=cutoff)
  137. for e, ubetweenv in ubetween.items():
  138. betweenness[e] += ubetweenv # cumulative total
  139. return betweenness
  140. def _edge_betweenness(G, source, nodes=None, cutoff=False):
  141. """Edge betweenness helper."""
  142. # get the predecessor data
  143. (pred, length) = nx.predecessor(G, source, cutoff=cutoff, return_seen=True)
  144. # order the nodes by path length
  145. onodes = [n for n, d in sorted(length.items(), key=itemgetter(1))]
  146. # initialize betweenness, doesn't account for any edge weights
  147. between = {}
  148. for u, v in G.edges(nodes):
  149. between[(u, v)] = 1.0
  150. between[(v, u)] = 1.0
  151. while onodes: # work through all paths
  152. v = onodes.pop()
  153. if v in pred:
  154. # Discount betweenness if more than one shortest path.
  155. num_paths = len(pred[v])
  156. for w in pred[v]:
  157. if w in pred:
  158. # Discount betweenness, mult path
  159. num_paths = len(pred[w])
  160. for x in pred[w]:
  161. between[(w, x)] += between[(v, w)] / num_paths
  162. between[(x, w)] += between[(w, v)] / num_paths
  163. return between