{"id":50180,"date":"2025-05-26T16:28:15","date_gmt":"2025-05-26T10:58:15","guid":{"rendered":"https:\/\/www.iquanta.in\/blog\/?p=50180"},"modified":"2025-05-26T16:28:18","modified_gmt":"2025-05-26T10:58:18","slug":"bellman-ford-algorithm-complete-guide-with-python-code","status":"publish","type":"post","link":"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/","title":{"rendered":"Bellman-Ford Algorithm: Complete Guide with Python Code"},"content":{"rendered":"\n<p>The Bellman-Ford Algorithm is a fundamental concept in graph theory especially it is useful when dealing with graphs that have negative weight edges. While many people are familiar with Dijkstra\u2019s Algorithm for finding the shortest path that does not work correctly when negative weights are involved. <\/p>\n\n\n\n<p>That is where Bellman-Ford Algorithm comes in. It allows you to find the shortest paths from a single starting point to all other nodes in a graph even if some of the paths include negative edge weights. Although it is not the fastest algorithm out there but it has the ability to handle more complex graphs makes it valuable in real-world scenarios like network routing or financial analysis.<\/p>\n\n\n\n<p>In this article we will break down how the algorithm works and how to compare it to Dijkstra\u2019s algorithm along with exploring its time complexity, walk through a Python implementation, and look at some practical use cases that all explained in a simple and easy manner.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_77 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#What_is_Bellman-Ford_Algorithm\" >What is Bellman-Ford Algorithm?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#Key_Features_of_Bellman-Ford_Algorithm\" >Key Features of Bellman-Ford Algorithm<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#How_Does_the_Bellman-Ford_Algorithm_Work\" >How Does the Bellman-Ford Algorithm Work?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#Bellman-Ford_vs_Dijkstra_Algorithm\" >Bellman-Ford vs Dijkstra Algorithm<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#Bellman-Ford_Algorithm_Python_Code\" >Bellman-Ford Algorithm Python Code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#Real-World_Applications_of_Bellman-Ford_Algorithm\" >Real-World Applications of Bellman-Ford Algorithm<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#Frequently_Asked_Questions_Bellman-Ford_Algorithm\" >Frequently Asked Questions (Bellman-Ford Algorithm)<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#What_is_the_Bellman-Ford_Algorithm_used_for\" >What is the Bellman-Ford Algorithm used for?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#Can_Bellman-Ford_work_with_negative_numbers\" >Can Bellman-Ford work with negative numbers?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#What_is_a_negative_weight_cycle\" >What is a negative weight cycle?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#Is_Bellman-Ford_better_than_Dijkstra\" >Is Bellman-Ford better than Dijkstra?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#Where_is_Bellman-Ford_used_in_real_life\" >Where is Bellman-Ford used in real life?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-bellman-ford-algorithm\"><span class=\"ez-toc-section\" id=\"What_is_Bellman-Ford_Algorithm\"><\/span><strong>What is Bellman-Ford Algorithm?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The Bellman-Ford Algorithm is a simple way to find the shortest path from one place (called the source node) to every other place in a network or graph. Imagine a<a href=\"https:\/\/www.iquanta.in\/blog\/graph-data-structure-its-types-and-representation\/\"> graph<\/a> as a bunch of points connected by lines. Each line has a number on it that could represent distance, time, or cost, how much it takes to travel from one point to another.<\/p>\n\n\n\n<p>Now here is what makes Bellman-Ford different from other algorithms. It can work even when some of those numbers are negative. That means if moving from one point to another actually reduces your total cost and Bellman-Ford can still handle it. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-key-features-of-bellman-ford-algorithm\"><span class=\"ez-toc-section\" id=\"Key_Features_of_Bellman-Ford_Algorithm\"><\/span><strong>Key Features of Bellman-Ford Algorithm<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ol>\n<li>Finds the shortest path from a single source to all other nodes.<\/li>\n\n\n\n<li>Works even if the graph has negative edge weights.<\/li>\n\n\n\n<li>Can detect negative weight cycles in the graph.<\/li>\n\n\n\n<li>Easier to understand and implement than some other algorithms.<\/li>\n\n\n\n<li>Slower than Dijkstra\u2019s for large graphs but more flexible.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-does-the-bellman-ford-algorithm-work\"><span class=\"ez-toc-section\" id=\"How_Does_the_Bellman-Ford_Algorithm_Work\"><\/span><strong>How Does the Bellman-Ford Algorithm Work?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The Bellman-Ford Algorithm works by finding the shortest way to reach every node from a starting node. First, it sets the distance to the starting node as 0 and to all other nodes as very large because we don not know their distances yet. Then it goes through all the connections (edges) in the graph again and again. Each time it checks if going through one node to reach another gives a shorter path. If it finds a shorter way then it will updates the distance.<\/p>\n\n\n\n<p>This process is repeated many times usually one less than the number of nodes in the graph. At the end the algorithm has the shortest distance to each node. It also checks if there is a cycle where the total distance keeps getting smaller, which would mean the graph has a negative weight cycle.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-bellman-ford-vs-dijkstra-algorithm\"><span class=\"ez-toc-section\" id=\"Bellman-Ford_vs_Dijkstra_Algorithm\"><\/span><strong>Bellman-Ford vs Dijkstra Algorithm<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong>Feature<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>Bellman-Ford Algorithm<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>Dijkstra<\/strong><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Handles Negative Weights<\/td><td class=\"has-text-align-center\" data-align=\"center\">Yes<\/td><td class=\"has-text-align-center\" data-align=\"center\">No<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Time Complexity<\/td><td class=\"has-text-align-center\" data-align=\"center\">O(V \u00d7 E)<\/td><td class=\"has-text-align-center\" data-align=\"center\">O((V + E) log V) with heap<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Detects Negative Cycles<\/td><td class=\"has-text-align-center\" data-align=\"center\">Yes<\/td><td class=\"has-text-align-center\" data-align=\"center\">No<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Efficiency<\/td><td class=\"has-text-align-center\" data-align=\"center\">Slower<\/td><td class=\"has-text-align-center\" data-align=\"center\">Faster<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-bellman-ford-algorithm-python-code\"><span class=\"ez-toc-section\" id=\"Bellman-Ford_Algorithm_Python_Code\"><\/span><strong>Bellman-Ford Algorithm Python Code<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>def bellman_ford(graph, V, E, source):\r\n    distance = &#091;float(\"inf\")] * V\r\n    distance&#091;source] = 0\r\n\r\n    for _ in range(V - 1):\r\n        for u, v, w in graph:\r\n            if distance&#091;u] != float(\"inf\") and distance&#091;u] + w &lt; distance&#091;v]:\r\n                distance&#091;v] = distance&#091;u] + w\r\n\r\n    for u, v, w in graph:\r\n        if distance&#091;u] != float(\"inf\") and distance&#091;u] + w &lt; distance&#091;v]:\r\n            print(\"Graph contains negative weight cycle\")\r\n            return None\r\n\r\n    print(\"Vertex Distance from Source:\")\r\n    for i in range(V):\r\n        print(f\"{i} \\t\\t {distance&#091;i]}\")\r\n    return distance\r\n\r\nV = 5\r\nE = 8\r\ngraph = &#091;\r\n    (0, 1, -1),\r\n    (0, 2, 4),\r\n    (1, 2, 3),\r\n    (1, 3, 2),\r\n    (1, 4, 2),\r\n    (3, 2, 5),\r\n    (3, 1, 1),\r\n    (4, 3, -3)\r\n]\r\n\r\nbellman_ford(graph, V, E, 0)\r<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Real-World_Applications_of_Bellman-Ford_Algorithm\"><\/span><strong>Real-World Applications of Bellman-Ford Algorithm<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ol>\n<li>Used in network routing protocols like RIP (Routing Information Protocol).<\/li>\n\n\n\n<li>Helps detect negative weight cycles in financial fraud detection systems.<\/li>\n\n\n\n<li>Used in transportation systems to find the cheapest or fastest route.<\/li>\n\n\n\n<li>Useful in currency exchange to detect arbitrage opportunities.<\/li>\n\n\n\n<li>Applied in game development for AI pathfinding in weighted maps.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-frequently-asked-questions-bellman-ford-algorithm\"><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions_Bellman-Ford_Algorithm\"><\/span><strong>Frequently Asked Questions (Bellman-Ford Algorithm)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-what-is-the-bellman-ford-algorithm-used-for\"><span class=\"ez-toc-section\" id=\"What_is_the_Bellman-Ford_Algorithm_used_for\"><\/span><strong>What is the Bellman-Ford Algorithm used for?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It is used to find the shortest path from one point to all other points in a graph.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-can-bellman-ford-work-with-negative-numbers\"><span class=\"ez-toc-section\" id=\"Can_Bellman-Ford_work_with_negative_numbers\"><\/span><strong>Can Bellman-Ford work with negative numbers?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Yes, it works even if some paths have negative weights.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-what-is-a-negative-weight-cycle\"><span class=\"ez-toc-section\" id=\"What_is_a_negative_weight_cycle\"><\/span><strong>What is a negative weight cycle?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It is a loop where the total path cost keeps getting smaller, forever.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-is-bellman-ford-better-than-dijkstra\"><span class=\"ez-toc-section\" id=\"Is_Bellman-Ford_better_than_Dijkstra\"><\/span><strong>Is Bellman-Ford better than Dijkstra?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Bellman-Ford is slower but works in more cases like with negative weights.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-where-is-bellman-ford-used-in-real-life\"><span class=\"ez-toc-section\" id=\"Where_is_Bellman-Ford_used_in_real_life\"><\/span><strong>Where is Bellman-Ford used in real life?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It is used in maps, internet routing, currency exchange, and games.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Bellman-Ford Algorithm is a fundamental concept in graph theory especially it is useful when dealing with graphs that have negative weight edges. While many people are familiar with Dijkstra\u2019s Algorithm for finding the shortest path that does not work correctly when negative weights are involved. That is where Bellman-Ford Algorithm comes in. It allows [&hellip;]<\/p>\n","protected":false},"author":560,"featured_media":50205,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1075,1073],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.4 (Yoast SEO v21.9.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Bellman-Ford Algorithm: Complete Guide with Python Code - iQuanta<\/title>\n<meta name=\"description\" content=\"The Bellman-Ford Algorithm is a fundamental concept in graph theory especially it is useful when dealing with graphs that have negative....\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bellman-Ford Algorithm: Complete Guide with Python Code\" \/>\n<meta property=\"og:description\" content=\"The Bellman-Ford Algorithm is a fundamental concept in graph theory especially it is useful when dealing with graphs that have negative....\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/\" \/>\n<meta property=\"og:site_name\" content=\"iQuanta\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/iquanta.in\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-26T10:58:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-26T10:58:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/Your-paragraph-text-22.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1600\" \/>\n\t<meta property=\"og:image:height\" content=\"900\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Nidhi Goswami\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nidhi Goswami\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/\"},\"author\":{\"name\":\"Nidhi Goswami\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/ec8c8c25d0526dd86557b6fed064f7f3\"},\"headline\":\"Bellman-Ford Algorithm: Complete Guide with Python Code\",\"datePublished\":\"2025-05-26T10:58:15+00:00\",\"dateModified\":\"2025-05-26T10:58:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/\"},\"wordCount\":667,\"publisher\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/#organization\"},\"articleSection\":[\"DSA and Competitive Programming\",\"iSkills\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/\",\"url\":\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/\",\"name\":\"Bellman-Ford Algorithm: Complete Guide with Python Code - iQuanta\",\"isPartOf\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/#website\"},\"datePublished\":\"2025-05-26T10:58:15+00:00\",\"dateModified\":\"2025-05-26T10:58:18+00:00\",\"description\":\"The Bellman-Ford Algorithm is a fundamental concept in graph theory especially it is useful when dealing with graphs that have negative....\",\"breadcrumb\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.iquanta.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bellman-Ford Algorithm: Complete Guide with Python Code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#website\",\"url\":\"https:\/\/www.iquanta.in\/blog\/\",\"name\":\"iQuanta | Cat Preparation Online\",\"description\":\"Building Learning Networks\",\"publisher\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.iquanta.in\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#organization\",\"name\":\"IQuanta\",\"url\":\"https:\/\/www.iquanta.in\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2018\/08\/IQuanta-1.png\",\"contentUrl\":\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2018\/08\/IQuanta-1.png\",\"width\":525,\"height\":200,\"caption\":\"IQuanta\"},\"image\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/facebook.com\/iquanta.in\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/ec8c8c25d0526dd86557b6fed064f7f3\",\"name\":\"Nidhi Goswami\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/21d234d87afd924b217d26b25a3cf1ee?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/21d234d87afd924b217d26b25a3cf1ee?s=96&d=mm&r=g\",\"caption\":\"Nidhi Goswami\"},\"url\":\"https:\/\/www.iquanta.in\/blog\/author\/nidhigoswami\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Bellman-Ford Algorithm: Complete Guide with Python Code - iQuanta","description":"The Bellman-Ford Algorithm is a fundamental concept in graph theory especially it is useful when dealing with graphs that have negative....","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:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/","og_locale":"en_US","og_type":"article","og_title":"Bellman-Ford Algorithm: Complete Guide with Python Code","og_description":"The Bellman-Ford Algorithm is a fundamental concept in graph theory especially it is useful when dealing with graphs that have negative....","og_url":"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/","og_site_name":"iQuanta","article_publisher":"https:\/\/facebook.com\/iquanta.in","article_published_time":"2025-05-26T10:58:15+00:00","article_modified_time":"2025-05-26T10:58:18+00:00","og_image":[{"width":1600,"height":900,"url":"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/Your-paragraph-text-22.png","type":"image\/png"}],"author":"Nidhi Goswami","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Nidhi Goswami","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#article","isPartOf":{"@id":"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/"},"author":{"name":"Nidhi Goswami","@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/ec8c8c25d0526dd86557b6fed064f7f3"},"headline":"Bellman-Ford Algorithm: Complete Guide with Python Code","datePublished":"2025-05-26T10:58:15+00:00","dateModified":"2025-05-26T10:58:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/"},"wordCount":667,"publisher":{"@id":"https:\/\/www.iquanta.in\/blog\/#organization"},"articleSection":["DSA and Competitive Programming","iSkills"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/","url":"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/","name":"Bellman-Ford Algorithm: Complete Guide with Python Code - iQuanta","isPartOf":{"@id":"https:\/\/www.iquanta.in\/blog\/#website"},"datePublished":"2025-05-26T10:58:15+00:00","dateModified":"2025-05-26T10:58:18+00:00","description":"The Bellman-Ford Algorithm is a fundamental concept in graph theory especially it is useful when dealing with graphs that have negative....","breadcrumb":{"@id":"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.iquanta.in\/blog\/bellman-ford-algorithm-complete-guide-with-python-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.iquanta.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Bellman-Ford Algorithm: Complete Guide with Python Code"}]},{"@type":"WebSite","@id":"https:\/\/www.iquanta.in\/blog\/#website","url":"https:\/\/www.iquanta.in\/blog\/","name":"iQuanta | Cat Preparation Online","description":"Building Learning Networks","publisher":{"@id":"https:\/\/www.iquanta.in\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.iquanta.in\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.iquanta.in\/blog\/#organization","name":"IQuanta","url":"https:\/\/www.iquanta.in\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2018\/08\/IQuanta-1.png","contentUrl":"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2018\/08\/IQuanta-1.png","width":525,"height":200,"caption":"IQuanta"},"image":{"@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/iquanta.in"]},{"@type":"Person","@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/ec8c8c25d0526dd86557b6fed064f7f3","name":"Nidhi Goswami","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/21d234d87afd924b217d26b25a3cf1ee?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/21d234d87afd924b217d26b25a3cf1ee?s=96&d=mm&r=g","caption":"Nidhi Goswami"},"url":"https:\/\/www.iquanta.in\/blog\/author\/nidhigoswami\/"}]}},"_links":{"self":[{"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/posts\/50180"}],"collection":[{"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/users\/560"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/comments?post=50180"}],"version-history":[{"count":4,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/posts\/50180\/revisions"}],"predecessor-version":[{"id":50206,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/posts\/50180\/revisions\/50206"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/media\/50205"}],"wp:attachment":[{"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/media?parent=50180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/categories?post=50180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/tags?post=50180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}