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
impl Distance
Sourcepub fn new<T, W>(builder: &Builder<T, W>) -> Selfwhere
W: Clone,
pub fn new<T, W>(builder: &Builder<T, W>) -> Selfwhere
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 Index<usize> for Distance
impl Index<usize> for Distance
Source§fn index(&self, index: usize) -> &Self::Output
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);