laplacian_matrix#

laplacian_matrix(G, nodelist=None, weight='weight')[源码]#

返回 G 的拉普拉斯矩阵。

图的拉普拉斯矩阵定义为 L = D - A,其中 A 是邻接矩阵,D 是节点度的对角矩阵。

参数:
G

一个 NetworkX 图

nodelistlist,可选

行和列按照 nodelist 中的节点顺序排列。如果 nodelist 为 None,则顺序由 G.nodes() 生成。

weight字符串或 None,可选 (默认为 ‘weight’)

用于计算矩阵中每个值的边数据键。如果为 None,则每条边的权重为 1。

返回:
LSciPy 稀疏数组

G 的拉普拉斯矩阵。

注意

对于 MultiGraph,边的权重会相加。

这会返回一个未归一化的矩阵。要获取归一化输出,请使用 normalized_laplacian_matrixdirected_laplacian_matrixdirected_combinatorial_laplacian_matrix

该计算使用图 G 的出度。若要使用入度进行计算,请使用 G.reverse(copy=False) 并进行转置。

参考文献

[1]

Langville, Amy N., and Carl D. Meyer. Google’s PageRank and Beyond: The Science of Search Engine Rankings. Princeton University Press, 2006.

示例

对于具有多个连通分量的图,L 与块对角矩阵通过置换相似,其中每个块是对应分量的拉普拉斯矩阵。

>>> G = nx.Graph([(1, 2), (2, 3), (4, 5)])
>>> print(nx.laplacian_matrix(G).toarray())
[[ 1 -1  0  0  0]
 [-1  2 -1  0  0]
 [ 0 -1  1  0  0]
 [ 0  0  0  1 -1]
 [ 0  0  0 -1  1]]
>>> edges = [
...     (1, 2),
...     (2, 1),
...     (2, 4),
...     (4, 3),
...     (3, 4),
... ]
>>> DiG = nx.DiGraph(edges)
>>> print(nx.laplacian_matrix(DiG).toarray())
[[ 1 -1  0  0]
 [-1  2 -1  0]
 [ 0  0  1 -1]
 [ 0  0 -1  1]]

注意,节点 4 由第三列和第三行表示。这是因为默认情况下,行/列的顺序是 G.nodes 的顺序(即节点添加的顺序——在边列表中,4 最早出现在边 (2, 4) 中,在边 (4, 3) 中的节点 3 之前)。要控制矩阵的节点顺序,请使用 nodelist 参数。

>>> print(nx.laplacian_matrix(DiG, nodelist=[1, 2, 3, 4]).toarray())
[[ 1 -1  0  0]
 [-1  2  0 -1]
 [ 0  0  1 -1]
 [ 0  0 -1  1]]

该计算使用图 G 的出度。若要使用入度进行计算,请使用 G.reverse(copy=False) 并进行转置。

>>> print(nx.laplacian_matrix(DiG.reverse(copy=False)).toarray().T)
[[ 1 -1  0  0]
 [-1  1 -1  0]
 [ 0  0  2 -1]
 [ 0  0 -1  1]]
----

其他后端实现了此函数

graphblas : 启用 OpenMP 的稀疏线性代数后端。