pub fn dfs_recursive<G, F>(
graph: &G,
start: NodeId,
visitor: &mut F,
) -> Vec<NodeId>Expand description
Runs a recursive depth-first search from start, calling visitor the
first time each node is discovered.
Returns nodes in finish order (post-order): a node appears in the output only after all nodes reachable from it have been visited. This ordering is the reverse of a topological sort for DAGs.
§Caveats
Recursive DFS uses the call stack. For graphs with millions of nodes this
may overflow. Use dfs_iterative for deep graphs.
§Examples
use graph_core::{AdjacencyList, Graph};
use graph_traversal::dfs_recursive;
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 mut visited = Vec::new();
let finish = dfs_recursive(&g, a, &mut |id| visited.push(id));
assert_eq!(visited[0], a); // a discovered first
assert_eq!(finish.last(), Some(&a)); // a finishes last (post-order)