Warning

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

EigenvaluesΒΆ

Create an G{n,m} random graph and compute the eigenvalues.

../../_images/sphx_glr_plot_eigenvalues_001.png

Out:

('Largest eigenvalue:', 1.5995765785709728)
('Smallest eigenvalue:', -1.3770535430656926e-17)

import matplotlib.pyplot as plt
import networkx as nx
import numpy.linalg

n = 1000  # 1000 nodes
m = 5000  # 5000 edges
G = nx.gnm_random_graph(n, m)

L = nx.normalized_laplacian_matrix(G)
e = numpy.linalg.eigvals(L.A)
print("Largest eigenvalue:", max(e))
print("Smallest eigenvalue:", min(e))
plt.hist(e, bins=100)  # histogram with 100 bins
plt.xlim(0, 2)  # eigenvalues between 0 and 2
plt.show()

Total running time of the script: ( 0 minutes 40.857 seconds)

Gallery generated by Sphinx-Gallery