#[non_exhaustive]pub struct PredecessorTree {
pub pred: Vec<Option<usize>>,
}
Expand description
A predecessor tree.
A PredecessorTree
contains each vertex’s predecessor on the shortest
path from the source vertex.
§Examples
This PredecessorTree
is generated by
BfsPred::predecessors
.
The path from vertex 0
is red. The dashed arcs mark the
PredecessorTree
.
use {
graaf::{
AddArc,
AdjacencyList,
BfsPred,
Empty,
PredecessorTree,
},
std::iter::once,
};
let mut digraph = AdjacencyList::empty(6);
digraph.add_arc(0, 1);
digraph.add_arc(1, 2);
digraph.add_arc(1, 4);
digraph.add_arc(2, 5);
assert!(BfsPred::new(&digraph, once(0))
.predecessors()
.into_iter()
.eq([None, Some(0), Some(1), None, Some(1), Some(2)]));
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.pred: Vec<Option<usize>>
The predecessors of each vertex.
Implementations§
Source§impl PredecessorTree
impl PredecessorTree
Sourcepub fn new(order: usize) -> Self
pub fn new(order: usize) -> Self
Construct a PredecessorTree
with a given order.
§Arguments
order
: The number of vertices in thePredecessorTree
.
§Panics
Panics if order
is zero.
§Examples
use graaf::PredecessorTree;
assert!(PredecessorTree::new(4)
.into_iter()
.eq([None, None, None, None]));
Sourcepub fn search(&self, s: usize, t: usize) -> Option<Vec<usize>>
pub fn search(&self, s: usize, t: usize) -> Option<Vec<usize>>
Search a PredecessorTree
for a path from a source vertex to a
target vertex.
§Arguments
s
: The source vertex.t
: The target vertex.
§Returns
If a path from s
to t
is found, the function returns it. Otherwise,
returns None
.
§Examples
use graaf::PredecessorTree;
let pred = PredecessorTree::from(vec![Some(1), Some(2), Some(3), None]);
assert!(pred.search(0, 3).into_iter().eq(Some(vec![0, 1, 2, 3])));
Sourcepub fn search_by<F>(&self, s: usize, is_target: F) -> Option<Vec<usize>>
pub fn search_by<F>(&self, s: usize, is_target: F) -> Option<Vec<usize>>
Search a PredecessorTree
for a path from a source vertex to a
vertex that satisfies a predicate.
§Arguments
s
: The source vertex.is_target
: A predicate determining if a vertex is the target.
§Returns
If it finds a target, it returns the path from the source to the
target. Otherwise, it returns None
.
§Panics
Panics if s
isn’t in the path.
§Examples
use graaf::PredecessorTree;
let pred = PredecessorTree::from(vec![Some(1), Some(2), Some(3), None]);
assert!(pred
.search_by(0, |&v, _| v > 1)
.into_iter()
.eq(Some(vec![0, 1, 2])));
let pred =
PredecessorTree::from(vec![Some(1), Some(2), Some(3), None, Some(0)]);
assert!(pred
.search_by(0, |_, v| v.is_none())
.into_iter()
.eq(Some(vec![0, 1, 2, 3])));
Trait Implementations§
Source§impl Clone for PredecessorTree
impl Clone for PredecessorTree
Source§fn clone(&self) -> PredecessorTree
fn clone(&self) -> PredecessorTree
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for PredecessorTree
impl Debug for PredecessorTree
Source§impl Hash for PredecessorTree
impl Hash for PredecessorTree
Source§impl Index<usize> for PredecessorTree
impl Index<usize> for PredecessorTree
Source§impl IndexMut<usize> for PredecessorTree
impl IndexMut<usize> for PredecessorTree
Source§impl IntoIterator for PredecessorTree
impl IntoIterator for PredecessorTree
Source§impl Ord for PredecessorTree
impl Ord for PredecessorTree
Source§fn cmp(&self, other: &PredecessorTree) -> Ordering
fn cmp(&self, other: &PredecessorTree) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialEq for PredecessorTree
impl PartialEq for PredecessorTree
Source§impl PartialOrd for PredecessorTree
impl PartialOrd for PredecessorTree
impl Eq for PredecessorTree
impl StructuralPartialEq for PredecessorTree
Auto Trait Implementations§
impl Freeze for PredecessorTree
impl RefUnwindSafe for PredecessorTree
impl Send for PredecessorTree
impl Sync for PredecessorTree
impl Unpin for PredecessorTree
impl UnwindSafe for PredecessorTree
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
Mutably borrows from an owned value. Read more