Complete the code to create a queue for BFS traversal.
let mut queue = std::collections::VecDeque::new();
queue.[1](start_node);We use push_back to add the start node at the end of the queue for BFS.
Complete the code to get the current node from the front of the queue.
while let Some(current) = queue.[1]() { // process current node }
We use pop_front to remove and get the node at the front of the queue in BFS.
Fix the error in the code to mark neighbors as visited and update their distance.
for &neighbor in graph[[1]].iter() { if !visited[neighbor] { visited[neighbor] = true; distance[neighbor] = distance[current] + 1; queue.push_back(neighbor); } }
We access neighbors of the current node, so the index must be current.
Fill both blanks to initialize the distance vector and visited vector correctly.
let mut distance = vec![[1]; n]; let mut visited = vec![[2]; n];
Distance is set to max value to represent infinity, visited is false initially.
Fill all three blanks to set the start node distance, mark it visited, and push it to the queue.
distance[[1]] = [2]; visited[[3]] = true; queue.push_back(start_node);
Start node distance is 0, mark start node visited, and add start node to queue.
