diff --git a/src/graph.rs b/src/graph.rs index 790d894..1a8ab17 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -99,8 +99,7 @@ pub fn calculate_properties(exzentrizitaeten: &Vec) -> (usize, usize, Vec connected = false; } } - let results: (usize, usize, Vec, bool) = (radius, diameter, centre, connected); - results + (radius, diameter, centre, connected) } pub fn find_components(weg_matrix: &Vec>) -> Vec> { @@ -122,52 +121,45 @@ pub fn find_components(weg_matrix: &Vec>) -> Vec> { components } -pub fn find_bridges(adjazenz_matrix: &mut Vec>, components: &Vec>) -> Vec> { +pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec>, components: &Vec>) -> (Vec,Vec>) { let mut bridges: Vec> = vec![]; let mut bridge: Vec; - let mut new_components: Vec>; - let mut weg_matrix: Vec>; 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 -} - -pub fn find_articulations(adjazenz_matrix: &Vec>, components: &Vec>) -> Vec { let mut articulations: Vec = vec![]; let mut new_components: Vec>; let mut temp_matrix: Vec> = adjazenz_matrix.clone(); let mut weg_matrix: Vec>; + let mut done: bool = false; 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; + + if done || 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; } } + done = true; + weg_matrix = calculate_weg_matrix(&temp_matrix); new_components = find_components(&weg_matrix); @@ -176,5 +168,6 @@ pub fn find_articulations(adjazenz_matrix: &Vec>, components: &Vec = calculate_exzentrizitaeten(&distanz_matrix); let properties = calculate_properties(&exzentrizitaeten); let components: Vec> = find_components(&weg_matrix); + let result = find_articulations_and_bridges(&mut adjazenz_matrix, &components); assert_eq!(adjazenz_matrix, vec![ vec![0, 0, 1, 1, 0], @@ -40,8 +41,8 @@ mod tests { assert_eq!(properties.2, vec![4]); assert_eq!(properties.3, true); assert_eq!(components, vec![vec![1, 2, 3, 4, 5]]); - assert_eq!(find_bridges(&mut adjazenz_matrix, &components), vec![vec![4, 5]]); - assert_eq!(find_articulations(&adjazenz_matrix, &components), vec![4]); + assert_eq!(result.1, vec![vec![4, 5]]); + assert_eq!(result.0, vec![4]); } #[test] diff --git a/src/main.rs b/src/main.rs index 6c0a4ca..e87d2df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,24 @@ +use crate::graph::{*, matrix::*}; + pub mod graph; pub fn main() { - let file_name = String::from("50n.csv"); + let file_name = String::from("art-brck.csv"); - let mut adjazenz_matrix: Vec> = graph::matrix::read_csv(file_name); - let distanz_matrix: Vec> = graph::calculate_distanz_matrix(&adjazenz_matrix); - let weg_matrix: Vec> = graph::calculate_weg_matrix(&adjazenz_matrix); + let mut adjazenz_matrix: Vec> = read_csv(file_name); + //let mut adjazenz_matrix: Vec> = fill_with_random(100); + let distanz_matrix: Vec> = calculate_distanz_matrix(&adjazenz_matrix); + let weg_matrix: Vec> = calculate_weg_matrix(&adjazenz_matrix); println!("adjazen matrix:"); - graph::matrix::show(&adjazenz_matrix); + show(&adjazenz_matrix); println!("\ndistanz matrix:"); - graph::matrix::show(&distanz_matrix); + show(&distanz_matrix); println!("\nweg matrix:"); - graph::matrix::show(&weg_matrix); + show(&weg_matrix); - let exzentrizitaeten = graph::calculate_exzentrizitaeten(&distanz_matrix); - let properties = graph::calculate_properties(&exzentrizitaeten); + let exzentrizitaeten = calculate_exzentrizitaeten(&distanz_matrix); + let properties = calculate_properties(&exzentrizitaeten); if properties.3 { println!("\nexzentrizitäten: {:?}", exzentrizitaeten); @@ -25,8 +28,10 @@ pub fn main() { println!("radius/diameter/centre: not connected"); } - let components: Vec> = graph::find_components(&weg_matrix); + let components: Vec> = find_components(&weg_matrix); println!("components: {:?}", components); - println!("bridges: {:?}", graph::find_bridges(&mut adjazenz_matrix, &components)); - println!("articulations: {:?}", graph::find_articulations(&adjazenz_matrix, &components)); + + let result = find_articulations_and_bridges(&mut adjazenz_matrix, &components); + println!("bridges: {:?}", result.1); + println!("articulations: {:?}", result.0); }