code cleanup

This commit is contained in:
AustrianToast 2023-06-03 10:12:59 +02:00
parent 3abc3a8e75
commit ece912872e
4 changed files with 154 additions and 131 deletions

View File

@ -1,28 +1,27 @@
use self::matrix::clone;
pub mod matrix; pub mod matrix;
pub fn calculate_distanz_matrix(adjazenz_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> { pub fn calculate_distanz_matrix(adjazenz_matrix: &Vec<Vec<u8>>) -> Vec<Vec<u8>> {
let mut distanz_matrix: Vec<Vec<usize>> = vec![]; let mut distanz_matrix: Vec<Vec<u8>> = vec![vec![]; adjazenz_matrix.len()];
let mut potenz_matrix = adjazenz_matrix.clone(); let mut potenz_matrix = clone(adjazenz_matrix);
for k in 1..adjazenz_matrix.len() { 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() { for i in 0..adjazenz_matrix.len() {
if k == 1 {
distanz_matrix.push(vec![]);
}
for j in 0..adjazenz_matrix.len() { for j in 0..adjazenz_matrix.len() {
if k != 1 { if k != 1 {
if potenz_matrix[i][j] != 0 && distanz_matrix[i][j] == usize::MAX { if potenz_matrix[i][j] != 0 && distanz_matrix[i][j] == u8::MAX {
distanz_matrix[i][j] = k; distanz_matrix[i][j] = u8::try_from(k).unwrap();
} }
continue; continue;
} }
if i == j { if i == j {
distanz_matrix[i].push(0) distanz_matrix[i].push(0);
} else if adjazenz_matrix[i][j] == 1 { } else if adjazenz_matrix[i][j] == 1 {
distanz_matrix[i].push(1); distanz_matrix[i].push(1);
} else { } 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<usize>>) -> Vec<Vec<us
distanz_matrix distanz_matrix
} }
pub fn calculate_weg_matrix(adjazenz_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> { pub fn calculate_weg_matrix(adjazenz_matrix: &Vec<Vec<u8>>) -> Vec<Vec<u8>> {
let mut weg_matrix: Vec<Vec<usize>> = vec![]; let mut weg_matrix: Vec<Vec<u8>> = vec![vec![]; adjazenz_matrix.len()];
let mut potenz_matrix = adjazenz_matrix.clone(); let mut potenz_matrix = clone(adjazenz_matrix);
for k in 1..adjazenz_matrix.len() { 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() { for i in 0..adjazenz_matrix.len() {
if k == 1 {
weg_matrix.push(vec![]);
}
for j in 0..adjazenz_matrix.len() { for j in 0..adjazenz_matrix.len() {
if k != 1 { if k != 1 {
if potenz_matrix[i][j] != 0 { if potenz_matrix[i][j] != 0 {
@ -47,9 +43,7 @@ pub fn calculate_weg_matrix(adjazenz_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>
} }
continue; continue;
} }
if i == j { if i == j || adjazenz_matrix[i][j] == 1 {
weg_matrix[i].push(1)
} else if adjazenz_matrix[i][j] == 1 {
weg_matrix[i].push(1); weg_matrix[i].push(1);
} else { } else {
weg_matrix[i].push(0); weg_matrix[i].push(0);
@ -60,15 +54,15 @@ 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<u8>>) -> Vec<u8> {
let mut exzentrizitaeten: Vec<usize> = vec![]; let mut exzentrizitaeten: Vec<u8> = vec![];
let mut exzentrizitaet: usize; let mut exzentrizitaet: u8;
for vector in distanz_matrix { for vector in distanz_matrix {
exzentrizitaet = 0; exzentrizitaet = 0;
for value in vector { for value in vector {
if value == usize::MAX { if value == u8::MAX {
continue; continue;
} }
exzentrizitaet = exzentrizitaet.max(value); exzentrizitaet = exzentrizitaet.max(value);
@ -78,42 +72,40 @@ pub fn calculate_exzentrizitaeten(distanz_matrix: Vec<Vec<usize>>) -> Vec<usize>
exzentrizitaeten exzentrizitaeten
} }
pub fn calculate_properties(exzentrizitaeten: &Vec<usize>) -> (usize, usize, Vec<usize>, bool) { pub fn calculate_properties(exzentrizitaeten: &[u8]) -> (u8, u8, Vec<u8>, bool) {
let mut radius: usize = usize::MAX; let mut radius = u8::MAX;
let mut diameter: usize = 0; let mut diameter: u8 = 0;
let mut centre: Vec<usize> = vec![]; let mut centre: Vec<u8> = vec![];
let mut connected: bool = true; let mut connected: bool = true;
for index in 0..exzentrizitaeten.len() { for (index, value) in exzentrizitaeten.iter().enumerate() {
let value = exzentrizitaeten[index]; if value > &diameter {
diameter = *value;
if value > diameter {
diameter = value;
} }
if value == radius { if value == &radius {
centre.push(index + 1); centre.push(u8::try_from(index + 1).unwrap());
} }
if value < radius { if value < &radius {
radius = value; radius = *value;
centre.clear(); centre.clear();
centre.push(index + 1); centre.push(u8::try_from(index + 1).unwrap());
} }
if value == 0 { if value == &0 {
connected = false; connected = false;
} }
} }
(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<u8>>) -> Vec<Vec<u8>> {
let mut components: Vec<Vec<usize>> = vec![]; let mut components: Vec<Vec<u8>> = vec![];
let mut component: Vec<usize>; let mut component: Vec<u8>;
for array in weg_matrix { for array in weg_matrix {
component = vec![]; component = vec![];
for index in 0..array.len() { for (index, value) in array.iter().enumerate() {
if array[index] == 1 { if value == &1 {
component.push(index + 1); component.push(u8::try_from(index + 1).unwrap());
} }
} }
if !components.contains(&component) { if !components.contains(&component) {
@ -123,10 +115,13 @@ pub fn find_components(weg_matrix: Vec<Vec<usize>>) -> Vec<Vec<usize>> {
components components
} }
pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec<Vec<usize>>, components: &Vec<Vec<usize>>) -> (Vec<usize>,Vec<Vec<usize>>) { pub fn find_articulations_and_bridges(
let mut bridges: Vec<Vec<usize>> = vec![]; adjazenz_matrix: &mut Vec<Vec<u8>>,
let mut articulations: Vec<usize> = vec![]; components: &Vec<Vec<u8>>,
let mut temp_matrix: Vec<Vec<usize>> = adjazenz_matrix.clone(); ) -> (Vec<u8>, Vec<Vec<u8>>) {
let mut bridges: Vec<Vec<u8>> = vec![];
let mut articulations: Vec<u8> = vec![];
let mut temp_matrix = adjazenz_matrix.clone();
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() {
@ -137,12 +132,17 @@ pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec<Vec<usize>>, com
if n != 0 { if n != 0 {
continue; 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]; let prev_value = adjazenz_matrix[i][j];
adjazenz_matrix[i][j] = 0; adjazenz_matrix[i][j] = 0;
adjazenz_matrix[j][i] = 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); bridges.push(bridge);
} }
adjazenz_matrix[i][j] = prev_value; adjazenz_matrix[i][j] = prev_value;
@ -151,7 +151,7 @@ pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec<Vec<usize>>, com
} }
if find_components(calculate_weg_matrix(&temp_matrix)).len() > (components.len() + 1) { 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(); temp_matrix = adjazenz_matrix.clone();
} }

View File

@ -1,7 +1,7 @@
use csv::ReaderBuilder; use csv::ReaderBuilder;
pub fn fill_with_random(size: usize) -> Vec<Vec<usize>> { pub fn fill_with_random(size: usize) -> Vec<Vec<u8>> {
let mut matrix: Vec<Vec<usize>> = vec![]; let mut matrix: Vec<Vec<u8>> = vec![];
for i in 0..size { for i in 0..size {
matrix.push(vec![]); matrix.push(vec![]);
for j in 0..size { for j in 0..size {
@ -9,36 +9,44 @@ pub fn fill_with_random(size: usize) -> Vec<Vec<usize>> {
matrix[i].push(0); matrix[i].push(0);
continue; continue;
} }
matrix[i].push(fastrand::usize(0..2)); matrix[i].push(fastrand::u8(0..2));
} }
} }
matrix matrix
} }
pub fn mult(matrix1: &Vec<Vec<usize>>, matrix2: &Vec<Vec<usize>>) -> Vec<Vec<usize>> { pub fn mult(matrix1: &Vec<Vec<usize>>, matrix2: &Vec<Vec<u8>>) -> Vec<Vec<usize>> {
let mut product: Vec<Vec<usize>> = vec![]; let mut product: Vec<Vec<usize>> = vec![vec![]; matrix2.len()];
let mut vector: Vec<usize>; let mut vector: Vec<usize>;
let mut index = 0;
for (index, k) in (0..matrix1.len()).enumerate() {
for k in 0..matrix1.len() {
vector = vec![]; vector = vec![];
for array in matrix2 { for array in matrix2 {
vector.push(array[index]); vector.push(array[index].into());
} }
index += 1;
product.push(vec![]);
for array in matrix1 { for array in matrix1 {
let sum: usize = array.iter() let sum = array.iter().zip(vector.iter()).map(|(x, y)| x * y).sum();
.zip(vector.iter()) //println!("{sum}");
.map(|(x, y)| x * y)
.sum();
product[k].push(sum); product[k].push(sum);
} }
} }
product product
} }
pub fn show(matrix: &Vec<Vec<usize>>) { pub fn clone(matrix: &Vec<Vec<u8>>) -> Vec<Vec<usize>> {
let mut new_matrix: Vec<Vec<usize>> = 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<Vec<u8>>) {
for vector in matrix { for vector in matrix {
for value in vector { for value in vector {
print!("{value} "); print!("{value} ");
@ -47,24 +55,21 @@ pub fn show(matrix: &Vec<Vec<usize>>) {
} }
} }
pub fn read_csv(file_name: &str) -> Vec<Vec<usize>> { pub fn read_csv(file_name: &str) -> Vec<Vec<u8>> {
let mut matrix: Vec<Vec<usize>> = vec![]; let mut matrix: Vec<Vec<u8>> = vec![];
let dir: String = String::from("/home/rene/projects/Java/graphprogram/csv/"); let dir: String = "/home/rene/projects/Java/graphprogram/csv/".into();
let file_path = dir + &file_name;
let mut csv = ReaderBuilder::new() let mut csv = ReaderBuilder::new()
.has_headers(false) .has_headers(false)
.delimiter(b';') .delimiter(b';')
.from_path(file_path) .from_path(dir + file_name)
.unwrap(); .unwrap();
let mut index = 0; for (index, result) in csv.records().enumerate() {
for result in csv.records() {
let record = result.unwrap(); let record = result.unwrap();
matrix.push(vec![]); matrix.push(vec![]);
for field in record.iter() { for field in record.iter() {
matrix[index].push(field.parse().unwrap()); matrix[index].push(field.parse().unwrap());
} }
index += 1;
} }
matrix matrix
} }

View File

@ -2,39 +2,48 @@ pub mod graph;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::graph::{*, matrix::*}; use crate::graph::{matrix::*, *};
#[test] #[test]
fn graph() { fn graph() {
let mut adjazenz_matrix: Vec<Vec<usize>> = read_csv("art-brck.csv"); let mut adjazenz_matrix = read_csv("art-brck.csv");
let distanz_matrix: Vec<Vec<usize>> = calculate_distanz_matrix(&adjazenz_matrix); let distanz_matrix = calculate_distanz_matrix(&adjazenz_matrix);
let weg_matrix: Vec<Vec<usize>> = calculate_weg_matrix(&adjazenz_matrix); let weg_matrix = calculate_weg_matrix(&adjazenz_matrix);
assert_eq!(adjazenz_matrix, vec![ assert_eq!(
vec![0, 0, 1, 1, 0], adjazenz_matrix,
vec![0, 0, 1, 1, 0], vec![
vec![1, 1, 0, 1, 0], vec![0, 0, 1, 1, 0],
vec![1, 1, 1, 0, 1], vec![0, 0, 1, 1, 0],
vec![0, 0, 0, 1, 0] vec![1, 1, 0, 1, 0],
]); vec![1, 1, 1, 0, 1],
assert_eq!(distanz_matrix, vec![ vec![0, 0, 0, 1, 0]
vec![0, 2, 1, 1, 2], ]
vec![2, 0, 1, 1, 2], );
vec![1, 1, 0, 1, 2], assert_eq!(
vec![1, 1, 1, 0, 1], distanz_matrix,
vec![2, 2, 2, 1, 0] vec![
]); vec![0, 2, 1, 1, 2],
assert_eq!(weg_matrix, vec![ vec![2, 0, 1, 1, 2],
vec![1, 1, 1, 1, 1], vec![1, 1, 0, 1, 2],
vec![1, 1, 1, 1, 1], vec![1, 1, 1, 0, 1],
vec![1, 1, 1, 1, 1], vec![2, 2, 2, 1, 0]
vec![1, 1, 1, 1, 1], ]
vec![1, 1, 1, 1, 1] );
]); 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<usize> = calculate_exzentrizitaeten(distanz_matrix); let exzentrizitaeten = calculate_exzentrizitaeten(distanz_matrix);
let properties = calculate_properties(&exzentrizitaeten); let properties = calculate_properties(&exzentrizitaeten);
let components: Vec<Vec<usize>> = find_components(weg_matrix); let components = find_components(weg_matrix);
let result = find_articulations_and_bridges(&mut adjazenz_matrix, &components); 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]);
@ -49,21 +58,27 @@ mod tests {
#[test] #[test]
fn matrix() { fn matrix() {
let adjazenz_matrix: Vec<Vec<usize>> = read_csv("art-brck.csv"); let adjazenz_matrix = read_csv("art-brck.csv");
assert_eq!(adjazenz_matrix, vec![ assert_eq!(
vec![0, 0, 1, 1, 0], adjazenz_matrix,
vec![0, 0, 1, 1, 0], vec![
vec![1, 1, 0, 1, 0], vec![0, 0, 1, 1, 0],
vec![1, 1, 1, 0, 1], vec![0, 0, 1, 1, 0],
vec![0, 0, 0, 1, 0] vec![1, 1, 0, 1, 0],
]); vec![1, 1, 1, 0, 1],
assert_eq!(mult(&adjazenz_matrix, &adjazenz_matrix), vec![ vec![0, 0, 0, 1, 0]
vec![2, 2, 1, 1, 1], ]
vec![2, 2, 1, 1, 1], );
vec![1, 1, 3, 2, 1], assert_eq!(
vec![1, 1, 2, 4, 0], mult(&clone(&adjazenz_matrix), &adjazenz_matrix),
vec![1, 1, 1, 0, 1] 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]
]
);
} }
} }

View File

@ -1,13 +1,13 @@
use crate::graph::{*, matrix::*}; use crate::graph::{*, matrix::{read_csv, show}};
pub mod graph; 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 = read_csv(file_name);
//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 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: Vec<Vec<usize>> = calculate_distanz_matrix(&adjazenz_matrix); let distanz_matrix = calculate_distanz_matrix(&adjazenz_matrix);
let weg_matrix: Vec<Vec<usize>> = calculate_weg_matrix(&adjazenz_matrix); let weg_matrix = calculate_weg_matrix(&adjazenz_matrix);
println!("adjazen matrix:"); println!("adjazen matrix:");
show(&adjazenz_matrix); show(&adjazenz_matrix);
@ -20,15 +20,18 @@ pub fn main() {
let properties = calculate_properties(&exzentrizitaeten); let properties = calculate_properties(&exzentrizitaeten);
if properties.3 { if properties.3 {
println!("\nexzentrizitäten: {:?}", exzentrizitaeten); println!("\nexzentrizitäten: {exzentrizitaeten:?}");
println!("radius: {}\ndiameter: {}\ncentre: {:?}", properties.0, properties.1, properties.2); println!(
"radius: {}\ndiameter: {}\ncentre: {:?}",
properties.0, properties.1, properties.2
);
} else { } else {
println!("\nexzentrizitäten: not connected"); println!("\nexzentrizitäten: not connected");
println!("radius/diameter/centre: not connected"); println!("radius/diameter/centre: not connected");
} }
let components: Vec<Vec<usize>> = find_components(weg_matrix); let components = 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);
println!("bridges: {:?}", result.1); println!("bridges: {:?}", result.1);