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 fn calculate_distanz_matrix(adjazenz_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
let mut distanz_matrix: Vec<Vec<usize>> = vec![];
let mut potenz_matrix = adjazenz_matrix.clone();
pub fn calculate_distanz_matrix(adjazenz_matrix: &Vec<Vec<u8>>) -> Vec<Vec<u8>> {
let mut distanz_matrix: Vec<Vec<u8>> = 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<usize>>) -> Vec<Vec<us
distanz_matrix
}
pub fn calculate_weg_matrix(adjazenz_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
let mut weg_matrix: Vec<Vec<usize>> = vec![];
let mut potenz_matrix = adjazenz_matrix.clone();
pub fn calculate_weg_matrix(adjazenz_matrix: &Vec<Vec<u8>>) -> Vec<Vec<u8>> {
let mut weg_matrix: Vec<Vec<u8>> = 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<usize>>) -> Vec<Vec<usize>
}
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<usize>>) -> Vec<Vec<usize>
weg_matrix
}
pub fn calculate_exzentrizitaeten(distanz_matrix: Vec<Vec<usize>>) -> Vec<usize> {
let mut exzentrizitaeten: Vec<usize> = vec![];
let mut exzentrizitaet: usize;
pub fn calculate_exzentrizitaeten(distanz_matrix: Vec<Vec<u8>>) -> Vec<u8> {
let mut exzentrizitaeten: Vec<u8> = 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<usize>>) -> Vec<usize>
exzentrizitaeten
}
pub fn calculate_properties(exzentrizitaeten: &Vec<usize>) -> (usize, usize, Vec<usize>, bool) {
let mut radius: usize = usize::MAX;
let mut diameter: usize = 0;
let mut centre: Vec<usize> = vec![];
pub fn calculate_properties(exzentrizitaeten: &[u8]) -> (u8, u8, Vec<u8>, bool) {
let mut radius = u8::MAX;
let mut diameter: u8 = 0;
let mut centre: Vec<u8> = 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<usize>>) -> Vec<Vec<usize>> {
let mut components: Vec<Vec<usize>> = vec![];
let mut component: Vec<usize>;
pub fn find_components(weg_matrix: Vec<Vec<u8>>) -> Vec<Vec<u8>> {
let mut components: Vec<Vec<u8>> = vec![];
let mut component: Vec<u8>;
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<usize>>) -> Vec<Vec<usize>> {
components
}
pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec<Vec<usize>>, components: &Vec<Vec<usize>>) -> (Vec<usize>,Vec<Vec<usize>>) {
let mut bridges: Vec<Vec<usize>> = vec![];
let mut articulations: Vec<usize> = vec![];
let mut temp_matrix: Vec<Vec<usize>> = adjazenz_matrix.clone();
pub fn find_articulations_and_bridges(
adjazenz_matrix: &mut Vec<Vec<u8>>,
components: &Vec<Vec<u8>>,
) -> (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 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 {
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<Vec<usize>>, 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();
}

View File

@ -1,7 +1,7 @@
use csv::ReaderBuilder;
pub fn fill_with_random(size: usize) -> Vec<Vec<usize>> {
let mut matrix: Vec<Vec<usize>> = vec![];
pub fn fill_with_random(size: usize) -> Vec<Vec<u8>> {
let mut matrix: Vec<Vec<u8>> = 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<Vec<usize>> {
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<Vec<usize>>, matrix2: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
let mut product: Vec<Vec<usize>> = vec![];
pub fn mult(matrix1: &Vec<Vec<usize>>, matrix2: &Vec<Vec<u8>>) -> Vec<Vec<usize>> {
let mut product: Vec<Vec<usize>> = vec![vec![]; matrix2.len()];
let mut vector: Vec<usize>;
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<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 value in vector {
print!("{value} ");
@ -47,24 +55,21 @@ pub fn show(matrix: &Vec<Vec<usize>>) {
}
}
pub fn read_csv(file_name: &str) -> Vec<Vec<usize>> {
let mut matrix: Vec<Vec<usize>> = 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<Vec<u8>> {
let mut matrix: Vec<Vec<u8>> = 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
}

View File

@ -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<Vec<usize>> = read_csv("art-brck.csv");
let distanz_matrix: Vec<Vec<usize>> = calculate_distanz_matrix(&adjazenz_matrix);
let weg_matrix: Vec<Vec<usize>> = 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![
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![
]
);
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![
]
);
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 components: Vec<Vec<usize>> = 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<Vec<usize>> = read_csv("art-brck.csv");
let adjazenz_matrix = read_csv("art-brck.csv");
assert_eq!(adjazenz_matrix, vec![
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![
]
);
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]
]);
]
);
}
}

View File

@ -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<Vec<usize>> = 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 distanz_matrix: Vec<Vec<usize>> = calculate_distanz_matrix(&adjazenz_matrix);
let weg_matrix: Vec<Vec<usize>> = 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<Vec<usize>> = 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);