spectrum.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. """
  2. Eigenvalue spectrum of graphs.
  3. """
  4. import networkx as nx
  5. __all__ = [
  6. "laplacian_spectrum",
  7. "adjacency_spectrum",
  8. "modularity_spectrum",
  9. "normalized_laplacian_spectrum",
  10. "bethe_hessian_spectrum",
  11. ]
  12. @nx._dispatchable(edge_attrs="weight")
  13. def laplacian_spectrum(G, weight="weight"):
  14. """Returns eigenvalues of the Laplacian of G
  15. Parameters
  16. ----------
  17. G : graph
  18. A NetworkX graph
  19. weight : string or None, optional (default='weight')
  20. The edge data key used to compute each value in the matrix.
  21. If None, then each edge has weight 1.
  22. Returns
  23. -------
  24. evals : NumPy array
  25. Eigenvalues
  26. Notes
  27. -----
  28. For MultiGraph/MultiDiGraph, the edges weights are summed.
  29. See :func:`~networkx.convert_matrix.to_numpy_array` for other options.
  30. See Also
  31. --------
  32. laplacian_matrix
  33. Examples
  34. --------
  35. The multiplicity of 0 as an eigenvalue of the laplacian matrix is equal
  36. to the number of connected components of G.
  37. >>> G = nx.Graph() # Create a graph with 5 nodes and 3 connected components
  38. >>> G.add_nodes_from(range(5))
  39. >>> G.add_edges_from([(0, 2), (3, 4)])
  40. >>> nx.laplacian_spectrum(G)
  41. array([0., 0., 0., 2., 2.])
  42. """
  43. import scipy as sp
  44. return sp.linalg.eigvalsh(nx.laplacian_matrix(G, weight=weight).todense())
  45. @nx._dispatchable(edge_attrs="weight")
  46. def normalized_laplacian_spectrum(G, weight="weight"):
  47. """Return eigenvalues of the normalized Laplacian of G
  48. Parameters
  49. ----------
  50. G : graph
  51. A NetworkX graph
  52. weight : string or None, optional (default='weight')
  53. The edge data key used to compute each value in the matrix.
  54. If None, then each edge has weight 1.
  55. Returns
  56. -------
  57. evals : NumPy array
  58. Eigenvalues
  59. Notes
  60. -----
  61. For MultiGraph/MultiDiGraph, the edges weights are summed.
  62. See to_numpy_array for other options.
  63. See Also
  64. --------
  65. normalized_laplacian_matrix
  66. """
  67. import scipy as sp
  68. return sp.linalg.eigvalsh(
  69. nx.normalized_laplacian_matrix(G, weight=weight).todense()
  70. )
  71. @nx._dispatchable(edge_attrs="weight")
  72. def adjacency_spectrum(G, weight="weight"):
  73. """Returns eigenvalues of the adjacency matrix of G.
  74. Parameters
  75. ----------
  76. G : graph
  77. A NetworkX graph
  78. weight : string or None, optional (default='weight')
  79. The edge data key used to compute each value in the matrix.
  80. If None, then each edge has weight 1.
  81. Returns
  82. -------
  83. evals : NumPy array
  84. Eigenvalues
  85. Notes
  86. -----
  87. For MultiGraph/MultiDiGraph, the edges weights are summed.
  88. See to_numpy_array for other options.
  89. See Also
  90. --------
  91. adjacency_matrix
  92. """
  93. import scipy as sp
  94. return sp.linalg.eigvals(nx.adjacency_matrix(G, weight=weight).todense())
  95. @nx._dispatchable
  96. def modularity_spectrum(G):
  97. """Returns eigenvalues of the modularity matrix of G.
  98. Parameters
  99. ----------
  100. G : Graph
  101. A NetworkX Graph or DiGraph
  102. Returns
  103. -------
  104. evals : NumPy array
  105. Eigenvalues
  106. See Also
  107. --------
  108. modularity_matrix
  109. References
  110. ----------
  111. .. [1] M. E. J. Newman, "Modularity and community structure in networks",
  112. Proc. Natl. Acad. Sci. USA, vol. 103, pp. 8577-8582, 2006.
  113. """
  114. import scipy as sp
  115. if G.is_directed():
  116. return sp.linalg.eigvals(nx.directed_modularity_matrix(G))
  117. else:
  118. return sp.linalg.eigvals(nx.modularity_matrix(G))
  119. @nx._dispatchable
  120. def bethe_hessian_spectrum(G, r=None):
  121. """Returns eigenvalues of the Bethe Hessian matrix of G.
  122. Parameters
  123. ----------
  124. G : Graph
  125. A NetworkX Graph or DiGraph
  126. r : float
  127. Regularizer parameter
  128. Returns
  129. -------
  130. evals : NumPy array
  131. Eigenvalues
  132. See Also
  133. --------
  134. bethe_hessian_matrix
  135. References
  136. ----------
  137. .. [1] A. Saade, F. Krzakala and L. Zdeborová
  138. "Spectral clustering of graphs with the bethe hessian",
  139. Advances in Neural Information Processing Systems. 2014.
  140. """
  141. import scipy as sp
  142. return sp.linalg.eigvalsh(nx.bethe_hessian_matrix(G, r).todense())