centrality.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import networkx as nx
  2. __all__ = ["degree_centrality", "betweenness_centrality", "closeness_centrality"]
  3. def degree_centrality(G, nodes):
  4. r"""Compute the degree centrality for nodes in a bipartite network.
  5. The degree centrality for a node `v` is the fraction of nodes
  6. connected to it.
  7. Parameters
  8. ----------
  9. G : graph
  10. A bipartite network
  11. nodes : list or container
  12. Container with all nodes in one bipartite node set.
  13. Returns
  14. -------
  15. centrality : dictionary
  16. Dictionary keyed by node with bipartite degree centrality as the value.
  17. See Also
  18. --------
  19. betweenness_centrality
  20. closeness_centrality
  21. :func:`~networkx.algorithms.bipartite.basic.sets`
  22. :func:`~networkx.algorithms.bipartite.basic.is_bipartite`
  23. Notes
  24. -----
  25. The nodes input parameter must contain all nodes in one bipartite node set,
  26. but the dictionary returned contains all nodes from both bipartite node
  27. sets. See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
  28. for further details on how bipartite graphs are handled in NetworkX.
  29. For unipartite networks, the degree centrality values are
  30. normalized by dividing by the maximum possible degree (which is
  31. `n-1` where `n` is the number of nodes in G).
  32. In the bipartite case, the maximum possible degree of a node in a
  33. bipartite node set is the number of nodes in the opposite node set
  34. [1]_. The degree centrality for a node `v` in the bipartite
  35. sets `U` with `n` nodes and `V` with `m` nodes is
  36. .. math::
  37. d_{v} = \frac{deg(v)}{m}, \mbox{for} v \in U ,
  38. d_{v} = \frac{deg(v)}{n}, \mbox{for} v \in V ,
  39. where `deg(v)` is the degree of node `v`.
  40. References
  41. ----------
  42. .. [1] Borgatti, S.P. and Halgin, D. In press. "Analyzing Affiliation
  43. Networks". In Carrington, P. and Scott, J. (eds) The Sage Handbook
  44. of Social Network Analysis. Sage Publications.
  45. https://dx.doi.org/10.4135/9781446294413.n28
  46. """
  47. top = set(nodes)
  48. bottom = set(G) - top
  49. s = 1.0 / len(bottom)
  50. centrality = {n: d * s for n, d in G.degree(top)}
  51. s = 1.0 / len(top)
  52. centrality.update({n: d * s for n, d in G.degree(bottom)})
  53. return centrality
  54. def betweenness_centrality(G, nodes):
  55. r"""Compute betweenness centrality for nodes in a bipartite network.
  56. Betweenness centrality of a node `v` is the sum of the
  57. fraction of all-pairs shortest paths that pass through `v`.
  58. Values of betweenness are normalized by the maximum possible
  59. value which for bipartite graphs is limited by the relative size
  60. of the two node sets [1]_.
  61. Let `n` be the number of nodes in the node set `U` and
  62. `m` be the number of nodes in the node set `V`, then
  63. nodes in `U` are normalized by dividing by
  64. .. math::
  65. \frac{1}{2} [m^2 (s + 1)^2 + m (s + 1)(2t - s - 1) - t (2s - t + 3)] ,
  66. where
  67. .. math::
  68. s = (n - 1) \div m , t = (n - 1) \mod m ,
  69. and nodes in `V` are normalized by dividing by
  70. .. math::
  71. \frac{1}{2} [n^2 (p + 1)^2 + n (p + 1)(2r - p - 1) - r (2p - r + 3)] ,
  72. where,
  73. .. math::
  74. p = (m - 1) \div n , r = (m - 1) \mod n .
  75. Parameters
  76. ----------
  77. G : graph
  78. A bipartite graph
  79. nodes : list or container
  80. Container with all nodes in one bipartite node set.
  81. Returns
  82. -------
  83. betweenness : dictionary
  84. Dictionary keyed by node with bipartite betweenness centrality
  85. as the value.
  86. See Also
  87. --------
  88. degree_centrality
  89. closeness_centrality
  90. :func:`~networkx.algorithms.bipartite.basic.sets`
  91. :func:`~networkx.algorithms.bipartite.basic.is_bipartite`
  92. Notes
  93. -----
  94. The nodes input parameter must contain all nodes in one bipartite node set,
  95. but the dictionary returned contains all nodes from both node sets.
  96. See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
  97. for further details on how bipartite graphs are handled in NetworkX.
  98. References
  99. ----------
  100. .. [1] Borgatti, S.P. and Halgin, D. In press. "Analyzing Affiliation
  101. Networks". In Carrington, P. and Scott, J. (eds) The Sage Handbook
  102. of Social Network Analysis. Sage Publications.
  103. https://dx.doi.org/10.4135/9781446294413.n28
  104. """
  105. top = set(nodes)
  106. bottom = set(G) - top
  107. n = len(top)
  108. m = len(bottom)
  109. s, t = divmod(n - 1, m)
  110. bet_max_top = (
  111. ((m**2) * ((s + 1) ** 2))
  112. + (m * (s + 1) * (2 * t - s - 1))
  113. - (t * ((2 * s) - t + 3))
  114. ) / 2.0
  115. p, r = divmod(m - 1, n)
  116. bet_max_bot = (
  117. ((n**2) * ((p + 1) ** 2))
  118. + (n * (p + 1) * (2 * r - p - 1))
  119. - (r * ((2 * p) - r + 3))
  120. ) / 2.0
  121. betweenness = nx.betweenness_centrality(G, normalized=False, weight=None)
  122. for node in top:
  123. betweenness[node] /= bet_max_top
  124. for node in bottom:
  125. betweenness[node] /= bet_max_bot
  126. return betweenness
  127. def closeness_centrality(G, nodes, normalized=True):
  128. r"""Compute the closeness centrality for nodes in a bipartite network.
  129. The closeness of a node is the distance to all other nodes in the
  130. graph or in the case that the graph is not connected to all other nodes
  131. in the connected component containing that node.
  132. Parameters
  133. ----------
  134. G : graph
  135. A bipartite network
  136. nodes : list or container
  137. Container with all nodes in one bipartite node set.
  138. normalized : bool, optional
  139. If True (default) normalize by connected component size.
  140. Returns
  141. -------
  142. closeness : dictionary
  143. Dictionary keyed by node with bipartite closeness centrality
  144. as the value.
  145. See Also
  146. --------
  147. betweenness_centrality
  148. degree_centrality
  149. :func:`~networkx.algorithms.bipartite.basic.sets`
  150. :func:`~networkx.algorithms.bipartite.basic.is_bipartite`
  151. Notes
  152. -----
  153. The nodes input parameter must contain all nodes in one bipartite node set,
  154. but the dictionary returned contains all nodes from both node sets.
  155. See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
  156. for further details on how bipartite graphs are handled in NetworkX.
  157. Closeness centrality is normalized by the minimum distance possible.
  158. In the bipartite case the minimum distance for a node in one bipartite
  159. node set is 1 from all nodes in the other node set and 2 from all
  160. other nodes in its own set [1]_. Thus the closeness centrality
  161. for node `v` in the two bipartite sets `U` with
  162. `n` nodes and `V` with `m` nodes is
  163. .. math::
  164. c_{v} = \frac{m + 2(n - 1)}{d}, \mbox{for} v \in U,
  165. c_{v} = \frac{n + 2(m - 1)}{d}, \mbox{for} v \in V,
  166. where `d` is the sum of the distances from `v` to all
  167. other nodes.
  168. Higher values of closeness indicate higher centrality.
  169. As in the unipartite case, setting normalized=True causes the
  170. values to normalized further to n-1 / size(G)-1 where n is the
  171. number of nodes in the connected part of graph containing the
  172. node. If the graph is not completely connected, this algorithm
  173. computes the closeness centrality for each connected part
  174. separately.
  175. References
  176. ----------
  177. .. [1] Borgatti, S.P. and Halgin, D. In press. "Analyzing Affiliation
  178. Networks". In Carrington, P. and Scott, J. (eds) The Sage Handbook
  179. of Social Network Analysis. Sage Publications.
  180. https://dx.doi.org/10.4135/9781446294413.n28
  181. """
  182. closeness = {}
  183. path_length = nx.single_source_shortest_path_length
  184. top = set(nodes)
  185. bottom = set(G) - top
  186. n = len(top)
  187. m = len(bottom)
  188. for node in top:
  189. sp = dict(path_length(G, node))
  190. totsp = sum(sp.values())
  191. if totsp > 0.0 and len(G) > 1:
  192. closeness[node] = (m + 2 * (n - 1)) / totsp
  193. if normalized:
  194. s = (len(sp) - 1) / (len(G) - 1)
  195. closeness[node] *= s
  196. else:
  197. closeness[node] = 0.0
  198. for node in bottom:
  199. sp = dict(path_length(G, node))
  200. totsp = sum(sp.values())
  201. if totsp > 0.0 and len(G) > 1:
  202. closeness[node] = (n + 2 * (m - 1)) / totsp
  203. if normalized:
  204. s = (len(sp) - 1) / (len(G) - 1)
  205. closeness[node] *= s
  206. else:
  207. closeness[node] = 0.0
  208. return closeness