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 -> parseUses 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
impl BfsDistances
Sourcepub fn of(graph: &Graph, source: u32) -> BfsDistances
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.
Sourcepub fn of_sources(graph: &Graph, sources: &[u32]) -> BfsDistances
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.
Sourcepub fn distance_of(&self, node: u32) -> Option<u32>
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.
Sourcepub fn is_reachable(&self, node: u32) -> bool
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.
Sourcepub fn path_to(&self, node: u32) -> Vec<u32>
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.
Sourcepub fn max_distance(&self) -> Option<u32>
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
impl Clone for BfsDistances
Source§fn clone(&self) -> BfsDistances
fn clone(&self) -> BfsDistances
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more