Dijkstra’s shortest path algorithm using set in STL
Given a graph and a source vertex in graph, find shortest paths from source to all vertices in the given graph.
Input : Source = 0 Output : Vertex Distance from Source 0 0 1 4 2 12 3 19 4 21 5 11 6 9 7 8 8 14
We have discussed Dijkstra’s shortest Path implementations.
- Dijkstra’s Algorithm for Adjacency Matrix Representation (In C/C++ with time complexity O(v2)
- Dijkstra’s Algorithm for Adjacency List Representation (In C with Time Complexity O(ELogV))
The second implementation is time complexity wise better, but is really complex as we have implemented our own priority queue. STL provides priority_queue, but the provided priority queue doesn’t support decrease key and delete operations. And in Dijkstra’s algorithm, we need a priority queue and below operations on priority queue :
- ExtractMin : from all those vertices whose shortest distance is not yet found, we need to get vertex with minimum distance.
- DecreaseKey : After extracting vertex we need to update distance of its adjacent vertices, and if new distance is smaller, then update that in data structure.
Above operations can be easily implemented by set data structure of c++ STL, set keeps all its keys in sorted order so minimum distant vertex will always be at beginning, we can extract it from there, which is the ExtractMin operation and update other adjacent vertex accordingly if any vetex’s distance becomes smaller then delete its previous entry and insert new updated entry which is DecreaseKey operation.
Below is algorithm based on set data structure.
1) Initialize distances of all vertices as infinite. 2) Create an empty set. Every item of set is a pair (weight, vertex). Weight (or distance) is used used as first item of pair as first item is by default used to compare two pairs. 3) Insert source vertex into the set and make its distance as 0. 4) While Set doesn't become empty, do following a) Extract minimum distance vertex from Set. Let the extracted vertex be u. b) Loop through all adjacent of u and do following for every vertex v. // If there is a shorter path to v // through u. If dist[v] > dist[u] + weight(u, v) (i) Update distance of v, i.e., do dist[v] = dist[u] + weight(u, v) (i) If v is in set, update its distance in set by removing it first, then inserting with new distance (ii) If v is not in set, then insert it in set with new distance 5) Print distance array dist to print all shortest paths.
Below is C++ implementation of above idea.
Vertex Distance from Source 0 0 1 4 2 12 3 19 4 21 5 11 6 9 7 8 8 14
Time Complexity: Set in C++ are typically implemented using Self-balancing binary search trees. Therefore, time complexity of set operations like insert, delete is logarithmic and time complexity of above solution is O(ELogV)).
Dijkstra’s Shortest Path Algorithm using priority_queue of STL
This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.