Negative weights using Dijkstra's Algorithm

I am trying to understand why Dijkstra's algorithm will not work with negative weights. Reading an example on Shortest Paths, I am trying to figure out the following scenario:

    2
A-------B
      /
3    / -2
    /
    C

From the website:

Assuming the edges are all directed from left to right, If we start with A, Dijkstra's algorithm will choose the edge (A,x) minimizing d(A,A)+length(edge), namely (A,B). It then sets d(A,B)=2 and chooses another edge (y,C) minimizing d(A,y)+d(y,C); the only choice is (A,C) and it sets d(A,C)=3. But it never finds the shortest path from A to B, via C, with total length 1.

I can not understand why using the following implementation of Dijkstra, d[B] will not be updated to 1 (When the algorithm reaches vertex C, it will run a relax on B, see that the d[B] equals to 2 , and therefore update its value to 1 ).

Dijkstra(G, w, s)  {
   Initialize-Single-Source(G, s)
   S ← Ø
   Q ← V[G]//priority queue by d[v]
   while Q ≠ Ø do
      u ← Extract-Min(Q)
      S ← S U {u}
      for each vertex v in Adj[u] do
         Relax(u, v)
}

Initialize-Single-Source(G, s) {
   for each vertex v  V(G)
      d[v] ← ∞
      π[v] ← NIL
   d[s] ← 0
}

Relax(u, v) {
   //update only if we found a strictly shortest path
   if d[v] > d[u] + w(u,v) 
      d[v] ← d[u] + w(u,v)
      π[v] ← u
      Update(Q, v)
}

Thanks,

Meir


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:

图的图

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 .


    Note, that Dijkstra works even for negative weights, if the Graph has no negative cycles, ie cycles whose summed up weight is less than zero.

    Of course one might ask, why in the example made by templatetypedef Dijkstra fails even though there are no negative cycles, infact not even cycles. That is because he is using another stop criterion, that holds the algorithm as soon as the target node is reached (or all nodes have been settled once, he did not specify that exactly). In a graph without negative weights this works fine.

    If one is using the alternative stop criterion, which stops the algorithm when the priority-queue (heap) runs empty (this stop criterion was also used in the question), then dijkstra will find the correct distance even for graphs with negative weights but without negative cycles.

    However, in this case, the asymptotic time bound of dijkstra for graphs without negative cycles is lost. This is because a previously settled node can be reinserted into the heap when a better distance is found due to negative weights. This property is called label correcting.


    you did not use S anywhere in your algorithm (besides modifying it). the idea of dijkstra is once a vertex is on S, it will not be modified ever again. in this case, once B is inside S, you will not reach it again via C.

    this fact ensures the complexity of O(E+VlogV) [otherwise, you will repeat edges more then once, and vertices more then once]

    in other words, the algorithm you posted, might not be in O(E+VlogV), as promised by dijkstra's algorithm.

    链接地址: http://www.djcxy.com/p/35184.html

    上一篇: 如何在dijkstra算法中保存最短路径

    下一篇: 使用Dijkstra算法的负权重