Why does Dijkstra's Algorithm fail on negative weights? Last Updated : 23 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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:The graph should be weighted.The weights should be non-negative.Advantages of Dijkstra's Algorithm:It has a linear time complexity so it can be easily used for large problems.It is useful in finding the shortest distance, so it is also used in google maps and calculating traffic.It has its use in areas such as telephone networks and geographical maps.Disadvantages of Dijkstra's Algorithm:It is unable to handle negative weights.It follows a kind of blind approach so there is a waste of time.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.Consider node A as the source node and the task is to find the shortest distance from source node A to all the other nodes present in the graph i.e., nodes B and C.So, first mark the distance as 0, at node A (as the distance from A to A is 0), and then mark this node as visited meaning that it has been included in the shortest path.Since in the beginning, the distance of the source node to all other nodes is not known so initialize it as infinity. Update this distance if any distance shorter than infinity is found (which is basically the greedy approach)Then, update the distance from source node A to B with the weight of the edge that connects it with A which is 5 (because 5 < infinity).In a similar way, also update the distance from A to C which was previously infinity to 6 (as 6 < infinity).Now check for the shortest distance from the source node A and as 5 is the least distance from A to B, so mark node B as 'visited'.Similarly, the next shortest is 6 so mark node C also as visited. At this point, all three nodes of the graph are visited.Now the most important step arises here, as it can be seen that by following this algorithm, the shortest distance from A --> B is 5 but if traveled the distance via node C that is the path A --> C --> B the distance will be as 3 (as A-->C = 6 and C-->B = -3 ), so 6 + (-3) = 3. As 3 is less than 5, but Dijkstra's algorithm gives the incorrect answer as 5, which is not the shortest distance. Therefore Dijkstra's Algorithm fails for negative cases.Conclusion:Since Dijkstra follows a Greedy Approach, once a node is marked as visited it cannot be reconsidered even if there is another path with less cost or distance. This issue arises only if there exists a negative weight or edge in the graph.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.If we remove back edges (example CB edge in the above graph) from a directed graph, then we can use Dijkstra for graphs with negative edges as the cases like above would not arise. Comment More infoAdvertise with us Next Article Applications of Dijkstra's shortest path algorithm T tausifsiddiqui Follow Improve Article Tags : Graph Analysis of Algorithms Greedy DSA Dijkstra Shortest Path Algorithms-Graph Shortest Paths Quiz +3 More Practice Tags : GraphGreedyShortest Path Similar Reads What is Dijkstraâs Algorithm? | Introduction to Dijkstra's Shortest Path Algorithm In this article, we will be discussing one of the most commonly known shortest-path algorithms i.e. Dijkstra's Shortest Path Algorithm which was developed by Dutch computer scientist Edsger W. Dijkstra in 1956. Moreover, we will do a complexity analysis for this algorithm and also see how it differs 10 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Dijkstraâs Algorithm for Adjacency List Representation | Greedy Algo-8 The Dijkstra's Algorithm, we can either use the matrix representation or the adjacency list representation to represent the graph, while the time complexity of Dijkstra's Algorithm using matrix representation is O(V^2). The time complexity of Dijkstra's Algorithm using adjacency list representation 15+ min read Printing Paths in Dijkstra's Shortest Path Algorithm Given a graph and a source vertex in the graph, find the shortest paths from the source to all vertices in the given graph.We have discussed Dijkstra's Shortest Path algorithm in the below posts. Dijkstraâs shortest path for adjacency matrix representationDijkstraâs shortest path for adjacency list 15 min read 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 4 min read Applications of Dijkstra's shortest path algorithm Dijkstraâs algorithm is one of the most popular algorithms for solving many single-source shortest path problems having non-negative edge weight in the graphs i.e., it is to find the shortest distance between two vertices on a graph. It was conceived by computer scientist Edsger W. Dijkstra in 1956 4 min read Dijkstra's Algorithm in different languageC / C++ Program for Dijkstra's shortest path algorithm | Greedy Algo-7Problem Statement: Given a graph and a source vertex in the graph, find the shortest paths from the source to all vertices in the given graph. What is Dijkstra's Algorithm? Dijkstra's algorithm is very similar to Prim's algorithm for minimum spanning tree. Like Prim's MST, we generate an SPT (shorte 5 min read Java Program for Dijkstra's shortest path algorithm | Greedy Algo-7Given a graph and a source vertex in the graph, find shortest paths from source to all vertices in the given graph. Dijkstra's algorithm is very similar to Prim's algorithm for minimum spanning tree. Like Prim's MST, we generate a SPT (shortest path tree) with given source as root. We maintain two s 5 min read Dijkstra's shortest path algorithm in PythonGiven a graph and a source vertex in the graph, find the shortest paths from source to all vertices in the given graph. Dijkstraâs algorithm is a popular algorithm for solving many single-source shortest path problems having non-negative edge weight in the graphs i.e., it is to find the shortest dis 4 min read C# Program for Dijkstra's shortest path algorithm | Greedy Algo-7Given a graph and a source vertex in the graph, find shortest paths from source to all vertices in the given graph. Dijkstra's algorithm is very similar to Prim's algorithm for minimum spanning tree. Like Prim's MST, we generate a SPT (shortest path tree) with given source as root. We maintain two s 5 min read Different ways to implement Dijkstra's algorithmDijkstraâs shortest path algorithm using setGiven a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Example:Input: src = 0, V = 5, edges[][] = [[0, 1, 4], [0, 2, 8], 8 min read Dijkstra's Shortest Path Algorithm using priority_queue of STLGiven a graph and a source vertex in graph, find shortest paths from source to all vertices in the given graph.Input : Source = 0Output : Vertex Distance from Source 0 0 1 4 2 12 3 19 4 21 5 11 6 9 7 8 8 14We have discussed Dijkstraâs shortest Path implementations.Dijkstraâs Algorithm for Adjacency 15+ min read Dijkstra's shortest path algorithm in Java using PriorityQueueDijkstraâs algorithm is very similar to Primâs algorithm for minimum spanning tree. Like Primâs MST, we generate a SPT (shortest path tree) with a given source as a root. We maintain two sets, one set contains vertices included in the shortest-path tree, other set includes vertices not yet included 5 min read Variations of Dijkstra's algorithmMinimum Cost using Dijkstra by Modifying Cost of an EdgeGiven an undirected weighted graph of N nodes and M edges in the form of a tuple lets say {X, Y, Z} such that there is an edge with cost Z between X and Y. We are supposed to compute the minimum cost of traversal from node 1 to N. However, we can perform one operation before the traversal such that 15 min read Minimum cost path from source node to destination node via an intermediate nodeGiven an undirected weighted graph. The task is to find the minimum cost of the path from source node to the destination node via an intermediate node. Note: If an edge is traveled twice, only once weight is calculated as cost. Examples: Input: source = 0, destination = 2, intermediate = 3; Output: 12 min read Find Maximum Shortest Distance in Each Component of a GraphGiven an adjacency matrix graph[][] of a weighted graph consisting of N nodes and positive weights, the task for each connected component of the graph is to find the maximum among all possible shortest distances between every pair of nodes. Examples: Input: Output: 8 0 11 Explanation: There are thre 15+ min read Comparison of Dijkstraâs and FloydâWarshall algorithmsDijkstra AlgorithmDijkstraâs Algorithm is a Single-Source Shortest Path SSSP algorithm, i.e., given a source vertex it finds the shortest path from the source to all other vertices. The idea is to generate a SPT (shortest path tree) with a given source as a root and with two sets, one set contains v 4 min read Minimum Weight Cycle in a Graph Given an undirected, weighted graph with V vertices numbered from 0 to V-1, and E edges represented as a 2D array edges[][], where each element edges[i] = [u, v, w] denotes an edge between nodes u and v with weight w, and all edge weights are positive integers, your task is to find the minimum weigh 15+ min read Like