Expand description
Implementations of the disjoint-set forest data structure that supports the union-find algorithm.
§Background / Context
§Getting Started
use union_find::prelude::*;
to import everything necessary- see the trait
UnionFind
for the core operations of union-find - see the struct
DisjointSets
for an implementation ofUnionFind
§Example
use std::collections::HashSet;
use union_find_rs::prelude::*;
let mut sets: DisjointSets<usize> = DisjointSets::new();
sets.make_set(1).unwrap();
sets.make_set(4).unwrap();
sets.make_set(9).unwrap();
sets.union(&1, &4).unwrap();
// the disjoint sets as a vector of vectors
let as_vec: Vec<HashSet<usize>> = sets.into_iter().collect();
// there should be 2 disjoint sets, where one of them only contains `9` and the other one
// only contains `1` and `4`
assert_eq!(as_vec.len(), 2);
assert!(
as_vec
.iter()
.any(|set| set == &vec![9].into_iter().collect::<HashSet<usize>>())
);
assert!(
as_vec
.iter()
.any(|set| set == &vec![1, 4].into_iter().collect::<HashSet<usize>>())
);
Modules§
- disjoint_
sets - Contains
DisjointSets
which is the main implementation of the disjoint-set forest in order to support the union-find algorithm.DisjointSets
uses union-by-rank and path-compression to improve the algorithmic complexity. - prelude
- Imports everything necessary to get started.
use union_find::prelude::*;
provides easy access to various traits and structs you will need. - traits
- Traits that define the core operations needed to support the union-find algorithm.