šŸ•·ļø Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 35 (from laksa061)

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
5 days ago
šŸ¤–
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.2 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.boardinfinity.com/blog/bellman-ford-algorithm/
Last Crawled2026-04-01 13:22:13 (5 days ago)
First Indexed2022-10-15 00:39:40 (3 years ago)
HTTP Status Code200
Meta TitleBellman Ford Algorithm | Board Infinity
Meta DescriptionExplanation, examples and C++ implementation for the graph based Bellman Ford algorithm along with popular applications.
Meta Canonicalnull
Boilerpipe Text
Introduction In this article you will understand the Bellman-Ford algorithm step-by-step, using the example as a guide. C++ implementation with the code for calculating the distance from the source vertex in a weighted graph is also covered in this article. Approach The Bellman-Ford algorithm imitates the shortest routes in a weighted digraph from a single site vertex to all other vertices. Since it can handle graphs with certain edge weights that are negative values, it is more flexible than Dijkstra's method for the identical issue. The Bellman-Ford algorithm makes use of dynamic programming. It starts with a beginning vertex and determines the shortest paths an edge can take to reach additional vertices. The process then continues in search of a path with two edges. The bottom-up method is employed by the Bellman-Ford algorithm. Negative Cycles Negative weight edges can produce negative weight cycles that, by going back to the same location, shorten the overall path length. Negative weight cycles could provide the wrong answer while trying to identify the shortest path. Because they may experience a negative weight cycle that shortens the trip, shortest path methods like Dijkstra's Algorithm that cannot identify such a cycle may generate inaccurate results. Steps Step 1: Make a list of every edge in the graph. If the graph is represented as an adjacency list, then this is easy. Step 2: The number of iterations is calculated using the formula "V - 1". The number of iterations will grow the same number of vertices since the shortest path to an edge can only be changed V - 1 times. Step 3 : Start with a random vertex at a distance of 0 minimum. You are overstating the real lengths, hence all other nodes ought to be given a distance of infinite. Relax the vertices' route lengths for each edge u-v: Distance[v] = distance[u] + edge weight uv if distance[v] is larger than distance[u] + edge weight uv. Step 4: Alter the distance for each Edge in every iteration if the new distance is smaller than the old one. The distance covered from the beginning node to each individual node is the distance to each node. Step 5: You must take alliterations into account to make sure all potential pathways are taken into account. If you accomplish this, the distance you have traveled will be the shortest. Code #include <bits/stdc++.h> struct Edge { Ā  int src, dest, weight; }; struct Graph { Ā  int V, E; Ā  struct Edge * edge ; }; struct Graph* createGraph ( int V, int E) { Ā  struct Graph * graph = new Graph ; Ā  graph->V = V; Ā  graph->E = E; Ā  graph->edge = new Edge[E]; Ā  return graph; } void BellmanFord (struct Graph* graph, int src) { Ā  int V = graph->V; Ā  int E = graph->E; Ā  int dist[V]; Ā  for ( int i = 0 ; i < V; i++) Ā  Ā  Ā  dist[i] = INT_MAX; Ā  Ā  Ā  dist[src] = 0 ; Ā  for ( int i = 1 ; i <= V - 1 ; i++) { Ā  Ā  Ā  for ( int j = 0 ; j < E; j++) { Ā  Ā  Ā  Ā  int u = graph->edge[j].src; Ā  Ā  Ā  Ā  int v = graph->edge[j].dest; Ā  Ā  Ā  Ā  int weight = graph->edge[j].weight; Ā  Ā  Ā  Ā  if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) Ā  Ā  Ā  Ā  dist[v] = dist[u] + weight; Ā  Ā  Ā  } Ā  } Ā  for ( int i = 0 ; i < E; i++) { Ā  Ā  Ā  int u = graph->edge[i].src; Ā  Ā  Ā  int v = graph->edge[i].dest; Ā  Ā  Ā  int weight = graph->edge[i].weight; Ā  Ā  Ā  if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) { Ā  Ā  Ā  Ā  printf ( "Graph contains negative weight cycle" ); Ā  Ā  Ā  Ā  return ; Ā  Ā  Ā  } Ā  } Ā  printf ( "Vertex :\t\t\t " ); Ā  for ( int i = 0 ; i < V; ++i) Ā  Ā  Ā  printf ( "%d \t" , i); Ā  Ā  Ā  printf ( "\nDistance (from the source): " ); Ā  for ( int i = 0 ; i < V; ++i) Ā  Ā  Ā  printf ( "%d \t" ,dist[i]); Ā  return ; } int main () { Ā  int V = 5 ; Ā  int E = 8 ; Ā  struct Graph * graph = createGraph ( V , E ); Ā  graph->edge[ 0 ].src = 0 ; Ā  graph->edge[ 0 ].dest = 1 ; Ā  graph->edge[ 0 ].weight = -1 ; Ā  graph->edge[ 1 ].src = 0 ; Ā  graph->edge[ 1 ].dest = 2 ; Ā  graph->edge[ 1 ].weight = 4 ; Ā  graph->edge[ 2 ].src = 1 ; Ā  graph->edge[ 2 ].dest = 2 ; Ā  graph->edge[ 2 ].weight = 3 ; Ā  graph->edge[ 3 ].src = 1 ; Ā  graph->edge[ 3 ].dest = 3 ; Ā  graph->edge[ 3 ].weight = 2 ; Ā  graph->edge[ 4 ].src = 1 ; Ā  graph->edge[ 4 ].dest = 4 ; Ā  graph->edge[ 4 ].weight = 2 ; Ā  graph->edge[ 5 ].src = 3 ; Ā  graph->edge[ 5 ].dest = 2 ; Ā  graph->edge[ 5 ].weight = 5 ; Ā  graph->edge[ 6 ].src = 3 ; Ā  graph->edge[ 6 ].dest = 1 ; Ā  graph->edge[ 6 ].weight = 1 ; Ā  graph->edge[ 7 ].src = 4 ; Ā  graph->edge[ 7 ].dest = 3 ; Ā  graph->edge[ 7 ].weight = -3 ; Ā  BellmanFord(graph, 0 ); Ā  return 0 ; } Output: Vertex : 0 1 2 3 4 Distance (from the source) Complexity Analysis The graph might present a worst-case scenario when you encounter a negative cycle. You would need to relax |E| * (|E| - 1) / 2 edges, (|V| - 1) number of times in a full graph with edges connecting every pair of vertices, assuming you identified the shortest route in the first few iterations or repetitions but continued with edge relaxation. The complete graph will lead to the worst case scenario where the time complexity will be: O(|V|^2) = O(E V). O(|V|) = O (V^3) Applications of Bellman Ford Algorithm Finding weight-loss relapse cycles Calculate the least amount of heat that can acquire or lose during a chemical process. Figuring out the most effective currency conversion technique. looking at a graph to see if there are any negative weight cycles. Find the graph's shortest route using negative weights. In data networks, the idea of routing is applied.
Markdown
![](https://www.facebook.com/tr?id=274992729856951&ev=PageView&noscript=1) [![](https://www.boardinfinity.com/blog/content/images/2026/03/BI-Logo.svg)](https://www.boardinfinity.com/) [Sign Up](https://www.boardinfinity.com/blog/bellman-ford-algorithm/#/portal/signup) ### Menu # Bellman Ford Algorithm in detail with code ![Bellman Ford Algorithm in detail with code](https://www.boardinfinity.com/blog/content/images/2022/10/jOhJW.png) ### Course Lessons ## **Introduction** In this article you will understand the Bellman-Ford algorithm step-by-step, using the example as a guide. C++ implementation with the code for calculating the distance from the source vertex in a weighted graph is also covered in this article. ## **Approach** The Bellman-Ford algorithm imitates the shortest routes in a weighted digraph from a single site vertex to all other vertices. Since it can handle graphs with certain edge weights that are negative values, it is more flexible than Dijkstra's method for the identical issue. The Bellman-Ford algorithm makes use of dynamic programming. It starts with a beginning vertex and determines the shortest paths an edge can take to reach additional vertices. The process then continues in search of a path with two edges. The bottom-up method is employed by the Bellman-Ford algorithm. ## **Negative Cycles** Negative weight edges can produce negative weight cycles that, by going back to the same location, shorten the overall path length. Negative weight cycles could provide the wrong answer while trying to identify the shortest path. Because they may experience a negative weight cycle that shortens the trip, shortest path methods like Dijkstra's Algorithm that cannot identify such a cycle may generate inaccurate results. **Steps** - **Step 1:** Make a list of every edge in the graph. If the graph is represented as an adjacency list, then this is easy. - **Step 2:** The number of iterations is calculated using the formula "V - 1". The number of iterations will grow the same number of vertices since the shortest path to an edge can only be changed V - 1 times. - **Step 3**: Start with a random vertex at a distance of 0 minimum. You are overstating the real lengths, hence all other nodes ought to be given a distance of infinite. Relax the vertices' route lengths for each edge u-v: - Distance\[v\] = distance\[u\] + edge weight uv if distance\[v\] is larger than distance\[u\] + edge weight uv. - **Step 4:** Alter the distance for each Edge in every iteration if the new distance is smaller than the old one. The distance covered from the beginning node to each individual node is the distance to each node. - **Step 5:** You must take alliterations into account to make sure all potential pathways are taken into account. If you accomplish this, the distance you have traveled will be the shortest. ## **Code** | | |---| | \#include \<bits/stdc++.h\> struct Edge { int src, dest, weight; }; struct Graph { int V, E; struct Edge\* edge; }; struct Graph\* createGraph(int V, int E) { struct Graph\* graph = new Graph; graph-\>V = V; graph-\>E = E; graph-\>edge = new Edge\[E\]; return graph; } void BellmanFord(struct Graph\* graph, int src) { int V = graph-\>V; int E = graph-\>E; int dist\[V\]; for (int i = 0; i \< V; i++) dist\[i\] = INT\_MAX; dist\[src\] = 0; for (int i = 1; i \<= V - 1; i++) { for (int j = 0; j \< E; j++) { int u = graph-\>edge\[j\].src; int v = graph-\>edge\[j\].dest; int weight = graph-\>edge\[j\].weight; if (dist\[u\] != INT\_MAX && dist\[u\] + weight \< dist\[v\]) dist\[v\] = dist\[u\] + weight; } } for (int i = 0; i \< E; i++) { int u = graph-\>edge\[i\].src; int v = graph-\>edge\[i\].dest; int weight = graph-\>edge\[i\].weight; if (dist\[u\] != INT\_MAX && dist\[u\] + weight \< dist\[v\]) { printf("Graph contains negative weight cycle"); return; } } printf("Vertex :\\t\\t\\t "); for (int i = 0; i \< V; ++i) printf("%d \\t", i); printf("\\nDistance (from the source): "); for (int i = 0; i \< V; ++i) printf("%d \\t",dist\[i\]); return; } int main() { int V = 5; int E = 8; struct Graph\* graph = createGraph(V, E); graph-\>edge\[0\].src = 0; graph-\>edge\[0\].dest = 1; graph-\>edge\[0\].weight = \-1; graph-\>edge\[1\].src = 0; graph-\>edge\[1\].dest = 2; graph-\>edge\[1\].weight = 4; graph-\>edge\[2\].src = 1; graph-\>edge\[2\].dest = 2; graph-\>edge\[2\].weight = 3; graph-\>edge\[3\].src = 1; graph-\>edge\[3\].dest = 3; graph-\>edge\[3\].weight = 2; graph-\>edge\[4\].src = 1; graph-\>edge\[4\].dest = 4; graph-\>edge\[4\].weight = 2; graph-\>edge\[5\].src = 3; graph-\>edge\[5\].dest = 2; graph-\>edge\[5\].weight = 5; graph-\>edge\[6\].src = 3; graph-\>edge\[6\].dest = 1; graph-\>edge\[6\].weight = 1; graph-\>edge\[7\].src = 4; graph-\>edge\[7\].dest = 3; graph-\>edge\[7\].weight = \-3; BellmanFord(graph, 0); return 0; } | write your code here: [Coding Playground](https://www.boardinfinity.com/playground/coding?ref=boardinfinity.com) **Output:** Vertex : 0 1 2 3 4 Distance (from the source) ## **Complexity Analysis** The graph might present a worst-case scenario when you encounter a negative cycle. You would need to relax \|E\| \* (\|E\| - 1) / 2 edges, (\|V\| - 1) number of times in a full graph with edges connecting every pair of vertices, assuming you identified the shortest route in the first few iterations or repetitions but continued with edge relaxation. The complete graph will lead to the worst case scenario where the time complexity will be: **O(\|V\|^2) = O(E V). O(\|V\|) = O (V^3)** ## **Applications of Bellman Ford Algorithm** 1. Finding weight-loss relapse cycles 2. Calculate the least amount of heat that can acquire or lose during a chemical process. 3. Figuring out the most effective currency conversion technique. looking at a graph to see if there are any negative weight cycles. 4. Find the graph's shortest route using negative weights. 5. In data networks, the idea of routing is applied. ### Mark Lesson Complete (Bellman Ford Algorithm in detail with code) Mark Complete [![](https://cdn.boardinfinity.com/logos/BI+Logo.svg)](https://www.boardinfinity.com/) Follow us on [![Facebook](https://cdn.boardinfinity.com/icons/facebook-icon.svg)](https://www.facebook.com/boardinfinity/) [![YouTube](https://cdn.boardinfinity.com/icons/youtube-icon.svg)](https://www.youtube.com/channel/UCFlNE8f8ReUAHLgkqkHHQhw) [![LinkedIn](https://cdn.boardinfinity.com/icons/linkedin-icon.svg)](https://www.linkedin.com/company/board-infinity/mycompany/) [![Twitter](https://cdn.boardinfinity.com/icons/twitter-icon.svg)](https://twitter.com/boardinfinity) [![Instagram](https://cdn.boardinfinity.com/icons/instagram-icon.svg)](https://www.instagram.com/accounts/login/?next=/boardinfinity/) ![](https://cdn.boardinfinity.com/home/location-icon.svg) Raghuleela Mall, Plot No 34, 2nd Floor AWFIS Coworking, Sector 30A, Vashi, Navi Mumbai, Maharashtra 400703 #### Connect with us - [connect@boardinfinity.com](mailto:connect@boardinfinity.com) - [\+91 88796 54341](tel:+918879654341) #### Explore BI - [About us](https://www.linkedin.com/company/board-infinity/) - [InfyLearn](https://www.infylearn.com/) - [Leadup Universe](https://www.leadupuniverse.com/) - [Employers of the Future](https://www.leadupuniverse.com/eotf) - [For Higher-Ed Colleges](https://www.boardinfinity.com/college/) #### Free Learning Resources - [Courses listed on Coursera](https://www.coursera.org/instructor/~128073001) - [All Courses](https://www.boardinfinity.com/programs/) - [Blogs](https://www.boardinfinity.com/) #### Help & Support - [Raise a ticket](https://help.boardinfinity.com/portal/en/newticket) - [Write to us](mailto:support@boardinfinity.com) *** [Privacy Policy](https://www.boardinfinity.com/blog/bellman-ford-algorithm/) \| [Disclaimer](https://www.boardinfinity.com/blog/bellman-ford-algorithm/) Copyright Ā© Board Infinity 2026
Readable Markdown
## **Introduction** In this article you will understand the Bellman-Ford algorithm step-by-step, using the example as a guide. C++ implementation with the code for calculating the distance from the source vertex in a weighted graph is also covered in this article. ## **Approach** The Bellman-Ford algorithm imitates the shortest routes in a weighted digraph from a single site vertex to all other vertices. Since it can handle graphs with certain edge weights that are negative values, it is more flexible than Dijkstra's method for the identical issue. The Bellman-Ford algorithm makes use of dynamic programming. It starts with a beginning vertex and determines the shortest paths an edge can take to reach additional vertices. The process then continues in search of a path with two edges. The bottom-up method is employed by the Bellman-Ford algorithm. ## **Negative Cycles** Negative weight edges can produce negative weight cycles that, by going back to the same location, shorten the overall path length. Negative weight cycles could provide the wrong answer while trying to identify the shortest path. Because they may experience a negative weight cycle that shortens the trip, shortest path methods like Dijkstra's Algorithm that cannot identify such a cycle may generate inaccurate results. **Steps** - **Step 1:** Make a list of every edge in the graph. If the graph is represented as an adjacency list, then this is easy. - **Step 2:** The number of iterations is calculated using the formula "V - 1". The number of iterations will grow the same number of vertices since the shortest path to an edge can only be changed V - 1 times. - **Step 3**: Start with a random vertex at a distance of 0 minimum. You are overstating the real lengths, hence all other nodes ought to be given a distance of infinite. Relax the vertices' route lengths for each edge u-v: - Distance\[v\] = distance\[u\] + edge weight uv if distance\[v\] is larger than distance\[u\] + edge weight uv. - **Step 4:** Alter the distance for each Edge in every iteration if the new distance is smaller than the old one. The distance covered from the beginning node to each individual node is the distance to each node. - **Step 5:** You must take alliterations into account to make sure all potential pathways are taken into account. If you accomplish this, the distance you have traveled will be the shortest. ## **Code** | | |---| | \#include \<bits/stdc++.h\> struct Edge { int src, dest, weight; }; struct Graph { int V, E; struct Edge\* edge; }; struct Graph\* createGraph(int V, int E) { struct Graph\* graph = new Graph; graph-\>V = V; graph-\>E = E; graph-\>edge = new Edge\[E\]; return graph; } void BellmanFord(struct Graph\* graph, int src) { int V = graph-\>V; int E = graph-\>E; int dist\[V\]; for (int i = 0; i \< V; i++) dist\[i\] = INT\_MAX; dist\[src\] = 0; for (int i = 1; i \<= V - 1; i++) { for (int j = 0; j \< E; j++) { int u = graph-\>edge\[j\].src; int v = graph-\>edge\[j\].dest; int weight = graph-\>edge\[j\].weight; if (dist\[u\] != INT\_MAX && dist\[u\] + weight \< dist\[v\]) dist\[v\] = dist\[u\] + weight; } } for (int i = 0; i \< E; i++) { int u = graph-\>edge\[i\].src; int v = graph-\>edge\[i\].dest; int weight = graph-\>edge\[i\].weight; if (dist\[u\] != INT\_MAX && dist\[u\] + weight \< dist\[v\]) { printf("Graph contains negative weight cycle"); return; } } printf("Vertex :\\t\\t\\t "); for (int i = 0; i \< V; ++i) printf("%d \\t", i); printf("\\nDistance (from the source): "); for (int i = 0; i \< V; ++i) printf("%d \\t",dist\[i\]); return; } int main() { int V = 5; int E = 8; struct Graph\* graph = createGraph(V, E); graph-\>edge\[0\].src = 0; graph-\>edge\[0\].dest = 1; graph-\>edge\[0\].weight = \-1; graph-\>edge\[1\].src = 0; graph-\>edge\[1\].dest = 2; graph-\>edge\[1\].weight = 4; graph-\>edge\[2\].src = 1; graph-\>edge\[2\].dest = 2; graph-\>edge\[2\].weight = 3; graph-\>edge\[3\].src = 1; graph-\>edge\[3\].dest = 3; graph-\>edge\[3\].weight = 2; graph-\>edge\[4\].src = 1; graph-\>edge\[4\].dest = 4; graph-\>edge\[4\].weight = 2; graph-\>edge\[5\].src = 3; graph-\>edge\[5\].dest = 2; graph-\>edge\[5\].weight = 5; graph-\>edge\[6\].src = 3; graph-\>edge\[6\].dest = 1; graph-\>edge\[6\].weight = 1; graph-\>edge\[7\].src = 4; graph-\>edge\[7\].dest = 3; graph-\>edge\[7\].weight = \-3; BellmanFord(graph, 0); return 0; } | **Output:** Vertex : 0 1 2 3 4 Distance (from the source) ## **Complexity Analysis** The graph might present a worst-case scenario when you encounter a negative cycle. You would need to relax \|E\| \* (\|E\| - 1) / 2 edges, (\|V\| - 1) number of times in a full graph with edges connecting every pair of vertices, assuming you identified the shortest route in the first few iterations or repetitions but continued with edge relaxation. The complete graph will lead to the worst case scenario where the time complexity will be: **O(\|V\|^2) = O(E V). O(\|V\|) = O (V^3)** ## **Applications of Bellman Ford Algorithm** 1. Finding weight-loss relapse cycles 2. Calculate the least amount of heat that can acquire or lose during a chemical process. 3. Figuring out the most effective currency conversion technique. looking at a graph to see if there are any negative weight cycles. 4. Find the graph's shortest route using negative weights. 5. In data networks, the idea of routing is applied.
Shard35 (laksa)
Root Hash10198582622409533635
Unparsed URLcom,boardinfinity!www,/blog/bellman-ford-algorithm/ s443