Open In App

Why does Dijkstra’s Algorithm fail on negative weights?

Dijkstra’s Algorithm: It is a graph searching algorithm that uses a Greedy Approach to find the shortest path from the source node to all other remaining nodes. It solves the single-source shortest path problem for a weighted graph. This algorithm keeps track of the weights of the edges for finding the path that minimizes the total distance.

Time Complexity: O(V + E*log(V)), when priority queue is used (where V are the nodes and E are the edges)



Limitations of Dijkstra’s Algorithm: For this algorithm to function properly:

Advantages of Dijkstra’s Algorithm:



Disadvantages of Dijkstra’s Algorithm:

Why does Dijkstra’s Algorithm fail on negative weights?

Let’s take a simple example for a better understanding of why Dijkstra’s Algorithm fails for negative weights.

Consider acyclic directed graph with nodes A, B, and C which is connected by edges having weights that represent the cost to use this edge. The following are the weights as mentioned in the above diagram:

A –>B = 5, A –>C = 6, C –>B = -3. Here one weight C -> B is negative.

Conclusion:

Article Tags :