scipy.sparse.csgraph.reconstruct_path

scipy.sparse.csgraph.reconstruct_path(csgraph, predecessors, directed=True)

从图表和前置任务列表构建一棵树。

0.11.0 新版功能.

参数
csgraph类阵列或稀疏矩阵

表示从中绘制前置任务的有向图或无向图的N x N矩阵。

predecessors类似数组,一维

树的前置索引的长度为N的数组。节点i的父代的索引由前辈给出 [i] 。

directed布尔值,可选

如果为True(默认值),则对有向图进行操作:仅沿路径csgraph从点i移动到点j [i, j] 。如果为false,则对无向图进行操作:算法可以沿着csgraph从i点前进到j点。 [i, j] 或csgraph [j, i] 。

退货
cstreeCSR矩阵

从csgraph中提取的树的N×N有向压缩稀疏表示,该csgraph由前导列表编码。

示例

>>> from scipy.sparse import csr_matrix
>>> from scipy.sparse.csgraph import reconstruct_path
>>> graph = [
... [0, 1, 2, 0],
... [0, 0, 0, 1],
... [0, 0, 0, 3],
... [0, 0, 0, 0]
... ]
>>> graph = csr_matrix(graph)
>>> print(graph)
  (0, 1)    1
  (0, 2)    2
  (1, 3)    1
  (2, 3)    3
>>> pred = np.array([-9999, 0, 0, 1], dtype=np.int32)
>>> cstree = reconstruct_path(csgraph=graph, predecessors=pred, directed=False)
>>> cstree.todense()
matrix([[0., 1., 2., 0.],
        [0., 0., 0., 1.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])