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(adj: &Adjacency) -> Self
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 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 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);