minor optimization
This commit is contained in:
parent
466bfcd102
commit
42b2e37765
41
src/graph.rs
41
src/graph.rs
@ -99,8 +99,7 @@ pub fn calculate_properties(exzentrizitaeten: &Vec<usize>) -> (usize, usize, Vec
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
let results: (usize, usize, Vec<usize>, bool) = (radius, diameter, centre, connected);
|
||||
results
|
||||
(radius, diameter, centre, connected)
|
||||
}
|
||||
|
||||
pub fn find_components(weg_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
|
||||
@ -122,16 +121,23 @@ pub fn find_components(weg_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
|
||||
components
|
||||
}
|
||||
|
||||
pub fn find_bridges(adjazenz_matrix: &mut Vec<Vec<usize>>, components: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
|
||||
pub fn find_articulations_and_bridges(adjazenz_matrix: &mut Vec<Vec<usize>>, components: &Vec<Vec<usize>>) -> (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;
|
||||
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>>;
|
||||
let mut done: bool = false;
|
||||
|
||||
for i in 0..adjazenz_matrix.len() {
|
||||
for j in 0..adjazenz_matrix.len() {
|
||||
if i == j {
|
||||
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![];
|
||||
@ -152,22 +158,8 @@ pub fn find_bridges(adjazenz_matrix: &mut Vec<Vec<usize>>, components: &Vec<Vec<
|
||||
adjazenz_matrix[j][i] = prev_value;
|
||||
}
|
||||
}
|
||||
bridges
|
||||
}
|
||||
done = true;
|
||||
|
||||
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();
|
||||
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);
|
||||
|
||||
@ -176,5 +168,6 @@ pub fn find_articulations(adjazenz_matrix: &Vec<Vec<usize>>, components: &Vec<Ve
|
||||
}
|
||||
temp_matrix = adjazenz_matrix.clone();
|
||||
}
|
||||
articulations
|
||||
|
||||
(articulations, bridges)
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ mod tests {
|
||||
let exzentrizitaeten: Vec<usize> = calculate_exzentrizitaeten(&distanz_matrix);
|
||||
let properties = calculate_properties(&exzentrizitaeten);
|
||||
let components: Vec<Vec<usize>> = 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]
|
||||
|
29
src/main.rs
29
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<Vec<usize>> = graph::matrix::read_csv(file_name);
|
||||
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 mut adjazenz_matrix: Vec<Vec<usize>> = read_csv(file_name);
|
||||
//let mut adjazenz_matrix: Vec<Vec<usize>> = fill_with_random(100);
|
||||
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:");
|
||||
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<Vec<usize>> = graph::find_components(&weg_matrix);
|
||||
let components: Vec<Vec<usize>> = 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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user