{"id":12429,"date":"2023-02-02T10:49:09","date_gmt":"2023-02-02T10:49:09","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=12429"},"modified":"2023-06-23T10:17:17","modified_gmt":"2023-06-23T10:17:17","slug":"floyd-warshall-algorithm","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/","title":{"rendered":"Floyd Warshall Algorithm"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232013-Floyd%20Warshall%20Algorithm.jpg\" alt=\"\" \/><\/p>\n<p>The Floyd Warshall algorithm in C is a dynamic programming approach used to find the shortest path between all pairs of vertices in a weighted graph. It is applicable to both directed and undirected graphs, with the exception of graphs that contain negative weight cycles. This algorithm proves to be the best choice when searching for the shortest path across every pair of vertices in a graph, such as finding the shortest routes between cities in a state or country. In this article, we will explore the workings of the Floyd Warshall algorithm in C and delve into its implementation details. We will also discuss its time complexity compared to other popular algorithms like Bellman-Ford and Dijkstra&#8217;s shortest path algorithm, highlighting why Floyd Warshall is often the preferred option. Additionally, we will explore the various applications of the Floyd-Warshall algorithm, showcasing its versatility and practical uses in different scenarios.<\/p>\n<h2>What is the Floyd Warshall Algorithm in C?<\/h2>\n<p>Floyd-Warshall Algorithm is an algorithm which follows dynamic programming approach to find the shortest path between all the pairs of vertices in a weighted graph. This algorithm works well for both the directed and undirected weighted graphs. But, it does not work for graphs that contain negative weight cycles.<\/p>\n<p>If you\u2019re looking for an algorithm for scenarios like finding the shortest path between every pair of cities in a state or in a country, then the best man at work is our polynomial-time Floyd-Warshall algorithm.<\/p>\n<p>In other words, the Floyd-Warshall algorithm is the best choice for finding the shortest path across every pair of vertex in a graph.<\/p>\n<p>One restriction we have to follow is that the graph shouldn\u2019t contain any negative weight cycles.<\/p>\n<p>You see, the Floyd-Warshall algorithm does support negative weight edges in a directed graph so long the weighted sum of such edges forming a cycle isn\u2019t negative.<\/p>\n<p>And that\u2019s what here means by a negative weight cycle.<\/p>\n<p>If there exists at least one such negative weight cycle, we can always just keep traversing this cycle over and over while making the length of the path smaller and smaller. Keep repeating it, and the length at some point reaches negative infinity which is wildly unreasonable.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232295-Floyd%20Warshall%20Algorithm1.png\" alt=\"\" \/><\/p>\n<p>Also if we notice, the algorithm cannot support negative weight edges in an undirected graph at all. Such an edge forms a negative cycle in and of itself since we can traverse back and forth along that edge infinitely as it\u2019s an undirected graph.<\/p>\n<p>Well, you may suspect why we are learning another algorithm when we can solve the same thing by expanding the Bellman-Ford or Dijkstra\u2019s shortest path algorithm on every vertex in the graph.<\/p>\n<p>Yes, you can, but the main reason why we are not using Bellman-Ford or Dijkstra\u2019s shortest path algorithm is their Time complexity which we will discuss later in this article.<\/p>\n<p>Now that we have developed a fair understanding as to what is and why we use the Floyd-Warshall algorithm, let\u2019s take our discussion ahead and see how it actually works.<\/p>\n<p><strong>Points to Remember:<\/strong><\/p>\n<ul>\n<li>Negative weight cycle graphs are graphs where the sum of the weights of edges in a cycle is negative).<\/li>\n<li>A weighted graph is a graph in which each edge has some weight(numerical value) associated with it.<\/li>\n<\/ul>\n<h2>How does Floyd Warshall Algorithm in C Work?<\/h2>\n<p>Given Graph:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232295-Floyd%20Warshall%20Algorithm2.png\" alt=\"\" \/><\/p>\n<p>Follow the steps mentioned below to find the shortest path between all the pairs of vertices:<\/p>\n<ul>\n<li>\n<p><strong>Step 1:<\/strong><br \/>\nCreate a matrix A0 of dimension V*V, where V is the number of vertices. The row and column are indexed as i and j, respectively. i and j are the graph&#8217;s vertices.<br \/>\nThe value of cell A[i][j] means the distance from the ith vertex to the jth vertex. If there is no path between the ith vertex and the jth vertex, the cell is left with infinity.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232333-Floyd%20Warshall%20Algorithm3.png\" alt=\"\" \/><\/p>\n<\/li>\n<li>\n<p><strong>Step 2:<\/strong><br \/>\nNow, using matrix A0, construct matrix A1. The elements in the first column and row stay unchanged. The remaining cells are filled out as follows.<\/p>\n<p>Let k be the intermediate vertex on the shortest path from source to destination. k is the 0th vertex (i.e k = 0) in this stage. If (A[i][j] &gt; A[i][k] + A[k][j]), A[i][j] is filled with (A[i][k] + A[k][j]).<\/p>\n<p>In other words, if the direct distance between the source and the destination is larger than the path through the vertex k, the cell is filled with A[i][k] + A[k][j].<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232334-Floyd%20Warshall%20Algorithm4.png\" alt=\"\" \/><\/p>\n<p>This vertex k is used to compute the distance from the source vertex to the destination vertex.<\/p>\n<\/li>\n<li>\n<p><strong>Step 3:<\/strong><br \/>\nSimilarly, A2 is derived from A1. The elements in the second column and second row remain unchanged.<\/p>\n<p>k is the 1st vertex in this stage (i.e. k = 1). The remaining steps are similar to those in step 2.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232338-Floyd%20Warshall%20Algorithm5.png\" alt=\"\" \/><\/p>\n<p>For example: For A2[3,2], the direct distance from vertex 3 to 2 is infinity and the sum of the distance from vertex 3 to 2 through vertex k(i.e. from vertex 3 to vertex 1 and from vertex 1 to vertex 2) is 0. Since 0 is less than infinity, A2[3,2] is filled with 0.<\/p>\n<\/li>\n<li>\n<p><strong>Step 4:<\/strong><br \/>\nSimilarly, A3 and A4 matrices are also constructed.<br \/>\nWhen generating A3, k is a second vertex(i.e. K = 2) and k is a third vertex(i.e. K = 3) during the construction of A4.<\/p>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232338-Floyd%20Warshall%20Algorithm6.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232339-Floyd%20Warshall%20Algorithm7.png\" alt=\"\" \/><\/p>\n<ul>\n<li>\n<p><strong>Step 5:<\/strong><br \/>\nA4 displays the shortest path between any pair of vertices.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232372-Floyd%20Warshall%20Algorithm8.png\" alt=\"\" \/><\/p>\n<\/li>\n<\/ul>\n<p>Now, let us try to implement the above steps in a code.<\/p>\n<h2>Implementation of Floyd Warshall Algorithm in C<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_12220 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_12220 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_12220 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_12220 .wpsm_nav-tabs > li.active > a, #tab_container_12220 .wpsm_nav-tabs > li.active > a:hover, #tab_container_12220 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_12220 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_12220 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_12220 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_12220 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_12220 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_12220 .wpsm_nav-tabs > li > a:hover , #tab_container_12220 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_12220 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_12220 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_12220 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_12220 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_12220 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_12220 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_12220 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12220 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_12220 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12220 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_12220 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_12220\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_12220\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12220_1\" aria-controls=\"tabs_desc_12220_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_12220\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_12220_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">\/\/ Implementation of Floyd-Warshall Algorithm in C++\r\n#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\n\/\/ defining the number of vertices\r\n#define V 4\r\n\r\n#define INF 9999\r\n\r\nvoid printMatrix(int matrix[][V]);\r\n\r\n\/\/ Implementing floyd warshall algorithm\r\nvoid floydWarshall(int graph[][V]) {\r\n  int matrix[V][V], i, j, k;\r\n  for (i = 0; i &lt; V; i++)\r\n    for (j = 0; j &lt; V; j++)\r\n      matrix[i][j] = graph[i][j];\r\n\r\n  \/\/ Adding vertices individually\r\n  for (k = 0; k &lt; V; k++) {\r\n    for (i = 0; i &lt; V; i++) {\r\n      for (j = 0; j &lt; V; j++) {\r\n        if (matrix[i][k] + matrix[k][j] &lt; matrix[i][j])\r\n          matrix[i][j] = matrix[i][k] + matrix[k][j];\r\n      }\r\n    }\r\n  }\r\n  printMatrix(matrix);\r\n}\r\n\r\nvoid printMatrix(int matrix[][V]) {\r\n  for (int i = 0; i &lt; V; i++) {\r\n    for (int j = 0; j &lt; V; j++) {\r\n        if(i==j){\r\n            continue;\r\n        }\r\n        else if (matrix[i][j] == INF){\r\n            cout&lt;&lt;\"no path exist between \"&lt;&lt;i&lt;&lt;\" and \"&lt;&lt;j&lt;&lt;endl;\r\n        }\r\n        else{\r\n            cout&lt;&lt;\"shortest path from \"&lt;&lt;i&lt;&lt;\" to \"&lt;&lt;j&lt;&lt;\" is \"&lt;&lt;matrix[i][j]&lt;&lt;endl;\r\n        }\r\n    }\r\n  }\r\n}\r\n\r\nint main() {\r\n  int graph[V][V] = {{0,INF,-3,INF},\r\n                     {5,0,4,INF},\r\n                     {INF,INF,0,3},\r\n                     {INF,-2,INF,0}};\r\n  floydWarshall(graph);\r\n  return 0;\r\n  \/\/ End of Program\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_12220 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_12220 a\"),jQuery(\"#tab-content_12220\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output:<\/strong><\/p>\n<pre><code>shortest path from 0 to 1 is -2\nshortest path from 0 to 2 is -3\nshortest path from 0 to 3 is 0\nshortest path from 1 to 0 is 5\nshortest path from 1 to 2 is 2\nshortest path from 1 to 3 is 5\nshortest path from 2 to 0 is 6\nshortest path from 2 to 1 is 1\nshortest path from 2 to 3 is 3\nshortest path from 3 to 0 is 3\nshortest path from 3 to 1 is -2\nshortest path from 3 to 2 is 0<\/code><\/pre>\n<p>Now, let&#8217;s discuss the time complexity of the Floyd warshall algorithm.<\/p>\n<h2>Time Complexity Analysis of the Floyd Warshall Algorithm<\/h2>\n<p>The overall time complexity of the Floyd Warshall algorithm is O(V^3) where V represents the number of vertexes in the graph. Let&#8217;s dive into more details in the time complexity analysis of the Floyd warshall algorithm.<\/p>\n<p>Since we are discussing time complexity, let\u2019s move our discussion in the direction of why Bellman-Ford or Dijkstra\u2019s shortest path algorithm is not a good choice for solving the shortest path problem concerning all pairs of vertices.<\/p>\n<p>The Bellman-Ford and Dijkstra\u2019s shortest path algorithm only computes the shortest path from one vertex to all other vertexes and has an upper bound of O(VE) and O(V + ElogV) where V and E denote the numbers of vertex and edges in the graph.<\/p>\n<p>One more point to note about Dijkstra\u2019s shortest path algorithm is that it doesn\u2019t work for negatively weighted edges.<\/p>\n<p>And if we expand the Bellman-Ford algorithm for all vertexes, the time complexity becomes V <em> O(V <\/em> E) but since the Floyd Warshall algorithm depends only on the number of vertexes, not on the number of edges in the graph.<br \/>\nThis makes the Floyd-Warshall algorithm an ideal choice for us.<\/p>\n<h2>Applications of the Floyd Warshall Algorithm in C<\/h2>\n<p>Here are some applications of the Floyd warshall algorithm:<\/p>\n<ol>\n<li>Performs fast computation of Pathfinder networks.<\/li>\n<li>Helps in finding the Inversion of real matrices.<\/li>\n<li>Helps in finding the transitive closure of directed graphs.<\/li>\n<li>Helps in finding out if an undirected graph is bipartite or not.<\/li>\n<li>Used in finding the shortest path in the graph.<\/li>\n<\/ol>\n<p><strong>Conclusion<\/strong><br \/>\nIn conclusion, the Floyd-Warshall algorithm in C is a powerful tool for finding the shortest path between all pairs of vertices in a weighted graph. Its dynamic programming approach makes it suitable for various scenarios, including route planning, transitive closure determination, bipartite graph identification, and more. Unlike other algorithms like Bellman-Ford or Dijkstra&#8217;s shortest path algorithm, Floyd-Warshall offers a time complexity of O(V^3), making it an efficient choice when dealing with large graphs. However, it is important to note that the algorithm cannot handle graphs with negative weight cycles. Despite this limitation, the Floyd-Warshall algorithm proves to be a valuable tool in solving complex shortest path problems. By understanding its inner workings and implementing it in C, developers can harness its capabilities for a wide range of applications in graph theory and optimization.<\/p>\n<h2>Frequently Asked Questions (FAQs)<\/h2>\n<p><strong>Q1. Can the Floyd-Warshall algorithm handle graphs with negative weight cycles?<\/strong><br \/>\nNo, the Floyd-Warshall algorithm cannot handle graphs that contain negative weight cycles. It assumes that there are no negative weight cycles in the graph for accurate shortest path calculations.<\/p>\n<p><strong>Q2. Is the Floyd-Warshall algorithm suitable for both directed and undirected graphs?<\/strong><br \/>\nYes, the Floyd-Warshall algorithm can be applied to both directed and undirected graphs. However, it cannot handle negative weight edges in undirected graphs since they inherently create negative cycles.<\/p>\n<p><strong>Q3. How does the Floyd-Warshall algorithm compare to Bellman-Ford and Dijkstra&#8217;s shortest path algorithm in terms of time complexity?<\/strong><br \/>\nThe Floyd-Warshall algorithm has a time complexity of O(V^3), where V represents the number of vertices. In contrast, Bellman-Ford has a time complexity of O(VE), and Dijkstra&#8217;s algorithm has a time complexity of O(V + ElogV). Floyd-Warshall&#8217;s dependency only on the number of vertices makes it an ideal choice for solving the shortest path problem across all pairs of vertices.<\/p>\n<p><strong>Q4. What are some applications of the Floyd-Warshall algorithm?<\/strong><br \/>\nThe Floyd-Warshall algorithm finds utility in various applications, including the computation of Pathfinder networks, inversion of real matrices, determination of transitive closure in directed graphs, identification of bipartite graphs, and finding the shortest path in a graph.<\/p>\n<p><strong>Q5. Can the Floyd-Warshall algorithm handle weighted graphs where each edge has different weights?<\/strong><br \/>\nYes, the Floyd-Warshall algorithm can handle weighted graphs with varying edge weights. It considers the weights assigned to each edge while calculating the shortest path between vertices. The algorithm constructs a matrix that stores the distances between all pairs of vertices, taking into account the individual weights of the edges.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Floyd Warshall algorithm in C is a dynamic programming approach used to find the shortest path between all pairs of vertices in a weighted graph. It is applicable to both directed and undirected graphs, with the exception of graphs that contain negative weight cycles. This algorithm proves to be the best choice when searching [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[126],"tags":[],"class_list":["post-12429","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Floyd Warshall Algorithm<\/title>\n<meta name=\"description\" content=\"Floyd-Warshall Algorithm is an algorithm which follows dynamic programming approach to find the shortest path between all the pairs of vertices in a weighted graph.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Floyd Warshall Algorithm\" \/>\n<meta property=\"og:description\" content=\"Floyd-Warshall Algorithm is an algorithm which follows dynamic programming approach to find the shortest path between all the pairs of vertices in a weighted graph.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-02T10:49:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-23T10:17:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232013-Floyd%20Warshall%20Algorithm.jpg\" \/>\n<meta name=\"author\" content=\"Neeraj\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Neeraj\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/\"},\"author\":{\"name\":\"Neeraj\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/733e021a73de3de59e28090347670998\"},\"headline\":\"Floyd Warshall Algorithm\",\"datePublished\":\"2023-02-02T10:49:09+00:00\",\"dateModified\":\"2023-06-23T10:17:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/\"},\"wordCount\":1616,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232013-Floyd%20Warshall%20Algorithm.jpg\",\"articleSection\":[\"Dynamic programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/\",\"name\":\"Floyd Warshall Algorithm\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232013-Floyd%20Warshall%20Algorithm.jpg\",\"datePublished\":\"2023-02-02T10:49:09+00:00\",\"dateModified\":\"2023-06-23T10:17:17+00:00\",\"description\":\"Floyd-Warshall Algorithm is an algorithm which follows dynamic programming approach to find the shortest path between all the pairs of vertices in a weighted graph.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232013-Floyd%20Warshall%20Algorithm.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232013-Floyd%20Warshall%20Algorithm.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Dynamic programming\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/dynamic-programming\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Floyd Warshall Algorithm\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/733e021a73de3de59e28090347670998\",\"name\":\"Neeraj\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/aad77ffe101d901c52dba56eac1e19c1b4b6a81dfaad55c1a1350bbd36c6edba?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/aad77ffe101d901c52dba56eac1e19c1b4b6a81dfaad55c1a1350bbd36c6edba?s=96&d=mm&r=g\",\"caption\":\"Neeraj\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/neeraj\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Floyd Warshall Algorithm","description":"Floyd-Warshall Algorithm is an algorithm which follows dynamic programming approach to find the shortest path between all the pairs of vertices in a weighted graph.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/","og_locale":"en_US","og_type":"article","og_title":"Floyd Warshall Algorithm","og_description":"Floyd-Warshall Algorithm is an algorithm which follows dynamic programming approach to find the shortest path between all the pairs of vertices in a weighted graph.","og_url":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-02-02T10:49:09+00:00","article_modified_time":"2023-06-23T10:17:17+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232013-Floyd%20Warshall%20Algorithm.jpg","type":"","width":"","height":""}],"author":"Neeraj","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Neeraj","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/"},"author":{"name":"Neeraj","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/733e021a73de3de59e28090347670998"},"headline":"Floyd Warshall Algorithm","datePublished":"2023-02-02T10:49:09+00:00","dateModified":"2023-06-23T10:17:17+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/"},"wordCount":1616,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232013-Floyd%20Warshall%20Algorithm.jpg","articleSection":["Dynamic programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/","url":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/","name":"Floyd Warshall Algorithm","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232013-Floyd%20Warshall%20Algorithm.jpg","datePublished":"2023-02-02T10:49:09+00:00","dateModified":"2023-06-23T10:17:17+00:00","description":"Floyd-Warshall Algorithm is an algorithm which follows dynamic programming approach to find the shortest path between all the pairs of vertices in a weighted graph.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232013-Floyd%20Warshall%20Algorithm.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1675334232013-Floyd%20Warshall%20Algorithm.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/floyd-warshall-algorithm\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Dynamic programming","item":"https:\/\/prepbytes.com\/blog\/category\/dynamic-programming\/"},{"@type":"ListItem","position":3,"name":"Floyd Warshall Algorithm"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/733e021a73de3de59e28090347670998","name":"Neeraj","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/aad77ffe101d901c52dba56eac1e19c1b4b6a81dfaad55c1a1350bbd36c6edba?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/aad77ffe101d901c52dba56eac1e19c1b4b6a81dfaad55c1a1350bbd36c6edba?s=96&d=mm&r=g","caption":"Neeraj"},"url":"https:\/\/prepbytes.com\/blog\/author\/neeraj\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/12429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=12429"}],"version-history":[{"count":4,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/12429\/revisions"}],"predecessor-version":[{"id":16859,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/12429\/revisions\/16859"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=12429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=12429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=12429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}