basically done with this
This commit is contained in:
parent
83c80f9b27
commit
10ec946b72
102
src/graph.rs
102
src/graph.rs
@ -1,15 +1,7 @@
|
|||||||
pub mod matrix;
|
pub mod matrix;
|
||||||
|
|
||||||
pub fn tests(arg: &str) {
|
|
||||||
match arg {
|
|
||||||
"matrix" => matrix::test(),
|
|
||||||
"graph" => test(),
|
|
||||||
&_ => println!("not a valid option"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn test() {
|
pub fn test() {
|
||||||
let adjazenz_matrix: Vec<Vec<usize>> = matrix::read_csv();
|
let mut adjazenz_matrix: Vec<Vec<usize>> = matrix::read_csv();
|
||||||
let distanz_matrix: Vec<Vec<usize>> = calculate_distanz_matrix(&adjazenz_matrix);
|
let distanz_matrix: Vec<Vec<usize>> = calculate_distanz_matrix(&adjazenz_matrix);
|
||||||
let weg_matrix: Vec<Vec<usize>> = calculate_weg_matrix(&adjazenz_matrix);
|
let weg_matrix: Vec<Vec<usize>> = calculate_weg_matrix(&adjazenz_matrix);
|
||||||
|
|
||||||
@ -26,9 +18,8 @@ pub fn test() {
|
|||||||
if exzentrizitaeten.contains(&0) {
|
if exzentrizitaeten.contains(&0) {
|
||||||
connected = false;
|
connected = false;
|
||||||
}
|
}
|
||||||
|
println!("\nis the graph connected: {connected}");
|
||||||
println!("\n{:?}", exzentrizitaeten);
|
println!("exzentrizitäten: {:?}", exzentrizitaeten);
|
||||||
println!("is the graph connected: {connected}");
|
|
||||||
|
|
||||||
let properties = calculate_properties(&exzentrizitaeten);
|
let properties = calculate_properties(&exzentrizitaeten);
|
||||||
let radius: usize = properties.0;
|
let radius: usize = properties.0;
|
||||||
@ -36,10 +27,18 @@ pub fn test() {
|
|||||||
let centre: Vec<usize> = properties.2;
|
let centre: Vec<usize> = properties.2;
|
||||||
|
|
||||||
println!("radius: {radius}\ndiameter: {diameter}\ncentre: {:?}", centre);
|
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>> {
|
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 distanz_matrix: Vec<Vec<usize>> = vec![];
|
||||||
let mut potenz_matrix = adjazenz_matrix.clone();
|
let mut potenz_matrix = adjazenz_matrix.clone();
|
||||||
|
|
||||||
@ -70,7 +69,6 @@ fn calculate_distanz_matrix(adjazenz_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn calculate_weg_matrix(adjazenz_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
|
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 weg_matrix: Vec<Vec<usize>> = vec![];
|
||||||
let mut potenz_matrix = adjazenz_matrix.clone();
|
let mut potenz_matrix = adjazenz_matrix.clone();
|
||||||
|
|
||||||
@ -139,3 +137,79 @@ fn calculate_properties(exzentrizitaeten: &Vec<usize>) -> (usize, usize, Vec<usi
|
|||||||
let results: (usize, usize, Vec<usize>) = (radius, diameter, centre);
|
let results: (usize, usize, Vec<usize>) = (radius, diameter, centre);
|
||||||
results
|
results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_components(weg_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
|
||||||
|
let mut components: Vec<Vec<usize>> = vec![];
|
||||||
|
let mut component: Vec<usize>;
|
||||||
|
|
||||||
|
for i in 0..weg_matrix.len() {
|
||||||
|
component = vec![];
|
||||||
|
|
||||||
|
for j in 0..weg_matrix.len() {
|
||||||
|
if weg_matrix[i][j] == 1 {
|
||||||
|
component.push(j + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !components.contains(&component) {
|
||||||
|
components.push(component);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
components
|
||||||
|
}
|
||||||
|
|
||||||
|
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>>;
|
||||||
|
let mut weg_matrix: Vec<Vec<usize>>;
|
||||||
|
let mut prev_value: usize;
|
||||||
|
|
||||||
|
for i in 0..adjazenz_matrix.len() {
|
||||||
|
for j in 0..adjazenz_matrix.len() {
|
||||||
|
if i == j {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
bridge = vec![];
|
||||||
|
bridge.push(usize::min(i + 1, j + 1));
|
||||||
|
bridge.push(usize::max(i + 1, j + 1));
|
||||||
|
|
||||||
|
prev_value = adjazenz_matrix[i][j];
|
||||||
|
adjazenz_matrix[i][j] = 0;
|
||||||
|
adjazenz_matrix[j][i] = 0;
|
||||||
|
|
||||||
|
weg_matrix = calculate_weg_matrix(&adjazenz_matrix);
|
||||||
|
new_components = find_components(&weg_matrix);
|
||||||
|
|
||||||
|
if new_components.len() > components.len() && !bridges.contains(&bridge) {
|
||||||
|
bridges.push(bridge);
|
||||||
|
}
|
||||||
|
adjazenz_matrix[i][j] = prev_value;
|
||||||
|
adjazenz_matrix[j][i] = prev_value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bridges
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
let mut weg_matrix: Vec<Vec<usize>>;
|
||||||
|
|
||||||
|
for n in 0..temp_matrix.len() {
|
||||||
|
for i in 0..temp_matrix.len() {
|
||||||
|
for j in 0..temp_matrix.len() {
|
||||||
|
temp_matrix[i][n] = 0;
|
||||||
|
temp_matrix[n][j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
weg_matrix = calculate_weg_matrix(&temp_matrix);
|
||||||
|
new_components = find_components(&weg_matrix);
|
||||||
|
|
||||||
|
if new_components.len() > (components.len() + 1) {
|
||||||
|
articulations.push(n + 1);
|
||||||
|
}
|
||||||
|
temp_matrix = adjazenz_matrix.clone();
|
||||||
|
}
|
||||||
|
articulations
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user