+ connections
+ }
+
+ fn add_to_set((b1, b2): (usize, usize), circuits: &mut Vec<HashSet<usize>>) {
+ // if b1 is one of the circuits sets
+ // - if b2 is in one of the circuit sets
+ // -> push the union of the sets
+ // - else add b2 to the set
+ // if b2 is one :of the circuits sets
+ // - if b1 is in one of the circuit sets
+ // -> push the union of the sets
+ // - else add b1 to the set
+ // else add a new set (b1, b2)
+ if let Some(i) = circuits.iter().position(|s| s.contains(&b1)) {
+ if let Some(j) = circuits.iter().position(|s| s.contains(&b2)) {
+ circuits.push( circuits[i].union(&circuits[j]).copied().collect() );
+ circuits.remove(i.max(j));
+ if i != j {
+ circuits.remove(i.min(j));
+ }
+ } else {
+ circuits[i].insert(b2);
+ }
+ } else if let Some(i) = circuits.iter().position(|s| s.contains(&b2)) {
+ if let Some(j) = circuits.iter().position(|s| s.contains(&b1)) {
+ circuits.push( circuits[i].union(&circuits[j]).copied().collect() );
+ circuits.remove(i.max(j));
+ if i != j {
+ circuits.remove(i.min(j));
+ }
+ } else {
+ circuits[i].insert(b1);
+ }
+ } else {
+ let mut s: HashSet<usize> = HashSet::new();
+ s.insert(b1);
+ s.insert(b2);
+ circuits.push(s);
+ }
+ // sort by set length
+ circuits.sort_by_key(|s| std::cmp::Reverse(s.len()));
+ }