Bellman Ford
Problem Statement
Given a weighted, directed and connected graph of V vertices and E edges, Find the shortest distance of all the vertex's from the source vertex S. If a vertices can't be reach from the S then mark the distance as 10^8. Note: If the Graph contains a negative cycle then return an array consisting of only -1.
Example 1:
Input:
E = [[0,1,9]]
S = 0
Output:
0 9
Explanation:
Shortest distance of all nodes from
source is printed.
Example 2:
Input:
E = [[0,1,5],[1,0,3],[1,2,-1],[2,0,1]]
S = 2
Output:
1 6 0
Explanation:
For nodes 2 to 0, we can follow the path-
2-0. This has a distance of 1.
For nodes 2 to 1, we cam follow the path-
2-0-1, which has a distance of 1+5 = 6,
Your Task: You don't need to read input or print anything. Your task is to complete the function bellman_ford( ) which takes a number of vertices V and an E-sized list of lists of three integers where the three integers are u,v, and w; denoting there's an edge from u to v, which has a weight of w and source node S as input parameters and returns a list of integers where the ith integer denotes the distance of an ith node from the source node.
If some node isn't possible to visit, then its distance should be 100000000(1e8). Also, If the Graph contains a negative cycle then return an array consisting of a single -1.
Intuition
Relax The edges V-1 times , Extra 1 to check cycle
TC = V*E
Links
https://practice.geeksforgeeks.org/problems/distance-from-the-source-bellman-ford-algorithm/1
Video Links
https://www.youtube.com/watch?v=0vVofAhAYjc&ab_channel=takeUforward
Approach 1:
vector<int> bellman_ford(int v, vector<vector<int>>& edges, int s) {
vector<int> distance (v, 1e8);
distance[s] = 0;
for(int i = 0; i < v-1; i++) {
for(auto &it: edges){
int src = it[0];
int des = it[1];
int dis = it[2];
if( distance[src] + dis < distance[des] )
distance[des] = distance[src] + dis;
}
}
for(auto &it: edges){
int src = it[0];
int des = it[1];
int dis = it[2];
if( distance[src] + dis < distance[des] ){
return {-1};
}
}
return distance;
}
Approach 2:
Approach 3:
Approach 4:
Similar Problems
Last updated