From ece912872ea4282f53d36c9052a10d76b40753ed Mon Sep 17 00:00:00 2001 From: AustrianToast Date: Sat, 3 Jun 2023 10:12:59 +0200 Subject: [PATCH] code cleanup --- src/graph.rs | 108 ++++++++++++++++++++++---------------------- src/graph/matrix.rs | 53 ++++++++++++---------- src/lib.rs | 103 ++++++++++++++++++++++++------------------ src/main.rs | 21 +++++---- 4 files changed, 154 insertions(+), 131 deletions(-) diff --git a/src/graph.rs b/src/graph.rs index cb20e2f..ed1d58a 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -1,28 +1,27 @@ +use self::matrix::clone; + pub mod matrix; -pub fn calculate_distanz_matrix(adjazenz_matrix: &Vec>) -> Vec> { - let mut distanz_matrix: Vec> = vec![]; - let mut potenz_matrix = adjazenz_matrix.clone(); +pub fn calculate_distanz_matrix(adjazenz_matrix: &Vec>) -> Vec> { + let mut distanz_matrix: Vec> = vec![vec![]; adjazenz_matrix.len()]; + let mut potenz_matrix = clone(adjazenz_matrix); for k in 1..adjazenz_matrix.len() { - potenz_matrix = matrix::mult(&potenz_matrix, &adjazenz_matrix); + potenz_matrix = matrix::mult(&potenz_matrix, adjazenz_matrix); for i in 0..adjazenz_matrix.len() { - if k == 1 { - distanz_matrix.push(vec![]); - } for j in 0..adjazenz_matrix.len() { if k != 1 { - if potenz_matrix[i][j] != 0 && distanz_matrix[i][j] == usize::MAX { - distanz_matrix[i][j] = k; + if potenz_matrix[i][j] != 0 && distanz_matrix[i][j] == u8::MAX { + distanz_matrix[i][j] = u8::try_from(k).unwrap(); } continue; } if i == j { - distanz_matrix[i].push(0) + distanz_matrix[i].push(0); } else if adjazenz_matrix[i][j] == 1 { distanz_matrix[i].push(1); } else { - distanz_matrix[i].push(usize::MAX); + distanz_matrix[i].push(u8::MAX); } } } @@ -30,16 +29,13 @@ pub fn calculate_distanz_matrix(adjazenz_matrix: &Vec>) -> Vec>) -> Vec> { - let mut weg_matrix: Vec> = vec![]; - let mut potenz_matrix = adjazenz_matrix.clone(); +pub fn calculate_weg_matrix(adjazenz_matrix: &Vec>) -> Vec> { + let mut weg_matrix: Vec> = vec![vec![]; adjazenz_matrix.len()]; + let mut potenz_matrix = clone(adjazenz_matrix); for k in 1..adjazenz_matrix.len() { - potenz_matrix = matrix::mult(&potenz_matrix, &adjazenz_matrix); + potenz_matrix = matrix::mult(&potenz_matrix, adjazenz_matrix); for i in 0..adjazenz_matrix.len() { - if k == 1 { - weg_matrix.push(vec![]); - } for j in 0..adjazenz_matrix.len() { if k != 1 { if potenz_matrix[i][j] != 0 { @@ -47,9 +43,7 @@ pub fn calculate_weg_matrix(adjazenz_matrix: &Vec>) -> Vec } continue; } - if i == j { - weg_matrix[i].push(1) - } else if adjazenz_matrix[i][j] == 1 { + if i == j || adjazenz_matrix[i][j] == 1 { weg_matrix[i].push(1); } else { weg_matrix[i].push(0); @@ -60,15 +54,15 @@ pub fn calculate_weg_matrix(adjazenz_matrix: &Vec>) -> Vec weg_matrix } -pub fn calculate_exzentrizitaeten(distanz_matrix: Vec>) -> Vec { - let mut exzentrizitaeten: Vec = vec![]; - let mut exzentrizitaet: usize; +pub fn calculate_exzentrizitaeten(distanz_matrix: Vec>) -> Vec { + let mut exzentrizitaeten: Vec = vec![]; + let mut exzentrizitaet: u8; for vector in distanz_matrix { exzentrizitaet = 0; for value in vector { - if value == usize::MAX { + if value == u8::MAX { continue; } exzentrizitaet = exzentrizitaet.max(value); @@ -78,42 +72,40 @@ pub fn calculate_exzentrizitaeten(distanz_matrix: Vec>) -> Vec exzentrizitaeten } -pub fn calculate_properties(exzentrizitaeten: &Vec) -> (usize, usize, Vec, bool) { - let mut radius: usize = usize::MAX; - let mut diameter: usize = 0; - let mut centre: Vec = vec![]; +pub fn calculate_properties(exzentrizitaeten: &[u8]) -> (u8, u8, Vec, bool) { + let mut radius = u8::MAX; + let mut diameter: u8 = 0; + let mut centre: Vec = vec![]; let mut connected: bool = true; - for index in 0..exzentrizitaeten.len() { - let value = exzentrizitaeten[index]; - - if value > diameter { - diameter = value; + for (index, value) in exzentrizitaeten.iter().enumerate() { + if value > &diameter { + diameter = *value; } - if value == radius { - centre.push(index + 1); + if value == &radius { + centre.push(u8::try_from(index + 1).unwrap()); } - if value < radius { - radius = value; + if value < &radius { + radius = *value; centre.clear(); - centre.push(index + 1); + centre.push(u8::try_from(index + 1).unwrap()); } - if value == 0 { + if value == &0 { connected = false; } } (radius, diameter, centre, connected) } -pub fn find_components(weg_matrix: Vec>) -> Vec> { - let mut components: Vec> = vec![]; - let mut component: Vec; +pub fn find_components(weg_matrix: Vec>) -> Vec> { + let mut components: Vec> = vec![]; + let mut component: Vec; for array in weg_matrix { component = vec![]; - for index in 0..array.len() { - if array[index] == 1 { - component.push(index + 1); + for (index, value) in array.iter().enumerate() { + if value == &1 { + component.push(u8::try_from(index + 1).unwrap()); } } if !components.contains(&component) { @@ -123,10 +115,13 @@ pub fn find_components(weg_matrix: Vec>) -> Vec> { components } -pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec>, components: &Vec>) -> (Vec,Vec>) { - let mut bridges: Vec> = vec![]; - let mut articulations: Vec = vec![]; - let mut temp_matrix: Vec> = adjazenz_matrix.clone(); +pub fn find_articulations_and_bridges( + adjazenz_matrix: &mut Vec>, + components: &Vec>, +) -> (Vec, Vec>) { + let mut bridges: Vec> = vec![]; + let mut articulations: Vec = vec![]; + let mut temp_matrix = adjazenz_matrix.clone(); for n in 0..temp_matrix.len() { for i in 0..temp_matrix.len() { @@ -137,12 +132,17 @@ pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec>, com if n != 0 { continue; } - let bridge = vec![usize::min(i + 1, j + 1), usize::max(i + 1, j + 1)]; + let bridge = vec![ + u8::try_from(usize::min(i + 1, j + 1)).unwrap(), + u8::try_from(usize::max(i + 1, j + 1)).unwrap(), + ]; let prev_value = adjazenz_matrix[i][j]; adjazenz_matrix[i][j] = 0; adjazenz_matrix[j][i] = 0; - - if find_components(calculate_weg_matrix(&adjazenz_matrix)).len() > components.len() && !bridges.contains(&bridge) { + + if find_components(calculate_weg_matrix(adjazenz_matrix)).len() > components.len() + && !bridges.contains(&bridge) + { bridges.push(bridge); } adjazenz_matrix[i][j] = prev_value; @@ -151,7 +151,7 @@ pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec>, com } if find_components(calculate_weg_matrix(&temp_matrix)).len() > (components.len() + 1) { - articulations.push(n + 1); + articulations.push(u8::try_from(n + 1).unwrap()); } temp_matrix = adjazenz_matrix.clone(); } diff --git a/src/graph/matrix.rs b/src/graph/matrix.rs index 2d27552..0f8dd9a 100644 --- a/src/graph/matrix.rs +++ b/src/graph/matrix.rs @@ -1,7 +1,7 @@ use csv::ReaderBuilder; -pub fn fill_with_random(size: usize) -> Vec> { - let mut matrix: Vec> = vec![]; +pub fn fill_with_random(size: usize) -> Vec> { + let mut matrix: Vec> = vec![]; for i in 0..size { matrix.push(vec![]); for j in 0..size { @@ -9,36 +9,44 @@ pub fn fill_with_random(size: usize) -> Vec> { matrix[i].push(0); continue; } - matrix[i].push(fastrand::usize(0..2)); + matrix[i].push(fastrand::u8(0..2)); } } matrix } -pub fn mult(matrix1: &Vec>, matrix2: &Vec>) -> Vec> { - let mut product: Vec> = vec![]; +pub fn mult(matrix1: &Vec>, matrix2: &Vec>) -> Vec> { + let mut product: Vec> = vec![vec![]; matrix2.len()]; let mut vector: Vec; - let mut index = 0; - - for k in 0..matrix1.len() { + + for (index, k) in (0..matrix1.len()).enumerate() { vector = vec![]; for array in matrix2 { - vector.push(array[index]); + vector.push(array[index].into()); } - index += 1; - product.push(vec![]); for array in matrix1 { - let sum: usize = array.iter() - .zip(vector.iter()) - .map(|(x, y)| x * y) - .sum(); + let sum = array.iter().zip(vector.iter()).map(|(x, y)| x * y).sum(); + //println!("{sum}"); product[k].push(sum); } } product } -pub fn show(matrix: &Vec>) { +pub fn clone(matrix: &Vec>) -> Vec> { + let mut new_matrix: Vec> = vec![]; + + for i in 0..matrix.len() { + new_matrix.push(vec![]); + for j in 0..matrix.len() { + new_matrix[i].push(matrix[i][j].into()); + } + } + + new_matrix +} + +pub fn show(matrix: &Vec>) { for vector in matrix { for value in vector { print!("{value} "); @@ -47,24 +55,21 @@ pub fn show(matrix: &Vec>) { } } -pub fn read_csv(file_name: &str) -> Vec> { - let mut matrix: Vec> = vec![]; - let dir: String = String::from("/home/rene/projects/Java/graphprogram/csv/"); - let file_path = dir + &file_name; +pub fn read_csv(file_name: &str) -> Vec> { + let mut matrix: Vec> = vec![]; + let dir: String = "/home/rene/projects/Java/graphprogram/csv/".into(); let mut csv = ReaderBuilder::new() .has_headers(false) .delimiter(b';') - .from_path(file_path) + .from_path(dir + file_name) .unwrap(); - let mut index = 0; - for result in csv.records() { + for (index, result) in csv.records().enumerate() { let record = result.unwrap(); matrix.push(vec![]); for field in record.iter() { matrix[index].push(field.parse().unwrap()); } - index += 1; } matrix } diff --git a/src/lib.rs b/src/lib.rs index 2347ab2..57c77a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,39 +2,48 @@ pub mod graph; #[cfg(test)] mod tests { - use crate::graph::{*, matrix::*}; + use crate::graph::{matrix::*, *}; #[test] fn graph() { - let mut adjazenz_matrix: Vec> = read_csv("art-brck.csv"); - let distanz_matrix: Vec> = calculate_distanz_matrix(&adjazenz_matrix); - let weg_matrix: Vec> = calculate_weg_matrix(&adjazenz_matrix); + let mut adjazenz_matrix = read_csv("art-brck.csv"); + let distanz_matrix = calculate_distanz_matrix(&adjazenz_matrix); + let weg_matrix = calculate_weg_matrix(&adjazenz_matrix); - assert_eq!(adjazenz_matrix, 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] - ]); - assert_eq!(distanz_matrix, 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] - ]); - assert_eq!(weg_matrix, 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, + 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] + ] + ); + assert_eq!( + distanz_matrix, + 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] + ] + ); + assert_eq!( + weg_matrix, + 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] + ] + ); - let exzentrizitaeten: Vec = calculate_exzentrizitaeten(distanz_matrix); + let exzentrizitaeten = calculate_exzentrizitaeten(distanz_matrix); let properties = calculate_properties(&exzentrizitaeten); - let components: Vec> = find_components(weg_matrix); + let components = find_components(weg_matrix); let result = find_articulations_and_bridges(&mut adjazenz_matrix, &components); assert_eq!(exzentrizitaeten, vec![2, 2, 2, 1, 2]); @@ -49,21 +58,27 @@ mod tests { #[test] fn matrix() { - let adjazenz_matrix: Vec> = read_csv("art-brck.csv"); - - assert_eq!(adjazenz_matrix, 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] - ]); - assert_eq!(mult(&adjazenz_matrix, &adjazenz_matrix), 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] - ]); + let adjazenz_matrix = read_csv("art-brck.csv"); + + assert_eq!( + adjazenz_matrix, + 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] + ] + ); + assert_eq!( + mult(&clone(&adjazenz_matrix), &adjazenz_matrix), + 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] + ] + ); } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index baf3d07..679fe2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ -use crate::graph::{*, matrix::*}; +use crate::graph::{*, matrix::{read_csv, show}}; pub mod graph; pub fn main() { let file_name = "24n.csv"; - let mut adjazenz_matrix: Vec> = read_csv(file_name); - //let mut adjazenz_matrix: Vec> = fill_with_random(45); // with this many verteces, it runs in about 10.2 seconds (2023-06-02 14:39) - let distanz_matrix: Vec> = calculate_distanz_matrix(&adjazenz_matrix); - let weg_matrix: Vec> = calculate_weg_matrix(&adjazenz_matrix); + let mut adjazenz_matrix = read_csv(file_name); + //let mut adjazenz_matrix = fill_with_random(45); // with this many verteces, it runs in about 10.2 seconds (2023-06-02 14:39) + let distanz_matrix = calculate_distanz_matrix(&adjazenz_matrix); + let weg_matrix = calculate_weg_matrix(&adjazenz_matrix); println!("adjazen matrix:"); show(&adjazenz_matrix); @@ -20,15 +20,18 @@ pub fn main() { let properties = calculate_properties(&exzentrizitaeten); if properties.3 { - println!("\nexzentrizitäten: {:?}", exzentrizitaeten); - println!("radius: {}\ndiameter: {}\ncentre: {:?}", properties.0, properties.1, properties.2); + println!("\nexzentrizitäten: {exzentrizitaeten:?}"); + println!( + "radius: {}\ndiameter: {}\ncentre: {:?}", + properties.0, properties.1, properties.2 + ); } else { println!("\nexzentrizitäten: not connected"); println!("radius/diameter/centre: not connected"); } - let components: Vec> = find_components(weg_matrix); - println!("components: {:?}", components); + let components = find_components(weg_matrix); + println!("components: {components:?}"); let result = find_articulations_and_bridges(&mut adjazenz_matrix, &components); println!("bridges: {:?}", result.1);