Skip to content

Commit

Permalink
Add: undirected graph tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FilippoFantinato committed Sep 15, 2024
1 parent 022ea8e commit 171cbb4
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 6 deletions.
Binary file added default_11408286742691170672_0_15681.profraw
Binary file not shown.
11 changes: 5 additions & 6 deletions src/cli/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ enum Algorithm {
KruskalNaive,
}

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
Expand All @@ -31,22 +30,22 @@ pub fn run_cli() {
match args.algorithm {
Algorithm::IsAcyclic => {
let g = read_graph(args.file);
let res = cycles::is_acyclic::run(&g);
let res = cycles::is_acyclic::run(g.deref());

println!("Is acylic result: {:}", res);
}
Algorithm::KruskalNaive => {
let g = read_graph(args.file);
let path = minimum_spanning_tree::kruskal_naive::run(&g);
let weight: i128 = path.iter().map(|e| (*e).2).sum();
let path = minimum_spanning_tree::kruskal_naive::run(g.deref());
let weight: i128 = path.iter().map(|e| e.2).sum();

println!("Kruskal naive path: {:?}", path);
println!("Kruskal naive weight: {:?}", weight);
}
}
}

fn read_graph(path: PathBuf) -> impl Graph {
fn read_graph(path: PathBuf) -> Box<dyn Graph> {
let lines = fs::read_to_string(path).unwrap();
let mut lines = lines.lines();
let mut header = lines
Expand All @@ -57,7 +56,7 @@ fn read_graph(path: PathBuf) -> impl Graph {
.next()
.map(|v| v.parse::<usize>().unwrap())
.unwrap_or_else(|| panic!("Invalid format"));
let mut g = UndirectedGraph::new();
let mut g = Box::new(UndirectedGraph::new());

lines.for_each(|v| {
let mut line = v.split_whitespace();
Expand Down
152 changes: 152 additions & 0 deletions src/graph/undirected_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::graph::graph::Graph;

use super::graph::{Edge, Vertex, Weight};

#[derive(PartialEq, Eq)]
pub struct UndirectedGraph {
adj_matrix: HashMap<Vertex, HashMap<Vertex, Weight>>,
vertices: HashSet<Vertex>,
Expand Down Expand Up @@ -63,7 +64,158 @@ impl Graph for UndirectedGraph {
let e = if u <= v { (*u, *v, w) } else { (*v, *u, w) };
self.adj_matrix.get_mut(u).unwrap().remove(v);
self.adj_matrix.get_mut(v).unwrap().remove(u);

if self.adj_matrix.get(u).unwrap().is_empty() {
self.adj_matrix.remove(u);
self.vertices.remove(u);
}

if self.adj_matrix.get(v).unwrap().is_empty() {
self.adj_matrix.remove(v);
self.vertices.remove(v);
}

self.edges.remove(&e);
});
}
}

mod tests {
use std::collections::{HashMap, HashSet};
use std::vec;

use crate::graph::graph::{Edge, Graph, Vertex, Weight};

use super::UndirectedGraph;

#[test]
fn add_edge_success() {
let mut g = UndirectedGraph::new();

g.add_edge(0, 1, 2);
g.add_edge(1, 4, 3);

let expected_edges: HashSet<(Vertex, Vertex, Weight)> =
HashSet::from([(0, 1, 2), (1, 4, 3)]);
assert_eq!(g.edges, expected_edges);

let expected_vertices: HashSet<Vertex> = HashSet::from([0, 1, 4]);
assert_eq!(g.vertices, expected_vertices);

let expected_adj_matrix: HashMap<Vertex, HashMap<Vertex, Weight>> = HashMap::from([
(0, HashMap::from([(1, 2)])),
(1, HashMap::from([(0, 2), (4, 3)])),
(4, HashMap::from([(1, 3)])),
]);
assert_eq!(g.adj_matrix, expected_adj_matrix);
}

#[test]
fn get_size_success() {
let mut g = UndirectedGraph::new();

g.add_edge(0, 1, 2);
g.add_edge(1, 4, 3);

let current = 3;
let expected = g._get_size();
assert_eq!(expected, current);
}

#[test]
fn get_adj_list_existing_vertex_success() {
let mut g = UndirectedGraph::new();

g.add_edge(0, 1, 2);
g.add_edge(1, 4, 3);

let adj_list = HashMap::from([(0, 2), (4, 3)]);
let expected = Some(&adj_list);
let current = g._get_adj_list(&1);

assert_eq!(expected, current);
}

#[test]
fn get_adj_list_non_existent_vertex_success() {
let mut g = UndirectedGraph::new();

g.add_edge(0, 1, 2);
g.add_edge(1, 4, 3);

let expected = None;
let current = g._get_adj_list(&5);

assert_eq!(expected, current);
}

#[test]
fn get_vertices_success() {
let mut g = UndirectedGraph::new();

g.add_edge(0, 1, 2);
g.add_edge(1, 4, 3);

let vertices = HashSet::from([0, 1, 4]);
let expected: &HashSet<Vertex> = &vertices;
let current = g.get_vertices();
assert_eq!(expected, current);
}

#[test]
fn get_edges_success() {
let mut g = UndirectedGraph::new();

g.add_edge(0, 1, 2);
g.add_edge(1, 4, 3);

let edges: HashSet<Edge> = HashSet::from([(0, 1, 2), (1, 4, 3)]);
let expected: &HashSet<Edge> = &edges;
let current = g.get_edges();
assert_eq!(expected, current);
}

#[test]
fn get_weight_existing_edge_success() {
let mut g = UndirectedGraph::new();

g.add_edge(0, 1, 2);
g.add_edge(1, 4, 3);

let expected = Some(&3);
let current = g.get_weight(&1, &4);
assert_eq!(expected, current);
}

#[test]
fn get_weight_non_existent_edge_success() {
let mut g = UndirectedGraph::new();

g.add_edge(0, 1, 2);
g.add_edge(1, 4, 3);

let expected = None;
let current = g.get_weight(&1, &5);
assert_eq!(expected, current);
}

#[test]
fn delete_edge_existing_edge_success() {
let mut g = UndirectedGraph::new();

g.add_edge(0, 1, 2);
g.add_edge(1, 4, 3);

g.delete_edge(&0, &1);

let expected_edges: HashSet<(Vertex, Vertex, Weight)> = HashSet::from([(1, 4, 3)]);
assert_eq!(g.edges, expected_edges);

let expected_vertices: HashSet<Vertex> = HashSet::from([1, 4]);
assert_eq!(g.vertices, expected_vertices);

let expected_adj_matrix: HashMap<Vertex, HashMap<Vertex, Weight>> =
HashMap::from([(1, HashMap::from([(4, 3)])), (4, HashMap::from([(1, 3)]))]);
assert_eq!(g.adj_matrix, expected_adj_matrix);
}
}

0 comments on commit 171cbb4

Please sign in to comment.