pub fn bfs_successors<G>(
graph: G,
node: G::NodeId,
) -> impl Iterator<Item = (G::NodeId, Vec<G::NodeId>)>
Expand description
Return the successors in a breadth-first-search from a source node
Each iterator step returns the node indices in a bfs order from the specified node in the form:
(Parent Node, vec![children nodes])
§Arguments:
graph
- The graph to searchnode
- The node to search from
§Returns
An iterator of nodes in BFS order where each item in the iterator
is a tuple of the NodeId
for the parent and a Vec
of node ids
it’s successors. If a node in the bfs traversal doesn’t have any
successors it will still be present but contain an empty vec.
§Example
use rustworkx_core::traversal::bfs_successors;
use rustworkx_core::petgraph::stable_graph::{StableDiGraph, NodeIndex};
let graph: StableDiGraph<(), ()> = StableDiGraph::from_edges(&[
(0, 1), (1, 2), (1, 3), (2, 4), (3, 4), (4, 5)
]);
let successors: Vec<(usize, Vec<usize>)> = bfs_successors(&graph, NodeIndex::new(3))
.map(|(x, succ)| (x.index(), succ.iter().map(|y| y.index()).collect()))
.collect();
assert_eq!(vec![(3_usize, vec![4_usize]), (4, vec![5]), (5, vec![])], successors);