plot_eigenvalues.py 505 B

12345678910111213141516171819202122
  1. """
  2. ===========
  3. Eigenvalues
  4. ===========
  5. Create an G{n,m} random graph and compute the eigenvalues.
  6. """
  7. import matplotlib.pyplot as plt
  8. import networkx as nx
  9. import numpy.linalg
  10. n = 1000 # 1000 nodes
  11. m = 5000 # 5000 edges
  12. G = nx.gnm_random_graph(n, m)
  13. L = nx.normalized_laplacian_matrix(G)
  14. e = numpy.linalg.eigvals(L.A)
  15. print("Largest eigenvalue:", max(e))
  16. print("Smallest eigenvalue:", min(e))
  17. plt.hist(e, bins=100) # histogram with 100 bins
  18. plt.xlim(0, 2) # eigenvalues between 0 and 2
  19. plt.show()