#[non_exhaustive]pub struct DistanceMatrix<W> {
pub dist: Vec<W>,
pub infinity: W,
pub order: usize,
}
Expand description
A distance matrix
A DistanceMatrix
contains the distance between each pair of vertices in
a digraph.
§Examples
A digraph of order 7
and size 15
.
§The distance matrix
The corresponding DistanceMatrix
generated by
FloydWarshall::distances
.
use graaf::{
AddArcWeighted,
AdjacencyListWeighted,
DistanceMatrix,
Empty,
FloydWarshall,
};
let mut digraph = AdjacencyListWeighted::<isize>::empty(7);
digraph.add_arc_weighted(0, 1, 5);
digraph.add_arc_weighted(0, 2, 3);
digraph.add_arc_weighted(0, 3, 2);
digraph.add_arc_weighted(0, 4, 4);
digraph.add_arc_weighted(1, 0, 3);
digraph.add_arc_weighted(1, 3, 1);
digraph.add_arc_weighted(1, 4, 2);
digraph.add_arc_weighted(2, 6, 4);
digraph.add_arc_weighted(3, 4, 1);
digraph.add_arc_weighted(3, 5, 1);
digraph.add_arc_weighted(4, 2, 3);
digraph.add_arc_weighted(5, 6, 1);
digraph.add_arc_weighted(6, 0, 9);
digraph.add_arc_weighted(6, 1, 8);
digraph.add_arc_weighted(6, 2, 5);
let mut floyd_warshall = FloydWarshall::new(&digraph);
let dist = floyd_warshall.distances();
assert!(dist[0..7].eq(&[0, 5, 3, 2, 3, 3, 4]));
assert!(dist[7..14].eq(&[3, 0, 5, 1, 2, 2, 3]));
assert!(dist[14..21].eq(&[13, 12, 0, 13, 14, 14, 4]));
assert!(dist[21..28].eq(&[11, 10, 4, 0, 1, 1, 2]));
assert!(dist[28..35].eq(&[16, 15, 3, 16, 0, 17, 7]));
assert!(dist[35..42].eq(&[10, 9, 6, 10, 11, 0, 1]));
assert!(dist[42..49].eq(&[9, 8, 5, 9, 10, 10, 0]));
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.dist: Vec<W>
The distance matrix.
infinity: W
The distance between unconnected vertices.
order: usize
The number of vertices.
Implementations§
Source§impl<W> DistanceMatrix<W>
impl<W> DistanceMatrix<W>
Sourcepub fn new(order: usize, infinity: W) -> Selfwhere
W: Copy,
pub fn new(order: usize, infinity: W) -> Selfwhere
W: Copy,
Construct a new DistanceMatrix
.
§Arguments
order
: The number of vertices.infinity
: The distance between unconnected vertices.
§Panics
Panics if order
is zero.
§Examples
use graaf::DistanceMatrix;
let dist = DistanceMatrix::new(4, 0);
assert_eq!(dist.infinity, 0);
assert_eq!(dist[0..4], vec![0; 4]);
assert_eq!(dist[4..8], vec![0; 4]);
assert_eq!(dist[8..12], vec![0; 4]);
assert_eq!(dist[12..16], vec![0; 4]);
Sourcepub fn center(&self) -> Vec<usize>
pub fn center(&self) -> Vec<usize>
Return a digraph’s center.
A digraph’s center is the set of vertices with the smallest eccentricity. The center is also known as the Jordan center.
§Examples
The digraph’s center is red.
use graaf::{
AddArcWeighted,
AdjacencyListWeighted,
DistanceMatrix,
Empty,
FloydWarshall,
};
let mut digraph = AdjacencyListWeighted::<isize>::empty(7);
digraph.add_arc_weighted(0, 1, 5);
digraph.add_arc_weighted(0, 2, 3);
digraph.add_arc_weighted(0, 3, 2);
digraph.add_arc_weighted(0, 4, 4);
digraph.add_arc_weighted(1, 0, 3);
digraph.add_arc_weighted(1, 3, 1);
digraph.add_arc_weighted(1, 4, 2);
digraph.add_arc_weighted(2, 6, 4);
digraph.add_arc_weighted(3, 4, 1);
digraph.add_arc_weighted(3, 5, 1);
digraph.add_arc_weighted(4, 2, 3);
digraph.add_arc_weighted(5, 6, 1);
digraph.add_arc_weighted(6, 0, 9);
digraph.add_arc_weighted(6, 1, 8);
digraph.add_arc_weighted(6, 2, 5);
assert!(FloydWarshall::new(&digraph)
.distances()
.center()
.iter()
.eq(&[0, 1]));
Sourcepub fn diameter(&self) -> &W
pub fn diameter(&self) -> &W
Return a digraph’s diameter.
A digraph’s diameter is its maximum eccentricity.
§Examples
The longest shortest path between vertices 4
and 5
is red.
use graaf::{
AddArcWeighted,
AdjacencyListWeighted,
DistanceMatrix,
Empty,
FloydWarshall,
};
let mut digraph = AdjacencyListWeighted::<isize>::empty(7);
digraph.add_arc_weighted(0, 1, 5);
digraph.add_arc_weighted(0, 2, 3);
digraph.add_arc_weighted(0, 3, 2);
digraph.add_arc_weighted(0, 4, 4);
digraph.add_arc_weighted(1, 0, 3);
digraph.add_arc_weighted(1, 3, 1);
digraph.add_arc_weighted(1, 4, 2);
digraph.add_arc_weighted(2, 6, 4);
digraph.add_arc_weighted(3, 4, 1);
digraph.add_arc_weighted(3, 5, 1);
digraph.add_arc_weighted(4, 2, 3);
digraph.add_arc_weighted(5, 6, 1);
digraph.add_arc_weighted(6, 0, 9);
digraph.add_arc_weighted(6, 1, 8);
digraph.add_arc_weighted(6, 2, 5);
assert_eq!(FloydWarshall::new(&digraph).distances().diameter(), &17);
Sourcepub fn eccentricities(&self) -> impl Iterator<Item = &W>where
W: Ord,
pub fn eccentricities(&self) -> impl Iterator<Item = &W>where
W: Ord,
Return the digraph’s eccentricities.
A vertex’s eccentricity is the maximum distance to any other vertex.
§Examples
use graaf::{
AddArcWeighted,
AdjacencyListWeighted,
DistanceMatrix,
Empty,
FloydWarshall,
};
let mut digraph = AdjacencyListWeighted::<isize>::empty(7);
digraph.add_arc_weighted(0, 1, 5);
digraph.add_arc_weighted(0, 2, 3);
digraph.add_arc_weighted(0, 3, 2);
digraph.add_arc_weighted(0, 4, 4);
digraph.add_arc_weighted(1, 0, 3);
digraph.add_arc_weighted(1, 3, 1);
digraph.add_arc_weighted(1, 4, 2);
digraph.add_arc_weighted(2, 6, 4);
digraph.add_arc_weighted(3, 4, 1);
digraph.add_arc_weighted(3, 5, 1);
digraph.add_arc_weighted(4, 2, 3);
digraph.add_arc_weighted(5, 6, 1);
digraph.add_arc_weighted(6, 0, 9);
digraph.add_arc_weighted(6, 1, 8);
digraph.add_arc_weighted(6, 2, 5);
assert!(FloydWarshall::new(&digraph)
.distances()
.eccentricities()
.eq(&[5, 5, 14, 11, 17, 11, 10]));
Sourcepub fn is_connected(&self) -> boolwhere
W: Ord,
pub fn is_connected(&self) -> boolwhere
W: Ord,
Check whether the distance matrix is connected.
A distance matrix is connected if the eccentricity of every vertex is finite.
§Examples
use graaf::{
AddArcWeighted,
AdjacencyListWeighted,
DistanceMatrix,
Empty,
FloydWarshall,
};
let mut digraph = AdjacencyListWeighted::<isize>::empty(7);
digraph.add_arc_weighted(0, 1, 5);
digraph.add_arc_weighted(0, 2, 3);
digraph.add_arc_weighted(0, 3, 2);
digraph.add_arc_weighted(0, 4, 4);
digraph.add_arc_weighted(1, 0, 3);
digraph.add_arc_weighted(1, 3, 1);
digraph.add_arc_weighted(1, 4, 2);
digraph.add_arc_weighted(2, 6, 4);
digraph.add_arc_weighted(3, 4, 1);
digraph.add_arc_weighted(3, 5, 1);
digraph.add_arc_weighted(4, 2, 3);
digraph.add_arc_weighted(5, 6, 1);
digraph.add_arc_weighted(6, 0, 9);
digraph.add_arc_weighted(6, 1, 8);
digraph.add_arc_weighted(6, 2, 5);
assert!(FloydWarshall::new(&digraph).distances().is_connected());
Sourcepub fn periphery(&self) -> impl Iterator<Item = usize> + '_
pub fn periphery(&self) -> impl Iterator<Item = usize> + '_
Return a digraph’s periphery.
A digraph’s periphery is the set of vertices with an eccentricity equal to its diameter.
§Examples
use graaf::{
AddArcWeighted,
AdjacencyListWeighted,
DistanceMatrix,
Empty,
FloydWarshall,
};
let mut digraph = AdjacencyListWeighted::<isize>::empty(7);
digraph.add_arc_weighted(0, 1, 5);
digraph.add_arc_weighted(0, 2, 3);
digraph.add_arc_weighted(0, 3, 2);
digraph.add_arc_weighted(0, 4, 4);
digraph.add_arc_weighted(1, 0, 3);
digraph.add_arc_weighted(1, 3, 1);
digraph.add_arc_weighted(1, 4, 2);
digraph.add_arc_weighted(2, 6, 4);
digraph.add_arc_weighted(3, 4, 1);
digraph.add_arc_weighted(3, 5, 1);
digraph.add_arc_weighted(4, 2, 3);
digraph.add_arc_weighted(5, 6, 1);
digraph.add_arc_weighted(6, 0, 9);
digraph.add_arc_weighted(6, 1, 8);
digraph.add_arc_weighted(6, 2, 5);
assert!(FloydWarshall::new(&digraph).distances().periphery().eq([4]));
Trait Implementations§
Source§impl<W: Clone> Clone for DistanceMatrix<W>
impl<W: Clone> Clone for DistanceMatrix<W>
Source§fn clone(&self) -> DistanceMatrix<W>
fn clone(&self) -> DistanceMatrix<W>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more