Skip to content

Instantly share code, notes, and snippets.

@changwu-tw
Last active August 29, 2015 14:22
Show Gist options
  • Save changwu-tw/c16708bcf2d4eba173e6 to your computer and use it in GitHub Desktop.
Save changwu-tw/c16708bcf2d4eba173e6 to your computer and use it in GitHub Desktop.
import math
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'A'), ('A', 'D')])
L = nx.laplacian_matrix(G)
e = nx.laplacian_spectrum(G)
print "Largest eigenvalue:", max(e)
print "Smallest eigenvalue:", min(e)
plt.hist(e, bins=100)
plt.xlim(0, math.ceil(max(e)))
plt.show()
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
A = np.matrix([[0,1,0,0,0,0],[1,0,1,0,0,0],[0,1,0,1,1,1],[0,0,1,0,1,0],[0,0,1,1,0,1],[0,0,1,0,1,0]])
print A
G = nx.from_numpy_matrix(A)
nx.draw(G, with_labels=True)
plt.show()
e = np.linalg.eigvals(A)
print e
print "Largest eigenvalue:", max(e)
print "Smallest eigenvalue:", min(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment