changes have been made

This commit is contained in:
René Fuhry 2023-05-31 14:49:55 +02:00 committed by GitHub
parent bf22a48d9b
commit f408292eb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 147 additions and 78 deletions

View File

@ -1,44 +1,6 @@
pub mod matrix;
pub fn test() {
let mut adjazenz_matrix: Vec<Vec<usize>> = matrix::read_csv();
let distanz_matrix: Vec<Vec<usize>> = calculate_distanz_matrix(&adjazenz_matrix);
let weg_matrix: Vec<Vec<usize>> = calculate_weg_matrix(&adjazenz_matrix);
println!("adjazen matrix:");
matrix::show(&adjazenz_matrix);
println!("\ndistanz matrix:");
matrix::show(&distanz_matrix);
println!("\nweg matrix:");
matrix::show(&weg_matrix);
let exzentrizitaeten = calculate_exzentrizitaeten(&distanz_matrix);
let mut connected: bool = true;
if exzentrizitaeten.contains(&0) {
connected = false;
}
println!("\nis the graph connected: {connected}");
println!("exzentrizitäten: {:?}", exzentrizitaeten);
let properties = calculate_properties(&exzentrizitaeten);
let radius: usize = properties.0;
let diameter: usize = properties.1;
let centre: Vec<usize> = properties.2;
println!("radius: {radius}\ndiameter: {diameter}\ncentre: {:?}", centre);
let components = find_components(&weg_matrix);
println!("components: {:?}", components);
let bridges = find_bridges(&mut adjazenz_matrix, &components);
println!("bridges: {:?}", bridges);
let articulations = find_articulations(&adjazenz_matrix, &components);
println!("articulations: {:?}", articulations);
}
fn calculate_distanz_matrix(adjazenz_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
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();
@ -68,7 +30,7 @@ fn calculate_distanz_matrix(adjazenz_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>
distanz_matrix
}
fn calculate_weg_matrix(adjazenz_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
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();
@ -98,7 +60,7 @@ fn calculate_weg_matrix(adjazenz_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
weg_matrix
}
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 exzentrizitaet: usize;
@ -115,7 +77,7 @@ fn calculate_exzentrizitaeten(distanz_matrix: &Vec<Vec<usize>>) -> Vec<usize> {
exzentrizitaeten
}
fn calculate_properties(exzentrizitaeten: &Vec<usize>) -> (usize, usize, Vec<usize>) {
pub fn calculate_properties(exzentrizitaeten: &Vec<usize>) -> (usize, usize, Vec<usize>) {
let mut radius: usize = usize::MAX;
let mut diameter: usize = 0;
let mut centre: Vec<usize> = vec![];
@ -138,7 +100,7 @@ fn calculate_properties(exzentrizitaeten: &Vec<usize>) -> (usize, usize, Vec<usi
results
}
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 component: Vec<usize>;
@ -157,7 +119,7 @@ fn find_components(weg_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
components
}
fn find_bridges(adjazenz_matrix: &mut Vec<Vec<usize>>, components: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
pub fn find_bridges(adjazenz_matrix: &mut Vec<Vec<usize>>, components: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
let mut bridges: Vec<Vec<usize>> = vec![];
let mut bridge: Vec<usize>;
let mut new_components: Vec<Vec<usize>>;
@ -190,7 +152,7 @@ fn find_bridges(adjazenz_matrix: &mut Vec<Vec<usize>>, components: &Vec<Vec<usiz
bridges
}
fn find_articulations(adjazenz_matrix: &Vec<Vec<usize>>, components: &Vec<Vec<usize>>) -> Vec<usize> {
pub fn find_articulations(adjazenz_matrix: &Vec<Vec<usize>>, components: &Vec<Vec<usize>>) -> Vec<usize> {
let mut articulations: Vec<usize> = vec![];
let mut new_components: Vec<Vec<usize>>;
let mut temp_matrix: Vec<Vec<usize>> = adjazenz_matrix.clone();

View File

@ -1,3 +1,4 @@
#[allow(unused_imports)]
use std::{fs::File, io::Read};
pub fn fill_with_random(matrix: &mut Vec<Vec<usize>>, size: usize) {
@ -48,7 +49,7 @@ pub fn read_csv() -> Vec<Vec<usize>> {
vec![0, 0, 0, 1, 0]
];
/*
let mut csv = File::open("/home/rene/projects/Java/graphprogram/csv/art-brck.csv").unwrap();
let mut csv = File::open("").unwrap();
let mut content = String::new();
csv.read_to_string(&mut content).unwrap();
println!("{content}");
@ -61,29 +62,3 @@ pub fn read_csv() -> Vec<Vec<usize>> {
*/
matrix
}
pub fn test() {
let matrix1: Vec<Vec<usize>> = 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]
];
let matrix2: Vec<Vec<usize>>;
matrix2 = matrix1.clone();
println!("A:");
show(&matrix1);
let mut product: Vec<Vec<usize>> = mult(&matrix1, &matrix2);
println!("\nA²:");
show(&product);
product = mult(&product, &matrix1);
println!("\nA³:");
show(&product);
}

101
src/lib.rs Normal file
View File

@ -0,0 +1,101 @@
pub mod graph;
#[cfg(test)]
mod tests {
use crate::graph::{self, matrix::*};
#[test]
fn graph() {
let mut adjazenz_matrix: Vec<Vec<usize>> = graph::matrix::read_csv();
let distanz_matrix: Vec<Vec<usize>> = graph::calculate_distanz_matrix(&adjazenz_matrix);
let weg_matrix: Vec<Vec<usize>> = graph::calculate_weg_matrix(&adjazenz_matrix);
let expected_adjazenz_matrix: Vec<Vec<usize>> = 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]
];
let expected_distanz_matrix: Vec<Vec<usize>> = 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]
];
let expected_weg_matrix: Vec<Vec<usize>> = 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, expected_adjazenz_matrix);
assert_eq!(distanz_matrix, expected_distanz_matrix);
assert_eq!(weg_matrix, expected_weg_matrix);
let exzentrizitaeten = graph::calculate_exzentrizitaeten(&distanz_matrix);
let mut connected: bool = true;
if exzentrizitaeten.contains(&0) {
connected = false;
}
assert_eq!(exzentrizitaeten, vec![2, 2, 2, 1, 2]);
assert_eq!(connected, true);
let properties = graph::calculate_properties(&exzentrizitaeten);
let radius: usize = properties.0;
let diameter: usize = properties.1;
let centre: Vec<usize> = properties.2;
assert_eq!(radius, 1);
assert_eq!(diameter, 2);
assert_eq!(centre, vec![4]);
let components = graph::find_components(&weg_matrix);
let bridges = graph::find_bridges(&mut adjazenz_matrix, &components);
let articulations = graph::find_articulations(&adjazenz_matrix, &components);
assert_eq!(components, vec![vec![1, 2, 3, 4, 5]]);
assert_eq!(bridges, vec![vec![4, 5]]);
assert_eq!(articulations, vec![4]);
}
#[test]
fn matrix() {
let matrix1 = read_csv();
let expected_matrix: Vec<Vec<usize>> = 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]
];
let matrix2: Vec<Vec<usize>> = matrix1.clone();
assert_eq!(matrix1, expected_matrix);
let mut product: Vec<Vec<usize>> = mult(&matrix1, &matrix2);
let mut expected_product: Vec<Vec<usize>> = 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]
];
assert_eq!(product, expected_product);
product = mult(&product, &matrix1);
expected_product = vec![
vec![2, 2, 5, 6, 1],
vec![2, 2, 5, 6, 1],
vec![5, 5, 4, 6, 2],
vec![6, 6, 6, 4, 4],
vec![1, 1, 2, 4, 0]
];
assert_eq!(product, expected_product)
}
}

View File

@ -1,13 +1,44 @@
pub mod graph;
pub fn main() {
tests("graph")
output();
}
pub fn tests(arg: &str) {
match arg {
"graph" => graph::test(),
"matrix" => graph::matrix::test(),
&_ => println!("{arg} is not a valid option"),
// This is just for the pupose of visualising the output
fn output() {
let mut adjazenz_matrix: Vec<Vec<usize>> = graph::matrix::read_csv();
let distanz_matrix: Vec<Vec<usize>> = graph::calculate_distanz_matrix(&adjazenz_matrix);
let weg_matrix: Vec<Vec<usize>> = graph::calculate_weg_matrix(&adjazenz_matrix);
println!("adjazen matrix:");
graph::matrix::show(&adjazenz_matrix);
println!("\ndistanz matrix:");
graph::matrix::show(&distanz_matrix);
println!("\nweg matrix:");
graph::matrix::show(&weg_matrix);
let exzentrizitaeten = graph::calculate_exzentrizitaeten(&distanz_matrix);
let mut connected: bool = true;
if exzentrizitaeten.contains(&0) {
connected = false;
}
println!("\nis the graph connected: {connected}");
println!("exzentrizitäten: {:?}", exzentrizitaeten);
let properties = graph::calculate_properties(&exzentrizitaeten);
let radius: usize = properties.0;
let diameter: usize = properties.1;
let centre: Vec<usize> = properties.2;
println!("radius: {radius}\ndiameter: {diameter}\ncentre: {:?}", centre);
let components = graph::find_components(&weg_matrix);
println!("components: {:?}", components);
let bridges = graph::find_bridges(&mut adjazenz_matrix, &components);
println!("bridges: {:?}", bridges);
let articulations = graph::find_articulations(&adjazenz_matrix, &components);
println!("articulations: {:?}", articulations);
}