testing other algorithm

main
Antonio De Lucreziis 6 months ago
parent 607d2fc14b
commit e654535280

@ -2,7 +2,6 @@ use std::{
cell::RefCell, cell::RefCell,
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
rc::Rc, rc::Rc,
result,
}; };
#[derive(Debug)] #[derive(Debug)]
@ -11,6 +10,7 @@ pub struct AdjacencyGraph {
adjacencies: HashMap<String, HashSet<String>>, adjacencies: HashMap<String, HashSet<String>>,
} }
#[allow(dead_code)]
impl AdjacencyGraph { impl AdjacencyGraph {
pub fn new() -> Self { pub fn new() -> Self {
AdjacencyGraph { AdjacencyGraph {
@ -137,14 +137,17 @@ impl AdjacencyGraph {
while let Some(node) = stack.pop() { while let Some(node) = stack.pop() {
println!("New CC: {:?}", new_cc.borrow()); println!("New CC: {:?}", new_cc.borrow());
new_cc.borrow_mut().insert(node.clone());
if cc.contains_key(&node) { if cc.contains_key(&node) {
// merge the two connected components and go to the next node // merge the two connected components and go to the next node
let old_cc = cc.get(&node).unwrap(); let old_cc = cc.get(&node).unwrap();
println!("Merging {:?} with {:?}", new_cc.borrow(), old_cc.borrow()); println!(
"Merging {:?} with {:?} due to link to {:?}",
new_cc.borrow(),
old_cc.borrow(),
node
);
new_cc new_cc
.borrow_mut() .borrow_mut()
@ -157,6 +160,8 @@ impl AdjacencyGraph {
continue; continue;
} }
new_cc.borrow_mut().insert(node.clone());
if let Some(adjacencies) = self.get_adjacencies(&node) { if let Some(adjacencies) = self.get_adjacencies(&node) {
for adj in adjacencies { for adj in adjacencies {
stack.push(adj.clone()); stack.push(adj.clone());

@ -76,7 +76,8 @@ fn main() -> std::io::Result<()> {
); );
} }
let cc = graph.compute_ccs(); let cc = graph.compute_ccs_2();
println!("CCs: {:?}", cc);
println!("Number of connected components: {}", cc.len()); println!("Number of connected components: {}", cc.len());
} }
} }

Loading…
Cancel
Save