Skip to content

Commit

Permalink
Add helper function for traversal direction
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelishman committed Mar 1, 2024
1 parent 8a221e9 commit 12a91ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
18 changes: 13 additions & 5 deletions src/dag_algo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ use petgraph::graph::NodeIndex;
use petgraph::prelude::*;
use petgraph::visit::NodeCount;

/// Return a pair of [`petgraph::Direction`] values corresponding to the "forwards" and "backwards"
/// direction of graph traversal, based on whether the graph is being traved forwards (following
/// the edges) or backward (reversing along edges). The order of returns is (forwards, backwards).
#[inline(always)]
pub fn traversal_directions(reverse: bool) -> (petgraph::Direction, petgraph::Direction) {
if reverse {
(petgraph::Direction::Outgoing, petgraph::Direction::Incoming)
} else {
(petgraph::Direction::Incoming, petgraph::Direction::Outgoing)
}
}

/// Find the longest path in a DAG
///
/// :param PyDiGraph graph: The graph to find the longest path on. The input
Expand Down Expand Up @@ -374,11 +386,7 @@ pub fn lexicographical_topological_sort(
};
// HashMap of node_index indegree
let node_count = dag.node_count();
let (in_dir, out_dir) = if reverse {
(petgraph::Direction::Outgoing, petgraph::Direction::Incoming)
} else {
(petgraph::Direction::Incoming, petgraph::Direction::Outgoing)
};
let (in_dir, out_dir) = traversal_directions(reverse);
let mut in_degree_map: HashMap<NodeIndex, usize> = HashMap::with_capacity(node_count);
for node in dag.graph.node_indices() {
in_degree_map.insert(node, dag.graph.edges_directed(node, in_dir).count());
Expand Down
15 changes: 3 additions & 12 deletions src/toposort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use pyo3::Python;
use petgraph::stable_graph::NodeIndex;
use petgraph::visit::IntoNodeIdentifiers;

use crate::dag_algo::is_directed_acyclic_graph;
use crate::dag_algo::{is_directed_acyclic_graph, traversal_directions};
use crate::DAGHasCycle;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -88,12 +88,7 @@ impl TopologicalSorter {
}
}

let in_dir = if reverse {
petgraph::Direction::Outgoing
} else {
petgraph::Direction::Incoming
};

let (in_dir, _) = traversal_directions(reverse);
let ready_nodes = {
let dag = &dag.borrow(py);

Expand Down Expand Up @@ -156,11 +151,7 @@ impl TopologicalSorter {
/// by "get_ready".
fn done(&mut self, py: Python, nodes: Vec<usize>) -> PyResult<()> {
let dag = &self.dag.borrow(py);
let (in_dir, out_dir) = if self.reverse {
(petgraph::Direction::Outgoing, petgraph::Direction::Incoming)
} else {
(petgraph::Direction::Incoming, petgraph::Direction::Outgoing)
};
let (in_dir, out_dir) = traversal_directions(self.reverse);
for node in nodes {
let node = NodeIndex::new(node);
match self.node2state.get_mut(&node) {
Expand Down

0 comments on commit 12a91ec

Please sign in to comment.