minor optimization
This commit is contained in:
parent
466bfcd102
commit
42b2e37765
61
src/graph.rs
61
src/graph.rs
@ -99,8 +99,7 @@ pub fn calculate_properties(exzentrizitaeten: &Vec<usize>) -> (usize, usize, Vec
|
|||||||
connected = false;
|
connected = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let results: (usize, usize, Vec<usize>, bool) = (radius, diameter, centre, connected);
|
(radius, diameter, centre, connected)
|
||||||
results
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_components(weg_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
|
pub fn find_components(weg_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
|
||||||
@ -122,52 +121,45 @@ pub fn find_components(weg_matrix: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
|
|||||||
components
|
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 bridges: Vec<Vec<usize>> = vec![];
|
||||||
let mut bridge: Vec<usize>;
|
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 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<Vec<usize>>, components: &Vec<Vec<usize>>) -> Vec<usize> {
|
|
||||||
let mut articulations: Vec<usize> = vec![];
|
let mut articulations: Vec<usize> = vec![];
|
||||||
let mut new_components: Vec<Vec<usize>>;
|
let mut new_components: Vec<Vec<usize>>;
|
||||||
let mut temp_matrix: Vec<Vec<usize>> = adjazenz_matrix.clone();
|
let mut temp_matrix: Vec<Vec<usize>> = adjazenz_matrix.clone();
|
||||||
let mut weg_matrix: Vec<Vec<usize>>;
|
let mut weg_matrix: Vec<Vec<usize>>;
|
||||||
|
let mut done: bool = false;
|
||||||
|
|
||||||
for n in 0..temp_matrix.len() {
|
for n in 0..temp_matrix.len() {
|
||||||
for i in 0..temp_matrix.len() {
|
for i in 0..temp_matrix.len() {
|
||||||
for j in 0..temp_matrix.len() {
|
for j in 0..temp_matrix.len() {
|
||||||
temp_matrix[i][n] = 0;
|
temp_matrix[i][n] = 0;
|
||||||
temp_matrix[n][j] = 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);
|
weg_matrix = calculate_weg_matrix(&temp_matrix);
|
||||||
new_components = find_components(&weg_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();
|
temp_matrix = adjazenz_matrix.clone();
|
||||||
}
|
}
|
||||||
articulations
|
|
||||||
|
(articulations, bridges)
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ mod tests {
|
|||||||
let exzentrizitaeten: Vec<usize> = calculate_exzentrizitaeten(&distanz_matrix);
|
let exzentrizitaeten: Vec<usize> = calculate_exzentrizitaeten(&distanz_matrix);
|
||||||
let properties = calculate_properties(&exzentrizitaeten);
|
let properties = calculate_properties(&exzentrizitaeten);
|
||||||
let components: Vec<Vec<usize>> = find_components(&weg_matrix);
|
let components: Vec<Vec<usize>> = find_components(&weg_matrix);
|
||||||
|
let result = find_articulations_and_bridges(&mut adjazenz_matrix, &components);
|
||||||
|
|
||||||
assert_eq!(adjazenz_matrix, vec![
|
assert_eq!(adjazenz_matrix, vec![
|
||||||
vec![0, 0, 1, 1, 0],
|
vec![0, 0, 1, 1, 0],
|
||||||
@ -40,8 +41,8 @@ mod tests {
|
|||||||
assert_eq!(properties.2, vec![4]);
|
assert_eq!(properties.2, vec![4]);
|
||||||
assert_eq!(properties.3, true);
|
assert_eq!(properties.3, true);
|
||||||
assert_eq!(components, vec![vec![1, 2, 3, 4, 5]]);
|
assert_eq!(components, vec![vec![1, 2, 3, 4, 5]]);
|
||||||
assert_eq!(find_bridges(&mut adjazenz_matrix, &components), vec![vec![4, 5]]);
|
assert_eq!(result.1, vec![vec![4, 5]]);
|
||||||
assert_eq!(find_articulations(&adjazenz_matrix, &components), vec![4]);
|
assert_eq!(result.0, vec![4]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
29
src/main.rs
29
src/main.rs
@ -1,21 +1,24 @@
|
|||||||
|
use crate::graph::{*, matrix::*};
|
||||||
|
|
||||||
pub mod graph;
|
pub mod graph;
|
||||||
|
|
||||||
pub fn main() {
|
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 mut adjazenz_matrix: Vec<Vec<usize>> = read_csv(file_name);
|
||||||
let distanz_matrix: Vec<Vec<usize>> = graph::calculate_distanz_matrix(&adjazenz_matrix);
|
//let mut adjazenz_matrix: Vec<Vec<usize>> = fill_with_random(100);
|
||||||
let weg_matrix: Vec<Vec<usize>> = graph::calculate_weg_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);
|
||||||
|
|
||||||
println!("adjazen matrix:");
|
println!("adjazen matrix:");
|
||||||
graph::matrix::show(&adjazenz_matrix);
|
show(&adjazenz_matrix);
|
||||||
println!("\ndistanz matrix:");
|
println!("\ndistanz matrix:");
|
||||||
graph::matrix::show(&distanz_matrix);
|
show(&distanz_matrix);
|
||||||
println!("\nweg matrix:");
|
println!("\nweg matrix:");
|
||||||
graph::matrix::show(&weg_matrix);
|
show(&weg_matrix);
|
||||||
|
|
||||||
let exzentrizitaeten = graph::calculate_exzentrizitaeten(&distanz_matrix);
|
let exzentrizitaeten = calculate_exzentrizitaeten(&distanz_matrix);
|
||||||
let properties = graph::calculate_properties(&exzentrizitaeten);
|
let properties = calculate_properties(&exzentrizitaeten);
|
||||||
|
|
||||||
if properties.3 {
|
if properties.3 {
|
||||||
println!("\nexzentrizitäten: {:?}", exzentrizitaeten);
|
println!("\nexzentrizitäten: {:?}", exzentrizitaeten);
|
||||||
@ -25,8 +28,10 @@ pub fn main() {
|
|||||||
println!("radius/diameter/centre: not connected");
|
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!("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