You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scipy 1.14.1中depth_first_order返回的前驱节点结果异常的问题咨询

Scipy 1.14.1中depth_first_order返回的前驱节点结果异常的问题咨询

我在使用Scipy 1.14.1对最小生成树进行深度优先遍历的时候,发现返回的前驱节点结果不太符合预期,有点搞不明白哪里出了问题。

对应的图结构可以通过代码里的节点连接关系体现,我也绘制了对应的示意图展示该图的拓扑结构。

以下是我使用的代码:

import numpy as np
from scipy.sparse import coo_matrix
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse.csgraph import depth_first_order

rows = np.array([0, 1, 2, 2, 4, 9, 2,  2, 10, 10, 8 ])
cols = np.array([1, 2, 3, 4, 9, 5, 6, 10, 11,  8, 7 ])

# construct undirected graph
X = coo_matrix( (12,12))
X.col = np.concatenate( (rows, cols), axis=0)
X.row = np.concatenate( (cols, rows), axis=0)
X.data = np.ones(len(X.row))

# the minimum spanning tree is the graph itself
tree = minimum_spanning_tree(X)
print(tree)

# traversing the graph
print(depth_first_order(tree, i_start=0, directed=False, return_predecessors=True))

运行代码后,得到的最小生成树(其实就是原图本身)输出如下:

Coords    Values
  (0, 1)    1.0
  (1, 2)    1.0
  (2, 3)    1.0
  (2, 4)    1.0
  (2, 6)    1.0
  (2, 10)   1.0
  (4, 9)    1.0
  (5, 9)    1.0
  (7, 8)    1.0
  (8, 10)   1.0
  (10, 11)  1.0

同时得到的深度优先遍历顺序是:
[ 0, 1, 2, 3, 4, 9, 5, 6, 10, 11, 8, 7]

前驱节点数组为:
[-9999, 0, 1, 2, 2, 9, 2, 8,10, 4, 2, 10]

我发现这里的结果有问题:按照这个前驱数组,它显示节点9的祖先是它自己?但实际上节点9的前驱应该是4才对,而且从这个位置开始,后续的结果看起来也不太连贯。

有没有大佬能帮忙解答一下这个问题?谢谢啦!

备注:内容来源于stack exchange,提问作者user11634

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 17:44:47