Function cnetworks::bfs_callback [−][src]
pub fn bfs_callback<F: FnMut(usize, Option<usize>, &Vec<usize>)>(
net: &Network,
root: usize,
targets: &[usize],
step_callback: F
) -> Result<Vec<(usize, Option<usize>)>, Err>
Expand description
Like bfs_many()
, but executes step_callback
on every queue pop.
The step_callback
is called for every visited node, providing acesss to current node, it’s
parent (None
if it’s the root) and a vector of it’s children (linked nodes which have not
been visited yet).
ReturnsErr
if the root or any of the target nodes do not exist.
Panics
This function pancis when the queue operations fail.
Examples
let net = Network::new(100, Model::ER { p: 0.05, whole: false }, Weight::None);
let mut visited = Vec::new();
for (target, distance) in bfs_callback(
&net,
1,
&[2, 3],
|current: usize, _parent: Option<usize>, _children: &Vec<usize>| visited.push(current)
)?
{
match distance {
Some(d) => println!("Distance from 1 to {} is {}", target, d),
None => println!("Cannot reach {} from 1", target),
}
}
println!("Visited {:?} in the process.", visited);