Function ancestors

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

Return the ancestors of a node in a graph.

node is included in the output

§Arguments:

  • node - The node to find the ancestors of

§Returns

An iterator where each item is a node id for an ancestor of node. This includes node in the returned ids.

§Example

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