Skip to main content

Distance

Struct Distance 

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

Distance matrix.

This data type stores the all-pairs shortest path distances for all nodes in a directed acyclic graph (DAG). It’s computed through the Floyd-Warshall algorithm, and allows for efficient retrieval of distances between any two nodes, which is essential for many graph algorithms.

Implementations§

Source§

impl Distance

Source

pub fn new(adj: &Adjacency) -> Self

Creates a distance matrix.

This method is called by Topology::new, and is not intended to be used on its own, since an adjacency list is needed to create the matrix. Computation is expensive, which is why Topology will defer creation via OnceCell, so it’s only computed when first accessed.

Trait Implementations§

Source§

impl Debug for Distance

Source§

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

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

impl Index<usize> for Distance

Source§

fn index(&self, index: usize) -> &Self::Output

Returns the column values for the row at the index.

This method returns a slice representing the distances from the node as identified by the given index to all other nodes in the graph. Distances are represented as the number of edges on the shortest path between the nodes. For all unreachable nodes, the distance equals u8::MAX.

§Panics

Panics if the index is out of bounds.

§Examples
use zrx_graph::topology::Topology;
use zrx_graph::Graph;

// Create graph builder and add nodes
let mut builder = Graph::builder();
let a = builder.add_node("a");
let b = builder.add_node("b");
let c = builder.add_node("c");

// Create edges between nodes
builder.add_edge(a, b)?;
builder.add_edge(b, c)?;

// Create topology
let topology = Topology::new(builder.len(), builder.edges());

// Obtain distance matrix
let dist = topology.distance();
assert_eq!(dist[a][c], 2);
Source§

type Output = [u8]

The returned type after indexing.

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> 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, 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.