Crates.io | union-find-rs |
lib.rs | union-find-rs |
version | 0.2.1 |
source | src |
created_at | 2021-12-27 20:50:19.862812 |
updated_at | 2021-12-29 16:32:33.606498 |
description | Disjoint-set forest implementation to support the union-find algorithm in Rust |
homepage | |
repository | https://gitlab.com/rustychoi/union_find |
max_upload_size | |
id | 503873 |
size | 42,912 |
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
.
[dependencies]
union-find-rs = "^0.2"
.rs
file
use union_find_rs::prelude::*;
UnionFind
for the core operations of union-findDisjointSets
for an implementation of UnionFind
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>>())
);