ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.2 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.dinocajic.com/bellman-ford-algorithm-visually-explained/ |
| Last Crawled | 2026-04-02 23:14:51 (6 days ago) |
| First Indexed | 2023-12-13 02:17:00 (2 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Bellman-Ford Algorithm Visually Explained - Algorithms |
| Meta Description | The Bellman-Ford algorithm finds the shortest path to each vertex in the directed graph from the source vertex. Bellman-Ford can have negative edges., The Bellman-Ford algorithm finds the shortest path to each vertex in the directed graph from the source vertex. Bellman-Ford can have negative edges. |
| Meta Canonical | null |
| Boilerpipe Text | The Bellman-Ford algorithm finds the shortest path to each vertex in the directed graph from the source vertex. Unlike Dijkstra’s algorithm, Bellman-Ford can have negative edges.
To begin, all the outbound edges are recorded in a table in alphabetical order.
Like Dijkstra’s algorithm, a table recording the distance to each
vertex
and the predecessor of each vertex is created. The distances for each vertex, except the source vertex, is initialized to infinity. Distance is represented by the variable d and the predecessor is represented by the variable π.
The Bellman-Ford algorithm will iterate through each of the edges. Since there are 9 edges, there will be up to 9 iterations. During each iteration, the specific edge is relaxed. During the first iteration, the cost to get to vertex C from A is -3. The current distance from the source to A is infinity. When -3 is added to infinity, the result is infinity, so the value of C remains infinity. Similarly, from A to E, the cost is 2, however, since the distance to A is infinity, the value of E remains infinity. Looking at edges B-F, C-B, C-H, F-G, G-B, and H-D, we can see that they all yield the same result, infinity. The last edge, S-A, yields a different result. The weight of edge S-A is 5. The current distance to S is 0, so the distance from S to A is 0 + 5 = 5. The predecessor to A is set to S. After the first iteration, Bellman-Ford found the path to A from S.
Since all the edges have been relaxed, Bellman-Ford starts on the second iteration. Although each edge is relaxed, the only edges that matter are the edges from S and from A since the distance to those vertices is already known. The distance to all other vertices is infinity. Looking at the table containing the edges, we start by relaxing edge A-C.
The weight of edge A-C is -3. The current distance to vertex A is 5 via edge S-A, so the distance to vertex C is 5 + (-3) = 2. The predecessor of C is A. The weight of edge A-E is 2. The distance to E is 5 + 2 = 7 via edge S-A. The predecessor of E is updated to A. Edge B-F cannot be relaxed yet.
Edge C-B can be relaxed since we know the distance to C. The distance to B is
2 + 7 = 9 and the predecessor of vertex B is C. Edge C-H can be relaxed since we know the distance to C. The distance to H is 2 + (-3) = -1 and the predecessor of vertex H is vertex C.
Edge F-G cannot yet be relaxed. Edge G-B cannot be relaxed. Edge H-D can be relaxed since we know the distance to vertex H is -1. The distance to vertex D is -1 + 1 = 0 and the predecessor to vertex D is vertex H.
The distance to A from edge S-A is already 5 so no update is necessary. This ends iteration 2.
During the third iteration, the Bellman-Ford algorithm examines all the edges again. Edges A-C and A-E yield the same results. Edge B-F can now be relaxed. The distance to B is 9, so the distance to vertex F is 9 + (-5) = 4. The predecessor to F is B.
Edges C-B and C-H yield the same results, so the table remains the same. Edge F-G can now be relaxed. The distance to vertex F is 4, so the distance to vertex G is 4 + 2 = 6. The predecessor of G is F.
Edge G-B can now be relaxed. The distance to vertex G is 6, so the distance to B is 6 + 4 = 10. Since vertex B can be reached with a shorter distance by going through edge C-B, the table remains the same.
During the fourth iteration, all the edges are examined. The algorithm sees that there are no changes, so the algorithm ends on the fourth iteration.
If this graph had a negative cycle, after the iteration is repeated n-1 times, theoretically the Bellman-Ford algorithm should have found the shortest paths to all vertices. During the nth iteration, where n represents the number of vertices, if there is a negative cycle, the distance to at least one vertex will change. Let’s look at a quick example.
The table with the distances and the predecessors is constructed. The distances are initialized to infinity for vertices A, B and C. The distance to S is 0.
Looking at the first edge, A-B cannot be relaxed yet and neither can edge B-C nor edge C-A. Edge S-A can be relaxed. The distance to S is 0, so the distance to A is 0 + 3 = 3. The predecessor of A is S.
Edge S-B can also be relaxed. The distance to vertex B is 0 + 6 = 6. Vertex B’s predecessor is S.
The first iteration is complete. During the second iteration, all of the edges are examined again.
Edge A-B can be relaxed during the second iteration. The distance to A is 3, so the distance to vertex B is 3 + 5 = 8. Since the distance to B is already less than the new value, the value of B is retained. Edge B-C can be reached in 6 + 2 = 8. Vertex C’s predecessor is vertex B.
Edge C-A is examined next. The distance to C is 8 units, so the distance to A via edge B-C is 8 + (-10) = -2. Since the distance to A via edge C-A is less than the distance to A via S-A, the distance to A is updated.
Edges S-A and S-B yield nothing better, so the second iteration is complete. The third iteration starts.
Edge A-B is relaxed. The distance to A is currently -2, so the distance to B via edge A-B is -2 + 5 = 3. Since the distance to B is less via A-B than S-B, the distance is updated to 3. Vertex B’s predecessor is updated to vertex A.
Edge B-C is relaxed next. The current distance to B is 3, so the distance to C is
3 + 2 = 5. The distance to C is updated to 5.
Edge C-A is relaxed. The distance to C is 5 + (-10) = -5. The distance to vertex A is updated to -5 units.
Edges S-A and S-B yield no better results. At this time, all shortest paths should have been found. If we examine another iteration, there should be no changes.
Edge A-B is relaxed. The distance to A is -5 so the distance to B is -5 + 5 = 0. The distance to B is updated to 0. Since the value changes on the nth iteration, values will change on the n+1th iteration as well; values will continue to change indefinitely. If we examine the graph closely, we can see that A-B-C yields a negative value: 5 + 2 + (-10) = -3.
If you liked what you read, check out my book,
An Illustrative Introduction to Algorithms
. |
| Markdown | [Skip to content](https://www.dinocajic.com/bellman-ford-algorithm-visually-explained/#content)
[](https://www.dinocajic.com/)
[Cajic Technology Partners LLC](https://www.dinocajic.com/)
### Make AI practical. Make IT dependable.
Toggle menu visibility.
- [instagram](https://www.instagram.com/think.dino/)
- [linkedin](https://www.linkedin.com/in/dinocajic/)
- [twitter](https://twitter.com/dino_cajic)
- [youtube](https://www.youtube.com/c/DinoCajic)
Toggle menu visibility.
- [Home](https://www.dinocajic.com/)
- [Consultation](https://www.dinocajic.com/consultation/)
- [Artificial Intelligence](https://www.dinocajic.com/ai-consultant/)
- [Chief Information Officer](https://www.dinocajic.com/cio-consultant/)
- [Chief Digital Officer](https://www.dinocajic.com/cdo-consultant/)
- [Wheel and Tire Industry](https://www.dinocajic.com/wheel-and-tire-industry-consulting/)
- [Software Dev Agency](https://www.dinocajic.com/software-dev-agency-consulting/)
- [Case Studies](https://www.dinocajic.com/chief-digital-and-information-officer-case-studies/)
- [First Choice Wheels and Tires](https://www.dinocajic.com/first-choice-wheels-and-tires/)
- [Western Wheel and Tire](https://www.dinocajic.com/western-wheel-and-tire/)
- [My Auto System](https://www.dinocajic.com/myautosystem/)
- [Absolute Biotech](https://www.dinocajic.com/absolute-biotech/)
- [NS Wheels](https://www.dinocajic.com/ns-wheels/)
- [XIX Wheels](https://www.dinocajic.com/xix-wheels/)
- [Auto-Body Shops](https://www.dinocajic.com/auto-body-shops/)
- [Software Dev Agencies](https://www.dinocajic.com/software-development-agencies/)
- [Wheel City Wholesalers](https://www.dinocajic.com/wheel-city-wholesalers/)
- [Wheel Vendors](https://www.dinocajic.com/wheel-vendors/)
- [BBY Wheels](https://www.dinocajic.com/bby-wheels/)
- [Car Mod Guy](https://www.dinocajic.com/car-mod-guy/)
- [Courses](https://www.dinocajic.com/courses/)
- [Artificial Intelligence](https://www.dinocajic.com/artificial-intelligence/)
- [Algorithms](https://www.dinocajic.com/algorithms/)
- [How to Become a CIO](https://www.dinocajic.com/how-to-become-a-cio/)
- [PHP and MySQL](https://www.dinocajic.com/php/)
- [Laravel](https://www.dinocajic.com/laravel/)
- [IT Project Management](https://www.dinocajic.com/it-project-management/)
- [Cybersecurity for Business](https://www.dinocajic.com/cybersecurity-for-business/)
- [YouTube Shorts](https://www.dinocajic.com/shorts/)
- [Videos](https://www.dinocajic.com/videos/)
- [Articles](https://www.dinocajic.com/articles/)
- [About](https://www.dinocajic.com/about/)
- [Contact](https://www.dinocajic.com/contact/)
Bellman-Ford Algorithm Visually Explained
Posted [2 years](https://www.dinocajic.com/bellman-ford-algorithm-visually-explained/) ago by [Dino Cajic](https://www.dinocajic.com/author/dinocajic_2kf42m/)

## Exploring Shortest Paths
The Bellman-Ford algorithm finds the shortest path to each vertex in the directed graph from the source vertex. Unlike Dijkstra’s algorithm, Bellman-Ford can have negative edges.

To begin, all the outbound edges are recorded in a table in alphabetical order.

Like Dijkstra’s algorithm, a table recording the distance to each *vertex*and the predecessor of each vertex is created. The distances for each vertex, except the source vertex, is initialized to infinity. Distance is represented by the variable d and the predecessor is represented by the variable π.

The Bellman-Ford algorithm will iterate through each of the edges. Since there are 9 edges, there will be up to 9 iterations. During each iteration, the specific edge is relaxed. During the first iteration, the cost to get to vertex C from A is -3. The current distance from the source to A is infinity. When -3 is added to infinity, the result is infinity, so the value of C remains infinity. Similarly, from A to E, the cost is 2, however, since the distance to A is infinity, the value of E remains infinity. Looking at edges B-F, C-B, C-H, F-G, G-B, and H-D, we can see that they all yield the same result, infinity. The last edge, S-A, yields a different result. The weight of edge S-A is 5. The current distance to S is 0, so the distance from S to A is 0 + 5 = 5. The predecessor to A is set to S. After the first iteration, Bellman-Ford found the path to A from S.

Since all the edges have been relaxed, Bellman-Ford starts on the second iteration. Although each edge is relaxed, the only edges that matter are the edges from S and from A since the distance to those vertices is already known. The distance to all other vertices is infinity. Looking at the table containing the edges, we start by relaxing edge A-C.

The weight of edge A-C is -3. The current distance to vertex A is 5 via edge S-A, so the distance to vertex C is 5 + (-3) = 2. The predecessor of C is A. The weight of edge A-E is 2. The distance to E is 5 + 2 = 7 via edge S-A. The predecessor of E is updated to A. Edge B-F cannot be relaxed yet.

Edge C-B can be relaxed since we know the distance to C. The distance to B is
2 + 7 = 9 and the predecessor of vertex B is C. Edge C-H can be relaxed since we know the distance to C. The distance to H is 2 + (-3) = -1 and the predecessor of vertex H is vertex C.

Edge F-G cannot yet be relaxed. Edge G-B cannot be relaxed. Edge H-D can be relaxed since we know the distance to vertex H is -1. The distance to vertex D is -1 + 1 = 0 and the predecessor to vertex D is vertex H.

The distance to A from edge S-A is already 5 so no update is necessary. This ends iteration 2.


During the third iteration, the Bellman-Ford algorithm examines all the edges again. Edges A-C and A-E yield the same results. Edge B-F can now be relaxed. The distance to B is 9, so the distance to vertex F is 9 + (-5) = 4. The predecessor to F is B.

Edges C-B and C-H yield the same results, so the table remains the same. Edge F-G can now be relaxed. The distance to vertex F is 4, so the distance to vertex G is 4 + 2 = 6. The predecessor of G is F.

Edge G-B can now be relaxed. The distance to vertex G is 6, so the distance to B is 6 + 4 = 10. Since vertex B can be reached with a shorter distance by going through edge C-B, the table remains the same.

During the fourth iteration, all the edges are examined. The algorithm sees that there are no changes, so the algorithm ends on the fourth iteration.
If this graph had a negative cycle, after the iteration is repeated n-1 times, theoretically the Bellman-Ford algorithm should have found the shortest paths to all vertices. During the nth iteration, where n represents the number of vertices, if there is a negative cycle, the distance to at least one vertex will change. Let’s look at a quick example.


The table with the distances and the predecessors is constructed. The distances are initialized to infinity for vertices A, B and C. The distance to S is 0.

Looking at the first edge, A-B cannot be relaxed yet and neither can edge B-C nor edge C-A. Edge S-A can be relaxed. The distance to S is 0, so the distance to A is 0 + 3 = 3. The predecessor of A is S.

Edge S-B can also be relaxed. The distance to vertex B is 0 + 6 = 6. Vertex B’s predecessor is S.


The first iteration is complete. During the second iteration, all of the edges are examined again.

Edge A-B can be relaxed during the second iteration. The distance to A is 3, so the distance to vertex B is 3 + 5 = 8. Since the distance to B is already less than the new value, the value of B is retained. Edge B-C can be reached in 6 + 2 = 8. Vertex C’s predecessor is vertex B.


Edge C-A is examined next. The distance to C is 8 units, so the distance to A via edge B-C is 8 + (-10) = -2. Since the distance to A via edge C-A is less than the distance to A via S-A, the distance to A is updated.


Edges S-A and S-B yield nothing better, so the second iteration is complete. The third iteration starts.

Edge A-B is relaxed. The distance to A is currently -2, so the distance to B via edge A-B is -2 + 5 = 3. Since the distance to B is less via A-B than S-B, the distance is updated to 3. Vertex B’s predecessor is updated to vertex A.

Edge B-C is relaxed next. The current distance to B is 3, so the distance to C is
3 + 2 = 5. The distance to C is updated to 5.

Edge C-A is relaxed. The distance to C is 5 + (-10) = -5. The distance to vertex A is updated to -5 units.

Edges S-A and S-B yield no better results. At this time, all shortest paths should have been found. If we examine another iteration, there should be no changes.

Edge A-B is relaxed. The distance to A is -5 so the distance to B is -5 + 5 = 0. The distance to B is updated to 0. Since the value changes on the nth iteration, values will change on the n+1th iteration as well; values will continue to change indefinitely. If we examine the graph closely, we can see that A-B-C yields a negative value: 5 + 2 + (-10) = -3.
*If you liked what you read, check out my book,* [*An Illustrative Introduction to Algorithms*](https://www.amazon.com/Illustrative-Introduction-Algorithms-Dino-Cajic-ebook-dp-B07WG48NV7/dp/B07WG48NV7/ref=mt_kindle?_encoding=UTF8&me=&qid=1586643862)*.*
[Algorithms](https://www.dinocajic.com/category/algorithms/), [Computer Science](https://www.dinocajic.com/category/computer-science/), [Education](https://www.dinocajic.com/category/education/) [Algorithms](https://www.dinocajic.com/tag/algorithms/), [Bellman-Ford](https://www.dinocajic.com/tag/bellman-ford/), [Bellman-Ford Algorithm](https://www.dinocajic.com/tag/bellman-ford-algorithm/), [Computer Science](https://www.dinocajic.com/tag/computer-science/)
## Post navigation
[← Johnson’s Algorithm Visually Explained](https://www.dinocajic.com/johnsons-algorithm-visually-explained/)
[Dijkstra’s Algorithm: Single Source Shortest Path Visually Explained →](https://www.dinocajic.com/dijkstras-algorithm-single-source-shortest-path-visually-explained/)
### Leave a Reply [Cancel Reply](https://www.dinocajic.com/bellman-ford-algorithm-visually-explained/#respond)
You must be [logged in](https://www.dinocajic.com/wp-login.php?redirect_to=https%3A%2F%2Fwww.dinocajic.com%2Fbellman-ford-algorithm-visually-explained%2F) to post a comment.

Cajic Technology Partners helps growing companies strengthen their AI and IT foundation so the business runs cleaner, faster, and with less friction. We focus on practical strategy, automation, and modern systems that improve reliability, security, and day-to-day execution.
Once the core is solid, we extend that impact across teams like marketing, sales, and operations, making workflows simpler, data easier to trust, and work easier to scale.
*Copyright © 2026 Cajic Technology Partners LLC. No content may be reproduced without explicit consent from Cajic Technology Partners LLC.*
[Privacy Policy](https://www.dinocajic.com/privacy-policy/) \| [Terms and Conditions](https://www.dinocajic.com/terms-and-conditions/ "Terms and Conditions") \| [Sitemap](https://www.dinocajic.com/sitemap.xml)
#### Useful Links
Toggle menu visibility.
- [Home](https://www.dinocajic.com/)
- [Consultation](https://www.dinocajic.com/consultation/)
- [Artificial Intelligence](https://www.dinocajic.com/ai-consultant/)
- [Chief Information Officer](https://www.dinocajic.com/cio-consultant/)
- [Chief Digital Officer](https://www.dinocajic.com/cdo-consultant/)
- [Wheel and Tire Industry](https://www.dinocajic.com/wheel-and-tire-industry-consulting/)
- [Software Dev Agency](https://www.dinocajic.com/software-dev-agency-consulting/)
- [Case Studies](https://www.dinocajic.com/chief-digital-and-information-officer-case-studies/)
- [First Choice Wheels and Tires](https://www.dinocajic.com/first-choice-wheels-and-tires/)
- [Western Wheel and Tire](https://www.dinocajic.com/western-wheel-and-tire/)
- [My Auto System](https://www.dinocajic.com/myautosystem/)
- [Absolute Biotech](https://www.dinocajic.com/absolute-biotech/)
- [NS Wheels](https://www.dinocajic.com/ns-wheels/)
- [XIX Wheels](https://www.dinocajic.com/xix-wheels/)
- [Auto-Body Shops](https://www.dinocajic.com/auto-body-shops/)
- [Software Dev Agencies](https://www.dinocajic.com/software-development-agencies/)
- [Wheel City Wholesalers](https://www.dinocajic.com/wheel-city-wholesalers/)
- [Wheel Vendors](https://www.dinocajic.com/wheel-vendors/)
- [BBY Wheels](https://www.dinocajic.com/bby-wheels/)
- [Car Mod Guy](https://www.dinocajic.com/car-mod-guy/)
- [Courses](https://www.dinocajic.com/courses/)
- [Artificial Intelligence](https://www.dinocajic.com/artificial-intelligence/)
- [Algorithms](https://www.dinocajic.com/algorithms/)
- [How to Become a CIO](https://www.dinocajic.com/how-to-become-a-cio/)
- [PHP and MySQL](https://www.dinocajic.com/php/)
- [Laravel](https://www.dinocajic.com/laravel/)
- [IT Project Management](https://www.dinocajic.com/it-project-management/)
- [Cybersecurity for Business](https://www.dinocajic.com/cybersecurity-for-business/)
- [YouTube Shorts](https://www.dinocajic.com/shorts/)
- [Videos](https://www.dinocajic.com/videos/)
- [Articles](https://www.dinocajic.com/articles/)
- [About](https://www.dinocajic.com/about/)
- [Contact](https://www.dinocajic.com/contact/)
#### Let’s Connect
[@think.dino](https://instagram.com/think.dino "Dino Cajic Instagram")
[@dino\_cajic](https://twitter.com/dino_cajic "Dino Cajic Twitter")
[@dinocajic](https://www.youtube.com/c/dinocajic "Dino Programming YouTube")
[Dino Cajic](https://www.linkedin.com/in/dinocajic/ "LinkedIn")
[Cajic Technology Partners LinkedIn](https://www.linkedin.com/company/cajic-technology-partners/) |
| Readable Markdown | The Bellman-Ford algorithm finds the shortest path to each vertex in the directed graph from the source vertex. Unlike Dijkstra’s algorithm, Bellman-Ford can have negative edges.

To begin, all the outbound edges are recorded in a table in alphabetical order.

Like Dijkstra’s algorithm, a table recording the distance to each *vertex*and the predecessor of each vertex is created. The distances for each vertex, except the source vertex, is initialized to infinity. Distance is represented by the variable d and the predecessor is represented by the variable π.

The Bellman-Ford algorithm will iterate through each of the edges. Since there are 9 edges, there will be up to 9 iterations. During each iteration, the specific edge is relaxed. During the first iteration, the cost to get to vertex C from A is -3. The current distance from the source to A is infinity. When -3 is added to infinity, the result is infinity, so the value of C remains infinity. Similarly, from A to E, the cost is 2, however, since the distance to A is infinity, the value of E remains infinity. Looking at edges B-F, C-B, C-H, F-G, G-B, and H-D, we can see that they all yield the same result, infinity. The last edge, S-A, yields a different result. The weight of edge S-A is 5. The current distance to S is 0, so the distance from S to A is 0 + 5 = 5. The predecessor to A is set to S. After the first iteration, Bellman-Ford found the path to A from S.

Since all the edges have been relaxed, Bellman-Ford starts on the second iteration. Although each edge is relaxed, the only edges that matter are the edges from S and from A since the distance to those vertices is already known. The distance to all other vertices is infinity. Looking at the table containing the edges, we start by relaxing edge A-C.

The weight of edge A-C is -3. The current distance to vertex A is 5 via edge S-A, so the distance to vertex C is 5 + (-3) = 2. The predecessor of C is A. The weight of edge A-E is 2. The distance to E is 5 + 2 = 7 via edge S-A. The predecessor of E is updated to A. Edge B-F cannot be relaxed yet.

Edge C-B can be relaxed since we know the distance to C. The distance to B is
2 + 7 = 9 and the predecessor of vertex B is C. Edge C-H can be relaxed since we know the distance to C. The distance to H is 2 + (-3) = -1 and the predecessor of vertex H is vertex C.

Edge F-G cannot yet be relaxed. Edge G-B cannot be relaxed. Edge H-D can be relaxed since we know the distance to vertex H is -1. The distance to vertex D is -1 + 1 = 0 and the predecessor to vertex D is vertex H.

The distance to A from edge S-A is already 5 so no update is necessary. This ends iteration 2.


During the third iteration, the Bellman-Ford algorithm examines all the edges again. Edges A-C and A-E yield the same results. Edge B-F can now be relaxed. The distance to B is 9, so the distance to vertex F is 9 + (-5) = 4. The predecessor to F is B.

Edges C-B and C-H yield the same results, so the table remains the same. Edge F-G can now be relaxed. The distance to vertex F is 4, so the distance to vertex G is 4 + 2 = 6. The predecessor of G is F.

Edge G-B can now be relaxed. The distance to vertex G is 6, so the distance to B is 6 + 4 = 10. Since vertex B can be reached with a shorter distance by going through edge C-B, the table remains the same.

During the fourth iteration, all the edges are examined. The algorithm sees that there are no changes, so the algorithm ends on the fourth iteration.
If this graph had a negative cycle, after the iteration is repeated n-1 times, theoretically the Bellman-Ford algorithm should have found the shortest paths to all vertices. During the nth iteration, where n represents the number of vertices, if there is a negative cycle, the distance to at least one vertex will change. Let’s look at a quick example.


The table with the distances and the predecessors is constructed. The distances are initialized to infinity for vertices A, B and C. The distance to S is 0.

Looking at the first edge, A-B cannot be relaxed yet and neither can edge B-C nor edge C-A. Edge S-A can be relaxed. The distance to S is 0, so the distance to A is 0 + 3 = 3. The predecessor of A is S.

Edge S-B can also be relaxed. The distance to vertex B is 0 + 6 = 6. Vertex B’s predecessor is S.


The first iteration is complete. During the second iteration, all of the edges are examined again.

Edge A-B can be relaxed during the second iteration. The distance to A is 3, so the distance to vertex B is 3 + 5 = 8. Since the distance to B is already less than the new value, the value of B is retained. Edge B-C can be reached in 6 + 2 = 8. Vertex C’s predecessor is vertex B.


Edge C-A is examined next. The distance to C is 8 units, so the distance to A via edge B-C is 8 + (-10) = -2. Since the distance to A via edge C-A is less than the distance to A via S-A, the distance to A is updated.


Edges S-A and S-B yield nothing better, so the second iteration is complete. The third iteration starts.

Edge A-B is relaxed. The distance to A is currently -2, so the distance to B via edge A-B is -2 + 5 = 3. Since the distance to B is less via A-B than S-B, the distance is updated to 3. Vertex B’s predecessor is updated to vertex A.

Edge B-C is relaxed next. The current distance to B is 3, so the distance to C is
3 + 2 = 5. The distance to C is updated to 5.

Edge C-A is relaxed. The distance to C is 5 + (-10) = -5. The distance to vertex A is updated to -5 units.

Edges S-A and S-B yield no better results. At this time, all shortest paths should have been found. If we examine another iteration, there should be no changes.

Edge A-B is relaxed. The distance to A is -5 so the distance to B is -5 + 5 = 0. The distance to B is updated to 0. Since the value changes on the nth iteration, values will change on the n+1th iteration as well; values will continue to change indefinitely. If we examine the graph closely, we can see that A-B-C yields a negative value: 5 + 2 + (-10) = -3.
*If you liked what you read, check out my book,* [*An Illustrative Introduction to Algorithms*](https://www.amazon.com/Illustrative-Introduction-Algorithms-Dino-Cajic-ebook-dp-B07WG48NV7/dp/B07WG48NV7/ref=mt_kindle?_encoding=UTF8&me=&qid=1586643862)*.* |
| Shard | 172 (laksa) |
| Root Hash | 10735637622951024172 |
| Unparsed URL | com,dinocajic!www,/bellman-ford-algorithm-visually-explained/ s443 |