Function cnetworks::bfs_callback [−][src]
pub fn bfs_callback<F: FnMut(usize, Option<usize>, &Vec<&usize>)>(
net: &Network,
root: usize,
target: usize,
step_callback: F
) -> Result<Option<usize>, CNErr>
Expand description
Perform a breadth-first search over the network, starting with root and looking for target,
executing step_callback
along the way. Returns distance (ie. number of edges) from root to
target in the resulting BFS tree or None
if no path is not found. Returns Err
if the root
or target nodes do not exist.
The step_callback
is called for every visited node, providing acesss to current node, it’s
parent (None
if it’s the root) and it’s children (linked nodes which have not been visited yet).
Panics
This function pancis when the queue operations fail.
Examples
use cnetworks::*;
let net = Network::new(100, Model::ER { p: 0.05, whole: false }, Weight::None);
let mut visited = Vec::new();
match bfs_callback(
&net,
1,
3,
|current: usize, parent: Option<usize>, children: &Vec<&usize>| visited.push(current)
).unwrap() {
Some(d) => println!("Edges between 1 and 3: {}", d),
None => println!("No path from 1 to 3 found!"),
}
println!("Visited {:?} in the process.", visited);