Complete the code to initialize the distance array with infinity.
for (int i = 0; i < V; i++) { dist[i] = [1]; }
We use INT_MAX to represent infinity for initial distances.
Complete the code to relax edges inside the main loop.
if (dist[u] != [1] && dist[u] + weight < dist[v]) { dist[v] = dist[u] + weight; }
We check if dist[u] is not infinity (INT_MAX) before relaxing.
Fix the error in the negative cycle detection condition.
if (dist[u] != [1] && dist[u] + weight < dist[v]) { printf("Graph contains negative weight cycle\n"); return 0; }
We must check dist[u] is not INT_MAX before detecting negative cycles.
Fill both blanks to complete the edge relaxation loop.
for (int i = 1; i <= [1] - 1; i++) { for (int j = 0; j < [2]; j++) { int u = edges[j].src; int v = edges[j].dest; int weight = edges[j].weight; if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) { dist[v] = dist[u] + weight; } } }
V-1 instead of V for outer loop causes insufficient iterations.E-1 for inner loop misses the last edge.The outer loop runs V-1 times and the inner loop runs over all E edges.
Fill all three blanks to print the shortest distances after Bellman-Ford completes.
printf("Vertex Distance from Source\n"); for (int i = 0; i < [1]; i++) { if (dist[i] == [2]) { printf("%d -> INF\n", [3]); } else { printf("%d -> %d\n", i, dist[i]); } }
Loop over all vertices V, check if distance is INT_MAX to print INF, and print vertex i.
