Given 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 sets, one set contains vertices included in shortest path tree, other set includes vertices not yet included in shortest path tree. At every step of the algorithm, we find a vertex which is in the other set (set of not yet included) and has a minimum distance from the source.
Below are the detailed steps used in Dijkstra’s algorithm to find the shortest path from a single source vertex to all other vertices in the given graph.
1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in shortest path tree, i.e., whose minimum distance from source is calculated and finalized. Initially, this set is empty.
2) Assign a distance value to all vertices in the input graph. Initialize all distance values as INFINITE. Assign distance value as 0 for the source vertex so that it is picked first.
3) While sptSet doesn’t include all vertices
….a) Pick a vertex u which is not there in sptSet and has minimum distance value.
….b) Include u to sptSet.
….c) Update distance value of all adjacent vertices of u. To update the distance values, iterate through all adjacent vertices. For every adjacent vertex v, if sum of distance value of u (from source) and weight of edge u-v, is less than the distance value of v, then update the distance value of v.
Vertex Distance from Source 0 0 1 4 2 12 3 19 4 21 5 11 6 9 7 8 8 14
Please refer complete article on Dijkstra’s shortest path algorithm | Greedy Algo-7 for more details!
- C / C++ Program for Dijkstra's shortest path algorithm | Greedy Algo-7
- Java Program for Dijkstra's shortest path algorithm | Greedy Algo-7
- Python Program for Dijkstra's shortest path algorithm | Greedy Algo-7
- Dijkstra's shortest path algorithm | Greedy Algo-7
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Printing Paths in Dijkstra's Shortest Path Algorithm
- Dijkstra’s shortest path algorithm using set in STL
- Dijkstra's Shortest Path Algorithm using priority_queue of STL
- Dijkstra's shortest path algorithm in Java using PriorityQueue
- Shortest path in a directed graph by Dijkstra’s algorithm
- Applications of Dijkstra's shortest path algorithm
- Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
- Some interesting shortest path questions | Set 1
- Multi Source Shortest Path in Unweighted Graph
- Dijkstra's shortest path with minimum edges
- Single source shortest path between two cities
- C# Program for Activity Selection Problem | Greedy Algo-1
- Java Program for Dijkstra's Algorithm with Path Printing
- Widest Path Problem | Practical application of Dijkstra's Algorithm
- Shortest paths from all vertices to a destination
Improved By : shubham_singh