Skip to content

Commit

Permalink
release: 0.107.0
Browse files Browse the repository at this point in the history
Changed

- Breaking: replace `impl<D> HasWalk for D where D: HasArc` with `impl HasWalk for [AdjacencyList, AdjacencyListWeighted, AdjacencyMap, AdjacencyMatrix, EdgeList]`.
  • Loading branch information
basdirks-purple committed Nov 25, 2024
1 parent da4ee05 commit b418cad
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 43 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
- Return iterators instead of vectors.
- Test for `order > 0` in `bellman_ford_moore` and other algorithms that take a digraph.

## [0.107.0] - 2024-11-25

Changed

- Breaking: replace `impl<D> HasWalk for D where D: HasArc` with `impl HasWalk for [AdjacencyList, AdjacencyListWeighted, AdjacencyMap, AdjacencyMatrix, EdgeList]`.

## [0.106.0] - 2024-11-24

Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "graaf"
version = "0.106.0"
version = "0.107.0"
edition = "2021"
authors = ["Bas Dirks <bas.dirks@protonmail.com>"]
categories = ["algorithms", "data-structures", "mathematics"]
Expand Down
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
graaf = "0.106.0"
graaf = "0.107.0"
```

## Representations
Expand Down Expand Up @@ -110,9 +110,7 @@ graaf = "0.106.0"

### Bellman-Ford-Moore

The Bellman-Ford-Moore algorithm finds the shortest distances from a source vertex to all other vertices in an arc-weighted digraph with negative weights.

- [`BellmanFordMoore::distances`] finds the shortest distances.
[`BellmanFordMoore::distances`] finds the shortest distances from a source vertex to all other vertices in an arc-weighted digraph with negative weights.

### Breadth-First Search

Expand Down Expand Up @@ -158,15 +156,11 @@ A [`DistanceMatrix`] contains the shortest distances between all vertex pairs in

### Floyd-Warshall

The Floyd-Warshall algorithm finds the distance between each vertex pair in an arc-weighted digraph.

- [`FloydWarshall::distances`] finds the shortest distances.
[`FloydWarshall::distances`] finds the distance between each vertex pair in an arc-weighted digraph.

### Johnson's Circuit-Finding Algorithm

Johnson's circuit-finding algorithm finds all circuits in a digraph.

- [`Johnson75::circuits`] finds all circuits.
[`Johnson75::circuits`] finds all circuits in a digraph.

### Predecessor Tree

Expand All @@ -177,9 +171,7 @@ A [`PredecessorTree`] contains the vertex predecessors.

### Tarjan

Tarjan's algorithm finds strongly connected components in a digraph.

- [`Tarjan::components`] finds strongly connected components.
The [`Tarjan::components`] algorithm finds strongly connected components in a digraph.

[`AddArcWeighted`]: https://docs.rs/graaf/latest/graaf/op/add_arc_weighted/trait.AddArcWeighted.html
[`AddArc`]: https://docs.rs/graaf/latest/graaf/op/add_arc/trait.AddArc.html
Expand Down
19 changes: 6 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@
//!
//! ## Bellman-Ford-Moore
//!
//! Find the shortest distances from a source vertex to all other vertices in
//! an arc-weighted digraph with negative weights.
//! - [`BellmanFordMoore::distances`] finds the shortest distances.
//! [`BellmanFordMoore::distances`] finds the shortest distances from a source
//! vertex to all other vertices in an arc-weighted digraph with negative
//! weights.
//!
//! ## Breadth-First Search
//!
Expand Down Expand Up @@ -161,16 +160,12 @@
//!
//! ## Floyd-Warshall
//!
//! The Floyd-Warshall algorithm finds the distance between each vertex pair in
//! [`FloydWarshall::distances`] finds the distance between each vertex pair in
//! an arc-weighted digraph.
//!
//! - [`FloydWarshall::distances`] finds the shortest distances.
//!
//! ## Johnson's Circuit-Finding Algorithm
//!
//! Johnson's circuit-finding algorithm finds a digraph's circuits.
//!
//! - [`Johnson75::circuits`] finds a digraph's circuits.
//! [`Johnson75::circuits`] finds a digraph's circuits.
//!
//! ## Predecessor Tree
//!
Expand All @@ -182,9 +177,7 @@
//!
//! ## Tarjan
//!
//! Tarjan's algorithm finds strongly connected components in a digraph.
//!
//! - [`Tarjan::components`] finds strongly connected components.
//! [`Tarjan::components`] finds strongly connected components in a digraph.
pub mod algo;
pub mod gen;
Expand Down
15 changes: 0 additions & 15 deletions src/op/has_walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
//! assert!(!digraph.has_walk(&[0, 2]));
//! ```
use crate::HasArc;

/// Digraph walk
pub trait HasWalk {
/// Check whether the sequence is a walk in the digraph.
Expand Down Expand Up @@ -59,16 +57,3 @@ pub trait HasWalk {
#[must_use]
fn has_walk(&self, walk: &[usize]) -> bool;
}

impl<D> HasWalk for D
where
D: HasArc,
{
fn has_walk(&self, walk: &[usize]) -> bool {
walk.len() > 1
&& walk
.iter()
.zip(walk.iter().skip(1))
.all(|(&u, &v)| self.has_arc(u, v))
}
}
11 changes: 11 additions & 0 deletions src/repr/adjacency_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ use {
GrowingNetwork,
HasArc,
HasEdge,
HasWalk,
InNeighbors,
Indegree,
IsComplete,
Expand Down Expand Up @@ -575,6 +576,16 @@ impl HasEdge for AdjacencyList {
}
}

impl HasWalk for AdjacencyList {
fn has_walk(&self, walk: &[usize]) -> bool {
walk.len() > 1
&& walk
.iter()
.zip(walk.iter().skip(1))
.all(|(&u, &v)| self.has_arc(u, v))
}
}

impl Indegree for AdjacencyList {
/// # Panics
///
Expand Down
39 changes: 38 additions & 1 deletion src/repr/adjacency_list_weighted/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use {
Empty,
HasArc,
HasEdge,
HasWalk,
InNeighbors,
Indegree,
IsComplete,
Expand Down Expand Up @@ -233,6 +234,16 @@ impl<W> HasEdge for AdjacencyListWeighted<W> {
}
}

impl<W> HasWalk for AdjacencyListWeighted<W> {
fn has_walk(&self, walk: &[usize]) -> bool {
walk.len() > 1
&& walk
.iter()
.zip(walk.iter().skip(1))
.all(|(&u, &v)| self.has_arc(u, v))
}
}

impl<W> Indegree for AdjacencyListWeighted<W> {
/// # Panics
///
Expand Down Expand Up @@ -1460,7 +1471,33 @@ mod tests {
}

#[test]
fn in_neighbors_bang_jensen_94_weighted() {
fn has_walk_bang_jensen_94() {
let digraph = bang_jensen_94_usize();

assert!(digraph.has_walk(&[0, 1, 3, 5]));
assert!(digraph.has_walk(&[0, 2, 1, 3, 5]));
assert!(digraph.has_walk(&[0, 2, 3, 5]));
assert!(digraph.has_walk(&[0, 2, 4, 6]));
assert!(digraph.has_walk(&[0, 2, 5]));
assert!(digraph.has_walk(&[1, 3, 5]));
assert!(digraph.has_walk(&[2, 1, 3, 5]));
assert!(digraph.has_walk(&[2, 3, 5]));
assert!(digraph.has_walk(&[2, 4, 6]));
assert!(digraph.has_walk(&[2, 5]));
assert!(digraph.has_walk(&[3, 5]));
assert!(digraph.has_walk(&[4, 6]));

assert!(!digraph.has_walk(&[0, 3]));
assert!(!digraph.has_walk(&[1, 0]));
assert!(!digraph.has_walk(&[2, 0]));
assert!(!digraph.has_walk(&[3, 0]));
assert!(!digraph.has_walk(&[4, 0]));
assert!(!digraph.has_walk(&[5]));
assert!(!digraph.has_walk(&[6]));
}

#[test]
fn in_neighbors_bang_jensen_94() {
let digraph = bang_jensen_94_usize();

assert!(digraph.in_neighbors(0).eq([]));
Expand Down
11 changes: 11 additions & 0 deletions src/repr/adjacency_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ use {
GrowingNetwork,
HasArc,
HasEdge,
HasWalk,
InNeighbors,
Indegree,
IsComplete,
Expand Down Expand Up @@ -606,6 +607,16 @@ impl HasEdge for AdjacencyMap {
}
}

impl HasWalk for AdjacencyMap {
fn has_walk(&self, walk: &[usize]) -> bool {
walk.len() > 1
&& walk
.iter()
.zip(walk.iter().skip(1))
.all(|(&u, &v)| self.has_arc(u, v))
}
}

impl Indegree for AdjacencyMap {
/// # Panics
///
Expand Down
11 changes: 11 additions & 0 deletions src/repr/adjacency_matrix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ use crate::{
GrowingNetwork,
HasArc,
HasEdge,
HasWalk,
InNeighbors,
Indegree,
IsComplete,
Expand Down Expand Up @@ -645,6 +646,16 @@ impl HasEdge for AdjacencyMatrix {
}
}

impl HasWalk for AdjacencyMatrix {
fn has_walk(&self, walk: &[usize]) -> bool {
walk.len() > 1
&& walk
.iter()
.zip(walk.iter().skip(1))
.all(|(&u, &v)| self.has_arc(u, v))
}
}

impl Indegree for AdjacencyMatrix {
/// Warning: this implementation runs in **O(v)**.
///
Expand Down
11 changes: 11 additions & 0 deletions src/repr/edge_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ use {
GrowingNetwork,
HasArc,
HasEdge,
HasWalk,
InNeighbors,
Indegree,
IsComplete,
Expand Down Expand Up @@ -549,6 +550,16 @@ impl HasEdge for EdgeList {
}
}

impl HasWalk for EdgeList {
fn has_walk(&self, walk: &[usize]) -> bool {
walk.len() > 1
&& walk
.iter()
.zip(walk.iter().skip(1))
.all(|(&u, &v)| self.has_arc(u, v))
}
}

impl Indegree for EdgeList {
/// Warning: runs in **O(a)** time, where **a** is the number of arcs.
///
Expand Down

0 comments on commit b418cad

Please sign in to comment.