{"id":2798,"date":"2025-06-06T13:39:30","date_gmt":"2025-06-06T13:39:30","guid":{"rendered":"https:\/\/diznr.com\/?p=2798"},"modified":"2025-06-06T13:39:30","modified_gmt":"2025-06-06T13:39:30","slug":"all-pair-shortest-path-floyd-warshall-algorithm-with-example-practical","status":"publish","type":"post","link":"https:\/\/www.reilsolar.com\/pdf\/all-pair-shortest-path-floyd-warshall-algorithm-with-example-practical\/","title":{"rendered":"All Pair Shortest Path Floyd-Warshall Algorithm With Practical Example"},"content":{"rendered":"<p>All Pair Shortest Path Floyd-Warshall Algorithm With Practical Example<\/p>\n<p>[fvplayer id=&#8221;118&#8243;]<\/p>\n<h3 data-start=\"0\" data-end=\"57\"><strong data-start=\"2\" data-end=\"55\">All-Pairs Shortest Path: Floyd-Warshall Algorithm<\/strong><\/h3>\n<p data-start=\"59\" data-end=\"338\">The <strong data-start=\"63\" data-end=\"91\">Floyd-Warshall algorithm<\/strong> is a <strong data-start=\"97\" data-end=\"120\">dynamic programming<\/strong> technique used to find the shortest paths between <strong data-start=\"171\" data-end=\"196\">all pairs of vertices<\/strong> in a weighted graph. It works for both <strong data-start=\"236\" data-end=\"248\">directed<\/strong> and <strong data-start=\"253\" data-end=\"267\">undirected<\/strong> graphs with <strong data-start=\"280\" data-end=\"312\">positive or negative weights<\/strong> (but no negative cycles).<\/p>\n<h3 data-start=\"345\" data-end=\"367\"><strong data-start=\"348\" data-end=\"367\">\u00a0Key Concepts<\/strong><\/h3>\n<ul data-start=\"368\" data-end=\"773\">\n<li data-start=\"368\" data-end=\"486\">The algorithm repeatedly updates the shortest path between every pair of vertices by considering intermediate nodes.<\/li>\n<li data-start=\"487\" data-end=\"600\">It uses a <strong data-start=\"499\" data-end=\"518\">distance matrix<\/strong> where each cell <code data-start=\"535\" data-end=\"543\">(i, j)<\/code> represents the shortest distance from vertex <code data-start=\"589\" data-end=\"592\">i<\/code> to <code data-start=\"596\" data-end=\"599\">j<\/code>.<\/li>\n<li data-start=\"601\" data-end=\"773\">The algorithm follows the principle: <span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\">dist[i][j]=min\u2061(dist[i][j],dist[i][k]+dist[k][j])\\text{dist}[i][j] = \\min(\\text{dist}[i][j], \\text{dist}[i][k] + \\text{dist}[k][j])<\/span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"mord text\"><span class=\"mord\">dist<\/span><\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">i<\/span><span class=\"mclose\">]<\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">j<\/span><span class=\"mclose\">]<\/span><span class=\"mrel\">=<\/span><\/span><span class=\"base\"><span class=\"mop\">min<\/span><span class=\"mopen\">(<\/span><span class=\"mord text\"><span class=\"mord\">dist<\/span><\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">i<\/span><span class=\"mclose\">]<\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">j<\/span><span class=\"mclose\">]<\/span><span class=\"mpunct\">,<\/span><span class=\"mord text\"><span class=\"mord\">dist<\/span><\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">i<\/span><span class=\"mclose\">]<\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">k<\/span><span class=\"mclose\">]<\/span><span class=\"mbin\">+<\/span><\/span><span class=\"base\"><span class=\"mord text\"><span class=\"mord\">dist<\/span><\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">k<\/span><span class=\"mclose\">]<\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">j<\/span><span class=\"mclose\">])<\/span><\/span><\/span><\/span><\/span> where <code data-start=\"745\" data-end=\"748\">k<\/code> is an intermediate node.<\/li>\n<\/ul>\n<h3 data-start=\"780\" data-end=\"805\"><strong data-start=\"783\" data-end=\"805\">\u00a0Algorithm Steps<\/strong><\/h3>\n<p data-start=\"806\" data-end=\"847\"><strong data-start=\"810\" data-end=\"844\">Initialize the distance matrix<\/strong>:<\/p>\n<ul data-start=\"851\" data-end=\"1032\">\n<li data-start=\"851\" data-end=\"933\">If there is a direct edge between <code data-start=\"887\" data-end=\"890\">i<\/code> and <code data-start=\"895\" data-end=\"898\">j<\/code>, set <code data-start=\"904\" data-end=\"916\">dist[i][j]<\/code> to its weight.<\/li>\n<li data-start=\"937\" data-end=\"975\">If <code data-start=\"942\" data-end=\"950\">i == j<\/code>, set <code data-start=\"956\" data-end=\"972\">dist[i][j] = 0<\/code>.<\/li>\n<li data-start=\"979\" data-end=\"1032\">If there is no direct edge, set <code data-start=\"1013\" data-end=\"1029\">dist[i][j] = \u221e<\/code>.<\/li>\n<\/ul>\n<p data-start=\"1034\" data-end=\"1095\"><strong data-start=\"1038\" data-end=\"1092\">Iterate through all nodes as intermediate vertices<\/strong>:<\/p>\n<ul data-start=\"1099\" data-end=\"1350\">\n<li data-start=\"1099\" data-end=\"1167\">For each vertex <code data-start=\"1117\" data-end=\"1120\">k<\/code>, update <code data-start=\"1129\" data-end=\"1141\">dist[i][j]<\/code> for all pairs <code data-start=\"1156\" data-end=\"1164\">(i, j)<\/code>.<\/li>\n<li data-start=\"1171\" data-end=\"1297\">Use the formula: <span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\">dist[i][j]=min\u2061(dist[i][j],dist[i][k]+dist[k][j])\\text{dist}[i][j] = \\min(\\text{dist}[i][j], \\text{dist}[i][k] + \\text{dist}[k][j])<\/span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"mord text\"><span class=\"mord\">dist<\/span><\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">i<\/span><span class=\"mclose\">]<\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">j<\/span><span class=\"mclose\">]<\/span><span class=\"mrel\">=<\/span><\/span><span class=\"base\"><span class=\"mop\">min<\/span><span class=\"mopen\">(<\/span><span class=\"mord text\"><span class=\"mord\">dist<\/span><\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">i<\/span><span class=\"mclose\">]<\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">j<\/span><span class=\"mclose\">]<\/span><span class=\"mpunct\">,<\/span><span class=\"mord text\"><span class=\"mord\">dist<\/span><\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">i<\/span><span class=\"mclose\">]<\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">k<\/span><span class=\"mclose\">]<\/span><span class=\"mbin\">+<\/span><\/span><span class=\"base\"><span class=\"mord text\"><span class=\"mord\">dist<\/span><\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">k<\/span><span class=\"mclose\">]<\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">j<\/span><span class=\"mclose\">])<\/span><\/span><\/span><\/span><\/span><\/li>\n<li data-start=\"1301\" data-end=\"1350\">This checks if a shorter path exists via <code data-start=\"1344\" data-end=\"1347\">k<\/code>.<\/li>\n<\/ul>\n<p data-start=\"1352\" data-end=\"1442\"><strong data-start=\"1356\" data-end=\"1380\">After all iterations<\/strong>, <code data-start=\"1382\" data-end=\"1394\">dist[i][j]<\/code> will contain the shortest path from <code data-start=\"1431\" data-end=\"1434\">i<\/code> to <code data-start=\"1438\" data-end=\"1441\">j<\/code>.<\/p>\n<h3 data-start=\"1449\" data-end=\"1476\"><strong data-start=\"1452\" data-end=\"1476\">\u00a0Practical Example<\/strong><\/h3>\n<h3 data-start=\"1478\" data-end=\"1533\"><strong data-start=\"1482\" data-end=\"1533\">Example Graph (Adjacency Matrix Representation)<\/strong><\/h3>\n<p data-start=\"1534\" data-end=\"1592\">We have a directed graph with <strong data-start=\"1564\" data-end=\"1591\">4 vertices (A, B, C, D)<\/strong>:<\/p>\n<table data-start=\"1594\" data-end=\"1781\">\n<thead data-start=\"1594\" data-end=\"1627\">\n<tr data-start=\"1594\" data-end=\"1627\">\n<th data-start=\"1594\" data-end=\"1606\">From \u2192 To<\/th>\n<th data-start=\"1606\" data-end=\"1611\">A<\/th>\n<th data-start=\"1611\" data-end=\"1616\">B<\/th>\n<th data-start=\"1616\" data-end=\"1621\">C<\/th>\n<th data-start=\"1621\" data-end=\"1627\">D<\/th>\n<\/tr>\n<\/thead>\n<tbody data-start=\"1662\" data-end=\"1781\">\n<tr data-start=\"1662\" data-end=\"1691\">\n<td><strong data-start=\"1664\" data-end=\"1669\">A<\/strong><\/td>\n<td>0<\/td>\n<td>3<\/td>\n<td>\u221e<\/td>\n<td>7<\/td>\n<\/tr>\n<tr data-start=\"1692\" data-end=\"1721\">\n<td><strong data-start=\"1694\" data-end=\"1699\">B<\/strong><\/td>\n<td>8<\/td>\n<td>0<\/td>\n<td>2<\/td>\n<td>\u221e<\/td>\n<\/tr>\n<tr data-start=\"1722\" data-end=\"1751\">\n<td><strong data-start=\"1724\" data-end=\"1729\">C<\/strong><\/td>\n<td>5<\/td>\n<td>\u221e<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<\/tr>\n<tr data-start=\"1752\" data-end=\"1781\">\n<td><strong data-start=\"1754\" data-end=\"1759\">D<\/strong><\/td>\n<td>2<\/td>\n<td>\u221e<\/td>\n<td>\u221e<\/td>\n<td>0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p data-start=\"1783\" data-end=\"1830\">(\u221e represents no direct edge between the nodes)<\/p>\n<h3 data-start=\"1837\" data-end=\"1870\"><strong data-start=\"1841\" data-end=\"1870\">\u00a0Step-by-Step Execution<\/strong><\/h3>\n<h4 data-start=\"1872\" data-end=\"1904\"><strong data-start=\"1877\" data-end=\"1904\">Initial Distance Matrix<\/strong><\/h4>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre\">      <span class=\"hljs-selector-tag\">A<\/span>   <span class=\"hljs-selector-tag\">B<\/span>   C   D<br \/>\n<span class=\"hljs-selector-tag\">A<\/span>  <span class=\"hljs-selector-attr\">[  0   3   \u221e   7  ]<\/span><br \/>\n<span class=\"hljs-selector-tag\">B<\/span>  <span class=\"hljs-selector-attr\">[  8   0   2   \u221e  ]<\/span><br \/>\nC  <span class=\"hljs-selector-attr\">[  5   \u221e   0   1  ]<\/span><br \/>\nD  <span class=\"hljs-selector-attr\">[  2   \u221e   \u221e   0  ]<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<h4 data-start=\"2026\" data-end=\"2066\"><strong data-start=\"2031\" data-end=\"2066\">Step 1: Consider Node A (k = 0)<\/strong><\/h4>\n<ul data-start=\"2067\" data-end=\"2099\">\n<li data-start=\"2067\" data-end=\"2099\">No improvement in paths via A.<\/li>\n<\/ul>\n<h4 data-start=\"2101\" data-end=\"2141\"><strong data-start=\"2106\" data-end=\"2141\">Step 2: Consider Node B (k = 1)<\/strong><\/h4>\n<ul data-start=\"2142\" data-end=\"2209\">\n<li data-start=\"2142\" data-end=\"2191\">Path C \u2192 B via A: <code data-start=\"2162\" data-end=\"2173\">5 + 3 = 8<\/code>, shorter than <code data-start=\"2188\" data-end=\"2191\">\u221e<\/code><\/li>\n<li data-start=\"2192\" data-end=\"2209\">Updated matrix:<\/li>\n<\/ul>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre\">      <span class=\"hljs-selector-tag\">A<\/span>   <span class=\"hljs-selector-tag\">B<\/span>   C   D<br \/>\n<span class=\"hljs-selector-tag\">A<\/span>  <span class=\"hljs-selector-attr\">[  0   3   \u221e   7  ]<\/span><br \/>\n<span class=\"hljs-selector-tag\">B<\/span>  <span class=\"hljs-selector-attr\">[  8   0   2   \u221e  ]<\/span><br \/>\nC  <span class=\"hljs-selector-attr\">[  5   8   0   1  ]<\/span><br \/>\nD  <span class=\"hljs-selector-attr\">[  2   \u221e   \u221e   0  ]<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<h4 data-start=\"2331\" data-end=\"2371\"><strong data-start=\"2336\" data-end=\"2371\">Step 3: Consider Node C (k = 2)<\/strong><\/h4>\n<ul data-start=\"2372\" data-end=\"2439\">\n<li data-start=\"2372\" data-end=\"2421\">Path B \u2192 D via C: <code data-start=\"2392\" data-end=\"2403\">2 + 1 = 3<\/code>, shorter than <code data-start=\"2418\" data-end=\"2421\">\u221e<\/code><\/li>\n<li data-start=\"2422\" data-end=\"2439\">Updated matrix:<\/li>\n<\/ul>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre\">      <span class=\"hljs-selector-tag\">A<\/span>   <span class=\"hljs-selector-tag\">B<\/span>   C   D<br \/>\n<span class=\"hljs-selector-tag\">A<\/span>  <span class=\"hljs-selector-attr\">[  0   3   5   7  ]<\/span><br \/>\n<span class=\"hljs-selector-tag\">B<\/span>  <span class=\"hljs-selector-attr\">[  8   0   2   3  ]<\/span><br \/>\nC  <span class=\"hljs-selector-attr\">[  5   8   0   1  ]<\/span><br \/>\nD  <span class=\"hljs-selector-attr\">[  2   \u221e   \u221e   0  ]<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<h4 data-start=\"2561\" data-end=\"2601\"><strong data-start=\"2566\" data-end=\"2601\">Step 4: Consider Node D (k = 3)<\/strong><\/h4>\n<ul data-start=\"2602\" data-end=\"2719\">\n<li data-start=\"2602\" data-end=\"2651\">Path A \u2192 C via D: <code data-start=\"2622\" data-end=\"2633\">7 + 1 = 6<\/code>, shorter than <code data-start=\"2648\" data-end=\"2651\">\u221e<\/code><\/li>\n<li data-start=\"2652\" data-end=\"2701\">Path B \u2192 C via D: <code data-start=\"2672\" data-end=\"2683\">3 + 1 = 4<\/code>, shorter than <code data-start=\"2698\" data-end=\"2701\">2<\/code><\/li>\n<li data-start=\"2702\" data-end=\"2719\">Updated matrix:<\/li>\n<\/ul>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre\">      <span class=\"hljs-selector-tag\">A<\/span>   <span class=\"hljs-selector-tag\">B<\/span>   C   D<br \/>\n<span class=\"hljs-selector-tag\">A<\/span>  <span class=\"hljs-selector-attr\">[  0   3   5   6  ]<\/span><br \/>\n<span class=\"hljs-selector-tag\">B<\/span>  <span class=\"hljs-selector-attr\">[  5   0   2   3  ]<\/span><br \/>\nC  <span class=\"hljs-selector-attr\">[  5   8   0   1  ]<\/span><br \/>\nD  <span class=\"hljs-selector-attr\">[  2   5   7   0  ]<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p data-start=\"2841\" data-end=\"2912\"><strong data-start=\"2843\" data-end=\"2912\">Final matrix gives the shortest path between every pair of nodes.<\/strong><\/p>\n<h3 data-start=\"2919\" data-end=\"2944\"><strong data-start=\"2922\" data-end=\"2944\">\u00a0Time Complexity<\/strong><\/h3>\n<p data-start=\"2945\" data-end=\"3045\">The Floyd-Warshall algorithm runs in <strong data-start=\"2982\" data-end=\"2991\">O(V\u00b3)<\/strong> time complexity, where <code data-start=\"3015\" data-end=\"3018\">V<\/code> is the number of vertices.<\/p>\n<h3 data-start=\"3052\" data-end=\"3072\"><strong data-start=\"3055\" data-end=\"3072\">\u00a0Advantages<\/strong><\/h3>\n<p data-start=\"3073\" data-end=\"3230\"><strong data-start=\"3075\" data-end=\"3107\">Simple and easy to implement<\/strong><br data-start=\"3107\" data-end=\"3110\" \/><strong data-start=\"3112\" data-end=\"3168\">Works with negative weights (but no negative cycles)<\/strong><br data-start=\"3168\" data-end=\"3171\" \/><strong data-start=\"3173\" data-end=\"3228\">Finds shortest paths for all pairs in one execution<\/strong><\/p>\n<h3 data-start=\"3232\" data-end=\"3255\"><strong data-start=\"3235\" data-end=\"3255\">\u00a0Disadvantages<\/strong><\/h3>\n<p data-start=\"3256\" data-end=\"3383\"><strong data-start=\"3258\" data-end=\"3314\">Does not work with graphs containing negative cycles<\/strong><br data-start=\"3314\" data-end=\"3317\" \/><strong data-start=\"3319\" data-end=\"3381\">Less efficient than Dijkstra\u2019s Algorithm for sparse graphs<\/strong><\/p>\n<h3 data-start=\"3390\" data-end=\"3421\"><strong data-start=\"3393\" data-end=\"3421\">Python Implementation<\/strong><\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\">\n<p><code class=\"!whitespace-pre language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np<\/code><\/p>\n<p><span class=\"hljs-comment\"># Number of vertices<\/span><br \/>\nV = <span class=\"hljs-number\">4<\/span><br \/>\nINF = <span class=\"hljs-built_in\">float<\/span>(<span class=\"hljs-string\">&#8216;inf&#8217;<\/span>)<\/p>\n<p><span class=\"hljs-comment\"># Adjacency matrix representation of the graph<\/span><br \/>\ngraph = [[<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">3<\/span>, INF, <span class=\"hljs-number\">7<\/span>],<br \/>\n[<span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">2<\/span>, INF],<br \/>\n[<span class=\"hljs-number\">5<\/span>, INF, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>],<br \/>\n[<span class=\"hljs-number\">2<\/span>, INF, INF, <span class=\"hljs-number\">0<\/span>]]<\/p>\n<p><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title function_\">floyd_warshall<\/span>(<span class=\"hljs-params\">graph<\/span>):<br \/>\ndist = np.array(graph) <span class=\"hljs-comment\"># Copy graph into dist matrix<\/span><\/p>\n<p><span class=\"hljs-keyword\">for<\/span> k <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-built_in\">range<\/span>(V): <span class=\"hljs-comment\"># Intermediate vertices<\/span><br \/>\n<span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-built_in\">range<\/span>(V): <span class=\"hljs-comment\"># Source vertex<\/span><br \/>\n<span class=\"hljs-keyword\">for<\/span> j <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-built_in\">range<\/span>(V): <span class=\"hljs-comment\"># Destination vertex<\/span><br \/>\ndist[i][j] = <span class=\"hljs-built_in\">min<\/span>(dist[i][j], dist[i][k] + dist[k][j])<\/p>\n<p><span class=\"hljs-keyword\">return<\/span> dist<\/p>\n<p><span class=\"hljs-comment\"># Compute shortest paths<\/span><br \/>\nshortest_paths = floyd_warshall(graph)<\/p>\n<p><span class=\"hljs-comment\"># Display the result<\/span><br \/>\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">&#8220;All-Pairs Shortest Path Matrix:&#8221;<\/span>)<br \/>\n<span class=\"hljs-built_in\">print<\/span>(shortest_paths)<\/p>\n<\/div>\n<\/div>\n<p data-start=\"4126\" data-end=\"4138\"><strong data-start=\"4128\" data-end=\"4138\">Output<\/strong><\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre\"><span class=\"hljs-keyword\">All<\/span><span class=\"hljs-selector-tag\">-Pairs<\/span> <span class=\"hljs-selector-tag\">Shortest<\/span> <span class=\"hljs-selector-tag\">Path<\/span> <span class=\"hljs-selector-tag\">Matrix<\/span>:<br \/>\n<span class=\"hljs-selector-attr\">[[ 0.  3.  5.  6.]<\/span><br \/>\n<span class=\"hljs-selector-attr\">[ 5.  0.  2.  3.]<\/span><br \/>\n<span class=\"hljs-selector-attr\">[ 5.  8.  0.  1.]<\/span><br \/>\n<span class=\"hljs-selector-attr\">[ 2.  5.  7.  0.]<\/span>]<br \/>\n<\/code><\/div>\n<\/div>\n<h3 data-start=\"4262\" data-end=\"4279\"><strong data-start=\"4265\" data-end=\"4279\">\u00a0Summary<\/strong><\/h3>\n<ul data-start=\"4280\" data-end=\"4622\">\n<li data-start=\"4280\" data-end=\"4378\">The <strong data-start=\"4286\" data-end=\"4314\">Floyd-Warshall Algorithm<\/strong> efficiently finds shortest paths between all pairs of vertices.<\/li>\n<li data-start=\"4379\" data-end=\"4501\">It follows the formula: <span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\">dist[i][j]=min\u2061(dist[i][j],dist[i][k]+dist[k][j])\\text{dist}[i][j] = \\min(\\text{dist}[i][j], \\text{dist}[i][k] + \\text{dist}[k][j])<\/span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"mord text\"><span class=\"mord\">dist<\/span><\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">i<\/span><span class=\"mclose\">]<\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">j<\/span><span class=\"mclose\">]<\/span><span class=\"mrel\">=<\/span><\/span><span class=\"base\"><span class=\"mop\">min<\/span><span class=\"mopen\">(<\/span><span class=\"mord text\"><span class=\"mord\">dist<\/span><\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">i<\/span><span class=\"mclose\">]<\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">j<\/span><span class=\"mclose\">]<\/span><span class=\"mpunct\">,<\/span><span class=\"mord text\"><span class=\"mord\">dist<\/span><\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">i<\/span><span class=\"mclose\">]<\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">k<\/span><span class=\"mclose\">]<\/span><span class=\"mbin\">+<\/span><\/span><span class=\"base\"><span class=\"mord text\"><span class=\"mord\">dist<\/span><\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">k<\/span><span class=\"mclose\">]<\/span><span class=\"mopen\">[<\/span><span class=\"mord mathnormal\">j<\/span><span class=\"mclose\">])<\/span><\/span><\/span><\/span><\/span><\/li>\n<li data-start=\"4502\" data-end=\"4534\"><strong data-start=\"4504\" data-end=\"4523\">Time Complexity<\/strong>: <strong data-start=\"4525\" data-end=\"4534\">O(V\u00b3)<\/strong><\/li>\n<li data-start=\"4535\" data-end=\"4622\"><strong data-start=\"4537\" data-end=\"4549\">Best for<\/strong>: <strong data-start=\"4551\" data-end=\"4621\">Dense graphs and graphs with negative weights (no negative cycles)<\/strong>.<\/li>\n<\/ul>\n<p data-start=\"4624\" data-end=\"4699\" data-is-last-node=\"\" data-is-only-node=\"\">Would you like a <strong data-start=\"4641\" data-end=\"4663\">real-world example<\/strong> or a <strong data-start=\"4669\" data-end=\"4693\">C\/C++ implementation<\/strong>?<\/p>\n<h3 data-start=\"4624\" data-end=\"4699\"><a href=\"https:\/\/home.cse.ust.hk\/~dekai\/271\/notes\/L13\/L13.pdf\" target=\"_blank\" rel=\"noopener\">All Pair Shortest Path Floyd-Warshall Algorithm With Practical Example<\/a><\/h3>\n<h3 class=\"LC20lb MBeuO DKV0Md\"><a href=\"https:\/\/ijisrt.com\/wp-content\/uploads\/2018\/03\/Implementation-Floyd-Warshall-Algorithm.pdf\" target=\"_blank\" rel=\"noopener\">Implementation Floyd-Warshall Algorithm for the Shortest &#8230;<\/a><\/h3>\n<h3 class=\"LC20lb MBeuO DKV0Md\">All-pair shortest Path (Floyd-Warshall Algorithm)<\/h3>\n<h3 class=\"LC20lb MBeuO DKV0Md\"><a href=\"http:\/\/www.cs.bilkent.edu.tr\/~ugur\/teaching\/cs502\/material\/cs502_8_APSP.pdf\" target=\"_blank\" rel=\"noopener\">Algorithms II, CS 502 &#8211; All-Pairs Shortest Paths<\/a><\/h3>\n<h3 class=\"LC20lb MBeuO DKV0Md\"><a href=\"https:\/\/www.cs.hunter.cuny.edu\/~sweiss\/course_materials\/csci493.65\/lecture_notes\/chapter05.pdf\" target=\"_blank\" rel=\"noopener\">Chapter 5: Floyd&#8217;s Algorithm<\/a><\/h3>\n<p>The <strong>Floyd-Warshall Algorithm<\/strong> is a classic <strong>dynamic programming<\/strong> algorithm used to find the <strong>shortest paths between all pairs of vertices<\/strong> in a weighted graph.<\/p>\n<p>It works for both <strong>directed<\/strong> and <strong>undirected<\/strong> graphs and can handle <strong>negative edge weights<\/strong> (but <strong>no negative cycles<\/strong>).<\/p>\n<hr \/>\n<h2>\ud83d\udd0d <strong>Key Concept:<\/strong><\/h2>\n<p>The Floyd-Warshall Algorithm updates a distance matrix <code>dist[i][j]<\/code>, representing the shortest distance from node <code>i<\/code> to node <code>j<\/code>, by checking if an <strong>intermediate vertex <code>k<\/code><\/strong> provides a shorter path.<\/p>\n<hr \/>\n<h2>\ud83e\udde0 <strong>Algorithm Steps:<\/strong><\/h2>\n<p>Given a graph with <code>V<\/code> vertices:<\/p>\n<ol>\n<li>Initialize the distance matrix:\n<ul>\n<li><code>dist[i][j] = weight of edge (i \u2192 j)<\/code><\/li>\n<li>If no direct edge exists: <code>dist[i][j] = \u221e<\/code><\/li>\n<li><code>dist[i][i] = 0<\/code><\/li>\n<\/ul>\n<\/li>\n<li>For each vertex <code>k<\/code> from <code>0<\/code> to <code>V-1<\/code>:\n<pre><code class=\"language-python\">for k in range(V):\n    for i in range(V):\n        for j in range(V):\n            if dist[i][j] &gt; dist[i][k] + dist[k][j]:\n                dist[i][j] = dist[i][k] + dist[k][j]\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr \/>\n<h2>\ud83e\uddea <strong>Practical Example<\/strong><\/h2>\n<h3>\ud83c\udfaf Given Graph:<\/h3>\n<p>Let&#8217;s take a graph with 4 nodes: A, B, C, D<br \/>\nLet the initial weighted adjacency matrix be:<\/p>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th>A<\/th>\n<th>B<\/th>\n<th>C<\/th>\n<th>D<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>A<\/td>\n<td>0<\/td>\n<td>3<\/td>\n<td>\u221e<\/td>\n<td>7<\/td>\n<\/tr>\n<tr>\n<td>B<\/td>\n<td>8<\/td>\n<td>0<\/td>\n<td>2<\/td>\n<td>\u221e<\/td>\n<\/tr>\n<tr>\n<td>C<\/td>\n<td>5<\/td>\n<td>\u221e<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>D<\/td>\n<td>2<\/td>\n<td>\u221e<\/td>\n<td>\u221e<\/td>\n<td>0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>We use \u221e (infinity) to represent that there is <strong>no direct path<\/strong>.<\/p>\n<hr \/>\n<h3>\ud83e\uddee Step-by-Step Floyd-Warshall Execution:<\/h3>\n<p>We iteratively update the matrix using each node as an intermediate:<\/p>\n<h4>Initial Matrix:<\/h4>\n<pre><code>0   3   \u221e   7\n8   0   2   \u221e\n5   \u221e   0   1\n2   \u221e   \u221e   0\n<\/code><\/pre>\n<h4>After using node A as intermediate (k=0):<\/h4>\n<pre><code>0   3   \u221e   7\n8   0   2   15\n5   8   0   1\n2   5   \u221e   0\n<\/code><\/pre>\n<h4>After using node B as intermediate (k=1):<\/h4>\n<pre><code>0   3   5   7\n8   0   2   15\n5   8   0   1\n2   5   7   0\n<\/code><\/pre>\n<h4>After using node C as intermediate (k=2):<\/h4>\n<pre><code>0   3   5   6\n8   0   2   3\n5   8   0   1\n2   5   7   0\n<\/code><\/pre>\n<h4>After using node D as intermediate (k=3):<\/h4>\n<p>No further updates \u2014 final matrix remains:<\/p>\n<pre><code>0   3   5   6\n8   0   2   3\n5   8   0   1\n2   5   7   0\n<\/code><\/pre>\n<hr \/>\n<h2>\u2705 Final Output (Shortest Paths Matrix):<\/h2>\n<table>\n<thead>\n<tr>\n<th>From\/To<\/th>\n<th>A<\/th>\n<th>B<\/th>\n<th>C<\/th>\n<th>D<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>A<\/strong><\/td>\n<td>0<\/td>\n<td>3<\/td>\n<td>5<\/td>\n<td>6<\/td>\n<\/tr>\n<tr>\n<td><strong>B<\/strong><\/td>\n<td>8<\/td>\n<td>0<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td><strong>C<\/strong><\/td>\n<td>5<\/td>\n<td>8<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td><strong>D<\/strong><\/td>\n<td>2<\/td>\n<td>5<\/td>\n<td>7<\/td>\n<td>0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2>\u23f1\ufe0f Time Complexity:<\/h2>\n<ul>\n<li><strong>O(V\u00b3)<\/strong> \u2192 efficient for small or medium-sized graphs.<\/li>\n<li>Not suitable for large sparse graphs; use <strong>Dijkstra<\/strong> or <strong>Bellman-Ford<\/strong> instead.<\/li>\n<\/ul>\n<hr \/>\n<h2>\u2757 When to Use:<\/h2>\n<ul>\n<li>Graph has <strong>negative edges<\/strong> but <strong>no negative cycles<\/strong><\/li>\n<li>You want <strong>shortest path between all pairs<\/strong><\/li>\n<li>Works well for <strong>dense graphs<\/strong> (many edges)<\/li>\n<\/ul>\n<hr \/>\n<p>Would you like a Python implementation or visualization for better understanding?<\/p>\n<h3><a href=\"https:\/\/webpages.charlotte.edu\/rbunescu\/courses\/ou\/cs4040\/lecture20.pdf\" target=\"_blank\" rel=\"noopener\">All Pair Shortest Path Floyd-Warshall Algorithm With Practical Example<\/a><\/h3>\n<h3 class=\"LC20lb MBeuO DKV0Md\"><a href=\"https:\/\/www.cs.toronto.edu\/~lalla\/373s16\/notes\/APSP.pdf\" target=\"_blank\" rel=\"noopener\">DP: All Pairs Shortest Paths, The Floyd-Warshall Algorithm<\/a><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>All Pair Shortest Path Floyd-Warshall Algorithm With Practical Example [fvplayer id=&#8221;118&#8243;] All-Pairs Shortest Path: Floyd-Warshall Algorithm The Floyd-Warshall algorithm is a dynamic programming technique used to find the shortest paths between all pairs of vertices in a weighted graph. It works for both directed and undirected graphs with positive or negative weights (but no negative [&hellip;]<\/p>\n","protected":false},"author":71,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-2798","post","type-post","status-publish","format-standard","hentry","category-algorithm-analysis-and-design"],"_links":{"self":[{"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/posts\/2798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/users\/71"}],"replies":[{"embeddable":true,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/comments?post=2798"}],"version-history":[{"count":0,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/posts\/2798\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/media?parent=2798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/categories?post=2798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/tags?post=2798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}