Graphs
Shortest Path (Unweighted, via BFS)
BFS explores a graph layer by layer, so the first time it reaches a vertex
is along a shortest path. Track dist[v] and parent[v] while exploring,
then walk parents back from the target to reconstruct the route.
Algorithm
On the canonical graph from graph-adjacency-list, the shortest path from
1 to 6 is [1, 2, 4, 5, 6] with distance 4. The path is rebuilt from parent:
6 -> 5 -> 4 -> 2 -> 1, reversed.
Basic Implementation
basic.rs
use std::collections::HashMap;
use std::collections::VecDeque;
fn main() {
let mut adj: HashMap<i32, Vec<i32>> = HashMap::new();
adj.insert(1, vec![2, 3]);
adj.insert(2, vec![1, 4]);
adj.insert(3, vec![1, 4]);
adj.insert(4, vec![2, 3, 5]);
adj.insert(5, vec![4, 6]);
adj.insert(6, vec![5]);
let src = 1;
let dst = 6;
let mut dist: HashMap<i32, i32> = HashMap::new();
let mut parent: HashMap<i32, i32> = HashMap::new();
dist.insert(src, 0);
parent.insert(src, 0);
let mut queue: VecDeque<i32> = VecDeque::new();
queue.push_back(src);
while let Some(v) = queue.pop_front() {
for &nb in &adj[&v] {
if !dist.contains_key(&nb) {
let d = dist[&v] + 1;
dist.insert(nb, d);
parent.insert(nb, v);
queue.push_back(nb);
}
}
}
let mut path: Vec<i32> = Vec::new();
let mut node = dst;
while node != 0 {
path.push(node);
node = parent[&node];
}
path.reverse();
println!("{:?}", path);
println!("{}", dist[&dst]);
}
Complexity
- Time: O(V + E)
- Space: O(V)
Implementation notes
- Rust: a
distHashMap doubles as the visited check,parentrecords predecessors (0 marks the source), and aVecDequegives FIFO order. - The replay shows
dist,parent, and the queue filling in, then the reconstructed path, matching the lesson spec.
layers equal distance
BFS order equals distance in an unweighted graph.