Negative weights using Dijkstra's Algorithm

来源:互联网 发布:luxsens 人工智能 编辑:程序博客网 时间:2024/06/03 17:43

expand过的才不expand!!! 定了T的就一定是expand过的,不用更新它,因为它已经最短了


The algorithm you have suggested will indeed find the shortest path in this graph, but not all graphs in general. For example, consider this graph:

Figure of graph

Assume the edges are directed from left to right as in your example,

Your algorithm will work as follows. First, you set d(A) to zero and the other distances to infinity. You then expand out node A, setting d(B) to 1, d(C) to zero, and d(D) to 99. Next, you expand out C, with no net changes. You then expand out B, which has no effect. Finally, you expand D, which changes d(B) to -201.

Notice that at the end of this, though, that d(C) is still 0, even though the shortest path to C has length -200. Your algorithm thus fails to accurately compute distances in some cases. Moreover, even if you were to store back pointers saying how to get from each node to the start node A, you'd end taking the wrong path back from C to A.

原创粉丝点击