What is the output of the BFS traversal starting from node 0?
use std::collections::{VecDeque, HashMap};
fn bfs(graph: &HashMap<i32, Vec<i32>>, start: i32) -> Vec<i32> {
let mut visited = std::collections::HashSet::new();
let mut queue = VecDeque::new();
let mut order = Vec::new();
visited.insert(start);
queue.push_back(start);
while let Some(node) = queue.pop_front() {
order.push(node);
if let Some(neighbors) = graph.get(&node) {
for &neighbor in neighbors {
if !visited.contains(&neighbor) {
visited.insert(neighbor);
queue.push_back(neighbor);
}
}
}
}
order
}
fn main() {
let mut graph = HashMap::new();
graph.insert(0, vec![1, 2]);
graph.insert(1, vec![2]);
graph.insert(2, vec![0, 3]);
graph.insert(3, vec![3]);
let result = bfs(&graph, 0);
println!("{:?}", result);
}Remember BFS visits neighbors level by level, starting from the start node.
BFS starts at node 0, visits neighbors 1 and 2 in order, then visits neighbors of 1 (which is 2, already visited), then neighbors of 2 (0 and 3), 0 is visited, so 3 is added last.
In BFS, what prevents infinite loops when traversing a graph with cycles?
Think about how BFS avoids revisiting the same node multiple times.
BFS uses a visited set to mark nodes already seen, so it does not enqueue them again, preventing infinite loops in cyclic graphs.
What error will this BFS code produce when run?
use std::collections::{VecDeque, HashMap};
fn bfs(graph: &HashMap<i32, Vec<i32>>, start: i32) -> Vec<i32> {
let mut visited = std::collections::HashSet::new();
let mut queue = VecDeque::new();
let mut order = Vec::new();
queue.push_back(start);
while let Some(node) = queue.pop_front() {
order.push(node);
if let Some(neighbors) = graph.get(&node) {
for &neighbor in neighbors {
if !visited.contains(&neighbor) {
visited.insert(neighbor);
queue.push_back(neighbor);
}
}
}
}
order
}
fn main() {
let mut graph = HashMap::new();
graph.insert(0, vec![1, 2]);
graph.insert(1, vec![2]);
graph.insert(2, vec![0, 3]);
graph.insert(3, vec![3]);
let result = bfs(&graph, 0);
println!("{:?}", result);
}Check if the start node is marked visited before starting the loop.
The start node is never added to the visited set before enqueueing, so it can be re-added multiple times causing infinite loop.
Given an unweighted graph, which BFS modification correctly computes the shortest path length from start to all nodes?
Think about how BFS layers correspond to distance from start.
BFS visits nodes in increasing order of distance from start. Recording distance when first visiting a node gives shortest path length.
What is the output of BFS starting from node 1 on this disconnected graph?
use std::collections::{VecDeque, HashMap};
fn bfs(graph: &HashMap<i32, Vec<i32>>, start: i32) -> Vec<i32> {
let mut visited = std::collections::HashSet::new();
let mut queue = VecDeque::new();
let mut order = Vec::new();
visited.insert(start);
queue.push_back(start);
while let Some(node) = queue.pop_front() {
order.push(node);
if let Some(neighbors) = graph.get(&node) {
for &neighbor in neighbors {
if !visited.contains(&neighbor) {
visited.insert(neighbor);
queue.push_back(neighbor);
}
}
}
}
order
}
fn main() {
let mut graph = HashMap::new();
graph.insert(0, vec![1]);
graph.insert(1, vec![]);
graph.insert(2, vec![3]);
graph.insert(3, vec![]);
let result = bfs(&graph, 1);
println!("{:?}", result);
}Consider which nodes are reachable from the start node.
Starting from node 1, which has no neighbors, BFS visits only node 1.
