minor optimizations

This commit is contained in:
René Fuhry 2023-06-02 14:59:36 +02:00 committed by GitHub
parent 6e9137151f
commit 40db6c2188
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 29 deletions

View File

@ -8,3 +8,14 @@ edition = "2021"
[dependencies] [dependencies]
csv = "1.2.2" csv = "1.2.2"
fastrand = "1.9.0" 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

View File

@ -60,7 +60,7 @@ pub fn calculate_weg_matrix(adjazenz_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>
weg_matrix weg_matrix
} }
pub fn calculate_exzentrizitaeten(distanz_matrix: &Vec<Vec<usize>>) -> Vec<usize> { pub fn calculate_exzentrizitaeten(distanz_matrix: Vec<Vec<usize>>) -> Vec<usize> {
let mut exzentrizitaeten: Vec<usize> = vec![]; let mut exzentrizitaeten: Vec<usize> = vec![];
let mut exzentrizitaet: usize; let mut exzentrizitaet: usize;
@ -105,16 +105,16 @@ pub fn calculate_properties(exzentrizitaeten: &Vec<usize>) -> (usize, usize, Vec
(radius, diameter, centre, connected) (radius, diameter, centre, connected)
} }
pub fn find_components(weg_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> { pub fn find_components(weg_matrix: Vec<Vec<usize>>) -> Vec<Vec<usize>> {
let mut components: Vec<Vec<usize>> = vec![]; let mut components: Vec<Vec<usize>> = vec![];
let mut component: Vec<usize>; let mut component: Vec<usize>;
let mut i: usize; let mut i: usize;
for array in weg_matrix.iter() { for array in weg_matrix {
component = vec![]; component = vec![];
i = 1; i = 1;
for value in array.iter() { for value in array {
if value == &1 { if value == 1 {
component.push(i); component.push(i);
} }
i += 1; i += 1;
@ -133,7 +133,6 @@ pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec<Vec<usize>>, com
let mut articulations: Vec<usize> = vec![]; let mut articulations: Vec<usize> = vec![];
let mut new_components: Vec<Vec<usize>>; let mut new_components: Vec<Vec<usize>>;
let mut temp_matrix: Vec<Vec<usize>> = adjazenz_matrix.clone(); let mut temp_matrix: Vec<Vec<usize>> = adjazenz_matrix.clone();
let mut weg_matrix: Vec<Vec<usize>>;
for n in 0..temp_matrix.len() { for n in 0..temp_matrix.len() {
for i 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<Vec<usize>>, com
adjazenz_matrix[i][j] = 0; adjazenz_matrix[i][j] = 0;
adjazenz_matrix[j][i] = 0; adjazenz_matrix[j][i] = 0;
weg_matrix = calculate_weg_matrix(&adjazenz_matrix); new_components = find_components(calculate_weg_matrix(&adjazenz_matrix));
new_components = find_components(&weg_matrix);
if new_components.len() > components.len() && !bridges.contains(&bridge) { if new_components.len() > components.len() && !bridges.contains(&bridge) {
bridges.push(bridge); bridges.push(bridge);
@ -163,8 +161,7 @@ pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec<Vec<usize>>, com
} }
} }
weg_matrix = calculate_weg_matrix(&temp_matrix); new_components = find_components(calculate_weg_matrix(&temp_matrix));
new_components = find_components(&weg_matrix);
if new_components.len() > (components.len() + 1) { if new_components.len() > (components.len() + 1) {
articulations.push(n + 1); articulations.push(n + 1);

View File

@ -19,18 +19,22 @@ pub fn fill_with_random(size: usize) -> Vec<Vec<usize>> {
pub fn mult(matrix1: &Vec<Vec<usize>>, matrix2: &Vec<Vec<usize>>) -> Vec<Vec<usize>> { pub fn mult(matrix1: &Vec<Vec<usize>>, matrix2: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
let mut product: Vec<Vec<usize>> = vec![]; let mut product: Vec<Vec<usize>> = vec![];
let mut sum: u128; let mut vector: Vec<usize>;
let mut index = 0;
for i in 0..matrix1.len() {
for j in 0..matrix1.len() { for k in 0..matrix1.len() {
if i == 0 { vector = vec![];
product.push(vec![]); for array in matrix2 {
} vector.push(array[index]);
sum = 0; }
for k in 0..matrix1.len() { index += 1;
sum += (matrix1[i][k] * matrix2[k][j]) as u128; product.push(vec![]);
} for array in matrix1 {
product[i].push(sum as usize); let sum: usize = array.iter()
.zip(vector.iter())
.map(|(x, y)| x * y)
.sum();
product[k].push(sum);
} }
} }
product product

View File

@ -9,10 +9,6 @@ mod tests {
let mut adjazenz_matrix: Vec<Vec<usize>> = read_csv("art-brck.csv"); let mut adjazenz_matrix: Vec<Vec<usize>> = read_csv("art-brck.csv");
let distanz_matrix: Vec<Vec<usize>> = calculate_distanz_matrix(&adjazenz_matrix); let distanz_matrix: Vec<Vec<usize>> = calculate_distanz_matrix(&adjazenz_matrix);
let weg_matrix: Vec<Vec<usize>> = calculate_weg_matrix(&adjazenz_matrix); let weg_matrix: Vec<Vec<usize>> = calculate_weg_matrix(&adjazenz_matrix);
let exzentrizitaeten: Vec<usize> = calculate_exzentrizitaeten(&distanz_matrix);
let properties = calculate_properties(&exzentrizitaeten);
let components: Vec<Vec<usize>> = find_components(&weg_matrix);
let result = find_articulations_and_bridges(&mut adjazenz_matrix, &components);
assert_eq!(adjazenz_matrix, vec![ assert_eq!(adjazenz_matrix, vec![
vec![0, 0, 1, 1, 0], vec![0, 0, 1, 1, 0],
@ -35,6 +31,12 @@ mod tests {
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<usize> = calculate_exzentrizitaeten(distanz_matrix);
let properties = calculate_properties(&exzentrizitaeten);
let components: Vec<Vec<usize>> = 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!(exzentrizitaeten, vec![2, 2, 2, 1, 2]);
assert_eq!(properties.0, 1); assert_eq!(properties.0, 1);
assert_eq!(properties.1, 2); assert_eq!(properties.1, 2);

View File

@ -5,7 +5,7 @@ pub mod graph;
pub fn main() { pub fn main() {
let file_name = "24n.csv"; let file_name = "24n.csv";
let mut adjazenz_matrix: Vec<Vec<usize>> = read_csv(file_name); let mut adjazenz_matrix: Vec<Vec<usize>> = read_csv(file_name);
//let mut adjazenz_matrix: Vec<Vec<usize>> = fill_with_random(100); //let mut adjazenz_matrix: Vec<Vec<usize>> = fill_with_random(45); // with this many verteces, it runs in about 10.2 seconds (2023-06-02 14:39)
let distanz_matrix: Vec<Vec<usize>> = calculate_distanz_matrix(&adjazenz_matrix); let distanz_matrix: Vec<Vec<usize>> = calculate_distanz_matrix(&adjazenz_matrix);
let weg_matrix: Vec<Vec<usize>> = calculate_weg_matrix(&adjazenz_matrix); let weg_matrix: Vec<Vec<usize>> = calculate_weg_matrix(&adjazenz_matrix);
@ -16,7 +16,7 @@ pub fn main() {
println!("\nweg matrix:"); println!("\nweg matrix:");
show(&weg_matrix); show(&weg_matrix);
let exzentrizitaeten = calculate_exzentrizitaeten(&distanz_matrix); let exzentrizitaeten = calculate_exzentrizitaeten(distanz_matrix);
let properties = calculate_properties(&exzentrizitaeten); let properties = calculate_properties(&exzentrizitaeten);
if properties.3 { if properties.3 {
@ -27,7 +27,7 @@ pub fn main() {
println!("radius/diameter/centre: not connected"); println!("radius/diameter/centre: not connected");
} }
let components: Vec<Vec<usize>> = find_components(&weg_matrix); let components: Vec<Vec<usize>> = find_components(weg_matrix);
println!("components: {:?}", components); println!("components: {:?}", components);
let result = find_articulations_and_bridges(&mut adjazenz_matrix, &components); let result = find_articulations_and_bridges(&mut adjazenz_matrix, &components);