当我调用此函数时,会出现以下错误,非常感谢任何想法。
错误: dist,next = Dijkstra(图,汇,来源) 第111行,在Dijkstra S,Q = list(sources [:]),range(1,n + 1) TypeError:'int'对象没有属性' getitem '
代码:
def Dijkstra(graph, sink, sources = None):
n = len (graph.nodes ())
dist = {k+1:np.inf for k in range(n)}
next = {k+1:None for k in range(n)}
dist[sink] = 0.0
S = list(sources[:])
Q = range(1,n+1)
while len(Q)>0:
min = np.inf
for v in Q:
if (dist[v] < min):
u=v
min = dist[v]
if min == np.inf: return dist, next
for s in S:
if u == s: S.remove(u)
if len(S)==0: return dist,next
Q.remove(u)
for a in graph.predecessors_iter(u):
v = a
alt = dist[u] + graph [v][u]["weight"]
if alt < dist[v]: dist[v] = alt; next[v] = u
return dist, next
def mainKSP(graph, sources, sink, K):
"""Find the K-shortest paths from sources to sink
Return value:
-------------
As: dictionary s.t. As[s]=[K-shortest paths from s to sink for s in sources]
"""
dist, next = Dijkstra(graph, sink, sources)
A0s = {s:get_path(s, sink, next) for s in sources}
return {s : YenKSP(graph, s, sink, K, A0s[s]) for s in sources}
graphnodes = [h for h in range (1, 24)]
path = mainKSP(network, graphnodes, 1, 3)
我是一名蟒蛇初学者,并且非常有能力弄清楚S列表有什么问题,但没有成功。 Dijkstra函数是从上面提供的另一个函数内部调用的。