{"id":25982,"date":"2017-10-26T09:25:16","date_gmt":"2017-10-26T03:55:16","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25982"},"modified":"2017-10-26T09:25:16","modified_gmt":"2017-10-26T03:55:16","slug":"shortest-paths-dijkstras-shortest-path-algorithm","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/shortest-paths-dijkstras-shortest-path-algorithm\/","title":{"rendered":"Shortest Paths &#8211; C\/C++ &#8211; Dijkstra\u2019s shortest path algorithm"},"content":{"rendered":"<p>Given a graph and a source vertex in graph, find shortest paths from source to all vertices in the given graph.<span id=\"more-27697\"><\/span><\/p>\n<p>Dijkstra\u2019s algorithm is very similar to <a href=\"http:\/\/www.geeksforgeeks.org\/archives\/27455\" target=\"_blank\" rel=\"noopener\">Prim\u2019s algorithm for minimum spanning tree<\/a>. Like Prim\u2019s MST, we generate a<em> SPT (shortest path tree)<\/em> 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[ad type=\u201dbanner\u201d]\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 \/>\n<strong>1)<\/strong> Create a set <em>sptSet<\/em> (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 \/>\n<strong>2)<\/strong> 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 \/>\n<strong>3)<\/strong> While <em>sptSet<\/em> doesn\u2019t include all vertices<br \/>\n\u2026.<strong>a)<\/strong> Pick a vertex u which is not there in <em>sptSet<\/em>and has minimum distance value.<br \/>\n\u2026.<strong>b)<\/strong> Include u to <em>sptSet<\/em>.<br \/>\n\u2026.<strong>c)<\/strong> 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<p>The set <em>sptSet<\/em>is 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 <em>sptSet<\/em>. So <em>sptSet <\/em>becomes {0}. After including 0 to <em>sptSet<\/em>, 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.<\/p>\n<p>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.<\/p>\n<p>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).<\/p>\n[ad type=\u201dbanner\u201d]\n<p>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.<\/p>\n<p>We repeat the above steps until <em>sptSet <\/em>doesn\u2019t include all vertices of given graph. Finally, we get the following Shortest Path Tree (SPT).<\/p>\n<p>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.<\/p>\n<p><strong>C\/C++ Programming<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20A%20C%20%2F%20C%2B%2B%20program%20for%20Dijkstra\u2019s%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\u2019s%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\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output:<\/p>\n<pre>Vertex   Distance from Source\r\n0                0\r\n1                4\r\n2                12\r\n3                19\r\n4                21\r\n5                11\r\n6                9\r\n7                8\r\n8                14\r\n\r\n<\/pre>\n<p><strong>Notes:<\/strong><br \/>\n<strong>1)<\/strong> The code calculates shortest distance, but doesn\u2019t calculate the path information. We can create a parent array, update the parent array when distance is updated (like <a href=\"http:\/\/www.geeksforgeeks.org\/archives\/27455\" target=\"_blank\" rel=\"noopener\">prim\u2019s implementation<\/a>) and use it show the shortest path from source to different vertices.<\/p>\n<p><strong>2)<\/strong> The code is for undirected graph, same dijkstra function can be used for directed graphs also.<\/p>\n<p><strong>3)<\/strong> 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).<\/p>\n<p><strong>4)<\/strong> Time Complexity of the implementation is O(V^2). If the input <a href=\"http:\/\/www.geeksforgeeks.org\/archives\/27134\" target=\"_blank\" rel=\"noopener\">graph is represented using adjacency list<\/a>, it can be reduced to O(E log V) with the help of binary heap. Please see<br \/>\n<a href=\"http:\/\/www.geeksforgeeks.org\/greedy-algorithms-set-7-dijkstras-algorithm-for-adjacency-list-representation\/\" target=\"_blank\" rel=\"noopener\">Dijkstra\u2019s Algorithm for Adjacency List Representation<\/a> for more details.<\/p>\n<p><strong>5)<\/strong> Dijkstra\u2019s algorithm doesn\u2019t work for graphs with negative weight edges. For graphs with negative weight edges, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Bellman-Ford_algorithm\" target=\"_blank\" rel=\"noopener noreferrer\">Bellman\u2013Ford algorithm<\/a> can be used, we will soon be discussing it as a separate post.<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Shortest Paths &#8211; C\/C++ &#8211; Dijkstra\u2019s shortest path 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,69866,83515,1,83582,73719],"tags":[76516,76521,76517,76515,76519,71346,71320,70241,71331,76518,71343,71348,71333,76520],"class_list":["post-25982","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-c-programming-3","category-coding","category-dijkstras-shortest-path-algorithm","category-divide-and-conquer","tag-basic-dijkstras-algorithm-c-code","tag-dijkstra-algorithm-c","tag-dijkstra-algorithm-c-adjacency-matrix","tag-dijkstra-algorithm-c-priority-queue","tag-dijkstra-c","tag-dijkstras-algorithm-c","tag-dijkstras-algorithm-complexity","tag-dijkstras-algorithm-example","tag-dijkstras-algorithm-example-step-by-step","tag-dijkstras-algorithm-in-c-with-output","tag-dijkstras-algorithm-in-java","tag-dijkstras-algorithm-java","tag-shortest-path-algorithm-example","tag-shortest-path-algorithms"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25982","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=25982"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25982\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}