Complete the code to initialize the distance matrix with the input graph.
let mut dist = graph.clone(); for i in 0..n { for j in 0..n { if i != j && dist[i][j] == 0 { dist[i][j] = [1]; } } }
We use usize::MAX to represent infinity for unreachable paths initially.
Complete the code to update the distance matrix inside the triple nested loops.
for k in 0..n { for i in 0..n { for j in 0..n { if dist[i][k] != [1] && dist[k][j] != [1] { dist[i][j] = dist[i][j].min(dist[i][k] + dist[k][j]); } } } }
We check if paths are not infinite before adding to avoid overflow.
Fix the error in the code to correctly print the shortest path matrix after computation.
for i in 0..n { for j in 0..n { if dist[i][j] == [1] { print!("INF "); } else { print!("{} ", dist[i][j]); } } println!(); }
We print "INF" for distances that remain infinite (usize::MAX).
Fill both blanks to complete the condition that updates the distance matrix only if a shorter path is found.
if dist[i][k] != [1] && dist[k][j] != [2] { let new_dist = dist[i][k] + dist[k][j]; if new_dist < dist[i][j] { dist[i][j] = new_dist; } }
We check both distances against infinity before adding to avoid overflow.
Fill all three blanks to complete the Rust function signature and return statement for Floyd Warshall.
fn floyd_warshall([1]: [2]) -> Vec<Vec<usize>> { let n = graph.len(); let mut dist = graph.clone(); // initialization and loops here [3] }
The function takes a reference to a 2D vector named graph and returns the distance matrix dist.
