đŸ•ˇī¸ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 103 (from laksa009)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

â„šī¸ Skipped - page is already crawled

📄
INDEXABLE
✅
CRAWLED
21 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.7 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://www.geeksforgeeks.org/dsa/bellman-ford-algorithm-in-python/
Last Crawled2026-03-20 12:55:33 (21 days ago)
First Indexed2025-06-22 08:43:27 (9 months ago)
HTTP Status Code200
Meta TitleBellman-Ford algorithm in Python - GeeksforGeeks
Meta DescriptionYour All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more., Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Meta Canonicalnull
Boilerpipe Textnull
Markdown
[![geeksforgeeks](https://media.geeksforgeeks.org/gfg-gg-logo.svg)](https://www.geeksforgeeks.org/) - Courses - Tutorials - Interview Prep - [DSA Tutorial](https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/) - [Interview Questions](https://www.geeksforgeeks.org/dsa/top-100-data-structure-and-algorithms-dsa-interview-questions-topic-wise/) - [Quizzes](https://www.geeksforgeeks.org/dsa/data-structures-and-algorithms-online-quiz/) - [Must Do](https://www.geeksforgeeks.org/dsa/must-do-coding-questions-for-companies-like-amazon-microsoft-adobe/) - [Advanced DSA](https://www.geeksforgeeks.org/dsa/advanced-data-structures/) - [System Design](https://www.geeksforgeeks.org/system-design/system-design-tutorial/) - [Aptitude](https://www.geeksforgeeks.org/aptitude/aptitude-questions-and-answers/) - [Puzzles](https://www.geeksforgeeks.org/aptitude/puzzles/) - [Interview Corner](https://www.geeksforgeeks.org/interview-prep/interview-corner/) - [DSA Python](https://www.geeksforgeeks.org/dsa/python-data-structures-and-algorithms/) # Bellman-Ford algorithm in Python Last Updated : 8 Mar, 2025 Given a ****weighted**** graph with ****V**** vertices and ****E**** edges, and a source vertex ****src****, find the ****shortest path**** from the source vertex to all vertices in the given graph. If a vertex cannot be reached from source vertex, mark its distance as 108. ****Note****: If a graph contains negative weight cycle, return -1. ****Bellman-Ford**** is a single source shortest path algorithm. It effectively works in the cases of negative edges and is able to detect negative cycles as well. It works on the principle of relaxation of the edges. ****Examples:**** > ****Input:**** V = 5, edges = \[\[1, 3, 2\], \[4, 3, -1\], \[2, 4, 1\], \[1, 2, 1\], \[0, 1, 5\]\], src = 0 > > ![bellman\_ford\_input\_images](https://media.geeksforgeeks.org/wp-content/uploads/20241018163210610023/bellman_ford_input_images.webp) > > Example > > ****Output******: \[0, 5, 6, 6, 7\]** > > ****Input******: V = 4, edges = \[\[0, 1, 4\], \[1, 2, -6\], \[2, 3, 5\], \[3, 1, -2\]\], src = 0** > > ![input-2](https://media.geeksforgeeks.org/wp-content/uploads/20241016175549040804/input-2.webp) > > Example > > ****Output******: \[-1\]** > ****Explanation:**** **The graph contains negative weight cycle.** ### ****Approach**** > The Bellman-Ford algorithm works by traversing all edges `|V| - 1` times, where `|V|` is the number of vertices. After each iteration, the algorithm relaxes all the edges. If a shorter path is found from the source to a vertex during any iteration, then the distance of that vertex is updated. ## Step-by-step algorithm 1. Initialize distances to all vertices as infinite and the distance to the source vertex as 0. 2. Relax all edges `|V| - 1` times. 3. If we can find a shorter path, then there is a negative weight cycle in the graph. ## Working of Bellman-Ford Algorithm Below is the Python implementation of the algorithm: Python `` **Output** ``` {'A': 0, 'B': -1, 'C': 2, 'D': -2, 'E': 1} ``` ****Time Complexity:**** O(V \* E \* E), where V is the number of vertices and E is the number of edges ****Auxiliary Space:**** O(V) Comment Article Tags: Article Tags: [Graph](https://www.geeksforgeeks.org/category/dsa/data-structures/graph/) [DSA](https://www.geeksforgeeks.org/category/dsa/) [Python-DSA](https://www.geeksforgeeks.org/tag/python-data-structures/) ### Explore [![GeeksforGeeks](https://media.geeksforgeeks.org/auth-dashboard-uploads/gfgFooterLogo.png)](https://www.geeksforgeeks.org/) ![location](https://media.geeksforgeeks.org/img-practice/Location-1685004904.svg) Corporate & Communications Address: A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305) ![location](https://media.geeksforgeeks.org/img-practice/Location-1685004904.svg) Registered Address: K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305 [![GFG App on Play Store](https://media.geeksforgeeks.org/auth-dashboard-uploads/googleplay-%281%29.png)](https://geeksforgeeksapp.page.link/gfg-app)[![GFG App on App Store](https://media.geeksforgeeks.org/auth-dashboard-uploads/appstore-%281%29.png)](https://geeksforgeeksapp.page.link/gfg-app) - Company - [About Us](https://www.geeksforgeeks.org/about/) - [Legal](https://www.geeksforgeeks.org/legal/) - [Privacy Policy](https://www.geeksforgeeks.org/legal/privacy-policy/) - [Contact Us](https://www.geeksforgeeks.org/about/contact-us/) - [Advertise with us](https://www.geeksforgeeks.org/advertise-with-us/) - [GFG Corporate Solution](https://www.geeksforgeeks.org/gfg-corporate-solution/) - [Campus Training Program](https://www.geeksforgeeks.org/campus-training-program/) - Explore - [POTD](https://www.geeksforgeeks.org/problem-of-the-day) - [Job-A-Thon](https://practice.geeksforgeeks.org/events/rec/job-a-thon/) - [Blogs](https://www.geeksforgeeks.org/category/blogs/?type=recent) - [Nation Skill Up](https://www.geeksforgeeks.org/nation-skill-up/) - Tutorials - [Programming Languages](https://www.geeksforgeeks.org/computer-science-fundamentals/programming-language-tutorials/) - [DSA](https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/) - [Web Technology](https://www.geeksforgeeks.org/web-tech/web-technology/) - [AI, ML & Data Science](https://www.geeksforgeeks.org/machine-learning/ai-ml-and-data-science-tutorial-learn-ai-ml-and-data-science/) - [DevOps](https://www.geeksforgeeks.org/devops/devops-tutorial/) - [CS Core Subjects](https://www.geeksforgeeks.org/gate/gate-exam-tutorial/) - [Interview Preparation](https://www.geeksforgeeks.org/aptitude/interview-corner/) - [Software and Tools](https://www.geeksforgeeks.org/websites-apps/software-and-tools-a-to-z-list/) - Courses - [ML and Data Science](https://www.geeksforgeeks.org/courses/category/machine-learning-data-science) - [DSA and Placements](https://www.geeksforgeeks.org/courses/category/dsa-placements) - [Web Development](https://www.geeksforgeeks.org/courses/category/development-testing) - [Programming Languages](https://www.geeksforgeeks.org/courses/category/programming-languages) - [DevOps & Cloud](https://www.geeksforgeeks.org/courses/category/cloud-devops) - [GATE](https://www.geeksforgeeks.org/courses/category/gate) - [Trending Technologies](https://www.geeksforgeeks.org/courses/category/trending-technologies/) - Videos - [DSA](https://www.geeksforgeeks.org/videos/category/sde-sheet/) - [Python](https://www.geeksforgeeks.org/videos/category/python/) - [Java](https://www.geeksforgeeks.org/videos/category/java-w6y5f4/) - [C++](https://www.geeksforgeeks.org/videos/category/c/) - [Web Development](https://www.geeksforgeeks.org/videos/category/web-development/) - [Data Science](https://www.geeksforgeeks.org/videos/category/data-science/) - [CS Subjects](https://www.geeksforgeeks.org/videos/category/cs-subjects/) - Preparation Corner - [Interview Corner](https://www.geeksforgeeks.org/interview-prep/interview-corner/) - [Aptitude](https://www.geeksforgeeks.org/aptitude/aptitude-questions-and-answers/) - [Puzzles](https://www.geeksforgeeks.org/aptitude/puzzles/) - [GfG 160](https://www.geeksforgeeks.org/courses/gfg-160-series) - [System Design](https://www.geeksforgeeks.org/system-design/system-design-tutorial/) [@GeeksforGeeks, Sanchhaya Education Private Limited](https://www.geeksforgeeks.org/), [All rights reserved](https://www.geeksforgeeks.org/copyright-information/)
Readable Markdownnull
Shard103 (laksa)
Root Hash12046344915360636903
Unparsed URLorg,geeksforgeeks!www,/dsa/bellman-ford-algorithm-in-python/ s443