{"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=&#8221;banner&#8221;]\n<p><strong>Java Programming<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ A Java program for Dijkstra&#039;s single source shortest path algorithm.<br\/>\/\/ The program is for adjacency matrix representation of the graph<br\/>import java.util.*;<br\/>import java.lang.*;<br\/>import java.io.*;<br\/> <br\/>class ShortestPath<br\/>{<br\/>    \/\/ A utility function to find the vertex with minimum distance value,<br\/>    \/\/ from the set of vertices not yet included in shortest path tree<br\/>    static final int V=9;<br\/>    int minDistance(int dist[], Boolean sptSet[])<br\/>    {<br\/>        \/\/ Initialize min value<br\/>        int min = Integer.MAX_VALUE, min_index=-1;<br\/> <br\/>        for (int v = 0; v &lt; V; v++)<br\/>            if (sptSet[v] == false &amp;&amp; dist[v] &lt;= min)<br\/>            {<br\/>                min = dist[v];<br\/>                min_index = v;<br\/>            }<br\/> <br\/>        return min_index;<br\/>    }<br\/> <br\/>    \/\/ A utility function to print the constructed distance array<br\/>    void printSolution(int dist[], int n)<br\/>    {<br\/>        System.out.println(&quot;Vertex   Distance from Source&quot;);<br\/>        for (int i = 0; i &lt; V; i++)<br\/>            System.out.println(i+&quot; \\t\\t &quot;+dist[i]);<br\/>    }<br\/> <br\/>    \/\/ Funtion that implements Dijkstra&#039;s single source shortest path<br\/>    \/\/ algorithm for a graph represented using adjacency matrix<br\/>    \/\/ representation<br\/>    void dijkstra(int graph[][], int src)<br\/>    {<br\/>        int dist[] = new int[V]; \/\/ The output array. dist[i] will hold<br\/>                                 \/\/ the shortest distance from src to i<br\/> <br\/>        \/\/ sptSet[i] will true if vertex i is included in shortest<br\/>        \/\/ path tree or shortest distance from src to i is finalized<br\/>        Boolean sptSet[] = new Boolean[V];<br\/> <br\/>        \/\/ Initialize all distances as INFINITE and stpSet[] as false<br\/>        for (int i = 0; i &lt; V; i++)<br\/>        {<br\/>            dist[i] = Integer.MAX_VALUE;<br\/>            sptSet[i] = false;<br\/>        }<br\/> <br\/>        \/\/ Distance of source vertex from itself is always 0<br\/>        dist[src] = 0;<br\/> <br\/>        \/\/ Find shortest path for all vertices<br\/>        for (int count = 0; count &lt; V-1; count++)<br\/>        {<br\/>            \/\/ Pick the minimum distance vertex from the set of vertices<br\/>            \/\/ not yet processed. u is always equal to src in first<br\/>            \/\/ iteration.<br\/>            int u = minDistance(dist, sptSet);<br\/> <br\/>            \/\/ Mark the picked vertex as processed<br\/>            sptSet[u] = true;<br\/> <br\/>            \/\/ Update dist value of the adjacent vertices of the<br\/>            \/\/ picked vertex.<br\/>            for (int v = 0; v &lt; V; v++)<br\/> <br\/>                \/\/ Update dist[v] only if is not in sptSet, there is an<br\/>                \/\/ edge from u to v, and total weight of path from src to<br\/>                \/\/ v through u is smaller than current value of dist[v]<br\/>                if (!sptSet[v] &amp;&amp; graph[u][v]!=0 &amp;&amp;<br\/>                        dist[u] != Integer.MAX_VALUE &amp;&amp;<br\/>                        dist[u]+graph[u][v] &lt; dist[v])<br\/>                    dist[v] = dist[u] + graph[u][v];<br\/>        }<br\/> <br\/>        \/\/ print the constructed distance array<br\/>        printSolution(dist, V);<br\/>    }<br\/> <br\/>    \/\/ Driver method<br\/>    public static void main (String[] args)<br\/>    {<br\/>        \/* Let us create the example graph discussed above *\/<br\/>       int graph[][] = new int[][]{{0, 4, 0, 0, 0, 0, 0, 8, 0},<br\/>                                  {4, 0, 8, 0, 0, 0, 0, 11, 0},<br\/>                                  {0, 8, 0, 7, 0, 4, 0, 0, 2},<br\/>                                  {0, 0, 7, 0, 9, 14, 0, 0, 0},<br\/>                                  {0, 0, 0, 9, 0, 10, 0, 0, 0},<br\/>                                  {0, 0, 4, 14, 10, 0, 2, 0, 0},<br\/>                                  {0, 0, 0, 0, 0, 2, 0, 1, 6},<br\/>                                  {8, 11, 0, 0, 0, 0, 1, 0, 7},<br\/>                                  {0, 0, 2, 0, 0, 0, 6, 7, 0}<br\/>                                 };<br\/>        ShortestPath t = new ShortestPath();<br\/>        t.dijkstra(graph, 0);<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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}]}}