Given a graph and a source vertex in 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 minimum distance from source.

[ad type=”banner”]

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.
Algorithm
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 sptSetand 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.

The set sptSetis initially empty and distances assigned to vertices are {0, INF, INF, INF, INF, INF, INF, INF} where INF indicates infinite. Now pick the vertex with minimum distance value. The vertex 0 is picked, include it in sptSet. So sptSet becomes {0}. After including 0 to sptSet, update distance values of its adjacent vertices. Adjacent vertices of 0 are 1 and 7. The distance values of 1 and 7 are updated as 4 and 8. Following subgraph shows vertices and their distance values, only the vertices with finite distance values are shown. The vertices included in SPT are shown in green color.

Pick the vertex with minimum distance value and not already included in SPT (not in sptSET). The vertex 1 is picked and added to sptSet. So sptSet now becomes {0, 1}. Update the distance values of adjacent vertices of 1. The distance value of vertex 2 becomes 12.

Pick the vertex with minimum distance value and not already included in SPT (not in sptSET). Vertex 7 is picked. So sptSet now becomes {0, 1, 7}. Update the distance values of adjacent vertices of 7. The distance value of vertex 6 and 8 becomes finite (15 and 9 respectively).

[ad type=”banner”]

Pick the vertex with minimum distance value and not already included in SPT (not in sptSET). Vertex 6 is picked. So sptSet now becomes {0, 1, 7, 6}. Update the distance values of adjacent vertices of 6. The distance value of vertex 5 and 8 are updated.

We repeat the above steps until sptSet doesn’t include all vertices of given graph. Finally, we get the following Shortest Path Tree (SPT).

We use a boolean array sptSet[] to represent the set of vertices included in SPT. If a value sptSet[v] is true, then vertex v is included in SPT, otherwise not. Array dist[] is used to store shortest distance values of all vertices.

C/C++ Programming

[pastacode lang=”c” manual=”%2F%2F%20A%20C%20%2F%20C%2B%2B%20program%20for%20Dijkstra’s%20single%20source%20shortest%20path%20algorithm.%0A%2F%2F%20The%20program%20is%20for%20adjacency%20matrix%20representation%20of%20the%20graph%0A%20%20%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Climits.h%3E%0A%20%20%0A%2F%2F%20Number%20of%20vertices%20in%20the%20graph%0A%23define%20V%209%0A%20%20%0A%2F%2F%20A%20utility%20function%20to%20find%20the%20vertex%20with%20minimum%20distance%20value%2C%20from%0A%2F%2F%20the%20set%20of%20vertices%20not%20yet%20included%20in%20shortest%20path%20tree%0Aint%20minDistance(int%20dist%5B%5D%2C%20bool%20sptSet%5B%5D)%0A%7B%0A%20%20%20%2F%2F%20Initialize%20min%20value%0A%20%20%20int%20min%20%3D%20INT_MAX%2C%20min_index%3B%0A%20%20%0A%20%20%20for%20(int%20v%20%3D%200%3B%20v%20%3C%20V%3B%20v%2B%2B)%0A%20%20%20%20%20if%20(sptSet%5Bv%5D%20%3D%3D%20false%20%26%26%20dist%5Bv%5D%20%3C%3D%20min)%0A%20%20%20%20%20%20%20%20%20min%20%3D%20dist%5Bv%5D%2C%20min_index%20%3D%20v%3B%0A%20%20%0A%20%20%20return%20min_index%3B%0A%7D%0A%20%20%0A%2F%2F%20A%20utility%20function%20to%20print%20the%20constructed%20distance%20array%0Aint%20printSolution(int%20dist%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20printf(%22Vertex%20%20%20Distance%20from%20Source%5Cn%22)%3B%0A%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20printf(%22%25d%20%5Ct%5Ct%20%25d%5Cn%22%2C%20i%2C%20dist%5Bi%5D)%3B%0A%7D%0A%20%20%0A%2F%2F%20Funtion%20that%20implements%20Dijkstra’s%20single%20source%20shortest%20path%20algorithm%0A%2F%2F%20for%20a%20graph%20represented%20using%20adjacency%20matrix%20representation%0Avoid%20dijkstra(int%20graph%5BV%5D%5BV%5D%2C%20int%20src)%0A%7B%0A%20%20%20%20%20int%20dist%5BV%5D%3B%20%20%20%20%20%2F%2F%20The%20output%20array.%20%20dist%5Bi%5D%20will%20hold%20the%20shortest%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20distance%20from%20src%20to%20i%0A%20%20%0A%20%20%20%20%20bool%20sptSet%5BV%5D%3B%20%2F%2F%20sptSet%5Bi%5D%20will%20true%20if%20vertex%20i%20is%20included%20in%20shortest%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20path%20tree%20or%20shortest%20distance%20from%20src%20to%20i%20is%20finalized%0A%20%20%0A%20%20%20%20%20%2F%2F%20Initialize%20all%20distances%20as%20INFINITE%20and%20stpSet%5B%5D%20as%20false%0A%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20dist%5Bi%5D%20%3D%20INT_MAX%2C%20sptSet%5Bi%5D%20%3D%20false%3B%0A%20%20%0A%20%20%20%20%20%2F%2F%20Distance%20of%20source%20vertex%20from%20itself%20is%20always%200%0A%20%20%20%20%20dist%5Bsrc%5D%20%3D%200%3B%0A%20%20%0A%20%20%20%20%20%2F%2F%20Find%20shortest%20path%20for%20all%20vertices%0A%20%20%20%20%20for%20(int%20count%20%3D%200%3B%20count%20%3C%20V-1%3B%20count%2B%2B)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%2F%2F%20Pick%20the%20minimum%20distance%20vertex%20from%20the%20set%20of%20vertices%20not%0A%20%20%20%20%20%20%20%2F%2F%20yet%20processed.%20u%20is%20always%20equal%20to%20src%20in%20first%20iteration.%0A%20%20%20%20%20%20%20int%20u%20%3D%20minDistance(dist%2C%20sptSet)%3B%0A%20%20%0A%20%20%20%20%20%20%20%2F%2F%20Mark%20the%20picked%20vertex%20as%20processed%0A%20%20%20%20%20%20%20sptSet%5Bu%5D%20%3D%20true%3B%0A%20%20%0A%20%20%20%20%20%20%20%2F%2F%20Update%20dist%20value%20of%20the%20adjacent%20vertices%20of%20the%20picked%20vertex.%0A%20%20%20%20%20%20%20for%20(int%20v%20%3D%200%3B%20v%20%3C%20V%3B%20v%2B%2B)%0A%20%20%0A%20%20%20%20%20%20%20%20%20%2F%2F%20Update%20dist%5Bv%5D%20only%20if%20is%20not%20in%20sptSet%2C%20there%20is%20an%20edge%20from%20%0A%20%20%20%20%20%20%20%20%20%2F%2F%20u%20to%20v%2C%20and%20total%20weight%20of%20path%20from%20src%20to%20%20v%20through%20u%20is%20%0A%20%20%20%20%20%20%20%20%20%2F%2F%20smaller%20than%20current%20value%20of%20dist%5Bv%5D%0A%20%20%20%20%20%20%20%20%20if%20(!sptSet%5Bv%5D%20%26%26%20graph%5Bu%5D%5Bv%5D%20%26%26%20dist%5Bu%5D%20!%3D%20INT_MAX%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%26%26%20dist%5Bu%5D%2Bgraph%5Bu%5D%5Bv%5D%20%3C%20dist%5Bv%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20dist%5Bv%5D%20%3D%20dist%5Bu%5D%20%2B%20graph%5Bu%5D%5Bv%5D%3B%0A%20%20%20%20%20%7D%0A%20%20%0A%20%20%20%20%20%2F%2F%20print%20the%20constructed%20distance%20array%0A%20%20%20%20%20printSolution(dist%2C%20V)%3B%0A%7D%0A%20%20%0A%2F%2F%20driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%2F*%20Let%20us%20create%20the%20example%20graph%20discussed%20above%20*%2F%0A%20%20%20int%20graph%5BV%5D%5BV%5D%20%3D%20%7B%7B0%2C%204%2C%200%2C%200%2C%200%2C%200%2C%200%2C%208%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B4%2C%200%2C%208%2C%200%2C%200%2C%200%2C%200%2C%2011%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%208%2C%200%2C%207%2C%200%2C%204%2C%200%2C%200%2C%202%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%207%2C%200%2C%209%2C%2014%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%200%2C%209%2C%200%2C%2010%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%204%2C%2014%2C%2010%2C%200%2C%202%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%200%2C%200%2C%200%2C%202%2C%200%2C%201%2C%206%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B8%2C%2011%2C%200%2C%200%2C%200%2C%200%2C%201%2C%200%2C%207%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%202%2C%200%2C%200%2C%200%2C%206%2C%207%2C%200%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%20%20%0A%20%20%20%20dijkstra(graph%2C%200)%3B%0A%20%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

Vertex   Distance from Source
0                0
1                4
2                12
3                19
4                21
5                11
6                9
7                8
8                14

Notes:
1) The code calculates shortest distance, but doesn’t calculate the path information. We can create a parent array, update the parent array when distance is updated (like prim’s implementation) and use it show the shortest path from source to different vertices.

2) The code is for undirected graph, same dijkstra function can be used for directed graphs also.

3) The code finds shortest distances from source to all vertices. If we are interested only in shortest distance from source to a single target, we can break the for loop when the picked minimum distance vertex is equal to target (Step 3.a of algorithm).

4) Time Complexity of the implementation is O(V^2). If the input graph is represented using adjacency list, it can be reduced to O(E log V) with the help of binary heap. Please see
Dijkstra’s Algorithm for Adjacency List Representation for more details.

5) Dijkstra’s algorithm doesn’t work for graphs with negative weight edges. For graphs with negative weight edges, Bellman–Ford algorithm can be used, we will soon be discussing it as a separate post.

[ad type=”banner”]