Skip to content

Commit

Permalink
version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
henke443 committed Apr 7, 2024
1 parent d8c7d66 commit 4ba887b
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 54 deletions.
25 changes: 3 additions & 22 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
Version 1.1.0 (2024-04-04)
Version 0.1.0 (2024-04-04)
==========================
Lots of breaking changes to correct some of my own concerns and also some feedback from other people.
Yanked version 1.x.x and reverted to 0.1.0 to follow semver since the api is not yet 100% stable.
I will try to avoid breaking changes but there almost certainly be a few more ones.
When I think the library is stable I will set the version to 2.0.0.
When I think the library is stable I will set the version to 1.0.0.

Breaking changes
----------------
- Removed SlotMapGraph
- Reduced Clone requirement
- `add_edge` now returns EdgeID.
- CategoryGraph renamed to CategorizedGraph

Version 1.0.2 (2024-04-04)
==========================

- Cleaned some things up mostly and changed documentation.
- For the next version I'm planning to add more tests and making the github prettier. I will also make what I got even neater and then the version after that I aim to add more graph algorithms. After that I can start creating some benchmarks to see how much/if there is a significant speedup in some situations compared to alternatives and when that happen.

Version 1.0.1 (2024-04-03)
==========================

- Some very minor fixes

Version 1.0.0 (2024-04-03)
==========================

Breaking changes
----------------

- Everything is new!

2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ documentation = "https://docs.rs/fast-graph"
readme = "README.md"
homepage = "https://github.com/henke443/fast-graph"
repository = "https://github.com/henke443/fast-graph"
version = "1.1.1"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
categories = ["data-structures", "database", "mathematics", "science::geo", "visualization"]
Expand All @@ -29,6 +29,7 @@ serde = ["dep:serde", "dep:serde_json", "slotmap/serde"]
specta = ["dep:specta", "dep:tauri-specta"]
hashbrown = ["dep:hashbrown", "hashbrown/serde"]
categories = []
std = []



7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ fast-graph
[![Rust CI](https://github.com/henke443/fast-graph/actions/workflows/rust-ci.yml/badge.svg)](https://github.com/henke443/fast-graph/actions/workflows/rust-ci.yml)
![MSRV][msrv-badge]

## Note
⚠️ There will be some breaking changes in the coming 1-2 weeks at the very least until version 1.0.0 arrives.

## Lightweight & fast.
⚠️ Version 1.x.x is a bit misleading and there will be some breaking changes in the coming 1-2 weeks at the very least.
## [Roadmap](ROADMAP.md)

## Lightweight & fast.
By default, [SlotMaps](https://docs.rs/slotmap/latest/slotmap/index.html) are used to store the nodes and edges which solves the [ABA problem] while also providing O(1) insertion, deletion and lookup times. Additionally, and optionally,
[HashBrown](https://docs.rs/hashbrown/latest/hashbrown/index.html) is used instead of [`std::HashMap`] to map category names to ids in the [`CategorizedGraph`](https://docs.rs/fast-graph/latest/fast_graph/categories/struct.CategorizedGraph.html) struct.

[ABA problem]: https://en.wikipedia.org/wiki/ABA_problem

Expand Down
19 changes: 19 additions & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@



# General
- No-std support [ ]
- Benchmarks [ ]
- Comparing benchmarks to alternatives [ ]
- Syntax improvements [ ]
- An undirected graph where the edges are just NodeIDs would make sense to skip the extra access for an edge. [ ]

# Algorithms
- Topological sort [ ]
- Has path to root [ ]
- Depth first walk (especially post order) [ ]
- Cycle detection [ ]
- Breadth first [ ]

# Parallelization
- Rayon support [ ]
71 changes: 71 additions & 0 deletions src/algorithms/bfs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#[cfg(feature = "hashbrown")]
use hashbrown::HashSet;

#[cfg(not(feature = "hashbrown"))]
use std::collections::HashSet;

//#[cfg(feature = "std")]
use std::collections::VecDeque;

// #[cfg(not(feature = "std"))]
// use alloc::collections::VecDeque;


use crate::{GraphInterface, NodeID};



pub struct BreadthFirstSearch<'a, G: GraphInterface> {
graph: &'a G,
start: NodeID,
visited: HashSet<NodeID>,
queue: VecDeque<NodeID>,
visited_edges: Vec<(NodeID, NodeID)>
}

impl<'a, G: GraphInterface> BreadthFirstSearch<'a, G> {
pub fn new(graph: &'a G, start: NodeID) -> Self {
Self {
graph,
start,
visited: HashSet::new(),
queue: VecDeque::from(vec![start]),
visited_edges: Vec::new(),
}
}
}

impl <'a, G: GraphInterface> Iterator for BreadthFirstSearch<'a, G> {
type Item = NodeID;

fn next(&mut self) -> Option<Self::Item> {
if let Some(node) = self.queue.pop_front() {
if self.visited.contains(&node) {
return self.next();
}
self.visited.insert(node);

let node = self.graph.node(node).unwrap();
for edge in &node.connections {
let edge = self.graph.edge(*edge).unwrap();
if !self.visited.contains(&edge.to) {
self.queue.push_back(edge.to);
self.visited_edges.push((edge.from, edge.to));
}
}

return Some(node.id);
}
None
}
}

impl<'a, G: GraphInterface> BreadthFirstSearch<'a, G> {
pub fn visited_edges(&self) -> &Vec<(NodeID, NodeID)> {
&self.visited_edges
}

pub fn visited(&self) -> &HashSet<NodeID> {
&self.visited
}
}
120 changes: 93 additions & 27 deletions src/algorithms/dfs.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
//! # Under development
#[cfg(feature = "hashbrown")]
use hashbrown::HashSet;
#[cfg(not(feature = "hashbrown"))]
use std::collections::HashSet;

use crate::{
Edge, Graph, GraphInterface, NodeID
};


/// Under development
#[derive(Clone)]
pub struct DepthFirstSearch<'a, G: GraphInterface> {
graph: &'a G,
start: NodeID,
Expand Down Expand Up @@ -35,7 +39,6 @@ impl <'a, G: GraphInterface> Iterator for DepthFirstSearch<'a, G> {
fn next(&mut self) -> Option<Self::Item> {
if let Some(node) = self.stack.pop() {
if self.visited.contains(&node) {
println!("Cyclic graph detected, visited edges: {:#?}", self.visited_edges);
self.cyclic = true;
return self.next();
}
Expand All @@ -61,27 +64,54 @@ impl <'a, G: GraphInterface> Iterator for DepthFirstSearch<'a, G> {

impl<'a, G: GraphInterface> std::iter::FusedIterator for DepthFirstSearch<'a, G> {}

impl<'a, G: GraphInterface> std::iter::ExactSizeIterator for DepthFirstSearch<'a, G> {
fn len(&self) -> usize {
self.graph.node_count() - self.visited.len()
}
}

/// Under development
pub trait IterDepthFirst<'a, G: GraphInterface> {
/// Returns a *depth first search* iterator starting from a given node
fn iter_depth_first(&'a self, start: NodeID) -> DepthFirstSearch<'a, G>;

/// Returns a vector of sets of node IDs, where each set is a connected component. \
/// Starts a DFS at every node (except if it's already been visited) and marks all reachable nodes as being part of the same component.
fn connected_components(&'a self) -> Vec<HashSet<NodeID>>;
}

impl<'a, G: GraphInterface> IterDepthFirst<'a, G> for G {
/// Under development
fn iter_depth_first(&'a self, start: NodeID) -> DepthFirstSearch<'a, G> {
DepthFirstSearch::new(self, start)
}

/// Returns a vector of sets of node IDs, where each set is a connected component. \
/// Starts a DFS at every node (except if it's already been visited) and marks all reachable nodes as being part of the same component.
fn connected_components(&'a self) -> Vec<HashSet<NodeID>> {
let mut visited = HashSet::new();
let mut components = Vec::new();
let mut current_component = 0usize;

// Starts a DFS at every node
for node_id in self.nodes() {
// (except if it's already been visited)
if visited.contains(&node_id) {
continue;
}
for node in self.iter_depth_first(node_id) {
visited.insert(node);

// and marks all reachable nodes as being part of the same component.
if current_component >= components.len() {
components.push(HashSet::new());
}
components[current_component].insert(node);
}
current_component += 1;
}

components
}
}





#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -99,16 +129,58 @@ mod tests {
}
}

macro_rules! get_graph {
($graph:ident, $n:expr) => {
{
let mut nodes = Vec::new();
for i in 0..$n {
nodes.push(NodeData::Int64(i));
}
let nodes = $graph.add_nodes(&nodes);
if nodes.len() != $n {
panic!("Failed to add nodes");
}
nodes[..].try_into().unwrap()
}
};
}

#[test]
fn test_dfs() {
fn test_dfs_connected_components() {
let mut graph: Graph<NodeData, ()> = Graph::new();
let [node0, node1, node2, node3, node4] = get_graph!(graph, 5);

let mut components = graph.connected_components();
println!("Connected components 1 ({}): {:#?}", components.len(), components);
assert_eq!(components.len(), 5);
assert_eq!(components[0].len(), 1);

graph.add_edges(&[
(node0, node1),
(node1, node0),
]);

components = graph.connected_components();
println!("Connected components 2 ({}): {:#?}", components.len(), components);
assert_eq!(components.len(), 4);
assert_eq!(components[0].len(), 2);

graph.add_edges(&[
(node2, node3),
(node3, node4),
]);

components = graph.connected_components();
println!("Connected components 3 ({}): {:#?}", components.len(), components);

assert_eq!(components.len(), 2);
assert_eq!(components[1].len(), 3);
}

#[test]
fn test_dfs_iter() {
let mut graph1: Graph<NodeData, ()> = Graph::new();
let [node0, node1, node2, node3, node4] = graph1.add_nodes(&[
NodeData::Int64(0),
NodeData::Int64(1),
NodeData::Int64(2),
NodeData::Int64(3),
NodeData::Int64(4),
])[..] else { panic!("Failed to add nodes") };
let [node0, node1, node2, node3, node4] = get_graph!(graph1, 5);


graph1.add_edges(&[
Expand All @@ -120,17 +192,11 @@ mod tests {
(node2, node0),
(node2, node4),
(node4, node2),
]);
]);


let mut graph2: Graph<NodeData, ()> = Graph::new();
let [node02, node12, node22, node32, node42] = graph2.add_nodes(&[
NodeData::Int64(0),
NodeData::Int64(1),
NodeData::Int64(2),
NodeData::Int64(3),
NodeData::Int64(4),
])[..] else { panic!("Failed to add nodes") };
let [node02, node12, node22, node32, node42] = get_graph!(graph2, 5);

graph2.add_edges(&[
(node02, node32),
Expand All @@ -148,7 +214,7 @@ mod tests {
let depth_first = graph1.iter_depth_first(node0);
for node in depth_first{
let node = graph1.node(node).unwrap();
println!("{:?}", node.data);
//println!("{:?}", node.data);
visited.push(node);
}

Expand All @@ -162,7 +228,7 @@ mod tests {
let mut visited = Vec::new();
for node in graph1.iter_depth_first(node0) {
let node = graph1.node(node).unwrap();
println!("{:?}", node.data);
//println!("{:?}", node.data);
visited.push(node);

if node.data == NodeData::Int64(4) {
Expand All @@ -178,7 +244,7 @@ mod tests {
let mut visited2 = Vec::new();
for node in graph2.iter_depth_first(node02) {
let node = graph2.node(node).unwrap();
println!("{:?}", node.data);
//println!("{:?}", node.data);
visited2.push(node);

if node.data == NodeData::Int64(4) {
Expand Down
1 change: 1 addition & 0 deletions src/algorithms/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod dfs;
//mod bfs;
pub use dfs::*;
Loading

0 comments on commit 4ba887b

Please sign in to comment.