Dijkstra's Algorithm Part 1
Dijkstra’s Algorithm finds shortest paths from a src
node to all the vertices of the graph like Bellman-Ford Algorithm. However it does not work for the graphs having negative weight edges. The benefit of Dijsktra’s algorithm
is that it is more efficient and can be used for processing large graphs.
Note: The below implementation is optimal for dense graphs.
Time Complexity: O
(n
2
+ m)
where n
: number of vertices and m
: number of edges
In case of sparse graphs, the complexity can be reduced to O(n + mlogm)
using priority queue data structure of STL.
Pseudo code for Dijsktra’s Algorithm (for Dense graphs):
References: GeeksforGeeks