Initial commit
This commit is contained in:
parent
8dda0fc339
commit
95d6a18c16
34
Cargo.lock
generated
Normal file
34
Cargo.lock
generated
Normal file
@ -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",
|
||||||
|
]
|
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@ -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"
|
141
src/graph.rs
Normal file
141
src/graph.rs
Normal file
@ -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<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!("\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<usize> = properties.2;
|
||||||
|
|
||||||
|
println!("radius: {radius}\ndiameter: {diameter}\ncentre: {:?}", centre);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate_distanz_matrix(adjazenz_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
|
||||||
|
#[allow(unused_assignments)]
|
||||||
|
let mut distanz_matrix: Vec<Vec<usize>> = 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<usize>>) -> Vec<Vec<usize>> {
|
||||||
|
#[allow(unused_assignments)]
|
||||||
|
let mut weg_matrix: Vec<Vec<usize>> = 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<usize>>) -> Vec<usize> {
|
||||||
|
let mut exzentrizitaeten: Vec<usize> = 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, usize, Vec<usize>) {
|
||||||
|
let mut radius: usize = usize::MAX;
|
||||||
|
let mut diameter: usize = 0;
|
||||||
|
let mut centre: Vec<usize> = 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<usize>) = (radius, diameter, centre);
|
||||||
|
results
|
||||||
|
}
|
78
src/graph/matrix.rs
Normal file
78
src/graph/matrix.rs
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
pub fn fill_with_random(matrix: &mut Vec<Vec<usize>>, 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<Vec<usize>>, matrix2: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
|
||||||
|
let mut product: Vec<Vec<usize>> = 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<Vec<usize>>) {
|
||||||
|
for vector in matrix {
|
||||||
|
for int in vector {
|
||||||
|
print!("{int} ");
|
||||||
|
}
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_csv() -> Vec<Vec<usize>> {
|
||||||
|
let matrix: Vec<Vec<usize>> = 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<usize>> = 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<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);
|
||||||
|
}
|
5
src/main.rs
Normal file
5
src/main.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
pub mod graph;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
graph::tests("graph");
|
||||||
|
}
|
Reference in New Issue
Block a user