From 95d6a18c16a948fef4521234149ba0a130810fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Tue, 30 May 2023 19:13:41 +0200 Subject: [PATCH] Initial commit --- Cargo.lock | 34 +++++++++++ Cargo.toml | 9 +++ src/graph.rs | 141 ++++++++++++++++++++++++++++++++++++++++++++ src/graph/matrix.rs | 78 ++++++++++++++++++++++++ src/main.rs | 5 ++ 5 files changed, 267 insertions(+) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/graph.rs create mode 100644 src/graph/matrix.rs create mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..b537000 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,34 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "fastrand" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" +dependencies = [ + "instant", +] + +[[package]] +name = "graphprogram_rust" +version = "0.1.0" +dependencies = [ + "fastrand", +] + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7ec32c7 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "graphprogram_rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +fastrand = "1.9.0" diff --git a/src/graph.rs b/src/graph.rs new file mode 100644 index 0000000..14d475c --- /dev/null +++ b/src/graph.rs @@ -0,0 +1,141 @@ +pub mod matrix; + +pub fn tests(arg: &str) { + match arg { + "matrix" => matrix::test(), + "graph" => test(), + &_ => println!("not a valid option"), + } +} + +pub fn test() { + let adjazenz_matrix: Vec> = matrix::read_csv(); + let distanz_matrix: Vec> = calculate_distanz_matrix(&adjazenz_matrix); + let weg_matrix: Vec> = 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!("\n{:?}", exzentrizitaeten); + println!("is the graph connected: {connected}"); + + let properties = calculate_properties(&exzentrizitaeten); + let radius: usize = properties.0; + let diameter: usize = properties.1; + let centre: Vec = properties.2; + + println!("radius: {radius}\ndiameter: {diameter}\ncentre: {:?}", centre); +} + +fn calculate_distanz_matrix(adjazenz_matrix: &Vec>) -> Vec> { + #[allow(unused_assignments)] + let mut distanz_matrix: Vec> = vec![]; + let mut potenz_matrix = adjazenz_matrix.clone(); + + for i in 0..adjazenz_matrix.len() { + distanz_matrix.push(vec![]); + for j in 0..adjazenz_matrix.len() { + if i == j { + distanz_matrix[i].push(0) + } else if adjazenz_matrix[i][j] == 1 { + distanz_matrix[i].push(1); + } else { + distanz_matrix[i].push(usize::MAX); + } + } + } + + for k in 2..adjazenz_matrix.len() { + potenz_matrix = matrix::mult(&potenz_matrix, &adjazenz_matrix); + for i in 0..adjazenz_matrix.len() { + for j in 0..adjazenz_matrix.len() { + if potenz_matrix[i][j] != 0 && distanz_matrix[i][j] == usize::MAX { + distanz_matrix[i][j] = k; + } + } + } + } + distanz_matrix +} + +fn calculate_weg_matrix(adjazenz_matrix: &Vec>) -> Vec> { + #[allow(unused_assignments)] + let mut weg_matrix: Vec> = vec![]; + let mut potenz_matrix = adjazenz_matrix.clone(); + + for i in 0..adjazenz_matrix.len() { + weg_matrix.push(vec![]); + for j in 0..adjazenz_matrix.len() { + if i == j { + weg_matrix[i].push(1) + } else if adjazenz_matrix[i][j] == 1 { + weg_matrix[i].push(1); + } else { + weg_matrix[i].push(0); + } + } + } + + for _k in 2..adjazenz_matrix.len() { + potenz_matrix = matrix::mult(&potenz_matrix, &adjazenz_matrix); + for i in 0..adjazenz_matrix.len() { + for j in 0..adjazenz_matrix.len() { + if potenz_matrix[i][j] != 0 { + weg_matrix[i][j] = 1; + } + } + } + } + weg_matrix +} + +fn calculate_exzentrizitaeten(distanz_matrix: &Vec>) -> Vec { + let mut exzentrizitaeten: Vec = vec![]; + let mut exzentrizitaet: usize; + + for i in 0..distanz_matrix.len() { + exzentrizitaet = 0; + + for j in 0..distanz_matrix.len() { + if distanz_matrix[i][j] > exzentrizitaet && i != j { + exzentrizitaet = distanz_matrix[i][j]; + } + } + exzentrizitaeten.push(exzentrizitaet); + } + exzentrizitaeten +} + +fn calculate_properties(exzentrizitaeten: &Vec) -> (usize, usize, Vec) { + let mut radius: usize = usize::MAX; + let mut diameter: usize = 0; + let mut centre: Vec = vec![]; + + for i in 0..exzentrizitaeten.len() { + if exzentrizitaeten[i] > diameter { + diameter = exzentrizitaeten[i]; + } + if exzentrizitaeten[i] == radius { + centre.push(i + 1); + } + if exzentrizitaeten[i] < radius { + radius = exzentrizitaeten[i]; + centre.clear(); + centre.push(i + 1); + } + } + + let results: (usize, usize, Vec) = (radius, diameter, centre); + results +} diff --git a/src/graph/matrix.rs b/src/graph/matrix.rs new file mode 100644 index 0000000..91dce53 --- /dev/null +++ b/src/graph/matrix.rs @@ -0,0 +1,78 @@ +pub fn fill_with_random(matrix: &mut Vec>, size: usize) { + for i in 0..size { + matrix.push(vec![]); + for j in 0..size { + if i == j { + matrix[i].push(0); + continue; + } + matrix[i].push(fastrand::usize(0..2)); + } + } +} + +pub fn mult(matrix1: &Vec>, matrix2: &Vec>) -> Vec> { + let mut product: Vec> = vec![]; + let mut sum: usize; + + for i in 0..matrix1.len() { + product.push(vec![]); + for j in 0..matrix1.len() { + sum = 0; + for k in 0..matrix1.len() { + sum += matrix1[i][k] * matrix2[k][j]; + } + product[i].push(sum); + } + } + product +} + +pub fn show(matrix: &Vec>) { + for vector in matrix { + for int in vector { + print!("{int} "); + } + println!(); + } +} + +pub fn read_csv() -> Vec> { + let matrix: Vec> = vec![ + vec![0, 0, 1, 0, 0], + vec![0, 0, 1, 0, 1], + vec![1, 1, 0, 1, 0], + vec![0, 0, 0, 0, 1], + vec![1, 1, 1, 0, 0] + ]; + /* + See std::fs::File + */ + matrix +} + +pub fn test() { + let matrix1: Vec> = vec![ + vec![0, 0, 1, 0, 0], + vec![0, 0, 1, 0, 1], + vec![1, 1, 0, 1, 0], + vec![0, 0, 0, 0, 1], + vec![1, 1, 1, 0, 0] + ]; + let matrix2: Vec>; + + matrix2 = matrix1.clone(); + + println!("A:"); + show(&matrix1); + + let mut product: Vec> = mult(&matrix1, &matrix2); + + println!("\nA²:"); + show(&product); + + product = mult(&product, &matrix1); + + println!("\nA³:"); + show(&product); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..ac83f46 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,5 @@ +pub mod graph; + +fn main() { + graph::tests("graph"); +}