Skip to content

Commit

Permalink
feat(algorithm): implement bellman-ford
Browse files Browse the repository at this point in the history
  • Loading branch information
slavik-pastushenko committed Oct 29, 2024
1 parent 91f14ad commit 7c360fa
Show file tree
Hide file tree
Showing 7 changed files with 474 additions and 64 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ BFS explores the graph level by level, starting from a given node. It is used fo
### Depth-First Search (DFS) (TODO)
DFS explores as far as possible along each branch before backtracking. It is used for pathfinding and topological sorting.

### Bellman-Ford Algorithm (TODO)
### Bellman-Ford Algorithm
The Bellman-Ford algorithm computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph. It can handle graphs with negative weight edges.

### Floyd-Warshall Algorithm (TODO)
Expand All @@ -43,7 +43,7 @@ This crate uses `#![forbid(unsafe_code)]` to ensure everything is implemented in
The `examples` directory contains example implementations of various graph algorithms:

```rust
use graph_algorithms::{Dijkstra, GraphAlgorithm};
use graph_algorithms::{DijkstraAlgorithm, GraphAlgorithm};
```

## Contributing
Expand Down
20 changes: 20 additions & 0 deletions examples/src/bellman_ford.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use graph_algorithms::{BellmanFordAlgorithm, GraphAlgorithm};

pub fn run() -> Vec<i32> {
let mut algorithm = BellmanFordAlgorithm::new();
algorithm.add_edge(0, vec![(1, 4), (2, 3)]);
algorithm.add_edge(1, vec![(2, 1), (3, 2)]);
algorithm.add_edge(2, vec![(3, 5)]);

algorithm.run(0).unwrap_or_default()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_algorithm_run() {
assert_eq!(run(), vec![0, 4, 3, 6]);
}
}
10 changes: 5 additions & 5 deletions examples/src/dijkstra.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use graph_algorithms::{Dijkstra, GraphAlgorithm};
use graph_algorithms::{DijkstraAlgorithm, GraphAlgorithm};

pub fn run() -> Vec<usize> {
let mut dijkstra = Dijkstra::new();
dijkstra.set_nodes(vec![
let mut algorithm = DijkstraAlgorithm::new();
algorithm.set_nodes(vec![
(0, vec![(1, 1), (2, 4)]),
(1, vec![(2, 2)]),
(2, vec![]),
]);

dijkstra.run(0)
algorithm.run(0).unwrap_or_default()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_dijkstra() {
fn test_algorithm_run() {
assert_eq!(run(), vec![0, 1, 3]);
}
}
5 changes: 4 additions & 1 deletion examples/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
mod bellman_ford;
mod dijkstra;

fn main() {
// Run the Dijkstra example
dijkstra::run();

// Run the Bellman-Ford example
bellman_ford::run();
}

#[cfg(test)]
Expand All @@ -11,7 +15,6 @@ mod tests {

#[test]
fn test_main() {
// Just test that the main function runs without panicking
main();
}
}
Loading

0 comments on commit 7c360fa

Please sign in to comment.