Dijkstra与堆,python-无法找到bug

时间:2018-03-27 08:45:12

标签: python heap dijkstra

我有一个大约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

0 个答案:

没有答案