scipy.sparse.csgraph.

depth_first_order#

scipy.sparse.csgraph.depth_first_order(csgraph, i_start, directed=True, return_predecessors=True)#

返回从指定节点开始的深度优先排序。

请注意,深度优先排序不是唯一的。此外,对于具有循环的图,深度优先搜索生成的树也不是唯一的。

0.11.0 版本新增。

参数:
csgraph类数组或稀疏数组或矩阵

N x N 压缩稀疏图。输入 csgraph 将转换为 csr 格式以进行计算。

i_startint

起始节点的索引。

directedbool, 可选

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

return_predecessorsbool, 可选

如果为 True (默认),则返回 predecessor 数组(见下文)。

返回值:
node_arrayndarray,一维

节点的深度优先列表,从指定节点开始。node_array 的长度是从指定节点可到达的节点数。

predecessorsndarray,一维

仅当 return_predecessors 为 True 时返回。深度优先树中每个节点的前任节点的长度为 N 的列表。如果节点 i 在树中,则其父节点由 predecessors[i] 给出。如果节点 i 不在树中(以及对于父节点),则 predecessors[i] = -9999。

注释

如果存在多个有效解决方案,则输出可能因 SciPy 和 Python 版本而异。

示例

>>> from scipy.sparse import csr_array
>>> from scipy.sparse.csgraph import depth_first_order
>>> graph = [
... [0, 1, 2, 0],
... [0, 0, 0, 1],
... [2, 0, 0, 3],
... [0, 0, 0, 0]
... ]
>>> graph = csr_array(graph)
>>> print(graph)
<Compressed Sparse Row sparse array of dtype 'int64'
    with 5 stored elements and shape (4, 4)>
    Coords  Values
    (0, 1)  1
    (0, 2)  2
    (1, 3)  1
    (2, 0)  2
    (2, 3)  3
>>> depth_first_order(graph,0)
(array([0, 1, 3, 2], dtype=int32), array([-9999,     0,     0,     1], dtype=int32))