bfs_tree

Function bfs_tree 

Source
pub fn bfs_tree<G>(graph: &G, start: NodeId) -> BfsTree
where G: Graph,
Expand description

Runs BFS from start and returns a BfsTree with distances and parents.

Use reconstruct_path on tree.parent to extract the shortest path to any reachable node.

ยงExamples

use graph_core::{AdjacencyList, Graph};
use graph_traversal::{bfs_tree, reconstruct_path};

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 tree = bfs_tree(&g, a);
let path = reconstruct_path(&tree.parent, a, c).unwrap();
assert_eq!(path, vec![a, b, c]);