communicability_alg.py 4.4 KB

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