pub struct Seq<G: RandomAccessGraph> { /* private fields */ }
Expand description
A sequential breadth-first visit.
This implementation uses an algorithm that is slightly different from the classical textbook algorithm, as we do not store parents or distances of the nodes from the root: Parents and distances are computed on the fly and passed to the callback function by visiting nodes when they are discovered, rather than when they are extracted from the queue.
This approach requires inserting a level separator between nodes at
different distances: to obtain this result in a compact way, nodes are
represented using NonMaxUsize
, so the None
variant of
Option<NonMaxUsize>
can be used as a separator.
§Examples
Let’s compute the distances from 0:
use webgraph_algo::visits::*;
use webgraph::graphs::vec_graph::VecGraph;
use webgraph::labels::proj::Left;
use std::ops::ControlFlow::Continue;
use no_break::NoBreak;
let graph = VecGraph::from_arcs([(0, 1), (1, 2), (2, 0), (1, 3)]);
let mut visit = breadth_first::Seq::new(&graph);
let mut d = [0; 4];
visit.visit(
[0],
|event| {
// Set distance from 0
if let breadth_first::EventPred::Unknown { node, distance, .. } = event {
d[node] = distance;
}
Continue(())
},
).continue_value_no_break();
assert_eq!(d, [0, 1, 2, 2]);
Here instead we compute the size of the ball of radius two around node 0: to
minimize resource usage, we count nodes in the filter function, rather than
as the result of an event. In this way, node at distance two are counted but
not included in the queue, as it would happen if we were counting during an
EventPred::Unknown
event.
use std::convert::Infallible;
use webgraph_algo::visits::*;
use webgraph::graphs::vec_graph::VecGraph;
use webgraph::labels::proj::Left;
use std::ops::ControlFlow::Continue;
use no_break::NoBreak;
let graph = VecGraph::from_arcs([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4)]);
let mut visit = breadth_first::Seq::new(&graph);
let mut count = 0;
visit.visit_filtered(
[0],
|event| { Continue(()) },
|breadth_first::FilterArgsPred { distance, .. }| {
if distance > 2 {
false
} else {
count += 1;
true
}
},
).continue_value_no_break();
assert_eq!(count, 3);
Implementations§
Trait Implementations§
Source§impl<G: RandomAccessGraph> Sequential<EventPred> for Seq<G>
impl<G: RandomAccessGraph> Sequential<EventPred> for Seq<G>
Source§fn visit_filtered_with<R: IntoIterator<Item = usize>, T, E, C: FnMut(&mut T, EventPred) -> ControlFlow<E, ()>, F: FnMut(&mut T, FilterArgsPred) -> bool>(
&mut self,
roots: R,
init: T,
callback: C,
filter: F,
) -> ControlFlow<E, ()>
fn visit_filtered_with<R: IntoIterator<Item = usize>, T, E, C: FnMut(&mut T, EventPred) -> ControlFlow<E, ()>, F: FnMut(&mut T, FilterArgsPred) -> bool>( &mut self, roots: R, init: T, callback: C, filter: F, ) -> ControlFlow<E, ()>
Source§fn visit_filtered<R: IntoIterator<Item = usize>, E, C: FnMut(A) -> ControlFlow<E, ()>, F: FnMut(A::FilterArgs) -> bool>(
&mut self,
roots: R,
callback: C,
filter: F,
) -> ControlFlow<E, ()>
fn visit_filtered<R: IntoIterator<Item = usize>, E, C: FnMut(A) -> ControlFlow<E, ()>, F: FnMut(A::FilterArgs) -> bool>( &mut self, roots: R, callback: C, filter: F, ) -> ControlFlow<E, ()>
Source§fn visit_with<R: IntoIterator<Item = usize>, T, E, C: FnMut(&mut T, A) -> ControlFlow<E, ()>>(
&mut self,
roots: R,
init: T,
callback: C,
) -> ControlFlow<E, ()>
fn visit_with<R: IntoIterator<Item = usize>, T, E, C: FnMut(&mut T, A) -> ControlFlow<E, ()>>( &mut self, roots: R, init: T, callback: C, ) -> ControlFlow<E, ()>
Source§fn visit<R: IntoIterator<Item = usize>, E, C: FnMut(A) -> ControlFlow<E, ()>>(
&mut self,
roots: R,
callback: C,
) -> ControlFlow<E, ()>
fn visit<R: IntoIterator<Item = usize>, E, C: FnMut(A) -> ControlFlow<E, ()>>( &mut self, roots: R, callback: C, ) -> ControlFlow<E, ()>
Auto Trait Implementations§
impl<G> Freeze for Seq<G>where
G: Freeze,
impl<G> RefUnwindSafe for Seq<G>where
G: RefUnwindSafe,
impl<G> Send for Seq<G>where
G: Send,
impl<G> Sync for Seq<G>where
G: Sync,
impl<G> Unpin for Seq<G>where
G: Unpin,
impl<G> UnwindSafe for Seq<G>where
G: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T, U> CastableInto<U> for Twhere
U: CastableFrom<T>,
impl<T, U> CastableInto<U> for Twhere
U: CastableFrom<T>,
Source§impl<T, U> CastableInto<U> for Twhere
U: CastableFrom<T>,
impl<T, U> CastableInto<U> for Twhere
U: CastableFrom<T>,
Source§impl<T> DowncastableFrom<T> for T
impl<T> DowncastableFrom<T> for T
Source§fn downcast_from(value: T) -> T
fn downcast_from(value: T) -> T
Source§impl<T> DowncastableFrom<T> for T
impl<T> DowncastableFrom<T> for T
Source§fn downcast_from(value: T) -> T
fn downcast_from(value: T) -> T
Source§impl<T, U> DowncastableInto<U> for Twhere
U: DowncastableFrom<T>,
impl<T, U> DowncastableInto<U> for Twhere
U: DowncastableFrom<T>,
Source§impl<T, U> DowncastableInto<U> for Twhere
U: DowncastableFrom<T>,
impl<T, U> DowncastableInto<U> for Twhere
U: DowncastableFrom<T>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more