Skip to content

Commit

Permalink
Split 'HasVertex' into itself and 'VertexIn'.
Browse files Browse the repository at this point in the history
'HasVertex' is now only about whether there is a vertex.
'VertexIn' specifies a list of vertices guaranteed to be in the graph.
  • Loading branch information
Emoun committed Jan 19, 2025
1 parent 55d7139 commit 310abff
Show file tree
Hide file tree
Showing 26 changed files with 252 additions and 189 deletions.
20 changes: 7 additions & 13 deletions src/algo/bfs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::core::{property::HasVertex, Graph};
use crate::core::{property::VertexIn, Graph};
use std::collections::VecDeque;

/// Performs [breadth-first traversal](https://mathworld.wolfram.com/Breadth-FirstTraversal.html)
Expand Down Expand Up @@ -83,23 +83,17 @@ where
{
/// Constructs a new `Bfs` to traverse the specified graph.
///
/// It calls [`get_vertex`] on the graph, making the traversal start from
/// the returned vertex. The first call to [`next`] on the constructed `Bfs`
/// is guaranteed to return the aforementioned vertex.
///
/// ### Hint
///
/// [`VertexInGraph`](../core/property/struct.VertexInGraph.html) can be
/// used to select which specific vertex is returned by [`get_vertex`] and
/// thereby the starting vertex for the traversal.
/// It calls [`vertex_at::<0>()`] on the graph, making the traversal start
/// from the returned vertex. The first call to [`next`] on the constructed
/// `Bfs` is guaranteed to return the aforementioned vertex.
///
/// [`next`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
/// [`get_vertex`]: ../core/property/trait.HasVertex.html#method.get_vertex
/// [`vertex_at`]: ../core/property/trait.VertexIn.html#method.vertex_at
pub fn new(graph: &'a G) -> Self
where
G: HasVertex,
G: VertexIn<1>,
{
let v = graph.get_vertex();
let v = graph.vertex_at::<0>();

let mut result = Self {
graph,
Expand Down
8 changes: 4 additions & 4 deletions src/algo/dfs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::core::{property::HasVertex, Graph};
use crate::core::{property::VertexIn, Graph};
use std::borrow::Borrow;

/// Performs [depth-first traversal](https://mathworld.wolfram.com/Depth-FirstTraversal.html)
Expand Down Expand Up @@ -140,9 +140,9 @@ where
payload: F,
) -> Self
where
G: HasVertex,
G: VertexIn<1>,
{
let v = g.get_vertex();
let v = g.vertex_at::<0>();
let mut result = Self {
graph: g,
visited: Vec::new(),
Expand Down Expand Up @@ -230,7 +230,7 @@ where

impl<'a, G> Dfs<'a, G, ()>
where
G: 'a + HasVertex,
G: 'a + VertexIn<1>,
{
/// Constructs a new `Dfs` to traverse the specified graph.
///
Expand Down
12 changes: 6 additions & 6 deletions src/algo/dijkstra_shortest_paths.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::core::{property::HasVertex, Edge, Graph};
use crate::core::{property::VertexIn, Edge, Graph};
use num_traits::{PrimInt, Unsigned, Zero};
use std::borrow::Borrow;

Expand All @@ -21,14 +21,14 @@ where
{
pub fn new(graph: &'a G) -> Self
where
G: HasVertex,
G: VertexIn<1>,
{
let mut dijk = Self {
graph,
visited: Vec::new(),
queue: Vec::new(),
};
dijk.visit(graph.get_vertex(), G::EdgeWeight::zero());
dijk.visit(graph.vertex_at::<0>(), G::EdgeWeight::zero());
dijk
}

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

DijkstraShortestPaths::new(graph).map(move |(so, si, w)| {
let dist = distances.iter().find(|(v, _)| so == *v).unwrap().1;
Expand Down Expand Up @@ -114,7 +114,7 @@ where
{
pub fn new(graph: &'a G) -> Self
where
G: HasVertex,
G: VertexIn<1>,
{
Self {
dijk: DijkstraShortestPaths::new(graph),
Expand Down
1 change: 0 additions & 1 deletion src/algo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod bfs;
mod dfs;
mod dijkstra_shortest_paths;
mod tarjan_scc;
mod shortest_path;

pub use self::{bfs::*, dfs::*, dijkstra_shortest_paths::*, tarjan_scc::*};
use crate::core::{property::VertexInGraph, Ensure, Graph};
Expand Down
13 changes: 6 additions & 7 deletions src/algo/tarjan_scc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
use crate::{
algo::Dfs,
core::{
property::{ConnectedGraph, HasVertex},
property::{ConnectedGraph, VertexIn},
proxy::SubgraphProxy,
Directed, Graph, Guard,
},
Expand Down Expand Up @@ -82,9 +82,9 @@ use std::cmp::min;
/// # algo::TarjanScc,
/// # common::AdjListGraph,
/// # core::{
/// # Guard,
/// # Ensure,
/// # property::{
/// # NewVertex, AddEdge, HasVertexGraph, Subgraph
/// # NewVertex, AddEdge, VertexInGraph, Subgraph
/// # }
/// # },
/// # };
Expand All @@ -105,8 +105,7 @@ use std::cmp::min;
/// // Connect first SCC to second
/// graph.add_edge(&v0,&v2).unwrap();
///
/// // We use `HasVertexGraph` because we don't care where we start
/// let graph = HasVertexGraph::guard(graph).unwrap();
/// let graph = VertexInGraph::ensure(graph, [v0]).unwrap();
///
/// // Initialize algorithm
/// let mut tarj = TarjanScc::new(&graph);
Expand Down Expand Up @@ -160,7 +159,7 @@ where

impl<'a, G> TarjanScc<'a, G>
where
G: 'a + Graph<Directedness = Directed> + HasVertex,
G: 'a + Graph<Directedness = Directed> + VertexIn<1>,
{
/// Constructs a new `TarjanScc` to find the [strongly connected components](https://mathworld.wolfram.com/StronglyConnectedComponent.html)
/// of the specified graph.
Expand Down Expand Up @@ -214,7 +213,7 @@ where
Dfs::do_nothing_on_visit,
on_exit,
Dfs::do_nothing_on_explore,
vec![(graph.get_vertex(), 0)],
vec![(graph.vertex_at::<0>(), 0)],
);
Self {
dfs,
Expand Down
10 changes: 6 additions & 4 deletions src/core/property/connected.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use crate::{
core::{
property::{
proxy_remove_edge_where_weight, proxy_remove_vertex, DirectedGraph, EdgeCount,
HasVertex, HasVertexGraph, RemoveEdge, RemoveVertex, Unilateral, VertexInGraph, Weak,
RemoveEdge, RemoveVertex, Unilateral, VertexIn, VertexInGraph, Weak,
},
proxy::ReverseGraph,
Ensure, Graph, GraphDerefMut,
Ensure, Graph, GraphDerefMut, Release,
},
};
use num_traits::{PrimInt, Unsigned, Zero};
Expand All @@ -25,7 +25,7 @@ pub trait Connected: Unilateral
/// edge(s) between them.
fn eccentricity(&self) -> Self::EdgeWeight
where
Self: EdgeCount + HasVertex + Sized,
Self: EdgeCount + VertexIn<1> + Sized,
Self::EdgeWeight: PrimInt + Unsigned,
{
// We search for all the shortest paths, the eccentricity is the longest one
Expand Down Expand Up @@ -133,8 +133,10 @@ impl<C: Ensure> Ensure for ConnectedGraph<C>
let g = c.graph();
let v_count = g.all_vertices().count();

if let Ok(g) = HasVertexGraph::ensure(g, ())
if v_count > 0
{
let v = g.all_vertices().next().unwrap();
let g = VertexInGraph::ensure_unchecked(g.release(), [v]);
let dfs_count = Dfs::new_simple(&g).count();
if (dfs_count + 1) == v_count
{
Expand Down
Loading

0 comments on commit 310abff

Please sign in to comment.