directed_breadth_traversal

Function directed_breadth_traversal 

Source
pub fn directed_breadth_traversal<K, N, E, F>(
    source: &Arc<Node<K, N, E>>,
    explorer: F,
) -> Option<Vec<Weak<Edge<K, N, E>>>>
where K: Hash + Eq + Clone + Debug + Display + Sync + Send, N: Clone + Debug + Display + Sync + Send, E: Clone + Debug + Display + Sync + Send, F: Fn(&Arc<Edge<K, N, E>>) -> Traverse + Sync + Send + Copy,
Expand description

§Breadth Traversal

Conduct a breadth first traversal starting from the source node. User provides an explorer closure which determines how nodes and edges are to be interpreted. The closure will return a Traverse enum which has 3 states Include, Skip and Finish. These states determine if we are to “go through” the edge and thus include it in our search. Include will include the edge and continue the search, Skip will indicate that the edge is not to be traversed and Finish will include the edge and finish the algorithm.

Function will return an Option<Vec<Weak<Edge<K, N, E>>>> where a Some value indicates that the traversal was successful ie. a Finish condition was reached. And WeakEdges is a collection of all the traversed edges. The last edge will contain the result that triggered the Finish condition. To get the shortest path for example, we’d backtrack the WeakEdges starting from the last edge which would contain our sink node.

§Examples

use fastgraph::core::*;
use std::sync::Arc;

let n1 = Arc::new(Node::<u32, Empty, Empty>::new(1, Empty));
let n2 = Arc::new(Node::<u32, Empty, Empty>::new(2, Empty));
let n3 = Arc::new(Node::<u32, Empty, Empty>::new(3, Empty));

connect(&n1, &n2, Empty);
connect(&n2, &n3, Empty);
connect(&n1, &n3, Empty);

let edges = directed_breadth_traversal(&n1,
	| edge | {
		if n3 == edge.target() {
			Traverse::Finish
		} else {
			Traverse::Include
		}
	})
	.unwrap();

let shortest_path = backtrack_edges(&edges);

assert!(shortest_path.len() == 1);