pub fn dfs_reach<N, FN, IN>(start: N, successors: FN) -> DfsReachable<N, FN> where
    N: Eq + Hash + Clone,
    FN: FnMut(&N) -> IN,
    IN: IntoIterator<Item = N>,
Expand description

Visit all nodes that are reachable from a start node. The node will be visited in DFS order, starting from the start node and following the order returned by the successors function.

Examples

The iterator stops when there are no new nodes to visit:

use pathfinding::prelude::dfs_reach;

let all_nodes = dfs_reach(3, |_| (1..=5)).collect::<Vec<_>>();
assert_eq!(all_nodes, vec![3, 1, 2, 4, 5]);

The iterator can be used as a generator. Here are for examples the multiples of 2 and 3 smaller than 15 (although not in natural order but in the order they are discovered by the DFS algorithm):

use pathfinding::prelude::dfs_reach;

let mut it = dfs_reach(1, |&n| vec![n*2, n*3].into_iter().filter(|&x| x < 15)).skip(1);
assert_eq!(it.next(), Some(2));  // 1*2
assert_eq!(it.next(), Some(4));  // (1*2)*2
assert_eq!(it.next(), Some(8));  // ((1*2)*2)*2
assert_eq!(it.next(), Some(12)); // ((1*2)*2)*3
assert_eq!(it.next(), Some(6));  // (1*2)*3
assert_eq!(it.next(), Some(3));  // 1*3
// (1*3)*2 == 6 which has been seen already
assert_eq!(it.next(), Some(9));  // (1*3)*3