import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import eigh, sqrtm, inv
Graph = nx.connected_watts_strogatz_graph(40,6,0.1)
L = nx.laplacian_matrix(Graph).toarray()
A = nx.adjacency_matrix(Graph).toarray()
#D is the diagonal matrix of node degrees.
D = L + A
L_norm = nx.normalized_laplacian_matrix(Graph).toarray()
val, vec = eigh(L_norm)
fiedler = np.matmul(inv(sqrtm(D)),vec[:,1])
color_map = []
for vertex in fiedler:
if vertex < 0:
color_map.append("red")
else:
color_map.append("white")
options = {
"font_size": 15,
"node_size": 550, "node_color": color_map, "linewidths": 1,
"edgecolors": "black", "width": 2,
"with_labels": True
}
pos = nx.spring_layout(Graph)
plt.figure(figsize=(10,10))
nx.draw(Graph, pos, **options)
plt.show()