0

enter image description here

What i have learnt , that dijkstra cannot work with negative edge weights . For that we have to use bellman ford .

Bellman fords works well with negative edge weights and negative cycles , which are not reachable from source otherwise, it will return a msg "Negative cycles exist".

But, this graph shown above works well with dijkstra , even though negative edge weights exist. So, how to know when to use dijkstra with negative edge weights ??

What is think , is that dijkstra can or cannot work with negative weight edges. If negative cycle exists, then it will not work. But if not exists, it can or cannot work.

Am i right ?? plz guide me for this ??

Garrick
  • 677
  • 4
  • 15
  • 34

4 Answers4

4

Dijkstra's algorithm cannot work with negative edge weights. This is because once it marks a node as "visited", it assumes the shortest path to it has been found, and can not change, an invariant easily violated in graphs with negative edges (and no negative cycles):

       A
      / \
    7/   \2
    /     \
   B------>C
      -6

Finding shortest paths with Dijkstra's algorithm starting from A will produce the wrong cost for C, 2.

The graph you posted doesn't work either: consider the shortest path starting from d to h. Dijkstra's on this graph will produce 4 for the path (d->g->h), whereas there's a cheaper path of 0 cost: d->a->b->c->h

Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
  • but , see the distance from A to E in my graph . The minimum path is -2 and after traversing the complete graph , i didn't found any distance to E with less than -2, that means that it is not violating here. In your example, surely its is violating. – Garrick Aug 06 '16 at 07:02
  • 2
    Just because one result is by chance correct, doesn't make all the other ones correct as well. I think I've demonstrated you can't rely on Dijkstra's algorithm to find shortest path if your graph contains at least one negative weight edge. – Michael Foukarakis Aug 06 '16 at 07:03
  • you are right , but if i want to find shortest path from A only , then will it work ? i understood about D , that it will not work. – Garrick Aug 06 '16 at 07:05
  • It will not work in the general case. How will you verify its output for A is correct, when I've shown you it can be wrong? Use the proper algorithm. – Michael Foukarakis Aug 06 '16 at 08:12
  • yes , i got it . thanks . wary of dijkstra for negative edge weights. – Garrick Aug 06 '16 at 08:28
0

Dijkstra cannot work with negative weight edges. There is a algotithm named Johnson, which "re-weight" all the edges in the graph and finally make all the edges be positive. But the algorithm call the bellman ford algorithm and time complexity of it is O(V2logV + VE). So the time complexity of Dijkstra + Johnson is not good. But if the graph can be processed, maybe you can use the algorithm in advance. PS: I'm sorry for my poor English.

qifeng
  • 11
  • 2
0

check the following code

    import networkx as nx
    g = nx.Graph()
    g.add_edge(1, 2, weigbt=-10)
    g.add_edge(2, 3, weight = -5)
    g.add_edge(1, 3, weight =-6)
    print(nx.single_source_dijkstra(g, 1, 3))

it doesn't matter if all of your edges are positive or negative, the Dijkstra SSSP will give you the same answer. HOWEVER, it doesn't mean that for any graph with negative edge, the Dijkstra shortest path MAY give right answer in case of negative edge but that doesn't mean it WILL give right answer.

darya life
  • 13
  • 1
  • 5
-1

You are right, Dijkstra will work for negative weights. However it won't work if sum of weights in any cycle is negative.

Jacek
  • 829
  • 12
  • 31