pub struct DisjointSet { /* private fields */ }Expand description
Union-Find (Disjoint Set Union) with path compression and union by rank.
Provides near-constant amortized time for find and union.
The amortized cost per operation is O(α(n)) where α is the inverse Ackermann function.
Implementations§
Source§impl DisjointSet
impl DisjointSet
Sourcepub fn new(n: usize) -> Self
pub fn new(n: usize) -> Self
Create a disjoint-set structure for n elements, each in its own set.
Sourcepub fn find(&mut self, x: usize) -> usize
pub fn find(&mut self, x: usize) -> usize
Find the representative (root) of the set containing element x.
Applies path compression for amortized efficiency.
Sourcepub fn union(&mut self, x: usize, y: usize) -> bool
pub fn union(&mut self, x: usize, y: usize) -> bool
Unite the sets containing x and y.
Returns true if they were in different sets, false if already united.
Uses union by rank to keep trees shallow.
Auto Trait Implementations§
impl Freeze for DisjointSet
impl RefUnwindSafe for DisjointSet
impl Send for DisjointSet
impl Sync for DisjointSet
impl Unpin for DisjointSet
impl UnsafeUnpin for DisjointSet
impl UnwindSafe for DisjointSet
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