|
1 | 1 | use crate::{
|
2 |
| - algo::Dfs, |
| 2 | + algo::{Dfs, DijkstraShortestPaths}, |
3 | 3 | core::{
|
4 | 4 | property::{
|
5 |
| - proxy_remove_edge_where_weight, proxy_remove_vertex, DirectedGraph, HasVertexGraph, |
6 |
| - RemoveEdge, RemoveVertex, Unilateral, Weak, |
| 5 | + proxy_remove_edge_where_weight, proxy_remove_vertex, DirectedGraph, EdgeCount, |
| 6 | + HasVertex, HasVertexGraph, RemoveEdge, RemoveVertex, Unilateral, VertexInGraph, Weak, |
7 | 7 | },
|
8 | 8 | proxy::ReverseGraph,
|
9 | 9 | Ensure, Graph, GraphDerefMut,
|
10 | 10 | },
|
11 | 11 | };
|
| 12 | +use num_traits::{PrimInt, Unsigned}; |
12 | 13 | use std::borrow::Borrow;
|
13 | 14 |
|
14 | 15 | /// A marker trait for graphs that are connected.
|
15 | 16 | ///
|
16 | 17 | /// A graph is connected if there is a path from any vertex to any other vertex.
|
17 | 18 | /// Graphs with one or zero vertices count as connected.
|
18 |
| -pub trait Connected: Unilateral {} |
| 19 | +pub trait Connected: Unilateral |
| 20 | +{ |
| 21 | + /// Calculates the maximum distance between the designated vertex and any other vertex ([the eccentricity](https://mathworld.wolfram.com/GraphEccentricity.html)). |
| 22 | + /// |
| 23 | + /// Takes a closure that converts an edge's weight into a distance value. |
| 24 | + /// The distance between two vertices is equal to the distance of the |
| 25 | + /// edge(s) between them. |
| 26 | + fn eccentricity_weighted<W: PrimInt + Unsigned>( |
| 27 | + &self, |
| 28 | + get_distance: fn(&Self::EdgeWeight) -> W, |
| 29 | + ) -> W |
| 30 | + where |
| 31 | + Self: EdgeCount + HasVertex + Sized, |
| 32 | + { |
| 33 | + // We search for all the shortest paths, the eccentricity is the longest one |
| 34 | + DijkstraShortestPaths::distances(self, get_distance).fold(W::zero(), |max_dist, (_, d2)| { |
| 35 | + if max_dist < d2 |
| 36 | + { |
| 37 | + d2 |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + max_dist |
| 42 | + } |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + /// Calculates the maximum eccentricity of the graph ([the diameter](https://mathworld.wolfram.com/GraphDiameter.html)). |
| 47 | + /// |
| 48 | + /// Takes a closure that converts an edge's weight into a distance value. |
| 49 | + /// The distance between two vertices is equal to the distance of the |
| 50 | + /// edge(s) between them. |
| 51 | + fn diameter_weighted<W: PrimInt + Unsigned>( |
| 52 | + &self, |
| 53 | + get_distance: fn(&Self::EdgeWeight) -> W, |
| 54 | + ) -> W |
| 55 | + where |
| 56 | + Self: EdgeCount + Sized, |
| 57 | + { |
| 58 | + self.all_vertices().fold(W::zero(), |max_ecc, v| { |
| 59 | + let new_ecc = |
| 60 | + VertexInGraph::ensure_unvalidated(self, v).eccentricity_weighted(get_distance); |
| 61 | + if new_ecc > max_ecc |
| 62 | + { |
| 63 | + new_ecc |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + max_ecc |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + /// Calculates the minimum eccentricity of the graph ([the radius](https://mathworld.wolfram.com/GraphDiameter.html)). |
| 73 | + /// |
| 74 | + /// Takes a closure that converts an edge's weight into a distance value. |
| 75 | + /// The distance between two vertices is equal to the distance of the |
| 76 | + /// edge(s) between them. |
| 77 | + fn radius_weighted<W: PrimInt + Unsigned>(&self, get_distance: fn(&Self::EdgeWeight) -> W) -> W |
| 78 | + where |
| 79 | + Self: EdgeCount + Sized, |
| 80 | + { |
| 81 | + self.all_vertices().fold(W::zero(), |min_ecc, v| { |
| 82 | + let new_ecc = |
| 83 | + VertexInGraph::ensure_unvalidated(self, v).eccentricity_weighted(get_distance); |
| 84 | + if new_ecc < min_ecc |
| 85 | + { |
| 86 | + new_ecc |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + min_ecc |
| 91 | + } |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + /// Returns the vertices with eccentricity equal to the radius ([the centers](https://mathworld.wolfram.com/GraphCenter.html)). |
| 96 | + /// |
| 97 | + /// Takes a closure that converts an edge's weight into a distance value. |
| 98 | + /// The distance between two vertices is equal to the distance of the |
| 99 | + /// edge(s) between them. |
| 100 | + fn centers_weighted<'a, W: 'a + PrimInt + Unsigned>( |
| 101 | + &'a self, |
| 102 | + get_distance: fn(&Self::EdgeWeight) -> W, |
| 103 | + ) -> impl Iterator<Item = Self::Vertex> + '_ |
| 104 | + where |
| 105 | + Self: EdgeCount + Sized, |
| 106 | + { |
| 107 | + let radius = self.radius_weighted(get_distance); |
| 108 | + self.all_vertices().filter(move |v| { |
| 109 | + VertexInGraph::ensure_unvalidated(self, *v).eccentricity_weighted(get_distance) |
| 110 | + == radius |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
19 | 114 |
|
20 | 115 | #[derive(Clone, Debug)]
|
21 | 116 | pub struct ConnectedGraph<C: Ensure>(C);
|
|
0 commit comments