{"id":26271,"date":"2017-10-26T20:48:00","date_gmt":"2017-10-26T15:18:00","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26271"},"modified":"2017-10-26T20:48:00","modified_gmt":"2017-10-26T15:18:00","slug":"c-programming-floyd-warshall-algorithm-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-floyd-warshall-algorithm-2\/","title":{"rendered":"Cpp Programming &#8211; Floyd Warshall Algorithm"},"content":{"rendered":"<p>The Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem. The problem is to find shortest distances between every pair of vertices in a given edge weighted directed Graph.<span id=\"more-19772\"><\/span><\/p>\n<p>Example:<\/p>\n<pre><strong>Input:<\/strong>\r\n       graph[][] = { {0,   5,  INF, 10},\r\n                    {INF,  0,  3,  INF},\r\n                    {INF, INF, 0,   1},\r\n                    {INF, INF, INF, 0} }\r\nwhich represents the following graph\r\n             10\r\n       (0)------->(3)\r\n        |         \/|\\\r\n      5 |          |\r\n        |          | 1\r\n       \\|\/         |\r\n       (1)------->(2)\r\n            3       \r\nNote that the value of graph[i][j] is 0 if i is equal to j \r\nAnd graph[i][j] is INF (infinite) if there is no edge from vertex i to j.\r\n\r\n<strong>Output:<\/strong>\r\nShortest distance matrix\r\n      0      5      8      9\r\n    INF      0      3      4\r\n    INF    INF      0      1\r\n    INF    INF    INF      0<\/pre>\n[ad type=\u201dbanner\u201d]\n<p><strong>Floyd Warshall Algorithm<\/strong><br \/>\nWe initialize the solution matrix same as the input graph matrix as a first step. Then we update the solution matrix by considering all vertices as an intermediate vertex. The idea is to one by one pick all vertices and update all shortest paths which include the picked vertex as an intermediate vertex in the shortest path. When we pick vertex number k as an intermediate vertex, we already have considered vertices {0, 1, 2, .. k-1} as intermediate vertices. For every pair (i, j) of source and destination vertices respectively, there are two possible cases.<\/p>\n<ul>\n<li>k is not an intermediate vertex in shortest path from i to j. We keep the value of dist[i][j] as it is.<\/li>\n<li>k is an intermediate vertex in shortest path from i to j. We update the value of dist[i][j] as dist[i][k] + dist[k][j].<\/li>\n<\/ul>\n<p>The following figure is taken from the Cormen book. It shows the above optimal substructure property in the all-pairs shortest path problem.<img decoding=\"async\" class=\"aligncenter size-full wp-image-26276\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Floyd-Warshell-Alogrithm.png\" alt=\"Floyd-Warshell Alogrithm\" width=\"350\" height=\"90\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Floyd-Warshell-Alogrithm.png 350w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Floyd-Warshell-Alogrithm-300x77.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>Following is C++ implementation of the Floyd Warshall algorithm.<\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20C%20Program%20for%20Floyd%20Warshall%20Algorithm%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20Number%20of%20vertices%20in%20the%20graph%0A%23define%20V%204%0A%20%0A%2F*%20Define%20Infinite%20as%20a%20large%20enough%20value.%20This%20value%20will%20be%20used%0A%20%20for%20vertices%20not%20connected%20to%20each%20other%20*%2F%0A%23define%20INF%2099999%0A%20%0A%2F%2F%20A%20function%20to%20print%20the%20solution%20matrix%0Avoid%20printSolution(int%20dist%5B%5D%5BV%5D)%3B%0A%20%0A%2F%2F%20Solves%20the%20all-pairs%20shortest%20path%20problem%20using%20Floyd%20Warshall%20algorithm%0Avoid%20floydWarshall%20(int%20graph%5B%5D%5BV%5D)%0A%7B%0A%20%20%20%20%2F*%20dist%5B%5D%5B%5D%20will%20be%20the%20output%20matrix%20that%20will%20finally%20have%20the%20shortest%20%0A%20%20%20%20%20%20distances%20between%20every%20pair%20of%20vertices%20*%2F%0A%20%20%20%20int%20dist%5BV%5D%5BV%5D%2C%20i%2C%20j%2C%20k%3B%0A%20%0A%20%20%20%20%2F*%20Initialize%20the%20solution%20matrix%20same%20as%20input%20graph%20matrix.%20Or%20%0A%20%20%20%20%20%20%20we%20can%20say%20the%20initial%20values%20of%20shortest%20distances%20are%20based%0A%20%20%20%20%20%20%20on%20shortest%20paths%20considering%20no%20intermediate%20vertex.%20*%2F%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20for%20(j%20%3D%200%3B%20j%20%3C%20V%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20dist%5Bi%5D%5Bj%5D%20%3D%20graph%5Bi%5D%5Bj%5D%3B%0A%20%0A%20%20%20%20%2F*%20Add%20all%20vertices%20one%20by%20one%20to%20the%20set%20of%20intermediate%20vertices.%0A%20%20%20%20%20%20\u2014%3E%20Before%20start%20of%20a%20iteration%2C%20we%20have%20shortest%20distances%20between%20all%0A%20%20%20%20%20%20pairs%20of%20vertices%20such%20that%20the%20shortest%20distances%20consider%20only%20the%0A%20%20%20%20%20%20vertices%20in%20set%20%7B0%2C%201%2C%202%2C%20..%20k-1%7D%20as%20intermediate%20vertices.%0A%20%20%20%20%20%20\u2014-%3E%20After%20the%20end%20of%20a%20iteration%2C%20vertex%20no.%20k%20is%20added%20to%20the%20set%20of%0A%20%20%20%20%20%20intermediate%20vertices%20and%20the%20set%20becomes%20%7B0%2C%201%2C%202%2C%20..%20k%7D%20*%2F%0A%20%20%20%20for%20(k%20%3D%200%3B%20k%20%3C%20V%3B%20k%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Pick%20all%20vertices%20as%20source%20one%20by%20one%0A%20%20%20%20%20%20%20%20for%20(i%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%20%2F%2F%20Pick%20all%20vertices%20as%20destination%20for%20the%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20above%20picked%20source%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(j%20%3D%200%3B%20j%20%3C%20V%3B%20j%2B%2B)%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%20%2F%2F%20If%20vertex%20k%20is%20on%20the%20shortest%20path%20from%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20i%20to%20j%2C%20then%20update%20the%20value%20of%20dist%5Bi%5D%5Bj%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(dist%5Bi%5D%5Bk%5D%20%2B%20dist%5Bk%5D%5Bj%5D%20%3C%20dist%5Bi%5D%5Bj%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dist%5Bi%5D%5Bj%5D%20%3D%20dist%5Bi%5D%5Bk%5D%20%2B%20dist%5Bk%5D%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Print%20the%20shortest%20distance%20matrix%0A%20%20%20%20printSolution(dist)%3B%0A%7D%0A%20%0A%2F*%20A%20utility%20function%20to%20print%20solution%20*%2F%0Avoid%20printSolution(int%20dist%5B%5D%5BV%5D)%0A%7B%0A%20%20%20%20printf%20(%22Following%20matrix%20shows%20the%20shortest%20distances%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%20between%20every%20pair%20of%20vertices%20%5Cn%22)%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%200%3B%20j%20%3C%20V%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(dist%5Bi%5D%5Bj%5D%20%3D%3D%20INF)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22%257s%22%2C%20%22INF%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf%20(%22%257d%22%2C%20dist%5Bi%5D%5Bj%5D)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20printf(%22%5Cn%22)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Let%20us%20create%20the%20following%20weighted%20graph%0A%20%20%20%20%20%20%20%20%20%20%20%2010%0A%20%20%20%20%20%20%20(0)\u2014\u2014-%3E(3)%0A%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%2F%7C%5C%0A%20%20%20%20%20%205%20%7C%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%7C%201%0A%20%20%20%20%20%20%20%5C%7C%2F%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%20%20%20(1)\u2014\u2014-%3E(2)%0A%20%20%20%20%20%20%20%20%20%20%20%203%20%20%20%20%20%20%20%20%20%20%20*%2F%0A%20%20%20%20int%20graph%5BV%5D%5BV%5D%20%3D%20%7B%20%7B0%2C%20%20%205%2C%20%20INF%2C%2010%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%7BINF%2C%200%2C%20%20%203%2C%20INF%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%7BINF%2C%20INF%2C%200%2C%20%20%201%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%7BINF%2C%20INF%2C%20INF%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%7D%3B%0A%20%0A%20%20%20%20%2F%2F%20Print%20the%20solution%0A%20%20%20%20floydWarshall(graph)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC++\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output :<\/strong><\/p>\n<pre>Following matrix shows the shortest distances between every pair of vertices\r\n      0      5      8      9\r\n    INF      0      3      4\r\n    INF    INF      0      1\r\n    INF    INF    INF      0\r\n<\/pre>\n<p>Time Complexity: O(V^3)<\/p>\n<p>The above program only prints the shortest distances. We can modify the solution to print the shortest paths also by storing the predecessor information in a separate 2D matrix.<br \/>\nAlso, the value of INF can be taken as INT_MAX from limits.h to make sure that we handle maximum possible value. When we take INF as INT_MAX, we need to change the if condition in the above program to avoid arithmatic overflow.<\/p>\n<pre>#include <limits.h>\r\n\r\n#define INF INT_MAX\r\n..........................\r\nif ( dist[i][k] != INF && \r\n     dist[k][j] != INF && \r\n     dist[i][k] + dist[k][j] < dist[i][j]\r\n    )\r\n dist[i][j] = dist[i][k] + dist[k][j];\r\n...........................<\/pre>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming &#8211; Floyd Warshall Algorithm &#8211; Dynamic Programming The Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,83515,1,70145],"tags":[71342,78081,78111,78115,72847,72842,72845,70483,72841,72848,72840,72849,72846,72843,72854,72844,72850,78088,78076,73997,78070,78082,78117,78118,78089,78077,78084,78095,78093,78078,78083,78086,78113,78109,78110,78116,78092,75855,78114,78112,78079,78071,72839,72852,72855,78087,72853,78075,71345,78072,72851],"class_list":["post-26271","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-3","category-coding","category-dynamic-programming","tag-all-pair-shortest-path-algorithm","tag-all-pair-shortest-path-floyd-warshall-algorithm-example","tag-c-code-for-floyd-warshall-algorithm","tag-c-program-to-implement-floyd-warshall-algorithm","tag-concept-of-dynamic-programming","tag-define-dynamic-programming","tag-definition-of-dynamic-programming","tag-dynamic-programming","tag-dynamic-programming-c","tag-dynamic-programming-code-generation-algorithm","tag-dynamic-programming-definition","tag-dynamic-programming-in-c","tag-dynamic-programming-in-data-structure","tag-dynamic-programming-problems","tag-dynamic-programming-set-1","tag-dynamic-programming-software","tag-explain-dynamic-programming","tag-find-shortest-path-using-floyd-warshall-algorithm","tag-floyd-algorithm-example","tag-floyd-warshall-algorithm","tag-floyd-warshall-algorithm-all-pair-shortest-path-graph-algorithm","tag-floyd-warshall-algorithm-animation","tag-floyd-warshall-algorithm-c","tag-floyd-warshall-algorithm-c-code","tag-floyd-warshall-algorithm-dynamic-programming","tag-floyd-warshall-algorithm-example","tag-floyd-warshall-algorithm-example-animation","tag-floyd-warshall-algorithm-example-pdf","tag-floyd-warshall-algorithm-example-ppt","tag-floyd-warshall-algorithm-example-step-by-step","tag-floyd-warshall-algorithm-explanation","tag-floyd-warshall-algorithm-for-all-pairs-shortest-path","tag-floyd-warshall-algorithm-in-c","tag-floyd-warshall-algorithm-in-c-code","tag-floyd-warshall-algorithm-in-c-program","tag-floyd-warshall-algorithm-in-c-with-output","tag-floyd-warshall-algorithm-pdf","tag-floyd-warshall-algorithm-ppt","tag-floyd-warshall-algorithm-program-in-c","tag-floyd-warshall-algorithm-program-in-c-with-output","tag-floyd-warshall-algorithm-solved-example","tag-floyd-warshall-algorithm-youtube","tag-how-to-solve-dynamic-programming-problems","tag-problems-on-dynamic-programming","tag-simple-dynamic-programming-example","tag-time-complexity-of-floyd-warshall-algorithm","tag-types-of-dynamic-programming","tag-warshall-algorithm-transitive-closure","tag-warshalls-algorithm-in-data-structure","tag-warshalls-algorithm-shortest-path","tag-youtube-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26271","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=26271"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26271\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}