Note
Click here to download the full example code
Eigenvalues#
Create an G{n,m} random graph and compute the eigenvalues.
<class 'networkx.utils.decorators.argmap'> compilation 66:4: FutureWarning: normalized_laplacian_matrix will return a scipy.sparse array instead of a matrix in Networkx 3.0.
Largest eigenvalue: 1.5924617911776051
Smallest eigenvalue: -5.239273865810698e-16
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, seed=5040) # Seed for reproducibility
L = nx.normalized_laplacian_matrix(G)
e = numpy.linalg.eigvals(L.toarray())
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 0.660 seconds)