File: plot_eigenvalues.py

package info (click to toggle)
networkx 2.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,764 kB
  • sloc: python: 78,797; makefile: 141; javascript: 120
file content (22 lines) | stat: -rw-r--r-- 505 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""
===========
Eigenvalues
===========

Create an G{n,m} random graph and compute the eigenvalues.
"""
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()