Skip to content

Commit 310abff

Browse files
committed
Split 'HasVertex' into itself and 'VertexIn'.
'HasVertex' is now only about whether there is a vertex. 'VertexIn' specifies a list of vertices guaranteed to be in the graph.
1 parent 55d7139 commit 310abff

26 files changed

+252
-189
lines changed

src/algo/bfs.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::core::{property::HasVertex, Graph};
1+
use crate::core::{property::VertexIn, Graph};
22
use std::collections::VecDeque;
33

44
/// Performs [breadth-first traversal](https://mathworld.wolfram.com/Breadth-FirstTraversal.html)
@@ -83,23 +83,17 @@ where
8383
{
8484
/// Constructs a new `Bfs` to traverse the specified graph.
8585
///
86-
/// It calls [`get_vertex`] on the graph, making the traversal start from
87-
/// the returned vertex. The first call to [`next`] on the constructed `Bfs`
88-
/// is guaranteed to return the aforementioned vertex.
89-
///
90-
/// ### Hint
91-
///
92-
/// [`VertexInGraph`](../core/property/struct.VertexInGraph.html) can be
93-
/// used to select which specific vertex is returned by [`get_vertex`] and
94-
/// thereby the starting vertex for the traversal.
86+
/// It calls [`vertex_at::<0>()`] on the graph, making the traversal start
87+
/// from the returned vertex. The first call to [`next`] on the constructed
88+
/// `Bfs` is guaranteed to return the aforementioned vertex.
9589
///
9690
/// [`next`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
97-
/// [`get_vertex`]: ../core/property/trait.HasVertex.html#method.get_vertex
91+
/// [`vertex_at`]: ../core/property/trait.VertexIn.html#method.vertex_at
9892
pub fn new(graph: &'a G) -> Self
9993
where
100-
G: HasVertex,
94+
G: VertexIn<1>,
10195
{
102-
let v = graph.get_vertex();
96+
let v = graph.vertex_at::<0>();
10397

10498
let mut result = Self {
10599
graph,

src/algo/dfs.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::core::{property::HasVertex, Graph};
1+
use crate::core::{property::VertexIn, Graph};
22
use std::borrow::Borrow;
33

44
/// Performs [depth-first traversal](https://mathworld.wolfram.com/Depth-FirstTraversal.html)
@@ -140,9 +140,9 @@ where
140140
payload: F,
141141
) -> Self
142142
where
143-
G: HasVertex,
143+
G: VertexIn<1>,
144144
{
145-
let v = g.get_vertex();
145+
let v = g.vertex_at::<0>();
146146
let mut result = Self {
147147
graph: g,
148148
visited: Vec::new(),
@@ -230,7 +230,7 @@ where
230230

231231
impl<'a, G> Dfs<'a, G, ()>
232232
where
233-
G: 'a + HasVertex,
233+
G: 'a + VertexIn<1>,
234234
{
235235
/// Constructs a new `Dfs` to traverse the specified graph.
236236
///

src/algo/dijkstra_shortest_paths.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::core::{property::HasVertex, Edge, Graph};
1+
use crate::core::{property::VertexIn, Edge, Graph};
22
use num_traits::{PrimInt, Unsigned, Zero};
33
use std::borrow::Borrow;
44

@@ -21,14 +21,14 @@ where
2121
{
2222
pub fn new(graph: &'a G) -> Self
2323
where
24-
G: HasVertex,
24+
G: VertexIn<1>,
2525
{
2626
let mut dijk = Self {
2727
graph,
2828
visited: Vec::new(),
2929
queue: Vec::new(),
3030
};
31-
dijk.visit(graph.get_vertex(), G::EdgeWeight::zero());
31+
dijk.visit(graph.vertex_at::<0>(), G::EdgeWeight::zero());
3232
dijk
3333
}
3434

@@ -66,9 +66,9 @@ where
6666
/// weighted distance to them
6767
pub fn distances(graph: &'a G) -> impl 'a + Iterator<Item = (G::Vertex, G::EdgeWeight)>
6868
where
69-
G: HasVertex,
69+
G: VertexIn<1>,
7070
{
71-
let mut distances = vec![(graph.get_vertex(), G::EdgeWeight::zero())];
71+
let mut distances = vec![(graph.vertex_at::<0>(), G::EdgeWeight::zero())];
7272

7373
DijkstraShortestPaths::new(graph).map(move |(so, si, w)| {
7474
let dist = distances.iter().find(|(v, _)| so == *v).unwrap().1;
@@ -114,7 +114,7 @@ where
114114
{
115115
pub fn new(graph: &'a G) -> Self
116116
where
117-
G: HasVertex,
117+
G: VertexIn<1>,
118118
{
119119
Self {
120120
dijk: DijkstraShortestPaths::new(graph),

src/algo/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ mod bfs;
44
mod dfs;
55
mod dijkstra_shortest_paths;
66
mod tarjan_scc;
7-
mod shortest_path;
87

98
pub use self::{bfs::*, dfs::*, dijkstra_shortest_paths::*, tarjan_scc::*};
109
use crate::core::{property::VertexInGraph, Ensure, Graph};

src/algo/tarjan_scc.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
use crate::{
5252
algo::Dfs,
5353
core::{
54-
property::{ConnectedGraph, HasVertex},
54+
property::{ConnectedGraph, VertexIn},
5555
proxy::SubgraphProxy,
5656
Directed, Graph, Guard,
5757
},
@@ -82,9 +82,9 @@ use std::cmp::min;
8282
/// # algo::TarjanScc,
8383
/// # common::AdjListGraph,
8484
/// # core::{
85-
/// # Guard,
85+
/// # Ensure,
8686
/// # property::{
87-
/// # NewVertex, AddEdge, HasVertexGraph, Subgraph
87+
/// # NewVertex, AddEdge, VertexInGraph, Subgraph
8888
/// # }
8989
/// # },
9090
/// # };
@@ -105,8 +105,7 @@ use std::cmp::min;
105105
/// // Connect first SCC to second
106106
/// graph.add_edge(&v0,&v2).unwrap();
107107
///
108-
/// // We use `HasVertexGraph` because we don't care where we start
109-
/// let graph = HasVertexGraph::guard(graph).unwrap();
108+
/// let graph = VertexInGraph::ensure(graph, [v0]).unwrap();
110109
///
111110
/// // Initialize algorithm
112111
/// let mut tarj = TarjanScc::new(&graph);
@@ -160,7 +159,7 @@ where
160159

161160
impl<'a, G> TarjanScc<'a, G>
162161
where
163-
G: 'a + Graph<Directedness = Directed> + HasVertex,
162+
G: 'a + Graph<Directedness = Directed> + VertexIn<1>,
164163
{
165164
/// Constructs a new `TarjanScc` to find the [strongly connected components](https://mathworld.wolfram.com/StronglyConnectedComponent.html)
166165
/// of the specified graph.
@@ -214,7 +213,7 @@ where
214213
Dfs::do_nothing_on_visit,
215214
on_exit,
216215
Dfs::do_nothing_on_explore,
217-
vec![(graph.get_vertex(), 0)],
216+
vec![(graph.vertex_at::<0>(), 0)],
218217
);
219218
Self {
220219
dfs,

src/core/property/connected.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use crate::{
33
core::{
44
property::{
55
proxy_remove_edge_where_weight, proxy_remove_vertex, DirectedGraph, EdgeCount,
6-
HasVertex, HasVertexGraph, RemoveEdge, RemoveVertex, Unilateral, VertexInGraph, Weak,
6+
RemoveEdge, RemoveVertex, Unilateral, VertexIn, VertexInGraph, Weak,
77
},
88
proxy::ReverseGraph,
9-
Ensure, Graph, GraphDerefMut,
9+
Ensure, Graph, GraphDerefMut, Release,
1010
},
1111
};
1212
use num_traits::{PrimInt, Unsigned, Zero};
@@ -25,7 +25,7 @@ pub trait Connected: Unilateral
2525
/// edge(s) between them.
2626
fn eccentricity(&self) -> Self::EdgeWeight
2727
where
28-
Self: EdgeCount + HasVertex + Sized,
28+
Self: EdgeCount + VertexIn<1> + Sized,
2929
Self::EdgeWeight: PrimInt + Unsigned,
3030
{
3131
// We search for all the shortest paths, the eccentricity is the longest one
@@ -133,8 +133,10 @@ impl<C: Ensure> Ensure for ConnectedGraph<C>
133133
let g = c.graph();
134134
let v_count = g.all_vertices().count();
135135

136-
if let Ok(g) = HasVertexGraph::ensure(g, ())
136+
if v_count > 0
137137
{
138+
let v = g.all_vertices().next().unwrap();
139+
let g = VertexInGraph::ensure_unchecked(g.release(), [v]);
138140
let dfs_count = Dfs::new_simple(&g).count();
139141
if (dfs_count + 1) == v_count
140142
{

0 commit comments

Comments
 (0)