Complete the code to initialize the distance to the source vertex as zero.
distance[source] = [1]The distance from the source vertex to itself is always zero in the Bellman-Ford algorithm.
Complete the code to relax an edge from u to v with weight w.
if distance[u] != [1] and distance[u] + w < distance[v]:
We check if the distance to u is not infinity before relaxing the edge, to ensure u is reachable.
Fix the error in the loop that runs the relaxation steps for all vertices.
for i in range([1] - 1):
The Bellman-Ford algorithm requires |V| - 1 iterations to relax all edges, where |V| is the number of vertices.
Fill both blanks to check for negative weight cycles after relaxation.
for u, v, w in edges: if distance[u] != [1] and distance[u] + w < [2]:
After all relaxations, if we can still relax an edge, it means a negative weight cycle exists.
Fill all three blanks to create a dictionary comprehension for distances after relaxation.
distances = {vertex: [1] for vertex in graph if graph[vertex] [2] [3]This comprehension sets distance 0 for vertices that are reachable (not None).