Complete the code to initialize the distance array with infinity values.
dist := make([]int, V) for i := 0; i < V; i++ { dist[i] = [1] }
We use math.MaxInt32 to represent infinity for initial distances.
Complete the code to relax edges in the Bellman-Ford algorithm.
for i := 0; i < E; i++ { u := edges[i].src v := edges[i].dest w := edges[i].weight if dist[u] != math.MaxInt32 && dist[u] + w [1] dist[v] { dist[v] = dist[u] + w } }
We relax edges only if the current distance to u plus weight w is less than the current distance to v.
Fix the error in the negative cycle detection condition.
for i := 0; i < E; i++ { u := edges[i].src v := edges[i].dest w := edges[i].weight if dist[u] != math.MaxInt32 && dist[u] + w [1] dist[v] { return true // Negative cycle detected } }
Negative cycle exists if we can still relax an edge, meaning dist[u] + w is less than dist[v].
Fill both blanks to complete the main loop and relaxation step.
for i := 1; i < [1]; i++ { for j := 0; j < E; j++ { u := edges[j].src v := edges[j].dest w := edges[j].weight if dist[u] != math.MaxInt32 && dist[u] + w [2] dist[v] { dist[v] = dist[u] + w } } }
The outer loop runs V-1 times, and relaxation happens if dist[u] + w is less than dist[v].
Fill all three blanks to create a map of vertex to distance for vertices with distance less than infinity.
result := map[int]int{}
for i := 0; i < V; i++ {
if dist[i] [1] math.MaxInt32 {
result[[2]] = [3]
}
}We add vertices with finite distances to the result map using their index and distance.
