pub fn compute_topological_ordering(&self) -> Result<Vec<usize>> {
        let mut permanent_marks: FxHashSet<usize> = FxHashSet::default();
        let mut temporary_marks: FxHashSet<usize> = FxHashSet::default();
        let mut order: Vec<usize> = Vec::new();

        fn dfs_walk<V: Vertex, E: Edge>(
            graph: &Graph<V, E>,
            node: usize,
            permanent_marks: &mut FxHashSet<usize>,
            temporary_marks: &mut FxHashSet<usize>,
            order: &mut Vec<usize>,
        ) -> Result<()> {
            if permanent_marks.contains(&node) {
                return Ok(());
            }
            if temporary_marks.contains(&node) {
                return Err("Graph contains a loop".into());
            }

            temporary_marks.insert(node);
            for successor in &graph.successors[&node] {
                dfs_walk(graph, *successor, permanent_marks, temporary_marks, order)?;
            }
            temporary_marks.remove(&node);
            permanent_marks.insert(node);
            order.push(node);
            Ok(())
        }

        for node in self.vertices.keys() {
            dfs_walk(
                self,
                *node,
                &mut permanent_marks,
                &mut temporary_marks,
                &mut order,
            )?;
        }

        Ok(order.into_iter().rev().collect())
    }
/// Computes the topological ordering of all vertices in the graph
Computes the topological ordering of all vertices in the graph
Computes the topological ordering of all vertices in the graph
pub fn compute_topological_ordering(&self) -> Result<Vec<usize>> {
        let mut permanent_marks: FxHashSet<usize> = FxHashSet::default();
        let mut temporary_marks: FxHashSet<usize> = FxHashSet::default();
        let mut order: Vec<usize> = Vec::new();
        fn dfs_walk<V: Vertex, E: Edge>(
            graph: &Graph<V, E>,
            node: usize,
            permanent_marks: &mut FxHashSet<usize>,
            temporary_marks: &mut FxHashSet<usize>,
            order: &mut Vec<usize>,
        ) -> Result<()> {
            if permanent_marks.contains(&node) {
                return Ok(());
            }
            if temporary_marks.contains(&node) {
                return Err("Graph contains a loop".into());
            }
            temporary_marks.insert(node);
            for successor in &graph.successors[&node] {
                dfs_walk(graph, *successor, permanent_marks, temporary_marks, order)?;
            }
            temporary_marks.remove(&node);
            permanent_marks.insert(node);
            order.push(node);
            Ok(())
        }
        for node in self.vertices.keys() {
            dfs_walk(
                self,
                *node,
                &mut permanent_marks,
                &mut temporary_marks,
                &mut order,
            )?;
        }
        Ok(order.into_iter().rev().collect())
    }
pub fn compute_topological_ordering ( & self ) -> Result < Vec < usize > > { let mut permanent_marks : FxHashSet < usize > = FxHashSet :: default ( ) ; let mut temporary_marks : FxHashSet < usize > = FxHashSet :: default ( ) ; let mut order : Vec < usize > = Vec :: new ( ) ; fn dfs_walk < V : Vertex , E : Edge > ( graph : & Graph < V , E > , node : usize , permanent_marks : & mut FxHashSet < usize > , temporary_marks : & mut FxHashSet < usize > , order : & mut Vec < usize > , ) -> Result < ( ) > { if permanent_marks . contains ( & node ) { return Ok ( ( ) ) ; } if temporary_marks . contains ( & node ) { return Err ( " " . into ( ) ) ; } temporary_marks . insert ( node ) ; for successor in & graph . successors [ & node ] { dfs_walk ( graph , * successor , permanent_marks , temporary_marks , order ) ? ; } temporary_marks . remove ( & node ) ; permanent_marks . insert ( node ) ; order . push ( node ) ; Ok ( ( ) ) } for node in self . vertices . keys ( ) { dfs_walk ( self , * node , & mut permanent_marks , & mut temporary_marks , & mut order , ) ? ; } Ok ( order . into_iter ( ) . rev ( ) . collect ( ) ) }
Computes the topological ordering of all vertices in the graph
Computes the topological ordering of all vertices in the graph
&self
&self