Input: V = 5, edges = [[1, 3, 2], [4, 3, -1], [2, 4, 1], [1, 2, 1], [0, 1, 5]], src = 0
ExampleOutput: [0, 5, 6, 6, 7]
Input: V = 4, edges = [[0, 1, 4], [1, 2, -6], [2, 3, 5], [3, 1, -2]], src = 0
ExampleOutput: [-1]
Explanation: The graph contains negative weight cycle.
The Bellman-Ford algorithm works by traversing all edges |V| - 1
times, where |V|
is the number of vertices. After each iteration, the algorithm relaxes all the edges. If a shorter path is found from the source to a vertex during any iteration, then the distance of that vertex is updated.
Output{'A': 0, 'B': -1, 'C': 2, 'D': -2, 'E': 1}