我有一个大约200个节点的无向图。 我似乎无法找到我的错误,因为我通过/失败的测试看起来非常相似。
def dijkstra(starting_node):
starting_node.set_sp(0)
starting_node.set_path("")
heap = []
heapy.heapify(heap)
heapy.heappush(heap, starting_node)
while True:
try:
node = heapy.heappop(heap)
node.Explored = True
for (neighbour, distance) in node.neighbours:
if not neighbour.Explored:
if node.shortest_path + distance < neighbour.shortest_path:
neighbour.set_sp(node.shortest_path + distance)
neighbour.set_path(node.path)
heapy.heappush(heap, neighbour)
except IndexError:
return