2023-04-19 10:57:17 +00:00
|
|
|
use rustc_index::Idx;
|
2016-06-09 15:49:07 -07:00
|
|
|
|
|
|
|
|
pub mod dominators;
|
2018-07-01 17:06:00 -04:00
|
|
|
pub mod implementation;
|
2016-06-09 15:49:07 -07:00
|
|
|
pub mod iterate;
|
|
|
|
|
mod reference;
|
2025-01-12 12:40:03 +11:00
|
|
|
pub mod reversed;
|
2018-07-02 10:40:03 -04:00
|
|
|
pub mod scc;
|
2019-06-11 13:40:24 -04:00
|
|
|
pub mod vec_graph;
|
2016-06-09 15:49:07 -07:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-08-01 23:57:23 +03:00
|
|
|
mod tests;
|
2016-06-09 15:49:07 -07:00
|
|
|
|
2018-07-01 16:54:01 -04:00
|
|
|
pub trait DirectedGraph {
|
2016-06-09 15:49:07 -07:00
|
|
|
type Node: Idx;
|
|
|
|
|
|
2025-01-26 14:00:46 +11:00
|
|
|
/// 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.
|
2016-06-09 15:49:07 -07:00
|
|
|
fn num_nodes(&self) -> usize;
|
2025-01-26 14:00:46 +11:00
|
|
|
|
|
|
|
|
/// 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)
|
|
|
|
|
}
|
2016-06-09 15:49:07 -07:00
|
|
|
}
|
|
|
|
|
|
2024-04-14 15:51:29 +00:00
|
|
|
pub trait NumEdges: DirectedGraph {
|
2019-06-11 13:40:24 -04:00
|
|
|
fn num_edges(&self) -> usize;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 15:51:29 +00:00
|
|
|
pub trait StartNode: DirectedGraph {
|
|
|
|
|
fn start_node(&self) -> Self::Node;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 15:40:26 +00:00
|
|
|
pub trait Successors: DirectedGraph {
|
2024-04-15 13:33:08 +00:00
|
|
|
fn successors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node>;
|
2018-07-01 16:54:01 -04:00
|
|
|
}
|
|
|
|
|
|
2024-04-14 15:40:26 +00:00
|
|
|
pub trait Predecessors: DirectedGraph {
|
2024-04-15 13:33:08 +00:00
|
|
|
fn predecessors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node>;
|
2016-10-20 00:25:19 +05:30
|
|
|
}
|
2018-07-01 16:54:01 -04:00
|
|
|
|
2024-04-14 15:53:38 +00:00
|
|
|
/// Alias for [`DirectedGraph`] + [`StartNode`] + [`Predecessors`] + [`Successors`].
|
|
|
|
|
pub trait ControlFlowGraph: DirectedGraph + StartNode + Predecessors + Successors {}
|
2024-04-14 15:51:29 +00:00
|
|
|
impl<T> ControlFlowGraph for T where T: DirectedGraph + StartNode + Predecessors + Successors {}
|
2019-09-18 14:35:56 -07:00
|
|
|
|
|
|
|
|
/// Returns `true` if the graph has a cycle that is reachable from the start node.
|
|
|
|
|
pub fn is_cyclic<G>(graph: &G) -> bool
|
|
|
|
|
where
|
2024-04-14 15:51:29 +00:00
|
|
|
G: ?Sized + DirectedGraph + StartNode + Successors,
|
2019-09-18 14:35:56 -07:00
|
|
|
{
|
|
|
|
|
iterate::TriColorDepthFirstSearch::new(graph)
|
|
|
|
|
.run_from_start(&mut iterate::CycleDetector)
|
|
|
|
|
.is_some()
|
|
|
|
|
}
|
2024-04-14 16:03:08 +00:00
|
|
|
|
2024-04-15 18:17:51 +00:00
|
|
|
pub fn depth_first_search<G>(graph: G, from: G::Node) -> iterate::DepthFirstSearch<G>
|
2024-04-14 16:03:08 +00:00
|
|
|
where
|
2024-04-15 18:17:51 +00:00
|
|
|
G: Successors,
|
2024-04-14 16:03:08 +00:00
|
|
|
{
|
|
|
|
|
iterate::DepthFirstSearch::new(graph).with_start_node(from)
|
|
|
|
|
}
|
2024-04-15 18:20:30 +00:00
|
|
|
|
|
|
|
|
pub fn depth_first_search_as_undirected<G>(
|
|
|
|
|
graph: G,
|
|
|
|
|
from: G::Node,
|
|
|
|
|
) -> iterate::DepthFirstSearch<impl Successors<Node = G::Node>>
|
|
|
|
|
where
|
|
|
|
|
G: Successors + Predecessors,
|
|
|
|
|
{
|
|
|
|
|
struct AsUndirected<G>(G);
|
|
|
|
|
|
|
|
|
|
impl<G: DirectedGraph> DirectedGraph for AsUndirected<G> {
|
|
|
|
|
type Node = G::Node;
|
|
|
|
|
|
|
|
|
|
fn num_nodes(&self) -> usize {
|
|
|
|
|
self.0.num_nodes()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<G: Successors + Predecessors> Successors for AsUndirected<G> {
|
|
|
|
|
fn successors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node> {
|
|
|
|
|
self.0.successors(node).chain(self.0.predecessors(node))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iterate::DepthFirstSearch::new(AsUndirected(graph)).with_start_node(from)
|
|
|
|
|
}
|