diff --git a/Cargo.toml b/Cargo.toml index f88f4dc..cbaf3f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,14 @@ edition = "2021" [dependencies] csv = "1.2.2" fastrand = "1.9.0" + +[profile.dev] +opt-level = 3 +debug = false +overflow-checks = false +lto = "thin" +panic = "unwind" +incremental = true +debug-assertions = false +codegen-units = 64 +rpath = false \ No newline at end of file diff --git a/src/graph.rs b/src/graph.rs index 9cd81be..3d6ebd8 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -60,7 +60,7 @@ pub fn calculate_weg_matrix(adjazenz_matrix: &Vec>) -> Vec weg_matrix } -pub fn calculate_exzentrizitaeten(distanz_matrix: &Vec>) -> Vec { +pub fn calculate_exzentrizitaeten(distanz_matrix: Vec>) -> Vec { let mut exzentrizitaeten: Vec = vec![]; let mut exzentrizitaet: usize; @@ -105,16 +105,16 @@ pub fn calculate_properties(exzentrizitaeten: &Vec) -> (usize, usize, Vec (radius, diameter, centre, connected) } -pub fn find_components(weg_matrix: &Vec>) -> Vec> { +pub fn find_components(weg_matrix: Vec>) -> Vec> { let mut components: Vec> = vec![]; let mut component: Vec; let mut i: usize; - for array in weg_matrix.iter() { + for array in weg_matrix { component = vec![]; i = 1; - for value in array.iter() { - if value == &1 { + for value in array { + if value == 1 { component.push(i); } i += 1; @@ -133,7 +133,6 @@ pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec>, com let mut articulations: Vec = vec![]; let mut new_components: Vec>; let mut temp_matrix: Vec> = adjazenz_matrix.clone(); - let mut weg_matrix: Vec>; for n in 0..temp_matrix.len() { for i in 0..temp_matrix.len() { @@ -152,8 +151,7 @@ pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec>, com adjazenz_matrix[i][j] = 0; adjazenz_matrix[j][i] = 0; - weg_matrix = calculate_weg_matrix(&adjazenz_matrix); - new_components = find_components(&weg_matrix); + new_components = find_components(calculate_weg_matrix(&adjazenz_matrix)); if new_components.len() > components.len() && !bridges.contains(&bridge) { bridges.push(bridge); @@ -163,8 +161,7 @@ pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec>, com } } - weg_matrix = calculate_weg_matrix(&temp_matrix); - new_components = find_components(&weg_matrix); + new_components = find_components(calculate_weg_matrix(&temp_matrix)); if new_components.len() > (components.len() + 1) { articulations.push(n + 1); diff --git a/src/graph/matrix.rs b/src/graph/matrix.rs index 378b01b..ff31240 100644 --- a/src/graph/matrix.rs +++ b/src/graph/matrix.rs @@ -19,18 +19,22 @@ pub fn fill_with_random(size: usize) -> Vec> { pub fn mult(matrix1: &Vec>, matrix2: &Vec>) -> Vec> { let mut product: Vec> = vec![]; - let mut sum: u128; - - for i in 0..matrix1.len() { - for j in 0..matrix1.len() { - if i == 0 { - product.push(vec![]); - } - sum = 0; - for k in 0..matrix1.len() { - sum += (matrix1[i][k] * matrix2[k][j]) as u128; - } - product[i].push(sum as usize); + let mut vector: Vec; + let mut index = 0; + + for k in 0..matrix1.len() { + vector = vec![]; + for array in matrix2 { + vector.push(array[index]); + } + index += 1; + product.push(vec![]); + for array in matrix1 { + let sum: usize = array.iter() + .zip(vector.iter()) + .map(|(x, y)| x * y) + .sum(); + product[k].push(sum); } } product diff --git a/src/lib.rs b/src/lib.rs index c657ccb..2347ab2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,10 +9,6 @@ mod tests { 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 exzentrizitaeten: Vec = calculate_exzentrizitaeten(&distanz_matrix); - let properties = calculate_properties(&exzentrizitaeten); - let components: Vec> = find_components(&weg_matrix); - let result = find_articulations_and_bridges(&mut adjazenz_matrix, &components); assert_eq!(adjazenz_matrix, vec![ vec![0, 0, 1, 1, 0], @@ -35,6 +31,12 @@ mod tests { vec![1, 1, 1, 1, 1], vec![1, 1, 1, 1, 1] ]); + + let exzentrizitaeten: Vec = calculate_exzentrizitaeten(distanz_matrix); + let properties = calculate_properties(&exzentrizitaeten); + let components: Vec> = find_components(weg_matrix); + let result = find_articulations_and_bridges(&mut adjazenz_matrix, &components); + assert_eq!(exzentrizitaeten, vec![2, 2, 2, 1, 2]); assert_eq!(properties.0, 1); assert_eq!(properties.1, 2); diff --git a/src/main.rs b/src/main.rs index 401237a..baf3d07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ 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(100); + //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); @@ -16,7 +16,7 @@ pub fn main() { println!("\nweg matrix:"); show(&weg_matrix); - let exzentrizitaeten = calculate_exzentrizitaeten(&distanz_matrix); + let exzentrizitaeten = calculate_exzentrizitaeten(distanz_matrix); let properties = calculate_properties(&exzentrizitaeten); if properties.3 { @@ -27,7 +27,7 @@ pub fn main() { println!("radius/diameter/centre: not connected"); } - let components: Vec> = find_components(&weg_matrix); + let components: Vec> = find_components(weg_matrix); println!("components: {:?}", components); let result = find_articulations_and_bridges(&mut adjazenz_matrix, &components);