pub struct UnionFind { /* private fields */ }Expand description
Disjoint-set forest with path compression and union by rank.
§Examples
use u_numflow::collections::UnionFind;
let mut uf = UnionFind::new(5);
assert_eq!(uf.component_count(), 5);
uf.union(0, 1);
uf.union(2, 3);
assert_eq!(uf.component_count(), 3);
assert!(uf.connected(0, 1));
assert!(!uf.connected(0, 2));
uf.union(1, 3);
assert!(uf.connected(0, 2)); // transitivity
assert_eq!(uf.component_count(), 2);Implementations§
Source§impl UnionFind
impl UnionFind
Sourcepub fn union(&mut self, x: usize, y: usize) -> bool
pub fn union(&mut self, x: usize, y: usize) -> bool
Merges the sets containing x and y.
Uses union by rank: the tree with smaller rank is attached under the root of the tree with larger rank.
§Returns
true if x and y were in different sets (and are now merged),
false if they were already in the same set.
§Complexity
Amortized O(α(n))
§Panics
Panics if x >= len() or y >= len().
Sourcepub fn component_count(&self) -> usize
pub fn component_count(&self) -> usize
Sourcepub fn component_size(&mut self, x: usize) -> usize
pub fn component_size(&mut self, x: usize) -> usize
Trait Implementations§
Auto Trait Implementations§
impl Freeze for UnionFind
impl RefUnwindSafe for UnionFind
impl Send for UnionFind
impl Sync for UnionFind
impl Unpin for UnionFind
impl UnsafeUnpin for UnionFind
impl UnwindSafe for UnionFind
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more