50. Bellman Ford Algo Code
Medium

Problem Statement

Implementation of the Bellman-Ford algorithm to compute shortest paths from a source node, and return [-1] if a negative weight cycle is detected.

Examples

1Example 1
Input:
{ "V": 5, "edges": [ [ 1, 3, 2 ], [ 4, 3, -1 ], [ 2, 4, 1 ], [ 1, 2, 1 ], [ 0, 1, 5 ] ], "S": 0 }
Output:
[ 0, 5, 6, 6, 7 ]
2Example 2
Input:
{ "V": 3, "edges": [ [ 0, 1, 5 ], [ 1, 2, -2 ], [ 2, 0, -5 ] ], "S": 0 }
Output:
[ -1 ]
3Example 3
Input:
{ "V": 4, "edges": [ [ 0, 1, 4 ], [ 0, 2, 8 ], [ 1, 2, 2 ], [ 2, 3, 3 ] ], "S": 0 }
Output:
[ 0, 4, 6, 9 ]
Loading...

Sign in to Run Code and Submit