Skip to content

Commit

Permalink
fix(*): rename add to set
Browse files Browse the repository at this point in the history
  • Loading branch information
slavik-pastushenko committed Nov 4, 2024
1 parent 962cb44 commit 0a30d7f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 73 deletions.
6 changes: 3 additions & 3 deletions examples/src/bellman_ford.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ 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.set_edge(0, vec![(1, 4), (2, 3)]);
algorithm.set_edge(1, vec![(2, 1), (3, 2)]);
algorithm.set_edge(2, vec![(3, 5)]);

algorithm.run(Some(0)).unwrap_or_default()
}
Expand Down
12 changes: 6 additions & 6 deletions examples/src/floyd_warshall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use graph_algorithms::{FloydWarshallAlgorithm, GraphAlgorithm};

pub fn run() -> Vec<Vec<i32>> {
let mut algorithm = FloydWarshallAlgorithm::new();
algorithm.add_edge(0, 1, 1);
algorithm.add_edge(0, 2, 2);
algorithm.add_edge(1, 2, 1);
algorithm.add_edge(1, 0, 3);
algorithm.add_edge(2, 0, 4);
algorithm.add_edge(2, 1, 5);
algorithm.set_edge(0, 1, 1);
algorithm.set_edge(0, 2, 2);
algorithm.set_edge(1, 2, 1);
algorithm.set_edge(1, 0, 3);
algorithm.set_edge(2, 0, 4);
algorithm.set_edge(2, 1, 5);

algorithm.run(None).unwrap_or_default()
}
Expand Down
64 changes: 32 additions & 32 deletions src/bellman_ford.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl BellmanFordAlgorithm {
///
/// - `source`: Source node.
/// - `edges`: Edges of the source node.
pub fn add_edge(&mut self, source: usize, edges: Vec<(usize, i32)>) {
pub fn set_edge(&mut self, source: usize, edges: Vec<(usize, i32)>) {
if edges.is_empty() {
self.total_vertices = self.total_vertices.max(source + 1);
return;
Expand All @@ -76,9 +76,9 @@ impl BellmanFordAlgorithm {
/// # Arguments
///
/// - `nodes`: Vector of tuples where each tuple contains a node and its associated edges.
pub fn add_edges(&mut self, nodes: Vec<(usize, Vec<(usize, i32)>)>) {
pub fn set_edges(&mut self, nodes: Vec<(usize, Vec<(usize, i32)>)>) {
for (source, edges) in nodes {
self.add_edge(source, edges);
self.set_edge(source, edges);
}
}
}
Expand Down Expand Up @@ -221,7 +221,7 @@ mod tests {
];

for (source, destination, weight) in edges {
algorithm.add_edge(source, vec![(destination, weight)]);
algorithm.set_edge(source, vec![(destination, weight)]);
}

let result = algorithm.run(Some(0)).unwrap();
Expand All @@ -240,38 +240,38 @@ mod tests {
#[test]
fn test_run_single_node_graph() {
let mut algorithm = BellmanFordAlgorithm::new();
algorithm.add_edge(0, vec![]);
algorithm.set_edge(0, vec![]);

assert_eq!(algorithm.run(Some(0)).unwrap(), vec![0]);
}

#[test]
fn test_run_simple_graph_no_negative_edges() {
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.set_edge(0, vec![(1, 4), (2, 3)]);
algorithm.set_edge(1, vec![(2, 1), (3, 2)]);
algorithm.set_edge(2, vec![(3, 5)]);

assert_eq!(algorithm.run(Some(0)).unwrap(), vec![0, 4, 3, 6]);
}

#[test]
fn test_run_graph_with_negative_edge() {
let mut algorithm = BellmanFordAlgorithm::new();
algorithm.add_edge(0, vec![(1, 4), (2, 3)]);
algorithm.add_edge(1, vec![(2, -2), (3, 2)]);
algorithm.add_edge(2, vec![(3, 3)]);
algorithm.set_edge(0, vec![(1, 4), (2, 3)]);
algorithm.set_edge(1, vec![(2, -2), (3, 2)]);
algorithm.set_edge(2, vec![(3, 3)]);

assert_eq!(algorithm.run(Some(0)).unwrap(), vec![0, 4, 2, 5]);
}

#[test]
fn test_run_graph_with_no_edges() {
let mut algorithm = BellmanFordAlgorithm::new();
algorithm.add_edge(0, vec![]);
algorithm.add_edge(1, vec![]);
algorithm.add_edge(2, vec![]);
algorithm.add_edge(3, vec![]);
algorithm.set_edge(0, vec![]);
algorithm.set_edge(1, vec![]);
algorithm.set_edge(2, vec![]);
algorithm.set_edge(3, vec![]);

assert_eq!(
algorithm.run(Some(0)).unwrap(),
Expand All @@ -282,18 +282,18 @@ mod tests {
#[test]
fn test_run_run_from_different_start_node() {
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.set_edge(0, vec![(1, 4), (2, 3)]);
algorithm.set_edge(1, vec![(2, 1), (3, 2)]);
algorithm.set_edge(2, vec![(3, 5)]);

assert_eq!(algorithm.run(Some(1)).unwrap(), vec![i32::MAX, 0, 1, 2]);
}

#[test]
fn test_run_disconnected_graph() {
let mut algorithm = BellmanFordAlgorithm::new();
algorithm.add_edge(0, vec![(1, 4)]);
algorithm.add_edge(2, vec![(3, 5)]);
algorithm.set_edge(0, vec![(1, 4)]);
algorithm.set_edge(2, vec![(3, 5)]);

assert_eq!(
algorithm.run(Some(0)).unwrap(),
Expand All @@ -304,7 +304,7 @@ mod tests {
#[test]
fn test_run_graph_with_negative_weight_cycle() {
let mut algorithm = BellmanFordAlgorithm::new();
algorithm.add_edges(vec![
algorithm.set_edges(vec![
(0, vec![(1, 1)]),
(1, vec![(2, -1)]),
(2, vec![(0, -1)]),
Expand All @@ -316,10 +316,10 @@ mod tests {
#[test]
fn test_run_early_exit_no_updates() {
let mut algorithm = BellmanFordAlgorithm::new();
algorithm.add_edge(0, vec![(1, 2)]);
algorithm.add_edge(1, vec![(2, 3)]);
algorithm.add_edge(2, vec![(3, 4)]);
algorithm.add_edge(3, vec![(4, 1)]);
algorithm.set_edge(0, vec![(1, 2)]);
algorithm.set_edge(1, vec![(2, 3)]);
algorithm.set_edge(2, vec![(3, 4)]);
algorithm.set_edge(3, vec![(4, 1)]);

let result = algorithm.run(Some(0)).unwrap();

Expand All @@ -333,10 +333,10 @@ mod tests {
#[test]
fn test_run_early_exit_with_no_negative_cycle() {
let mut algorithm = BellmanFordAlgorithm::new();
algorithm.add_edge(0, vec![(1, 2)]);
algorithm.add_edge(1, vec![(2, 3)]);
algorithm.add_edge(2, vec![(3, -5)]);
algorithm.add_edge(3, vec![(4, 1)]);
algorithm.set_edge(0, vec![(1, 2)]);
algorithm.set_edge(1, vec![(2, 3)]);
algorithm.set_edge(2, vec![(3, -5)]);
algorithm.set_edge(3, vec![(4, 1)]);

let result = algorithm.run(Some(0)).unwrap();

Expand All @@ -350,9 +350,9 @@ mod tests {
#[test]
fn test_run_early_exit_with_negative_cycle() {
let mut algorithm = BellmanFordAlgorithm::new();
algorithm.add_edge(0, vec![(1, 1)]);
algorithm.add_edge(1, vec![(2, -2)]);
algorithm.add_edge(2, vec![(0, -1)]); // Negative cycle here
algorithm.set_edge(0, vec![(1, 1)]);
algorithm.set_edge(1, vec![(2, -2)]);
algorithm.set_edge(2, vec![(0, -1)]); // Negative cycle here

assert_eq!(algorithm.run(Some(0)), Err(GraphError::NegativeWeightCycle));
}
Expand Down
64 changes: 32 additions & 32 deletions src/floyd_warshall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ impl FloydWarshallAlgorithm {
}
}

/// Add a single edge to the graph.
/// Set a single edge to the graph.
///
/// # Arguments
///
/// - `source`: Source node.
/// - `target`: Target node.
/// - `weight`: Weight of the edge.
pub fn add_edge(&mut self, source: usize, target: usize, weight: i32) {
pub fn set_edge(&mut self, source: usize, target: usize, weight: i32) {
self.edges.push((source, target, weight));
self.total_nodes = self.total_nodes.max(source + 1).max(target + 1);
}
Expand All @@ -52,10 +52,10 @@ impl FloydWarshallAlgorithm {
/// # Arguments
///
/// - `nodes`: Vector of tuples where each tuple contains a node and its associated edges.
pub fn add_edges(&mut self, nodes: Vec<(usize, Vec<(usize, i32)>)>) {
pub fn set_edges(&mut self, nodes: Vec<(usize, Vec<(usize, i32)>)>) {
for (source, edges) in nodes {
for (target, weight) in edges {
self.add_edge(source, target, weight);
self.set_edge(source, target, weight);
}
}
}
Expand Down Expand Up @@ -128,20 +128,20 @@ mod tests {
fn test_run() {
let mut algorithm = FloydWarshallAlgorithm::new();

algorithm.add_edge(0, 1, 4);
algorithm.add_edge(1, 2, 1);
algorithm.add_edge(0, 2, 7);
algorithm.add_edge(2, 3, 3);
algorithm.add_edge(3, 4, 2);
algorithm.add_edge(4, 5, 1);
algorithm.add_edge(5, 6, 6);
algorithm.add_edge(0, 6, 15);
algorithm.add_edge(1, 4, 8);
algorithm.add_edge(2, 5, 12);
algorithm.add_edge(3, 6, 7);
algorithm.add_edge(4, 2, 5);
algorithm.add_edge(5, 0, 10);
algorithm.add_edge(6, 1, 11);
algorithm.set_edge(0, 1, 4);
algorithm.set_edge(1, 2, 1);
algorithm.set_edge(0, 2, 7);
algorithm.set_edge(2, 3, 3);
algorithm.set_edge(3, 4, 2);
algorithm.set_edge(4, 5, 1);
algorithm.set_edge(5, 6, 6);
algorithm.set_edge(0, 6, 15);
algorithm.set_edge(1, 4, 8);
algorithm.set_edge(2, 5, 12);
algorithm.set_edge(3, 6, 7);
algorithm.set_edge(4, 2, 5);
algorithm.set_edge(5, 0, 10);
algorithm.set_edge(6, 1, 11);

let result = algorithm.run(None).unwrap();

Expand All @@ -162,7 +162,7 @@ mod tests {
#[test]
fn test_run_single_node() {
let mut algorithm = FloydWarshallAlgorithm::new();
algorithm.add_edge(0, 0, 0);
algorithm.set_edge(0, 0, 0);

let result = algorithm.run(None).unwrap();

Expand All @@ -172,7 +172,7 @@ mod tests {
#[test]
fn test_run_two_nodes_one_edge() {
let mut algorithm = FloydWarshallAlgorithm::new();
algorithm.add_edge(0, 1, 5);
algorithm.set_edge(0, 1, 5);

let result = algorithm.run(None).unwrap();

Expand All @@ -183,7 +183,7 @@ mod tests {
#[test]
fn test_run_negative_weights_without_cycle() {
let mut algorithm = FloydWarshallAlgorithm::new();
algorithm.add_edges(vec![
algorithm.set_edges(vec![
(0, vec![(1, -2), (2, 4)]),
(1, vec![(2, 1)]),
(0, vec![(2, 4)]),
Expand All @@ -198,12 +198,12 @@ mod tests {
#[test]
fn test_run_complete_graph() {
let mut algorithm = FloydWarshallAlgorithm::new();
algorithm.add_edge(0, 1, 1);
algorithm.add_edge(0, 2, 2);
algorithm.add_edge(1, 2, 1);
algorithm.add_edge(1, 0, 3);
algorithm.add_edge(2, 0, 4);
algorithm.add_edge(2, 1, 5);
algorithm.set_edge(0, 1, 1);
algorithm.set_edge(0, 2, 2);
algorithm.set_edge(1, 2, 1);
algorithm.set_edge(1, 0, 3);
algorithm.set_edge(2, 0, 4);
algorithm.set_edge(2, 1, 5);

let result = algorithm.run(None).unwrap();

Expand All @@ -215,8 +215,8 @@ mod tests {
#[test]
fn test_run_disconnected_nodes() {
let mut algorithm = FloydWarshallAlgorithm::new();
algorithm.add_edge(0, 1, 3);
algorithm.add_edge(1, 2, 4);
algorithm.set_edge(0, 1, 3);
algorithm.set_edge(1, 2, 4);
algorithm.set_total_nodes(4);

let result = algorithm.run(None).unwrap();
Expand All @@ -228,9 +228,9 @@ mod tests {
#[test]
fn test_run_zero_weight_cycle() {
let mut algorithm = FloydWarshallAlgorithm::new();
algorithm.add_edge(0, 1, 2);
algorithm.add_edge(1, 2, -3);
algorithm.add_edge(2, 0, 1);
algorithm.set_edge(0, 1, 2);
algorithm.set_edge(1, 2, -3);
algorithm.set_edge(2, 0, 1);

let result = algorithm.run(None).unwrap();

Expand Down

0 comments on commit 0a30d7f

Please sign in to comment.