Greedy Algorithms | Set 7 (Dijkstra’s shortest path algorithm)
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.

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.

[ad type=”banner”]

Java Programming

[pastacode lang=”java” manual=”%2F%2F%20A%20Java%20program%20for%20Dijkstra’s%20single%20source%20shortest%20path%20algorithm.%0A%2F%2F%20The%20program%20is%20for%20adjacency%20matrix%20representation%20of%20the%20graph%0Aimport%20java.util.*%3B%0Aimport%20java.lang.*%3B%0Aimport%20java.io.*%3B%0A%20%0Aclass%20ShortestPath%0A%7B%0A%20%20%20%20%2F%2F%20A%20utility%20function%20to%20find%20the%20vertex%20with%20minimum%20distance%20value%2C%0A%20%20%20%20%2F%2F%20from%20the%20set%20of%20vertices%20not%20yet%20included%20in%20shortest%20path%20tree%0A%20%20%20%20static%20final%20int%20V%3D9%3B%0A%20%20%20%20int%20minDistance(int%20dist%5B%5D%2C%20Boolean%20sptSet%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Initialize%20min%20value%0A%20%20%20%20%20%20%20%20int%20min%20%3D%20Integer.MAX_VALUE%2C%20min_index%3D-1%3B%0A%20%0A%20%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%20%20%20%20%20%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%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20min%20%3D%20dist%5Bv%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20min_index%20%3D%20v%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20return%20min_index%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20A%20utility%20function%20to%20print%20the%20constructed%20distance%20array%0A%20%20%20%20void%20printSolution(int%20dist%5B%5D%2C%20int%20n)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20System.out.println(%22Vertex%20%20%20Distance%20from%20Source%22)%3B%0A%20%20%20%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%20%20%20%20%20System.out.println(i%2B%22%20%5Ct%5Ct%20%22%2Bdist%5Bi%5D)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Funtion%20that%20implements%20Dijkstra’s%20single%20source%20shortest%20path%0A%20%20%20%20%2F%2F%20algorithm%20for%20a%20graph%20represented%20using%20adjacency%20matrix%0A%20%20%20%20%2F%2F%20representation%0A%20%20%20%20void%20dijkstra(int%20graph%5B%5D%5B%5D%2C%20int%20src)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20dist%5B%5D%20%3D%20new%20int%5BV%5D%3B%20%2F%2F%20The%20output%20array.%20dist%5Bi%5D%20will%20hold%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%2F%2F%20the%20shortest%20distance%20from%20src%20to%20i%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20sptSet%5Bi%5D%20will%20true%20if%20vertex%20i%20is%20included%20in%20shortest%0A%20%20%20%20%20%20%20%20%2F%2F%20path%20tree%20or%20shortest%20distance%20from%20src%20to%20i%20is%20finalized%0A%20%20%20%20%20%20%20%20Boolean%20sptSet%5B%5D%20%3D%20new%20Boolean%5BV%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Initialize%20all%20distances%20as%20INFINITE%20and%20stpSet%5B%5D%20as%20false%0A%20%20%20%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%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20dist%5Bi%5D%20%3D%20Integer.MAX_VALUE%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20sptSet%5Bi%5D%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Distance%20of%20source%20vertex%20from%20itself%20is%20always%200%0A%20%20%20%20%20%20%20%20dist%5Bsrc%5D%20%3D%200%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Find%20shortest%20path%20for%20all%20vertices%0A%20%20%20%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%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Pick%20the%20minimum%20distance%20vertex%20from%20the%20set%20of%20vertices%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20not%20yet%20processed.%20u%20is%20always%20equal%20to%20src%20in%20first%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20iteration.%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20u%20%3D%20minDistance(dist%2C%20sptSet)%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Mark%20the%20picked%20vertex%20as%20processed%0A%20%20%20%20%20%20%20%20%20%20%20%20sptSet%5Bu%5D%20%3D%20true%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Update%20dist%20value%20of%20the%20adjacent%20vertices%20of%20the%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20picked%20vertex.%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20v%20%3D%200%3B%20v%20%3C%20V%3B%20v%2B%2B)%0A%20%0A%20%20%20%20%20%20%20%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%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20edge%20from%20u%20to%20v%2C%20and%20total%20weight%20of%20path%20from%20src%20to%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20v%20through%20u%20is%20smaller%20than%20current%20value%20of%20dist%5Bv%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(!sptSet%5Bv%5D%20%26%26%20graph%5Bu%5D%5Bv%5D!%3D0%20%26%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dist%5Bu%5D%20!%3D%20Integer.MAX_VALUE%20%26%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%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%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%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20print%20the%20constructed%20distance%20array%0A%20%20%20%20%20%20%20%20printSolution(dist%2C%20V)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20method%0A%20%20%20%20public%20static%20void%20main%20(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20Let%20us%20create%20the%20example%20graph%20discussed%20above%20*%2F%0A%20%20%20%20%20%20%20int%20graph%5B%5D%5B%5D%20%3D%20new%20int%5B%5D%5B%5D%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%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%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%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%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%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%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%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%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%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%20%20%20%20%20%20%20%20ShortestPath%20t%20%3D%20new%20ShortestPath()%3B%0A%20%20%20%20%20%20%20%20t.dijkstra(graph%2C%200)%3B%0A%20%20%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]