union_find
Implementations of the disjoint-set forest data structure that supports the union-find algorithm.
This crate focuses on ease of use and simplicity.
Specify union-find-rs
as a dependency in your Cargo.toml
.
toml
[dependencies]
union-find-rs = "^0.2"
.rs
file
rust
use union_find_rs::prelude::*;
UnionFind
for the core operations of union-findDisjointSets
for an implementation of UnionFind
```rust use std::collections::HashSet; use unionfindrs::prelude::*;
let mut sets: DisjointSets
sets.makeset(1).unwrap(); sets.makeset(4).unwrap(); sets.make_set(9).unwrap();
sets.union(&1, &4).unwrap();
// the disjoint sets as a vector of vectors
let asvec: Vec
// there should be 2 disjoint sets, where one of them only contains 9
and the other one
// only contains 1
and 4
asserteq!(asvec.len(), 2);
assert!(
asvec
.iter()
.any(|set| set == &vec![9].intoiter().collect::