我已將this answer for Dijkstra Algorithm複製並粘貼到我的項目中。經過幾次簡單的測試後,這似乎很好Python Dijkstra算法內存錯誤?
在我的具體實現中,我需要算法返回節點列表。所以我必須修改原始代碼,以便它總是返回一個列表。更具體地說,我刪除了所有return "string"
線。由我修改後的代碼如下:
## using Dijkstra Algorithm ##
def choosePath(s, t):
net = {'0':{'1':138, '9':150},
'1':{'0':138, '2':178, '8':194},
'2':{'1':178, '3':47.5},
'3':{'2':47.5, '4':70},
'4':{'3':70, '5':70},
'5':{'4':70, '6':36},
'6':{'5':36, '7':50},
'7':{'6':50, '8':81},
'8':{'7':81, '9':138, '1':194},
'9':{'8':138, '0':150}}
# sanity check
if s == t:
return []
# create a labels dictionary
labels={}
# record whether a label was updated
order={}
# populate an initial labels dictionary
for i in net.keys():
if i == s: labels[i] = 0 # shortest distance form s to s is 0
else: labels[i] = float("inf") # initial labels are infinity
from copy import copy
drop1 = copy(labels) # used for looping
## begin algorithm
while len(drop1) > 0:
# find the key with the lowest label
minNode = min(drop1, key = drop1.get) #minNode is the node with the smallest label
# update labels for nodes that are connected to minNode
for i in net[minNode]:
if labels[i] > (labels[minNode] + net[minNode][i]):
labels[i] = labels[minNode] + net[minNode][i]
drop1[i] = labels[minNode] + net[minNode][i]
order[i] = minNode
del drop1[minNode] # once a node has been visited, it's excluded from drop1
## end algorithm
# print shortest path
temp = copy(t)
rpath = []
path = []
while 1:
rpath.append(temp)
if order.has_key(temp):
temp = order[temp]
if temp == s:
rpath.append(temp)
break
for j in range(len(rpath)-1,-1,-1):
path.append(rpath[j])
return [junctions[int(elem)] for elem in path]
然後當我運行它,我結束了以下錯誤:顯然
>>> Traceback (most recent call last):
File "C:\Users\...\simulation.py", line 162, in choosePath
rpath.append(temp)
MemoryError
,那是因爲我已經刪除了回報「字符串「行。但是,我沒有找到哪個刪除使它死亡。爲什麼這樣?
我該如何讓它重新工作,並總是返回一個列表而不是字符串,因爲我希望?
它肯定聽起來像是將節點添加到'rpath'中,直到內存不足。我不知道爲什麼會發生這種情況,但也許你可以添加代碼來捕獲異常,如果它發生並打印'rpath'和'order'字典?這應該給你一個關於發生了什麼問題的好主意。 – Blckknght