use crate::graph::VertexInfo;
use std::collections::LinkedList;
pub fn dfs<G>(
graph: &G,
marked: &mut [bool],
edge_to: &mut Vec<usize>,
origin: usize,
component: usize,
mut_edge_to: bool, is_component: bool, ) where
G: VertexInfo,
{
assert!(VertexInfo::nb_vertices(graph) >= std::cmp::max(origin, component));
marked[origin] = true;
let source = if is_component { component } else { origin };
let adjacent_vertices = graph.vertex_edges(&origin);
if mut_edge_to {
for u in adjacent_vertices {
if !marked[*u] {
dfs(
graph,
marked,
edge_to,
*u,
component,
mut_edge_to,
is_component,
);
edge_to[*u] = source;
}
}
} else {
for u in adjacent_vertices {
if !marked[*u] {
dfs(
graph,
marked,
edge_to,
*u,
component,
mut_edge_to,
is_component,
);
}
}
edge_to.push(origin);
}
}
pub fn bfs<G>(graph: &G, marked: &mut [bool], edge_to: &mut [usize], w: usize)
where
G: VertexInfo,
{
assert!(VertexInfo::nb_vertices(graph) >= w);
let mut queue = LinkedList::<usize>::new();
queue.push_back(w);
marked[w] = true;
while let Some(x) = queue.pop_front() {
let adj_x = graph.vertex_edges(&x);
for u in adj_x {
if !marked[*u] {
queue.push_back(*u);
marked[*u] = true;
edge_to[*u] = x;
}
}
}
}