{"id":396,"date":"2025-06-26T22:26:42","date_gmt":"2025-06-26T16:56:42","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=396"},"modified":"2025-06-26T22:27:28","modified_gmt":"2025-06-26T16:57:28","slug":"bellman-ford-algorithm","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/bellman-ford-algorithm\/","title":{"rendered":"Bellman Ford Algorithm | Distance from the Source Explained Step-by-Step"},"content":{"rendered":"\n<p>Bellman Ford Algorithm made easy: learn how to find shortest paths (and detect negatives) with clear intuition, commented Python code, dry run, and Big-O.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/distance-from-the-source-bellman-ford-algorithm\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-89de2c62-6175-407a-895c-6726ee42986d\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bellman-ford-algorithm\/#0-1-what-does-the-problem-ask\" style=\"\">1. What does the problem ask?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bellman-ford-algorithm\/#1-2-mini-example\" style=\"\">2. Mini example<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bellman-ford-algorithm\/#2-3-why-use-the-bellman-ford-algorithm-\" style=\"\">3. Why use the Bellman Ford Algorithm?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bellman-ford-algorithm\/#3-4-python-code\" style=\"\">4. Python code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bellman-ford-algorithm\/#4-5-step-by-step-explanation\" style=\"\">5. Step-by-step explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bellman-ford-algorithm\/#5-6-dry-run-tiny-graph\" style=\"\">6. Dry run (tiny graph)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bellman-ford-algorithm\/#6-7-complexity\" style=\"\">7. Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bellman-ford-algorithm\/#7-8-conclusion\" style=\"\">8. Conclusion<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-1-what-does-the-problem-ask\">1. What does the problem ask?<\/h2>\n\n\n\n<p>Given:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>V<\/code> vertices (<code>0 \u2026 V-1<\/code>)<\/li>\n\n\n\n<li>a list of <strong>directed edges<\/strong> <code>[u, v, w]<\/code> (<code>w<\/code> may be negative)<\/li>\n\n\n\n<li>a <strong>source<\/strong> vertex <code>src<\/code><\/li>\n<\/ul>\n\n\n\n<p>Return an array <code>dist<\/code> where <code>dist[i]<\/code> is the <strong>shortest distance<\/strong> from <code>src<\/code> to <code>i<\/code>.<br>If the graph has a <strong>negative-weight cycle<\/strong> reachable from <code>src<\/code>, return <code>[-1]<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-2-mini-example\">2. Mini example<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"V = 5\nedges = [\n  [0,1,6], [0,2,7], [1,2,8], [1,3,5],\n  [1,4,-4], [2,3,-3], [2,4,9], [3,1,-2],\n  [4,3,7],  [4,0,2]\n]\nsrc = 0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">V = <\/span><span style=\"color: #B5CEA8\">5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">edges = [<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  [<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\">], [<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">], [<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">8<\/span><span style=\"color: #D4D4D4\">], [<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">],<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  [<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">,-<\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">], [<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">,-<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">], [<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">9<\/span><span style=\"color: #D4D4D4\">], [<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">,-<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">],<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  [<\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">],  [<\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">src = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Shortest distances = <code>[0, 2, 7, 4, -2]<\/code><br>(No negative cycle exists.)<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-3-why-use-the-bellman-ford-algorithm-\">3. Why use the <strong>Bellman Ford Algorithm<\/strong>?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Handles <strong>negative weights<\/strong> safely.<\/li>\n\n\n\n<li>Detects negative cycles.<\/li>\n\n\n\n<li>Works in <code>O(V \u00d7 E)<\/code> time \u2013 slower than Dijkstra but more general.<\/li>\n<\/ul>\n\n\n\n<p>Idea:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Relax<\/strong> every edge <strong><code>V \u2013 1<\/code> times<\/strong>.<br>After these rounds, all shortest paths (which have at most <code>V-1<\/code> edges) are fixed.<\/li>\n\n\n\n<li><strong>One more scan<\/strong> \u2013 if any edge can still relax, a negative cycle is reachable.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-4-python-code\">4. Python code<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def bellmanFord(self, V, edges, src):\n        INF = 10**8                       # a very large number\n        dist = [INF for _ in range(V)]\n        dist[src] = 0                     # distance to source is zero\n\n        # repeat edge relaxation V-1 times\n        for _ in range(V - 1):\n            for u, v, w in edges:\n                if dist[u] != INF and dist[u] + w &lt; dist[v]:\n                    dist[v] = dist[u] + w\n\n        # check for negative-weight cycles\n        for u, v, w in edges:\n            if dist[u] != INF and dist[u] + w &lt; dist[v]:\n                return [-1]               # cycle found\n\n        return dist                       # shortest distances\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">bellmanFord<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">V<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">edges<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">src<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        INF = <\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">**<\/span><span style=\"color: #B5CEA8\">8<\/span><span style=\"color: #D4D4D4\">                       <\/span><span style=\"color: #6A9955\"># a very large number<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        dist = [INF <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> _ <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(V)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        dist[src] = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">                     <\/span><span style=\"color: #6A9955\"># distance to source is zero<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># repeat edge relaxation V-1 times<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> _ <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(V - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> u, v, w <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> edges:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> dist[u] != INF <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> dist[u] + w &lt; dist[v]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    dist[v] = dist[u] + w<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># check for negative-weight cycles<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> u, v, w <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> edges:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> dist[u] != INF <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> dist[u] + w &lt; dist[v]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> [-<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]               <\/span><span style=\"color: #6A9955\"># cycle found<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> dist                       <\/span><span style=\"color: #6A9955\"># shortest distances<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-5-step-by-step-explanation\">5. Step-by-step explanation<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>dist<\/code> array<\/strong> \u2013 start with \u201cinfinity\u201d except the source.<\/li>\n\n\n\n<li><strong>Main loop (<code>V-1<\/code> rounds)<\/strong>\n<ul class=\"wp-block-list\">\n<li>For every edge, try to go \u201cone step further\u201d.<\/li>\n\n\n\n<li>If going through <code>u<\/code> shortens <code>v<\/code>, store the better value.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Cycle test<\/strong>\n<ul class=\"wp-block-list\">\n<li>After <code>V-1<\/code> rounds, shortest paths are done.<\/li>\n\n\n\n<li>A further improvement means <strong>distance can shrink forever<\/strong> \u2192 negative cycle.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Return<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>[-1]<\/code> if a cycle exists; otherwise the <code>dist<\/code> list.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-6-dry-run-tiny-graph\">6. Dry run (tiny graph)<\/h2>\n\n\n\n<p>Edges<br><code>0\u21921 (4)<\/code>, <code>0\u21922 (5)<\/code>, <code>1\u21922 (-2)<\/code><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Round<\/th><th>Relaxations<\/th><th><code>dist<\/code><\/th><\/tr><\/thead><tbody><tr><td>Init<\/td><td>\u2013<\/td><td><code>[0, \u221e, \u221e]<\/code><\/td><\/tr><tr><td>1<\/td><td>0\u21921 = 4, 0\u21922 = 5, 1\u21922 = 2<\/td><td><code>[0, 4, 2]<\/code><\/td><\/tr><tr><td>2<\/td><td>no changes<\/td><td><code>[0, 4, 2]<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Final distances: <code>0, 4, 2<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-7-complexity\">7. Complexity<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Measure<\/th><th>Value<\/th><\/tr><\/thead><tbody><tr><td><strong>Time<\/strong><\/td><td><code>O(V \u00d7 E)<\/code><\/td><\/tr><tr><td><strong>Space<\/strong><\/td><td><code>O(V)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-8-conclusion\">8. Conclusion<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>Bellman Ford Algorithm<\/strong> is the go-to tool when edges may be negative.<\/li>\n\n\n\n<li>Relax <strong>each edge<\/strong> for <strong><code>V-1<\/code> rounds<\/strong>, then do <strong>one extra check<\/strong> for cycles.<\/li>\n\n\n\n<li>Simple loops, no fancy data structures \u2013 perfect for interviews dealing with tricky weights.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bellman Ford Algorithm made easy: learn how to find shortest paths (and detect negatives) with clear intuition, commented Python code, dry run, and Big-O. Here&#8217;s the [Problem Link] to begin with. 1. What does the problem ask? Given: Return an array dist where dist[i] is the shortest distance from src to i.If the graph has<\/p>\n","protected":false},"author":1,"featured_media":402,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[17,18],"class_list":{"0":"post-396","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-expert","9":"tag-graph","10":"tag-hard"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/bellman-ford-algorithm-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=396"}],"version-history":[{"count":3,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/396\/revisions"}],"predecessor-version":[{"id":412,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/396\/revisions\/412"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/402"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}