Skip to main content

BfsDistances

Struct BfsDistances 

Source
pub struct BfsDistances { /* private fields */ }
Expand description

The distances of the nodes of a graph from one or several sources, with a shortest path to every node reached.

The sources are the nodes where the search starts. The distance of a node is the number of edges on a shortest path to it from the nearest source. A source has distance 0, and a node that no source reaches has distance None - Java’s -1. Edges are followed in their direction. For distances against their direction, pass the transposed graph: BiAdjacentGraph::of(&graph).transposed(). Here, calls are the calls between the methods of a program, and the search finds a shortest chain of calls from main to parse:

let from_main = BfsDistances::of(&calls, main);
let depth = from_main.distance_of(parse);
let chain = from_main.path_to(parse);
// depth: Some(3)
// chain: [0, 2, 3, 6] - main -> run -> load -> parse

Uses a breadth-first search that takes the sources in list order and the out-nodes of each node in ascending order. For the reachable nodes alone, graphs::reachable_from_sources needs less memory.

Instances are immutable and safe to share between threads (Send and Sync).

Memory: 8 x node_count bytes: the distance of each node and the node before it on its shortest path.

Implementations§

Source§

impl BfsDistances

Source

pub fn of(graph: &Graph, source: u32) -> BfsDistances

Returns the distances of the nodes of graph from source. Equivalent to of_sources(graph, &[source]) - Java’s overload of(Graph, int).

Time: O(node_count + edge_count). Memory: as for of_sources.

§Panics

With an index out of bounds message if source is not in 0..node_count.

Source

pub fn of_sources(graph: &Graph, sources: &[u32]) -> BfsDistances

Returns the distances of the nodes of graph from the nearest of sources - Java’s overload of(Graph, IntList).

A source given twice counts once, and with no sources, no node is reachable.

Time: O(sources.len() + node_count + edge_count). Memory: 8 x node_count bytes for the result; while it runs, 4 x node_count bytes more.

§Panics

With an index out of bounds message if a source is not in 0..node_count; then no work is done.

Source

pub fn distance_of(&self, node: u32) -> Option<u32>

Returns the distance of node, or None if no source reaches it - Java’s -1. The distance is the number of edges on a shortest path to node from the nearest source.

Time: O(1).

§Panics

With an index out of bounds message if node is not in 0..node_count.

Source

pub fn is_reachable(&self, node: u32) -> bool

Returns whether a source reaches node. Equivalent to distance_of(node).is_some().

Time: O(1).

§Panics

With an index out of bounds message if node is not in 0..node_count.

Source

pub fn path_to(&self, node: u32) -> Vec<u32>

Returns a shortest path to node from the nearest source, or an empty vector if no source reaches node.

The path has one node more than the distance of node, with the source first and node last. Of several shortest paths, this method returns the one that the search finds first. The result is a vector the caller owns - Java returns an unmodifiable IntList.

Time: O(d), where d is the distance of node. Memory: 4 x (d + 1) bytes for the result.

§Panics

With an index out of bounds message if node is not in 0..node_count.

Source

pub fn max_distance(&self) -> Option<u32>

Returns the largest distance of a node that a source reaches, or None if there are no sources - Java’s -1.

Time: O(1).

Trait Implementations§

Source§

impl Clone for BfsDistances

Source§

fn clone(&self) -> BfsDistances

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BfsDistances

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for BfsDistances

Source§

impl PartialEq for BfsDistances

Source§

fn eq(&self, other: &BfsDistances) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for BfsDistances

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.