spectrum.py 4.2 KB

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