Problem
Section titled “Problem”Given a weighted graph with V vertices numbered from 0 to V-1 and edges, represented by a 2d array edges[][], where edges[i] = [u, v, w] represents a direct edge from node u to v having w edge weight. You are also given a source vertex src.
Compute the shortest distances from the src to all other vertices. If a vertex is unreachable from the src, its distance should be marked as . Additionally, if the graph contains a negative weight cycle, return [-1] to indicate that shortest paths cannot be reliably computed.
Complete the function bellmanFord() which takes V, edges and src and returns the list of distances.
Example 1:

Input: V = 5, edges[][] = [[1, 3, 2], [4, 3, -1], [2, 4, 1], [1, 2, 1], [0, 1, 5]], src = 0Output: [0, 5, 6, 6, 7]Explanation: Shortest Paths:For 0 to 1 minimum distance will be 5. By following path 0 -> 1For 0 to 2 minimum distance will be 6. By following path 0 -> 1 -> 2For 0 to 3 minimum distance will be 6. By following path 0 -> 1 -> 2 -> 4 -> 3For 0 to 4 minimum distance will be 7. By following path 0 -> 1 -> 2 -> 4Example 2:

Input: V = 4, edges[][] = [[0, 1, 4], [1, 2, -6], [2, 3, 5], [3, 1, -2]], src = 0Output: [-1]Explanation: The graph contains a negative weight cycle formed by the path 1 -> 2 -> 3 -> 1, where the total weight of the cycle is negative.Constraints:
-
edges.size() -
src
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Relaxing an edge u → v of weight w means trying dist[u] + w as a better value for dist[v]. If there is no negative cycle, every shortest path is simple, so it uses at most edges. After round of relaxing all edges, every vertex whose shortest path has at most edges already has its correct distance (by induction: the last edge of that path is relaxed in round with its predecessor already correct). So rounds are enough — and unlike Dijkstra, this does not care whether weights are negative.
That same fact gives the negative-cycle test: if after rounds some edge can still be relaxed, distances are still decreasing, which is only possible if a negative cycle is reachable from src.
Algorithm
Section titled “Algorithm”- Set
dist[i] = INF() for every vertex anddist[src] = 0. - Repeat up to
V - 1rounds. In each round, for every edge[u, v, w]withdist[u] != INF, setdist[v] = dist[u] + wif that is smaller, and remember that somethingchanged. If a whole round changes nothing, the distances are final, so stop early. - Do one more pass over the edges. If any reachable edge can still be relaxed, return
{-1}. - Otherwise return
dist; unreachable vertices still holdINF= , as required.
Walkthrough
Section titled “Walkthrough”Example 1, with the edges relaxed in input order. Because the edge 0 → 1 comes last in the list, each round only pushes correct distances one edge further — this is exactly why up to rounds are needed:
Complexity
Section titled “Complexity”- Time: — at most rounds plus the check pass, each scanning all edges.
- Space: — the
distarray (the edge list is read in place).
Edge cases
Section titled “Edge cases”- Unreachable vertices with negative outgoing edges: edges are relaxed only when
dist[u] != INF, so “infinity minus something” never leaks intodist[v], and those vertices stay at . - Negative cycle not reachable from
src: its edges are never relaxed, so it does not cause{-1}; the distances fromsrcare still well defined. - Overflow: distances are bounded by in absolute value, far within
int.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: vector<int> bellmanFord(int V, vector<vector<int>>& edges, int src) { const int INF = 100000000; vector<int> dist(V, INF); dist[src] = 0;
for (int round = 1; round < V; round++) { bool changed = false; for (const auto& e : edges) { int u = e[0], v = e[1], w = e[2]; if (dist[u] != INF && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; changed = true; } } if (!changed) break; // distances are already final }
// An edge that can still be relaxed means a reachable negative cycle. for (const auto& e : edges) { int u = e[0], v = e[1], w = e[2]; if (dist[u] != INF && dist[u] + w < dist[v]) return {-1}; } return dist; }};class Solution { public ArrayList<Integer> bellmanFord(int V, int[][] edges, int src) { final int INF = 100000000; int[] dist = new int[V]; Arrays.fill(dist, INF); dist[src] = 0;
for (int round = 1; round < V; round++) { boolean changed = false; for (int[] e : edges) { int u = e[0], v = e[1], w = e[2]; if (dist[u] != INF && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; changed = true; } } if (!changed) break; // distances are already final }
// An edge that can still be relaxed means a reachable negative cycle. for (int[] e : edges) { int u = e[0], v = e[1], w = e[2]; if (dist[u] != INF && dist[u] + w < dist[v]) return new ArrayList<>(List.of(-1)); } ArrayList<Integer> ans = new ArrayList<>(V); for (int d : dist) ans.add(d); return ans; }}class Solution: def bellmanFord(self, V: int, edges: list[list[int]], src: int) -> list[int]: INF = 10**8 dist = [INF] * V dist[src] = 0
for _ in range(V - 1): changed = False for u, v, w in edges: if dist[u] != INF and dist[u] + w < dist[v]: dist[v] = dist[u] + w changed = True if not changed: break # distances are already final
# An edge that can still be relaxed means a reachable negative cycle. for u, v, w in edges: if dist[u] != INF and dist[u] + w < dist[v]: return [-1] return distGive it a try first
Attempt the problem on GeeksforGeeks — the code will still be here.





