Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Dijkstra's algorithm work with negative edges if there is no "processed" check?

Typically, in Dijkstra's algorithm, for each encountered node, we check whether that node was processed before attempting to update the distances of its neighbors and adding them to the queue. This method is under the assumption that if a distance to a node is set once then the distance to that node cannot improve for the rest of the algorithm, and so if the node was processed once already, then the distances to its neighbors cannot improve. However, this is not true for graphs with negative edges.

If there are no negatives cycles then if we remove that "processed" check, then will the algorithm always work for graphs with negative edges?

Edit: an example of a graph where the algorithm would fail would be nice

Edit 2: Java code https://pastebin.com/LSnfzBW4

Example usage:

3 3 1 <-- 3 nodes, 3 edges, starting point at node 1
1 2 5 <-- edge of node 1 and node 2 with a weight of 5 (unidirectional) 
2 3 -20 <-- more edges
1 3 2
like image 200
scdivad Avatar asked Mar 17 '20 18:03

scdivad


People also ask

Does Dijkstra work for negative weights without cycles?

4.3. Dijkstra's algorithm solves the shortest-path problem for any weighted, directed graph with non-negative weights. It can handle graphs consisting of cycles, but negative weights will cause this algorithm to produce incorrect results.

Can we use Dijkstra algorithm for negative edges?

You can use dijkstra's algorithm with negative edges not including negative cycle, but you must allow a vertex can be visited multiple times and that version will lose it's fast time complexity.

Why does Dijkstra's algorithm not work with negative edges?

It happens because, in each iteration, the algorithm only updates the answer for the nodes in the queue. So, Dijkstra's algorithm does not reconsider a node once it marks it as visited even if a shorter path exists than the previous one. Hence, Dijkstra's algorithm fails in graphs with negative edge weights.

In which scenario Dijkstra's algorithm fails to provide accurate results?

So this algorithm fails to find the minimum distance in case of negative weights, so as an alternative Bellman-Ford algorithm is used to find the shortest distance in case of negative weights, as it stops the loop when it encounters a negative cycle.


1 Answers

The algorithm will produce the correct answer, but since nodes can now be visited multiple times the time complexity will be exponential.

Here's an example demonstrating the exponential complexity:

w(1, 3) = 4
w(1, 2) = 100
w(2, 3) = -100
w(3, 5) = 2
w(3, 4) = 50
w(4, 5) = -50
w(5, 7) = 1
w(5, 6) = 25
w(6, 7) = -25

If the algorithm is trying to find the shortest path from node 1 to node 7, it will first reach node 3 via the edge with weight 4 and then explore the rest of the graph. Then, it will find a shorter path to node 3 by going to node 2 first, and then it will explore the rest of the graph again.

Every time the algorithm reaches one of the odd indexed nodes, it will first go to the next odd indexed node via the direct edge and explore the rest of the graph. Then it will find a shorter path to the next odd indexed node via the even indexed node and explore the rest of the graph again. This means that every time one of the odd indexed nodes is reached, the rest of the graph will be explored twice, leading to a complexity of at least O(2^(|V|/2)).

like image 196
eesiraed Avatar answered Oct 05 '22 23:10

eesiraed