{"id":26015,"date":"2017-10-26T09:33:28","date_gmt":"2017-10-26T04:03:28","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26015"},"modified":"2017-10-26T09:33:28","modified_gmt":"2017-10-26T04:03:28","slug":"shortest-path-java-programming-dijsktras-algorithm","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/shortest-path-java-programming-dijsktras-algorithm\/","title":{"rendered":"Shortest Path-Java Programming-Dijsktra&#8217;s algorithm"},"content":{"rendered":"<h4 id=\"greedy-algorithms-set-7-dijkstras-shortest-path-algorithmgiven-a-graph-and-a-source-vertex-in-graph-find-shortest-paths-from-source-to-all-vertices-in-the-given-graph\">Greedy Algorithms | Set 7 (Dijkstra\u2019s shortest path algorithm)<br \/>\nGiven a graph and a source vertex in graph, find shortest paths from source to all vertices in the given graph.<\/h4>\n<p>Dijkstra\u2019s algorithm is very similar to Prim\u2019s algorithm for minimum spanning tree. Like Prim\u2019s 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.<\/p>\n<p>Below are the detailed steps used in Dijkstra\u2019s algorithm to find the shortest path from a single source vertex to all other vertices in the given graph.<br \/>\nAlgorithm<br \/>\n1) 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.<br \/>\n2) 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.<br \/>\n3) While sptSet doesn\u2019t include all vertices<br \/>\n\u2026.a) Pick a vertex u which is not there in sptSetand has minimum distance value.<br \/>\n\u2026.b) Include u to sptSet.<br \/>\n\u2026.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.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Java Programming<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20A%20Java%20program%20for%20Dijkstra\u2019s%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\u2019s%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\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>shortest path java programming &#8211; dijsktras&#8217;s algorithm &#8211; Given a graph and a source vertex in graph, find shortest paths from source to all vertices.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,74168,83485,2139],"tags":[71342,76593,76595,71337,76596,76597,76594,76591,76589,71365,76588,76590,76592],"class_list":["post-26015","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-dfs-and-bfs","category-dijkstras-algorithm","category-java","tag-all-pair-shortest-path-algorithm","tag-all-pair-shortest-path-algorithm-with-example","tag-all-pair-shortest-path-program-in-c","tag-all-pairs-shortest-path-algorithm","tag-all-pairs-shortest-path-dynamic-programming-example","tag-all-pairs-shortest-path-matrix-multiplication","tag-all-pairs-shortest-path-problem","tag-dijkstra-algorithm-in-java-with-output","tag-dijkstra-algorithm-java-adjacency-matrix","tag-dijkstra-algorithm-java-code-download","tag-dijkstras-algorithm-java-using-priority-queue","tag-find-the-shortest-path-between-two-nodes-in-a-graph-in-java","tag-single-source-shortest-path-program-in-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26015"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26015\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}