Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/linalg/spectrum.py: 50%
24 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-20 07:00 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-20 07:00 +0000
1"""
2Eigenvalue spectrum of graphs.
3"""
4import networkx as nx
6__all__ = [
7 "laplacian_spectrum",
8 "adjacency_spectrum",
9 "modularity_spectrum",
10 "normalized_laplacian_spectrum",
11 "bethe_hessian_spectrum",
12]
15@nx._dispatch(edge_attrs="weight")
16def laplacian_spectrum(G, weight="weight"):
17 """Returns eigenvalues of the Laplacian of G
19 Parameters
20 ----------
21 G : graph
22 A NetworkX graph
24 weight : string or None, optional (default='weight')
25 The edge data key used to compute each value in the matrix.
26 If None, then each edge has weight 1.
28 Returns
29 -------
30 evals : NumPy array
31 Eigenvalues
33 Notes
34 -----
35 For MultiGraph/MultiDiGraph, the edges weights are summed.
36 See :func:`~networkx.convert_matrix.to_numpy_array` for other options.
38 See Also
39 --------
40 laplacian_matrix
42 Examples
43 --------
44 The multiplicity of 0 as an eigenvalue of the laplacian matrix is equal
45 to the number of connected components of G.
47 >>> G = nx.Graph() # Create a graph with 5 nodes and 3 connected components
48 >>> G.add_nodes_from(range(5))
49 >>> G.add_edges_from([(0, 2), (3, 4)])
50 >>> nx.laplacian_spectrum(G)
51 array([0., 0., 0., 2., 2.])
53 """
54 import scipy as sp
56 return sp.linalg.eigvalsh(nx.laplacian_matrix(G, weight=weight).todense())
59@nx._dispatch(edge_attrs="weight")
60def normalized_laplacian_spectrum(G, weight="weight"):
61 """Return eigenvalues of the normalized Laplacian of G
63 Parameters
64 ----------
65 G : graph
66 A NetworkX graph
68 weight : string or None, optional (default='weight')
69 The edge data key used to compute each value in the matrix.
70 If None, then each edge has weight 1.
72 Returns
73 -------
74 evals : NumPy array
75 Eigenvalues
77 Notes
78 -----
79 For MultiGraph/MultiDiGraph, the edges weights are summed.
80 See to_numpy_array for other options.
82 See Also
83 --------
84 normalized_laplacian_matrix
85 """
86 import scipy as sp
88 return sp.linalg.eigvalsh(
89 nx.normalized_laplacian_matrix(G, weight=weight).todense()
90 )
93@nx._dispatch(edge_attrs="weight")
94def adjacency_spectrum(G, weight="weight"):
95 """Returns eigenvalues of the adjacency matrix of G.
97 Parameters
98 ----------
99 G : graph
100 A NetworkX graph
102 weight : string or None, optional (default='weight')
103 The edge data key used to compute each value in the matrix.
104 If None, then each edge has weight 1.
106 Returns
107 -------
108 evals : NumPy array
109 Eigenvalues
111 Notes
112 -----
113 For MultiGraph/MultiDiGraph, the edges weights are summed.
114 See to_numpy_array for other options.
116 See Also
117 --------
118 adjacency_matrix
119 """
120 import scipy as sp
122 return sp.linalg.eigvals(nx.adjacency_matrix(G, weight=weight).todense())
125@nx._dispatch
126def modularity_spectrum(G):
127 """Returns eigenvalues of the modularity matrix of G.
129 Parameters
130 ----------
131 G : Graph
132 A NetworkX Graph or DiGraph
134 Returns
135 -------
136 evals : NumPy array
137 Eigenvalues
139 See Also
140 --------
141 modularity_matrix
143 References
144 ----------
145 .. [1] M. E. J. Newman, "Modularity and community structure in networks",
146 Proc. Natl. Acad. Sci. USA, vol. 103, pp. 8577-8582, 2006.
147 """
148 import scipy as sp
150 if G.is_directed():
151 return sp.linalg.eigvals(nx.directed_modularity_matrix(G))
152 else:
153 return sp.linalg.eigvals(nx.modularity_matrix(G))
156@nx._dispatch
157def bethe_hessian_spectrum(G, r=None):
158 """Returns eigenvalues of the Bethe Hessian matrix of G.
160 Parameters
161 ----------
162 G : Graph
163 A NetworkX Graph or DiGraph
165 r : float
166 Regularizer parameter
168 Returns
169 -------
170 evals : NumPy array
171 Eigenvalues
173 See Also
174 --------
175 bethe_hessian_matrix
177 References
178 ----------
179 .. [1] A. Saade, F. Krzakala and L. Zdeborová
180 "Spectral clustering of graphs with the bethe hessian",
181 Advances in Neural Information Processing Systems. 2014.
182 """
183 import scipy as sp
185 return sp.linalg.eigvalsh(nx.bethe_hessian_matrix(G, r).todense())