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.
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 only -1.
 
Expected Time Complexity: O(V*E).
Expected Auxiliary Space: O(V).
 
Constraints:
1 ≤ V ≤ 500
1 ≤ E ≤ V*(V-1)
-1000 ≤ adj[i][j] ≤ 1000
0 ≤ S < V

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Dynamic programming

Graph algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    # Function to construct and return cost of MST for a graph

    # represented using adjacency matrix representation

    '''

    V: nodes in graph

    edges: adjacency list for the graph

    S: Source

    '''

    def bellman_ford(self, V, edges, S):

        #code here

Solutions:
import math

class Solution:

	def bellman_ford(self, V, edges, S):
		dis = [100000000 for i in range(V)]
		dis[S] = 0
		for i in range(V - 1):
			for (m, n, o) in edges:
				if dis[m] != 100000000 and dis[m] + o < dis[n]:
					dis[n] = dis[m] + o
		for (m, n, o) in edges:
			if dis[m] != 100000000 and dis[m] + o < dis[n]:
				return [-1]
		return dis


from collections import *
import heapq

class Solution:

	def bellman_ford(self, V, edges, src):
		ans = [100000000] * V
		ans[src] = 0
		for _ in range(V - 1):
			for (i, j, wt) in edges:
				if ans[i] + wt < ans[j]:
					ans[j] = ans[i] + wt
		for (i, j, wt) in edges:
			if ans[i] + wt < ans[j]:
				return [-1]
		return ans


class Solution:

	def bellman_ford(self, V, edges, S):
		G = convert(edges, V)
		return my_bellman_ford(G, S, V)

def convert(G, size):
	graph = [[] for _ in range(size + 1)]
	for (u, v, w) in G:
		graph[u].append((v, w))
	return graph

def my_bellman_ford(G, source, size):
	distance = [100000000] * size
	parent = [None] * size
	distance[source] = 0
	for i in range(size - 1):
		test = True
		for j in range(size):
			for (k, w) in G[j]:
				if distance[k] > distance[j] + w:
					test = False
					distance[k] = distance[j] + w
					parent[k] = j
		if test:
			break
	for j in range(size):
		for (k, w) in G[j]:
			if distance[k] > distance[j] + w:
				return [-1]
	return distance