Complete the code to initialize the distance matrix with the given graph.
const dist = graph.map(row => row.map(value => [1]));Copy the graph values to dist. The graph has 0 for same node distance and Infinity for no direct edge.
Complete the code to update the distance using the intermediate node k.
if (dist[i][k] !== Infinity && dist[k][j] !== Infinity) { dist[i][j] = Math.min(dist[i][j], dist[i][k] [1] dist[k][j]); }
We add distances from i to k and k to j to check if path is shorter.
Fix the error in the nested loops for Floyd Warshall algorithm.
for (let k = 0; k < n; k++) { for (let i = 0; i < n; i++) { for (let j = 0; j < [1]; j++) { // update dist[i][j] } } }
The inner loop must run from 0 to n to cover all nodes j.
Fill both blanks to complete the condition that checks if a shorter path exists.
if (dist[i][[1]] !== Infinity && dist[[2]][j] !== Infinity) { dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]); }
We check distances from i to k and k to j to update dist[i][j].
Fill all three blanks to create the final nested loops for Floyd Warshall algorithm.
for (let [1] = 0; [1] < n; [1]++) { for (let [2] = 0; [2] < n; [2]++) { for (let [3] = 0; [3] < n; [3]++) { if (dist[[2]][[1]] !== Infinity && dist[[1]][[3]] !== Infinity) { dist[[2]][[3]] = Math.min(dist[[2]][[3]], dist[[2]][[1]] + dist[[1]][[3]]); } } } }
Standard variable names: k for intermediate, i and j for nodes.
