Add graph::depth_first_search_as_undirected

This commit is contained in:
Maybe Waffle 2024-04-15 18:20:30 +00:00
parent 7d2cb3dda7
commit fa134b5e0f

View File

@ -52,3 +52,29 @@ pub fn depth_first_search<G>(graph: G, from: G::Node) -> iterate::DepthFirstSear
{
iterate::DepthFirstSearch::new(graph).with_start_node(from)
}
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)
}