Harsh Shah
Bellman Ford Algorithm is an algorithm to find Single Source Shortest Paths similar to the except the fact that it allows the use of negative edge weights but not a negative cycle .
def BellmanFord(adj, source):
rows, cols, x = adj.shape
distance = {k: float('inf') for k in range(rows)}
distance[source] = 0
for i in range(rows):
for u in range(rows):
for j in range(cols):
if adj[i, j, 0] == 1:
distance[j] = min(distance[j], distance[i] + adj[i, j, 1])
return distance
def BellmanFord(adj, source):
vertices = len(adj.keys())
distance = {k: float('inf') for k in range(vertices)}
distance[source] = 0
for i in adj:
for u in adj:
for vertex, dist in adj[u]:
distance[vertex] = min(distance[vertex], distance[u] + dist)
return distance
Using the numpy method the time complexity is .Shifting to the Adjacency List the time complexity becomes where is the number of edges and is the number of vertices.
I write articles on web dev and more specifically on react and next js.