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<T, W>(builder: &Builder<T, W>) -> Self
where W: Clone,

Creates a distance matrix.

§Examples
use zrx_graph::topology::Distance;
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, 0)?;
builder.add_edge(b, c, 0)?;

// Create distance matrix
let dist = Distance::new(&builder);
assert_eq!(dist[a][c], 2);

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 given row.

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::Distance;
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, 0)?;
builder.add_edge(b, c, 0)?;

// Create distance matrix
let dist = Distance::new(&builder);
assert_eq!(dist[a][c], 2);
Source§

type Output = [u8]

The returned type after indexing.

Auto Trait Implementations§

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.