python 中 sparse matrix的应用和基本操作(CSR矩阵)
https://blog.csdn.net/weixin_42067234/article/details/80247194
laplacian矩阵
法一:通过L=D-A求
(参考:http://www.ryanzhang.info/tag/聚类/)
def laplacianOfGraph(graph):
n = graph.number_of_nodes()
A = nx.to_numpy_matrix(graph)
D = np.matrix(np.zeros((n,n)))
for node in graph.degree():
D[node, node] = graph.degree()[node]
L = D-A
return L
法二:直接用nx的函数求
def laplacian_matrix(G):
L = nx.laplacian_matrix(G) #这里得到的L是CSR格式的
L_2 = L.todense() #由稀疏矩阵(sparse matrix)转成普通矩阵(dense matrix)
return L_2