# Behaviour of Dijkstra in Case of Negative Edge Weight [duplicate]

I came across a serious doubt regarding the implementation of the Dijkstra algorithm and hence wanted to discuss.

For the given below graph

What should be the Cost to reach Node G from Source Vertex A?

I was getting 2, as vertex E is the last processed vertex and when it was being processed

$$E.d=9,(E,G)=-7$$ so $$G.d=2$$ changed.

Some implementation of Dijkstra says that once a vertex v has been relaxed, then it's estimated $$v.d$$ is never changed.

Like here

https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/

Here $$v.d$$ is changed only when sptSet[v] for that vertex is false, means that vertex was not processed before.

But CLRS says that for each vertex U that belongs to queue and results from U=EXTRACT_MIN(Q), we relax all edges leaving U.

So, If I follow the implementation of Dijsktra of what is given in CLRS, I get cost of 2 to reach node G from source Vertex A.

## marked as duplicate by Raphael♦ algorithms StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Nov 19 '18 at 18:12

The article on Dijkstra's algorithm at GeekForGeeks say "Dijkstra’s algorithm doesn’t work for graphs with negative weight edges in general."

Introduction to Algorithms by Corman et al. says "Dijkstra’s algorithm solves the single-source shortest-paths problem on a weighted, directed graph for the case in which all edge weights are nonnegative."

As explained in the linked question and answer and as OP has found, the general proof of the correctness of Dijkstra's algorithm will fail if there are negative edges, hence discussing Dijkstra on graphs with negative weight edges doesn't make sense.

I would advise you to stay away from Dijkstra’s algorithm when a graph that has negative weights appears. It might work in some cases, but it may lead you into troubles that will be avoided if you apply Bellman–Ford algorithm or Floyd–Warshall algorithm.

By the way, the shortest distance from $$A$$ to $$G$$ is 2.

• The shortest path is indeed two, but the question is - what will be the cost calculated if we run Dijkstra on it? Multiple sources say that once the cost to a path is updated, it's never changed again. However, CLRS doesn't say anything about it - it simply says that once we reach a node, we relax all the edges going out from it. So in this case, when we reach $E$, the cost till that point is two and if we use the edge $EG$, we'll get the shortest path as two. – Gokul Nov 19 '18 at 18:32
• Let us make it clear first, in general, Dijkstra's algorithm is NOT defined in the case when there is an edge with a negative weight. So a discussion on its behavior on such graphs is not considered much meaningful. – Apass.Jack Nov 19 '18 at 18:36
• Okay, if I understood it clearly - the proof of Dijkstra relies on the fact that once we have found the shortest path to a node, there exists no other path which is shorter than the one we've found. This proof will fail if there are negative edges, hence discussing Dijkstra on graphs with negative weight edges doesn't make sense? – Gokul Nov 19 '18 at 18:39
• In the case of graphs with nonnegative weights, both "once the cost to a path is updated, it's never changed again" and "once we reach a node, we relax all the edges going out from it" are right. They complement each other. There is no contradiction between them. – Apass.Jack Nov 19 '18 at 18:39
• "This proof will fail if there are negative edges, hence discussing Dijkstra on graphs with negative weight edges doesn't make sense." Exactly! – Apass.Jack Nov 19 '18 at 18:40