Bellman-Ford Algorithm - Problem

The Bellman-Ford algorithm is a single-source shortest path algorithm that can handle graphs with negative edge weights, unlike Dijkstra's algorithm. It can also detect negative weight cycles in the graph.

Given a weighted directed graph represented as an edge list and a source vertex, implement the Bellman-Ford algorithm to:

1. Find the shortest distances from the source to all other vertices

2. Detect if there's a negative weight cycle reachable from the source

Return a result object containing:

  • distances: Array of shortest distances from source to each vertex (use float('inf') for unreachable vertices)
  • hasNegativeCycle: Boolean indicating whether a negative weight cycle exists

If a negative cycle exists, the distances array should still contain the distances computed before cycle detection.

Input & Output

Example 1 — Basic Graph with Negative Edge
$ Input: n = 5, edges = [[0,1,4],[0,2,2],[1,2,-3],[2,3,3],[1,3,2],[1,4,3],[3,4,1]], source = 0
Output: {"distances":[0,4,-1,2,3],"hasNegativeCycle":false}
💡 Note: From source 0: direct to 1 costs 4, direct to 2 costs 2, then 2→1 with weight -3 makes path 0→2→1 cost -1, which is better than direct path to 1. No negative cycles exist.
Example 2 — Graph with Negative Cycle
$ Input: n = 3, edges = [[0,1,1],[1,2,-3],[2,1,1]], source = 0
Output: {"distances":[0,1,-2],"hasNegativeCycle":true}
💡 Note: Path 0→1→2 costs -2, but vertices 1 and 2 form a negative cycle (1→2→1 = -3+1 = -2), allowing infinite cost reduction.
Example 3 — Disconnected Graph
$ Input: n = 4, edges = [[0,1,2],[2,3,-1]], source = 0
Output: {"distances":[0,2,"Infinity","Infinity"],"hasNegativeCycle":false}
💡 Note: Source 0 can reach vertex 1 with cost 2, but vertices 2 and 3 are unreachable from source 0, so their distances remain infinity.

Constraints

  • 1 ≤ n ≤ 100
  • 0 ≤ edges.length ≤ n*(n-1)
  • edges[i] = [u, v, weight] where 0 ≤ u, v < n
  • -100 ≤ weight ≤ 100
  • 0 ≤ source < n

Visualization

Tap to expand
INPUT GRAPHALGORITHM STEPSFINAL RESULT0123442-3Source: Vertex 0Edges: 7 totalNegative edge: 1→2 (-3)1Initialize: d[0]=0, others=∞2Iteration 1: Relax all edges3Iteration 2: Find better paths4Iteration 3: More improvements5Check for negative cyclesV-1 = 4 relaxation rounds+ 1 cycle detection roundSHORTEST DISTANCESd[0] = 0 d[3] = 2d[1] = 4 d[4] = 3d[2] = -1CYCLE DETECTIONNo negative cycle foundAlgorithm terminatesFinal check: no distanceimprovements in extra passKey Insight:The Bellman-Ford algorithm can detect negative cycles because if distanceskeep improving after V-1 iterations, there must be a cycle that reduces costs infinitely.This makes it unique among shortest-path algorithms for handling negative weights.TutorialsPoint - Bellman-Ford Algorithm | Standard Implementation
Asked in
Google 35 Amazon 28 Microsoft 22 Facebook 18
23.4K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen