graaf::algo::distance_matrix

Struct DistanceMatrix

Source
pub struct DistanceMatrix<W> {
    pub infinity: W,
    /* private fields */
}
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.

A digraph of order 7 and size 15

§The distance matrix

The corresponding DistanceMatrix generated by FloydWarshall::distances.

The corresponding distance matrix generated by the Floyd-Warshall algorithm

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].eq(&[0, 5, 3, 2, 3, 3, 4]));
assert!(dist[1].eq(&[3, 0, 5, 1, 2, 2, 3]));
assert!(dist[2].eq(&[13, 12, 0, 13, 14, 14, 4]));
assert!(dist[3].eq(&[11, 10, 4, 0, 1, 1, 2]));
assert!(dist[4].eq(&[16, 15, 3, 16, 0, 17, 7]));
assert!(dist[5].eq(&[10, 9, 6, 10, 11, 0, 1]));
assert!(dist[6].eq(&[9, 8, 5, 9, 10, 10, 0]));

Fields§

§infinity: W

The distance between unconnected vertices.

Implementations§

Source§

impl<W> DistanceMatrix<W>

Source

pub fn new(order: usize, infinity: W) -> Self
where 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], vec![0; 4]);
assert_eq!(dist[1], vec![0; 4]);
assert_eq!(dist[2], vec![0; 4]);
assert_eq!(dist[3], vec![0; 4]);
Source

pub fn center(&self) -> Vec<usize>
where W: Copy + Ord,

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.

A digraph and its center

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]));
Source

pub fn diameter(&self) -> &W
where W: Copy + Ord,

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.

A digraph and its diameter

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);
Source

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

A digraph and the vertices’ eccentricities

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]));
Source

pub fn is_connected(&self) -> bool
where 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());
Source

pub fn periphery(&self) -> impl Iterator<Item = usize> + '_
where W: Copy + Ord,

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>

Source§

fn clone(&self) -> DistanceMatrix<W>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<W: Debug> Debug for DistanceMatrix<W>

Source§

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

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

impl<W: Hash> Hash for DistanceMatrix<W>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<W> Index<usize> for DistanceMatrix<W>

Source§

type Output = Vec<W>

The returned type after indexing.
Source§

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

Performs the indexing (container[index]) operation. Read more
Source§

impl<W> IndexMut<usize> for DistanceMatrix<W>

Source§

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

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<W: Ord> Ord for DistanceMatrix<W>

Source§

fn cmp(&self, other: &DistanceMatrix<W>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<W: PartialEq> PartialEq for DistanceMatrix<W>

Source§

fn eq(&self, other: &DistanceMatrix<W>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<W: PartialOrd> PartialOrd for DistanceMatrix<W>

Source§

fn partial_cmp(&self, other: &DistanceMatrix<W>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<W: Eq> Eq for DistanceMatrix<W>

Source§

impl<W> StructuralPartialEq for DistanceMatrix<W>

Auto Trait Implementations§

§

impl<W> Freeze for DistanceMatrix<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for DistanceMatrix<W>
where W: RefUnwindSafe,

§

impl<W> Send for DistanceMatrix<W>
where W: Send,

§

impl<W> Sync for DistanceMatrix<W>
where W: Sync,

§

impl<W> Unpin for DistanceMatrix<W>
where W: Unpin,

§

impl<W> UnwindSafe for DistanceMatrix<W>
where W: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.