5. You want to find the shortest path length from node 0 to node 4 in an unweighted graph using BFS. Which modification to the BFS code below will correctly compute the shortest distance?
use std::collections::VecDeque;
fn shortest_path_length(graph: &Vec, start: usize, target: usize) -> Option<usize> {
let mut visited = vec![false; graph.len()];
let mut distance = vec![0; graph.len()];
let mut queue = VecDeque::new();
visited[start] = true;
queue.push_back(start);
while let Some(node) = queue.pop_front() {
if node == target {
return Some(distance[node]);
}
for &neighbor in &graph[node] {
if !visited[neighbor] {
visited[neighbor] = true;
queue.push_back(neighbor);
// What to add here?
}
}
}
None
}
fn main() {
let graph = vec![
vec![1, 2],
vec![0, 3],
vec![0, 4],
vec![1],
vec![2]
];
let result = shortest_path_length(&graph, 0, 4);
println!("{:?}", result);
}