pub fn topological_sort_dfs<G>(graph: &G) -> Result<Vec<NodeId>, GraphError>where
G: Graph,Expand description
Returns a topological ordering of the nodes using DFS finish order.
Works only on directed acyclic graphs (DAGs). If the graph contains a
cycle, returns GraphError::InvalidOperation.
The reverse of the DFS finish order is a valid topological sort: a node appears before all nodes that depend on it.
§Errors
Returns Err(GraphError::InvalidOperation) if the graph has a cycle.
§Examples
use graph_core::{AdjacencyList, Graph};
use graph_traversal::topological_sort_dfs;
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 order = topological_sort_dfs(&g).unwrap();
// a must come before b, b before c
let pos: std::collections::HashMap<_, _> =
order.iter().enumerate().map(|(i, &n)| (n, i)).collect();
assert!(pos[&a] < pos[&b]);
assert!(pos[&b] < pos[&c]);