pub fn tree_topo_sort(&self, tree_root: usize) -> Option<Vec<usize>> {
        let mut topo_order = Vec::new();
        let mut to_visit = vec![tree_root];
        // a node is marked as visited when it is first added to to_visit
        let mut visited = HashSet::new();
        visited.insert(tree_root);

        while let Some(next) = to_visit.pop() {
            topo_order.push(next);
            let mut n_visited_neighbors = 0;
            for neighbor in self.neighbors(next) {
                if visited.contains(neighbor) {
                    n_visited_neighbors += 1;
                } else {
                    to_visit.push(*neighbor);
                    visited.insert(next);
                }
            }
            if n_visited_neighbors > 1 {
                return None; // cycle detected
            }
        }
        Some(topo_order)
    }
/// Topologically sort the graph from the given root, assuming it is a tree
/// If the connected component with the root has a cycle, return None
/// If the graph is not connected, only sort the component with the root
Topologically sort the graph from the given root, assuming it is a tree
If the connected component with the root has a cycle, return None
If the graph is not connected, only sort the component with the root
Topologically sort the graph from the given root assuming it is a tree If the connected component with the root has a cycle return None If the graph is not connected only sort the component with the root
pub fn tree_topo_sort(&self, tree_root: usize) -> Option<Vec<usize>> {
        let mut topo_order = Vec::new();
        let mut to_visit = vec![tree_root];
        // a node is marked as visited when it is first added to to_visit
        let mut visited = HashSet::new();
        visited.insert(tree_root);
        while let Some(next) = to_visit.pop() {
            topo_order.push(next);
            let mut n_visited_neighbors = 0;
            for neighbor in self.neighbors(next) {
                if visited.contains(neighbor) {
                    n_visited_neighbors += 1;
                } else {
                    to_visit.push(*neighbor);
                    visited.insert(next);
                }
            }
            if n_visited_neighbors > 1 {
                return None; // cycle detected
            }
        }
        Some(topo_order)
    }
pub fn tree_topo_sort ( & self , tree_root : usize ) -> Option < Vec < usize > > { let mut topo_order = Vec :: new ( ) ; let mut to_visit = vec ! [ tree_root ] ; // a node is marked as visited when it is first added to to_visit let mut visited = HashSet :: new ( ) ; visited . insert ( tree_root ) ; while let Some ( next ) = to_visit . pop ( ) { topo_order . push ( next ) ; let mut n_visited_neighbors = 0 ; for neighbor in self . neighbors ( next ) { if visited . contains ( neighbor ) { n_visited_neighbors += 1 ; } else { to_visit . push ( * neighbor ) ; visited . insert ( next ) ; } } if n_visited_neighbors > 1 { return None ; // cycle detected } } Some ( topo_order ) }
Topologically sort the graph from the given root, assuming it is a tree
If the connected component with the root has a cycle, return None
If the graph is not connected, only sort the component with the root
Topologically sort the graph from the given root assuming it is a tree If the connected component with the root has a cycle return None If the graph is not connected only sort the component with the root
&self
tree_root
usize