Incorporate iter_nodes
into graph::DirectedGraph
This assumes that the set of valid node IDs is exactly `0..num_nodes`. In practice, we have a lot of graph-algorithm code that already assumes that nodes are densely numbered, by using `num_nodes` to allocate per-node indexed data structures.
This commit is contained in:
parent
6fb03584cf
commit
d36e2b88d6
6 changed files with 18 additions and 23 deletions
|
@ -14,7 +14,23 @@ mod tests;
|
|||
pub trait DirectedGraph {
|
||||
type Node: Idx;
|
||||
|
||||
/// Returns the total number of nodes in this graph.
|
||||
///
|
||||
/// Several graph algorithm implementations assume that every node ID is
|
||||
/// strictly less than the number of nodes, i.e. nodes are densely numbered.
|
||||
/// That assumption allows them to use `num_nodes` to allocate per-node
|
||||
/// data structures, indexed by node.
|
||||
fn num_nodes(&self) -> usize;
|
||||
|
||||
/// Iterates over all nodes of a graph in ascending numeric order.
|
||||
///
|
||||
/// Assumes that nodes are densely numbered, i.e. every index in
|
||||
/// `0..num_nodes` is a valid node.
|
||||
fn iter_nodes(
|
||||
&self,
|
||||
) -> impl Iterator<Item = Self::Node> + DoubleEndedIterator + ExactSizeIterator {
|
||||
(0..self.num_nodes()).map(<Self::Node as Idx>::new)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait NumEdges: DirectedGraph {
|
||||
|
|
|
@ -333,8 +333,8 @@ where
|
|||
to_annotation,
|
||||
};
|
||||
|
||||
let scc_indices = (0..num_nodes)
|
||||
.map(G::Node::new)
|
||||
let scc_indices = graph
|
||||
.iter_nodes()
|
||||
.map(|node| match this.start_walk_from(node) {
|
||||
WalkReturn::Complete { scc_index, .. } => scc_index,
|
||||
WalkReturn::Cycle { min_depth, .. } => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue