Last Updated : 23 Jul, 2025
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 from other shortest-path algorithms.
Dijkstra’s AlgorithmDijkstra’s algorithm is a popular algorithm for solving 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 Dutch computer scientist Edsger W. Dijkstra in 1956.
The algorithm maintains a set of visited vertices and a set of unvisited vertices. It starts at the source vertex and iteratively selects the unvisited vertex with the smallest tentative distance from the source. It then visits the neighbors of this vertex and updates their tentative distances if a shorter path is found. This process continues until the destination vertex is reached, or all reachable vertices have been visited.
Can Dijkstra's Algorithm work on both Directed and Undirected graphs?Yes, Dijkstra's algorithm can work on both directed graphs and undirected graphs as this algorithm is designed to work on any type of graph as long as it meets the requirements of having non-negative edge weights and being connected.
Let's see how Dijkstra's Algorithm works with an example given below:
Dijkstra's Algorithm will generate the shortest path from Node 0 to all other Nodes in the graph.
Consider the below graph:
Dijkstra's AlgorithmThe algorithm will generate the shortest path from node 0 to all the other nodes in the graph.
For this graph, we will assume that the weight of the edges represents the distance between two nodes.
Initially we have:
- The Distance from the source node to itself is 0. In this example the source node is 0.
- The distance from the source node to all other node is unknown so we mark all of them as infinity.
Example: 0 -> 0, 1-> ∞,2-> ∞,3-> ∞,4-> ∞,5-> ∞,6-> ∞.
- we'll also have an array of unvisited elements that will keep track of unvisited or unmarked Nodes.
- Algorithm will complete when all the nodes marked as visited and the distance between them added to the path. Unvisited Nodes:- 0 1 2 3 4 5 6.
Step 1: Start from Node 0 and mark Node as visited as you can check in below image visited Node is marked red.
Dijkstra's AlgorithmStep 2: Check for adjacent Nodes, Now we have to choices (Either choose Node1 with distance 2 or either choose Node 2 with distance 6 ) and choose Node with minimum distance. In this step Node 1 is Minimum distance adjacent Node, so marked it as visited and add up the distance.
Distance: Node 0 -> Node 1 = 2
Dijkstra's AlgorithmStep 3: Then Move Forward and check for adjacent Node which is Node 3, so marked it as visited and add up the distance, Now the distance will be:
Distance: Node 0 -> Node 1 -> Node 3 = 2 + 5 = 7
Dijkstra's AlgorithmStep 4: Again we have two choices for adjacent Nodes (Either we can choose Node 4 with distance 10 or either we can choose Node 5 with distance 15) so choose Node with minimum distance. In this step Node 4 is Minimum distance adjacent Node, so marked it as visited and add up the distance.
Distance: Node 0 -> Node 1 -> Node 3 -> Node 4 = 2 + 5 + 10 = 17
Dijkstra's AlgorithmStep 5: Again, Move Forward and check for adjacent Node which is Node 6, so marked it as visited and add up the distance, Now the distance will be:
Distance: Node 0 -> Node 1 -> Node 3 -> Node 4 -> Node 6 = 2 + 5 + 10 + 2 = 19
Dijkstra's AlgorithmSo, the Shortest Distance from the Source Vertex is 19 which is optimal one
For an implementation of Dijkstra's algorithm, refer to this article.
Why Dijkstra's Algorithms fails for the Graphs having Negative Edges ?The problem with negative weights arises from the fact that Dijkstra's algorithm assumes that once a node is added to the set of visited nodes, its distance is finalized and will not change. However, in the presence of negative weights, this assumption can lead to incorrect results.
Consider the following graph for the example:
In the above graph, A is the source node, among the edges A to B and A to C , A to B is the smaller weight and Dijkstra assigns the shortest distance of B as 2, but because of existence of a negative edge from C to B , the actual shortest distance reduces to 1 which Dijkstra fails to detect.
Note: We use Bellman Ford's Shortest path algorithm in case we have negative edges in the graph.
Dijkstra's Algorithms vs Bellman-Ford AlgorithmDijkstra's algorithm and Bellman-Ford algorithm are both used to find the shortest path in a weighted graph, but they have some key differences. Here are the main differences between Dijkstra's algorithm and Bellman-Ford algorithm:
Feature: Dijkstra's Bellman Ford Optimization optimized for finding the shortest path between a single source node and all other nodes in a graph with non-negative edge weights Bellman-Ford algorithm is optimized for finding the shortest path between a single source node and all other nodes in a graph with negative edge weights. Relaxation Dijkstra's algorithm uses a greedy approach where it chooses the node with the smallest distance and updates its neighbors the Bellman-Ford algorithm relaxes all edges in each iteration, updating the distance of each node by considering all possible paths to that node Time Complexity Dijkstra's algorithm has a time complexity of O(V^2) for a dense graph and O(E log V) for a sparse graph, where V is the number of vertices and E is the number of edges in the graph. Bellman-Ford algorithm has a time complexity of O(VE), where V is the number of vertices and E is the number of edges in the graph. Negative Weights Dijkstra's algorithm does not work with graphs that have negative edge weights, as it assumes that all edge weights are non-negative. Bellman-Ford algorithm can handle negative edge weights and can detect negative-weight cycles in the graph. Dijkstra's Algorithm vs Floyd-Warshall AlgorithmDijkstra's algorithm and Floyd-Warshall algorithm are both used to find the shortest path in a weighted graph, but they have some key differences. Here are the main differences between Dijkstra's algorithm and Floyd-Warshall algorithm:
Feature: Dijkstra's Floyd-Warshall Algorithm Optimization Optimized for finding the shortest path between a single source node and all other nodes in a graph with non-negative edge weights Floyd-Warshall algorithm is optimized for finding the shortest path between all pairs of nodes in a graph. Technique Dijkstra's algorithm is a single-source shortest path algorithm that uses a greedy approach and calculates the shortest path from the source node to all other nodes in the graph. Floyd-Warshall algorithm, on the other hand, is an all-pairs shortest path algorithm that uses dynamic programming to calculate the shortest path between all pairs of nodes in the graph. Time Complexity Dijkstra's algorithm has a time complexity of O(V^2) for a dense graph and O(E log V) for a sparse graph, where V is the number of vertices and E is the number of edges in the graph. Floyd-Warshall algorithm, on the other hand, is an all-pairs shortest path algorithm that uses dynamic programming to calculate the shortest path between all pairs of nodes in the graph. Negative Weights Dijkstra's algorithm does not work with graphs that have negative edge weights, as it assumes that all edge weights are non-negative. Floyd-Warshall algorithm, on the other hand, is an all-pairs shortest path algorithm that uses dynamic programming to calculate the shortest path between all pairs of nodes in the graph. Dijkstra's Algorithm vs A* AlgorithmDijkstra's algorithm and A* algorithm are both used to find the shortest path in a weighted graph, but they have some key differences. Here are the main differences between Dijkstra's algorithm and A* algorithm:
Feature: A* Algorithm Search Technique Optimized for finding the shortest path between a single source node and all other nodes in a graph with non-negative edge weights A* algorithm is an informed search algorithm that uses a heuristic function to guide the search towards the goal node. Heuristic Function Dijkstra's algorithm, does not use any heuristic function and considers all the nodes in the graph. A* algorithm uses a heuristic function that estimates the distance from the current node to the goal node. This heuristic function is admissible, meaning that it never overestimates the actual distance to the goal node Time Complexity Dijkstra's algorithm has a time complexity of O(V^2) for a dense graph and O(E log V) for a sparse graph, where V is the number of vertices and E is the number of edges in the graph. The time complexity of A* algorithm depends on the quality of the heuristic function. Application Dijkstra's algorithm is used in many applications such as routing algorithms, GPS navigation systems, and network analysis . A* algorithm is commonly used in pathfinding and graph traversal problems, such as video games, robotics, and planning algorithms. Practice Problems on Dijkstra's Algorithm:Overall, Dijkstra's Algorithm is a simple and efficient way to find the shortest path in a graph with non-negative edge weights. However, it may not work well with graphs that have negative edge weights or cycles. In such cases, more advanced algorithms such as the Bellman-Ford algorithm or the Floyd-Warshall algorithm may be used.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4