Note
Go to the end to download the full example code.
特征值#
创建一个 G{n,m} 随机图并计算其特征值。

Largest eigenvalue: 1.5924617911775985
Smallest eigenvalue: -4.0441619592303294e-17
import matplotlib.pyplot as plt
import networkx as nx
import numpy.linalg
n = 1000 # 1000个节点
m = 5000 # 5000条边
G = nx.gnm_random_graph(n, m, seed=5040) # 用于可重复性的种子
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) # 直方图,包含100个箱子
plt.xlim(0, 2) # 特征值在0到2之间
plt.show()
Total running time of the script: (0 minutes 0.464 seconds)