当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python SciPy csgraph.breadth_first_order用法及代码示例


本文简要介绍 python 语言中 scipy.sparse.csgraph.breadth_first_order 的用法。

用法:

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

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

请注意,广度优先顺序不是唯一的,但它生成的树是唯一的。

参数

csgraph 数组 或稀疏矩阵

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

i_start int

起始节点的索引。

directed 布尔型,可选

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

return_predecessors 布尔型,可选

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

返回

node_array ndarray,一维

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

predecessors ndarray,一维

仅当 return_predecessors 为 True 时才返回。广度优先树中每个节点的前驱的 length-N 列表。如果节点 i 在树中,则其父节点由前驱[i] 给出。如果节点 i 不在树中(并且对于父节点),则前驱 [i] = -9999。

注意

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

例子

>>> from scipy.sparse import csr_matrix
>>> from scipy.sparse.csgraph import breadth_first_order
>>> graph = [
... [0, 1, 2, 0],
... [0, 0, 0, 1],
... [2, 0, 0, 3],
... [0, 0, 0, 0]
... ]
>>> graph = csr_matrix(graph)
>>> print(graph)
  (0, 1)    1
  (0, 2)    2
  (1, 3)    1
  (2, 0)    2
  (2, 3)    3
>>> breadth_first_order(graph,0)
(array([0, 1, 2, 3], dtype=int32), array([-9999,     0,     0,     1], dtype=int32))

相关用法


注:本文由纯净天空筛选整理自scipy.org大神的英文原创作品 scipy.sparse.csgraph.breadth_first_order。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。