communicability_alg.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. """
  2. Communicability.
  3. """
  4. import networkx as nx
  5. from networkx.utils import not_implemented_for
  6. __all__ = ["communicability", "communicability_exp"]
  7. @not_implemented_for("directed")
  8. @not_implemented_for("multigraph")
  9. @nx._dispatchable
  10. def communicability(G):
  11. r"""Returns communicability between all pairs of nodes in G.
  12. The communicability between pairs of nodes in G is the sum of
  13. walks of different lengths starting at node u and ending at node v.
  14. Parameters
  15. ----------
  16. G: graph
  17. Returns
  18. -------
  19. comm: dictionary of dictionaries
  20. Dictionary of dictionaries keyed by nodes with communicability
  21. as the value.
  22. Raises
  23. ------
  24. NetworkXError
  25. If the graph is not undirected and simple.
  26. See Also
  27. --------
  28. communicability_exp:
  29. Communicability between all pairs of nodes in G using spectral
  30. decomposition.
  31. communicability_betweenness_centrality:
  32. Communicability betweenness centrality for each node in G.
  33. Notes
  34. -----
  35. This algorithm uses a spectral decomposition of the adjacency matrix.
  36. Let G=(V,E) be a simple undirected graph. Using the connection between
  37. the powers of the adjacency matrix and the number of walks in the graph,
  38. the communicability between nodes `u` and `v` based on the graph spectrum
  39. is [1]_
  40. .. math::
  41. C(u,v)=\sum_{j=1}^{n}\phi_{j}(u)\phi_{j}(v)e^{\lambda_{j}},
  42. where `\phi_{j}(u)` is the `u\rm{th}` element of the `j\rm{th}` orthonormal
  43. eigenvector of the adjacency matrix associated with the eigenvalue
  44. `\lambda_{j}`.
  45. References
  46. ----------
  47. .. [1] Ernesto Estrada, Naomichi Hatano,
  48. "Communicability in complex networks",
  49. Phys. Rev. E 77, 036111 (2008).
  50. https://arxiv.org/abs/0707.0756
  51. Examples
  52. --------
  53. >>> G = nx.Graph([(0, 1), (1, 2), (1, 5), (5, 4), (2, 4), (2, 3), (4, 3), (3, 6)])
  54. >>> c = nx.communicability(G)
  55. """
  56. import numpy as np
  57. nodelist = list(G) # ordering of nodes in matrix
  58. A = nx.to_numpy_array(G, nodelist)
  59. # convert to 0-1 matrix
  60. A[A != 0.0] = 1
  61. w, vec = np.linalg.eigh(A)
  62. expw = np.exp(w)
  63. mapping = dict(zip(nodelist, range(len(nodelist))))
  64. c = {}
  65. # computing communicabilities
  66. for u in G:
  67. c[u] = {}
  68. for v in G:
  69. s = 0
  70. p = mapping[u]
  71. q = mapping[v]
  72. for j in range(len(nodelist)):
  73. s += vec[:, j][p] * vec[:, j][q] * expw[j]
  74. c[u][v] = float(s)
  75. return c
  76. @not_implemented_for("directed")
  77. @not_implemented_for("multigraph")
  78. @nx._dispatchable
  79. def communicability_exp(G):
  80. r"""Returns communicability between all pairs of nodes in G.
  81. Communicability between pair of node (u,v) of node in G is the sum of
  82. walks of different lengths starting at node u and ending at node v.
  83. Parameters
  84. ----------
  85. G: graph
  86. Returns
  87. -------
  88. comm: dictionary of dictionaries
  89. Dictionary of dictionaries keyed by nodes with communicability
  90. as the value.
  91. Raises
  92. ------
  93. NetworkXError
  94. If the graph is not undirected and simple.
  95. See Also
  96. --------
  97. communicability:
  98. Communicability between pairs of nodes in G.
  99. communicability_betweenness_centrality:
  100. Communicability betweenness centrality for each node in G.
  101. Notes
  102. -----
  103. This algorithm uses matrix exponentiation of the adjacency matrix.
  104. Let G=(V,E) be a simple undirected graph. Using the connection between
  105. the powers of the adjacency matrix and the number of walks in the graph,
  106. the communicability between nodes u and v is [1]_,
  107. .. math::
  108. C(u,v) = (e^A)_{uv},
  109. where `A` is the adjacency matrix of G.
  110. References
  111. ----------
  112. .. [1] Ernesto Estrada, Naomichi Hatano,
  113. "Communicability in complex networks",
  114. Phys. Rev. E 77, 036111 (2008).
  115. https://arxiv.org/abs/0707.0756
  116. Examples
  117. --------
  118. >>> G = nx.Graph([(0, 1), (1, 2), (1, 5), (5, 4), (2, 4), (2, 3), (4, 3), (3, 6)])
  119. >>> c = nx.communicability_exp(G)
  120. """
  121. import scipy as sp
  122. nodelist = list(G) # ordering of nodes in matrix
  123. A = nx.to_numpy_array(G, nodelist)
  124. # convert to 0-1 matrix
  125. A[A != 0.0] = 1
  126. # communicability matrix
  127. expA = sp.linalg.expm(A)
  128. mapping = dict(zip(nodelist, range(len(nodelist))))
  129. c = {}
  130. for u in G:
  131. c[u] = {}
  132. for v in G:
  133. c[u][v] = float(expA[mapping[u], mapping[v]])
  134. return c