bfs

Function bfs 

Source
pub fn bfs<G>(graph: &G, start: NodeId) -> HashMap<NodeId, usize>
where G: Graph,
Expand description

Runs a breadth-first search from start and returns a map of NodeId → shortest hop distance from start.

The distance to start itself is 0. Only nodes reachable from start appear in the map.

§Examples

use graph_core::{AdjacencyList, Graph};
use graph_traversal::bfs;

let mut g: AdjacencyList<()> = AdjacencyList::directed();
let a = g.add_node(());
let b = g.add_node(());
let c = g.add_node(());
g.add_edge(a, b, 1.0).unwrap();
g.add_edge(b, c, 1.0).unwrap();

let dist = bfs(&g, a);
assert_eq!(dist[&a], 0);
assert_eq!(dist[&b], 1);
assert_eq!(dist[&c], 2);