diff --git a/src/graph.rs b/src/graph.rs index ccf91f3..4aca8d7 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -1,44 +1,6 @@ pub mod matrix; -pub fn test() { - let mut adjazenz_matrix: Vec> = matrix::read_csv(); - let distanz_matrix: Vec> = calculate_distanz_matrix(&adjazenz_matrix); - let weg_matrix: Vec> = calculate_weg_matrix(&adjazenz_matrix); - - println!("adjazen matrix:"); - matrix::show(&adjazenz_matrix); - println!("\ndistanz matrix:"); - matrix::show(&distanz_matrix); - println!("\nweg matrix:"); - matrix::show(&weg_matrix); - - let exzentrizitaeten = calculate_exzentrizitaeten(&distanz_matrix); - let mut connected: bool = true; - - if exzentrizitaeten.contains(&0) { - connected = false; - } - println!("\nis the graph connected: {connected}"); - println!("exzentrizitäten: {:?}", exzentrizitaeten); - - let properties = calculate_properties(&exzentrizitaeten); - let radius: usize = properties.0; - let diameter: usize = properties.1; - let centre: Vec = properties.2; - - println!("radius: {radius}\ndiameter: {diameter}\ncentre: {:?}", centre); - - let components = find_components(&weg_matrix); - println!("components: {:?}", components); - - let bridges = find_bridges(&mut adjazenz_matrix, &components); - println!("bridges: {:?}", bridges); - - let articulations = find_articulations(&adjazenz_matrix, &components); - println!("articulations: {:?}", articulations); -} - -fn calculate_distanz_matrix(adjazenz_matrix: &Vec>) -> Vec> { +pub fn calculate_distanz_matrix(adjazenz_matrix: &Vec>) -> Vec> { let mut distanz_matrix: Vec> = vec![]; let mut potenz_matrix = adjazenz_matrix.clone(); @@ -68,7 +30,7 @@ fn calculate_distanz_matrix(adjazenz_matrix: &Vec>) -> Vec distanz_matrix } -fn calculate_weg_matrix(adjazenz_matrix: &Vec>) -> Vec> { +pub fn calculate_weg_matrix(adjazenz_matrix: &Vec>) -> Vec> { let mut weg_matrix: Vec> = vec![]; let mut potenz_matrix = adjazenz_matrix.clone(); @@ -98,7 +60,7 @@ fn calculate_weg_matrix(adjazenz_matrix: &Vec>) -> Vec> { weg_matrix } -fn calculate_exzentrizitaeten(distanz_matrix: &Vec>) -> Vec { +pub fn calculate_exzentrizitaeten(distanz_matrix: &Vec>) -> Vec { let mut exzentrizitaeten: Vec = vec![]; let mut exzentrizitaet: usize; @@ -115,7 +77,7 @@ fn calculate_exzentrizitaeten(distanz_matrix: &Vec>) -> Vec { exzentrizitaeten } -fn calculate_properties(exzentrizitaeten: &Vec) -> (usize, usize, Vec) { +pub fn calculate_properties(exzentrizitaeten: &Vec) -> (usize, usize, Vec) { let mut radius: usize = usize::MAX; let mut diameter: usize = 0; let mut centre: Vec = vec![]; @@ -138,7 +100,7 @@ fn calculate_properties(exzentrizitaeten: &Vec) -> (usize, usize, Vec>) -> Vec> { +pub fn find_components(weg_matrix: &Vec>) -> Vec> { let mut components: Vec> = vec![]; let mut component: Vec; @@ -157,7 +119,7 @@ fn find_components(weg_matrix: &Vec>) -> Vec> { components } -fn find_bridges(adjazenz_matrix: &mut Vec>, components: &Vec>) -> Vec> { +pub fn find_bridges(adjazenz_matrix: &mut Vec>, components: &Vec>) -> Vec> { let mut bridges: Vec> = vec![]; let mut bridge: Vec; let mut new_components: Vec>; @@ -190,7 +152,7 @@ fn find_bridges(adjazenz_matrix: &mut Vec>, components: &Vec>, components: &Vec>) -> Vec { +pub fn find_articulations(adjazenz_matrix: &Vec>, components: &Vec>) -> Vec { let mut articulations: Vec = vec![]; let mut new_components: Vec>; let mut temp_matrix: Vec> = adjazenz_matrix.clone(); diff --git a/src/graph/matrix.rs b/src/graph/matrix.rs index 732b82c..004a748 100644 --- a/src/graph/matrix.rs +++ b/src/graph/matrix.rs @@ -1,3 +1,4 @@ +#[allow(unused_imports)] use std::{fs::File, io::Read}; pub fn fill_with_random(matrix: &mut Vec>, size: usize) { @@ -48,7 +49,7 @@ pub fn read_csv() -> Vec> { vec![0, 0, 0, 1, 0] ]; /* - let mut csv = File::open("/home/rene/projects/Java/graphprogram/csv/art-brck.csv").unwrap(); + let mut csv = File::open("").unwrap(); let mut content = String::new(); csv.read_to_string(&mut content).unwrap(); println!("{content}"); @@ -61,29 +62,3 @@ pub fn read_csv() -> Vec> { */ matrix } - -pub fn test() { - let matrix1: Vec> = vec![ - vec![0, 0, 1, 1, 0], - vec![0, 0, 1, 1, 0], - vec![1, 1, 0, 1, 0], - vec![1, 1, 1, 0, 1], - vec![0, 0, 0, 1, 0] - ]; - let matrix2: Vec>; - - matrix2 = matrix1.clone(); - - println!("A:"); - show(&matrix1); - - let mut product: Vec> = mult(&matrix1, &matrix2); - - println!("\nA²:"); - show(&product); - - product = mult(&product, &matrix1); - - println!("\nA³:"); - show(&product); -} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..118ba2c --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,101 @@ +pub mod graph; + +#[cfg(test)] +mod tests { + use crate::graph::{self, matrix::*}; + + #[test] + fn graph() { + let mut adjazenz_matrix: Vec> = graph::matrix::read_csv(); + let distanz_matrix: Vec> = graph::calculate_distanz_matrix(&adjazenz_matrix); + let weg_matrix: Vec> = graph::calculate_weg_matrix(&adjazenz_matrix); + let expected_adjazenz_matrix: Vec> = vec![ + vec![0, 0, 1, 1, 0], + vec![0, 0, 1, 1, 0], + vec![1, 1, 0, 1, 0], + vec![1, 1, 1, 0, 1], + vec![0, 0, 0, 1, 0] + ]; + let expected_distanz_matrix: Vec> = vec![ + vec![0, 2, 1, 1, 2], + vec![2, 0, 1, 1, 2], + vec![1, 1, 0, 1, 2], + vec![1, 1, 1, 0, 1], + vec![2, 2, 2, 1, 0] + ]; + let expected_weg_matrix: Vec> = vec![ + vec![1, 1, 1, 1, 1], + vec![1, 1, 1, 1, 1], + vec![1, 1, 1, 1, 1], + vec![1, 1, 1, 1, 1], + vec![1, 1, 1, 1, 1] + ]; + + assert_eq!(adjazenz_matrix, expected_adjazenz_matrix); + assert_eq!(distanz_matrix, expected_distanz_matrix); + assert_eq!(weg_matrix, expected_weg_matrix); + + let exzentrizitaeten = graph::calculate_exzentrizitaeten(&distanz_matrix); + let mut connected: bool = true; + + if exzentrizitaeten.contains(&0) { + connected = false; + } + assert_eq!(exzentrizitaeten, vec![2, 2, 2, 1, 2]); + assert_eq!(connected, true); + + let properties = graph::calculate_properties(&exzentrizitaeten); + let radius: usize = properties.0; + let diameter: usize = properties.1; + let centre: Vec = properties.2; + + assert_eq!(radius, 1); + assert_eq!(diameter, 2); + assert_eq!(centre, vec![4]); + + let components = graph::find_components(&weg_matrix); + let bridges = graph::find_bridges(&mut adjazenz_matrix, &components); + let articulations = graph::find_articulations(&adjazenz_matrix, &components); + + assert_eq!(components, vec![vec![1, 2, 3, 4, 5]]); + assert_eq!(bridges, vec![vec![4, 5]]); + assert_eq!(articulations, vec![4]); + } + + #[test] + fn matrix() { + let matrix1 = read_csv(); + let expected_matrix: Vec> = vec![ + vec![0, 0, 1, 1, 0], + vec![0, 0, 1, 1, 0], + vec![1, 1, 0, 1, 0], + vec![1, 1, 1, 0, 1], + vec![0, 0, 0, 1, 0] + ]; + let matrix2: Vec> = matrix1.clone(); + + assert_eq!(matrix1, expected_matrix); + + let mut product: Vec> = mult(&matrix1, &matrix2); + let mut expected_product: Vec> = vec![ + vec![2, 2, 1, 1, 1], + vec![2, 2, 1, 1, 1], + vec![1, 1, 3, 2, 1], + vec![1, 1, 2, 4, 0], + vec![1, 1, 1, 0, 1] + ]; + + assert_eq!(product, expected_product); + + product = mult(&product, &matrix1); + expected_product = vec![ + vec![2, 2, 5, 6, 1], + vec![2, 2, 5, 6, 1], + vec![5, 5, 4, 6, 2], + vec![6, 6, 6, 4, 4], + vec![1, 1, 2, 4, 0] + ]; + + assert_eq!(product, expected_product) + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6e39dea..1a62396 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,44 @@ pub mod graph; pub fn main() { - tests("graph") + output(); } -pub fn tests(arg: &str) { - match arg { - "graph" => graph::test(), - "matrix" => graph::matrix::test(), - &_ => println!("{arg} is not a valid option"), +// This is just for the pupose of visualising the output +fn output() { + let mut adjazenz_matrix: Vec> = graph::matrix::read_csv(); + let distanz_matrix: Vec> = graph::calculate_distanz_matrix(&adjazenz_matrix); + let weg_matrix: Vec> = graph::calculate_weg_matrix(&adjazenz_matrix); + + println!("adjazen matrix:"); + graph::matrix::show(&adjazenz_matrix); + println!("\ndistanz matrix:"); + graph::matrix::show(&distanz_matrix); + println!("\nweg matrix:"); + graph::matrix::show(&weg_matrix); + + let exzentrizitaeten = graph::calculate_exzentrizitaeten(&distanz_matrix); + let mut connected: bool = true; + + if exzentrizitaeten.contains(&0) { + connected = false; } + println!("\nis the graph connected: {connected}"); + println!("exzentrizitäten: {:?}", exzentrizitaeten); + + let properties = graph::calculate_properties(&exzentrizitaeten); + let radius: usize = properties.0; + let diameter: usize = properties.1; + let centre: Vec = properties.2; + + println!("radius: {radius}\ndiameter: {diameter}\ncentre: {:?}", centre); + + let components = graph::find_components(&weg_matrix); + println!("components: {:?}", components); + + let bridges = graph::find_bridges(&mut adjazenz_matrix, &components); + println!("bridges: {:?}", bridges); + + let articulations = graph::find_articulations(&adjazenz_matrix, &components); + println!("articulations: {:?}", articulations); } \ No newline at end of file