Function bfs_predecessors

Source
pub fn bfs_predecessors<G>(
    graph: G,
    node: G::NodeId,
) -> impl Iterator<Item = (G::NodeId, Vec<G::NodeId>)>
Expand description

Return the predecessor 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:

(Child Node, vec![parent nodes])

§Arguments:

  • graph - The graph to search
  • node - 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 child and a Vec of node ids it’s predecessors. If a node in the bfs traversal doesn’t have any predecessors it will still be present but contain an empty vec.

§Example

use rustworkx_core::traversal::bfs_predecessors;
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 predecessors: Vec<(usize, Vec<usize>)> = bfs_predecessors(&graph, NodeIndex::new(3))
    .map(|(x, succ)| (x.index(), succ.iter().map(|y| y.index()).collect()))
    .collect();
assert_eq!(vec![(3_usize, vec![1_usize]), (1, vec![0]), (0, vec![])], predecessors);