Skip to main content
 首页 » 编程设计

python之为什么由 Networkx 和关联矩阵计算的拉普拉斯算子不同

2024年10月25日15lexus

import networkx as bx 
import numpy as np 
G1 = nx.erdos_renyi_graph(20, .3) 
L1 = nx.linalg.laplacian_matrix(G1) 
A1=nx.incidence_matrix(G1) 
L1_inc = A1*np.transpose(A1) 
L1_inc == L1 

但答案并非对所有元素都为真。由于拉普拉斯算子没有方向性,有什么问题?

如果您需要更多信息,请告诉我。

请您参考如下方法:

函数 nx.incidence_matrix() 默认给出一个无向关联矩阵。您可以传递 oriented=True 以返回定向版本。 例如:

In [1]: import networkx as nx 
 
In [2]: G = nx.path_graph(4) 
 
In [3]: I = nx.incidence_matrix(G,oriented=True) 
 
In [4]: I.todense() 
Out[4]:  
matrix([[-1.,  0.,  0.], 
        [ 1., -1.,  0.], 
        [ 0.,  1., -1.], 
        [ 0.,  0.,  1.]]) 
 
In [5]: L = nx.laplacian_matrix(G) 
 
In [6]: L.todense() 
Out[6]:  
matrix([[ 1, -1,  0,  0], 
        [-1,  2, -1,  0], 
        [ 0, -1,  2, -1], 
        [ 0,  0, -1,  1]]) 
 
In [7]: (I*I.T).todense() 
Out[7]:  
matrix([[ 1., -1.,  0.,  0.], 
        [-1.,  2., -1.,  0.], 
        [ 0., -1.,  2., -1.], 
        [ 0.,  0., -1.,  1.]])