0

我打算尝试使用此代码来进行关键路径分析。运行此代码时,我收到以下错误,但我不知道这意味着什么(因为我现在不知道代码是如何工作的)。

回溯(最近一次通话最后):文件“/Users/PeterVanvoorden/Desktop/test.py”,第 22 行,在 G.add_edge('A','B',1) 文件“/Library/Python/2.7/site -packages/python_graph_core-1.8.2-py2.7.egg/pygraph/classes/digraph.py",第 161 行,在 add_edge u 中,v = edge ValueError:需要超过 1 个值才能解包

# Copyright (c) 2007-2008 Pedro Matiello <pmatiello@gmail.com>
# License: MIT (see COPYING file)


import sys
sys.path.append('..')
import pygraph
from pygraph.classes.digraph import digraph
from pygraph.algorithms.critical import transitive_edges, critical_path

#demo of the critical path algorithm and the transitivity detection algorithm

G = digraph()

G.add_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')
G.add_node('F')

G.add_edge('A','B',1)
G.add_edge('A','C',2)
G.add_edge('B','C',10)
G.add_edge('B','D',2)
G.add_edge('B','E',8)
G.add_edge('C','D',7)
G.add_edge('C','E',3)
G.add_edge('E','D',1)
G.add_edge('D','F',3)
G.add_edge('E','F',1)
#add this edge to add a cycle
#G.add_edge('E','A',1)

print transitive_edges(G)
print critical_path(G)

我知道在不理解代码的情况下复制代码有点愚蠢,但我想我首先尝试示例代码以查看程序包是否正常工作,但显然它不工作。现在我想知道这是否只是因为示例代码中的一个小错误,或者它是否是一个更根本的问题。

4

1 回答 1

1

我查看了源代码,发现它add_edge试图将第一个位置参数解压缩为 2 元组。

如果您更改这些行:

G.add_edge('A','B',1)
G.add_edge('A','C',2)
...

到:

G.add_edge(('A', 'B'), 1)  # note the extra parens
G.add_edge(('A', 'C'), 2)
...

它应该工作。但是,我以前没有使用pygraph过,所以这可能仍然不会产生预期的结果。

于 2014-03-31T21:54:09.523 回答