Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

# Source code for networkx.linalg.spectrum

"""
Eigenvalue spectrum of graphs.
"""
#    Aric Hagberg <hagberg@lanl.gov>
#    Dan Schult <dschult@colgate.edu>
#    Pieter Swart <swart@lanl.gov>
import networkx as nx
__author__ = "\n".join(['Aric Hagberg <aric.hagberg@gmail.com>',
'Pieter Swart (swart@lanl.gov)',
'Dan Schult(dschult@colgate.edu)',
'Jean-Gabriel Young (jean.gabriel.young@gmail.com)'])

'normalized_laplacian_spectrum']

[docs]def laplacian_spectrum(G, weight='weight'):
"""Returns eigenvalues of the Laplacian of G

Parameters
----------
G : graph
A NetworkX graph

weight : string or None, optional (default='weight')
The edge data key used to compute each value in the matrix.
If None, then each edge has weight 1.

Returns
-------
evals : NumPy array
Eigenvalues

Notes
-----
For MultiGraph/MultiDiGraph, the edges weights are summed.
See to_numpy_matrix for other options.

--------
laplacian_matrix
"""
from scipy.linalg import eigvalsh
return eigvalsh(nx.laplacian_matrix(G, weight=weight).todense())

def normalized_laplacian_spectrum(G, weight='weight'):
"""Return eigenvalues of the normalized Laplacian of G

Parameters
----------
G : graph
A NetworkX graph

weight : string or None, optional (default='weight')
The edge data key used to compute each value in the matrix.
If None, then each edge has weight 1.

Returns
-------
evals : NumPy array
Eigenvalues

Notes
-----
For MultiGraph/MultiDiGraph, the edges weights are summed.
See to_numpy_matrix for other options.

--------
normalized_laplacian_matrix
"""
from scipy.linalg import eigvalsh
return eigvalsh(nx.normalized_laplacian_matrix(G, weight=weight).todense())

"""Returns eigenvalues of the adjacency matrix of G.

Parameters
----------
G : graph
A NetworkX graph

weight : string or None, optional (default='weight')
The edge data key used to compute each value in the matrix.
If None, then each edge has weight 1.

Returns
-------
evals : NumPy array
Eigenvalues

Notes
-----
For MultiGraph/MultiDiGraph, the edges weights are summed.
See to_numpy_matrix for other options.

--------
"""
from scipy.linalg import eigvals

[docs]def modularity_spectrum(G):
"""Returns eigenvalues of the modularity matrix of G.

Parameters
----------
G : Graph
A NetworkX Graph or DiGraph

Returns
-------
evals : NumPy array
Eigenvalues

--------
modularity_matrix

References
----------
.. [1] M. E. J. Newman, "Modularity and community structure in networks",
Proc. Natl. Acad. Sci. USA, vol. 103, pp. 8577-8582, 2006.
"""
from scipy.linalg import eigvals
if G.is_directed():
return eigvals(nx.directed_modularity_matrix(G))
else:
return eigvals(nx.modularity_matrix(G))

# fixture for nose tests

def setup_module(module):
from nose import SkipTest
try:
import scipy.linalg
except:
raise SkipTest("scipy.linalg not available")