# Why does Dijkstra's algorithm fail on a negative weighted graphs? [duplicate]

I know this is probably very basic, I just can't wrap my head around it.
We recently studied about Dijkstra's algorithm for finding the shortest path between two vertices on a weighted graph.

My professor said this algorithm will not work on a graph with negative edges, so I tried to figure out what could be wrong with shifting all the edges weights by a positive number, so that they all be positive, when the input graph has negative edges in it.

For example, let's consider the following input graph:

Now if I'll add 3 to all edges, it's obvious that the shortest path (between $$s$$ and $$t$$) has changed:
Thus this kind of operation might result in wrong output.

And this, basically, what I don't get. Why does this happen? Why is shifting the values has such a dramatic effect on the shortest path? This is totally counter-intuitive, at least for me.

• The paths might have different amounts of edges in them, and so adding constant values to all edge weights will distort paths weights differently. Look into Johnson's algorithm, that should give you a good idea about this problem. – G. Bach Jan 16 '14 at 19:05
• For those arriving here: See also cs.stackexchange.com/q/52080/755. – D.W. Jan 21 '16 at 11:22
• Notice that a graph with negative edge weights might not have a shortest walk. Consider a graph with two nodes, A and B weight(AB) = -1. There are walks from A to B of weight -1, -3, -5, ... . If all weights are positive, then the shortest walk is a path, so we don't have to worry about the difference. If we have negative weights, we have to be very careful about what we want; the Bellman-Ford and Floyd-Warshall algorithms do different things. – Max Jul 28 '16 at 22:02
• "This is totally counter-intuitive." This is another example that intuition cannot be trusted especially when you are coming to a new field. – John L. Jan 5 '19 at 18:44

Dijkstra relies on one "simple" fact: if all weights are non-negative, adding an edge can never make a path shorter. That's why picking the shortest candidate edge (local optimality) always ends up being correct (global optimality).

If that is not the case, the "frontier" of candidate edges does not send the right signals; a cheap edge might lure you down a path with positive weights while an expensive one hides a path with negative weights.

For details, I recommend you check out a correctness proof and try to do it with negative weights; observe where it breaks.

Adding a constant amount to each edge length can change the shortest path for the simple reason that it increases the length of a path with many edges by more than it increases the length of a path with only a few edges.

For a simple case, consider the graph with vertices $\{a,b,c\}$ and edges $\{ab,bc,ac\}$, where $ab$ and $bc$ have length $1$ and $ac$ has length $3$. The shortest $a$–$c$ path is $abc$ with length $2$. However, if you increase each edge's length by $2$, then the length of $abc$ increases by $4$, while the length of $ac$ increases only by $2$. So, now, $ac$ is the shortest path, with length $5$ versus $6$.

Actually , Dijkstra's algorithm fails to work for most of the negative weight edged graphs , but sometimes it works with some of the graphs with negative weighted edges too provided the graph doesn't have negative weight cycles , This is one case in which dijkstra's algorithm works fine and finds the shortest path between whatever the point u give

And adding some positive number X to all the edges just to make them positive doesn't result in correct answer. Shortest distance can be P through one path and P1 through another path containing number of edges N and N1 respectively , suppose P > P1 but P1 has more edges than P , that might actually make P less than P1 since P's distance is added to N * whatever the actual number u added to all the edges and thus results in a wrong answer!!

edit: Yes , Dijkstra also works for some of the graphs with negative weighted cycle too as long as the element that is already considered shortest is not relaxed anymore.

• There are instances of the shortest path problem with negative weight cycles where Dijkstra gives the correct answer. It requires (1) that the destination is not reachable from the cycle, and (2) that the destination is reached before any element of the cycle. – Peter Taylor Aug 30 '18 at 9:58
• @PeterTaylor Yes , edited . thank you . – jeshwanth chowdary Sep 20 '18 at 5:24