pub struct Dsu { /* private fields */ }Expand description
Disjoint-set union (DSU) data structure.
§Example
let mut dsu = Dsu::new(5);
for i in 0..5 {
assert_eq!(dsu.find(i), i);
assert_eq!(dsu.set_size(i), 1);
}
assert!(dsu.union(0, 1));
assert!(dsu.union(1, 2));
assert!(!dsu.union(0, 2));
assert!(dsu.union(3, 4));
assert_eq!(dsu.find(0), dsu.find(1));
assert_eq!(dsu.find(0), dsu.find(2));
assert_eq!(dsu.set_size(0), 3);
assert_eq!(dsu.find(3), dsu.find(4));
assert_eq!(dsu.set_size(3), 2);
assert_ne!(dsu.find(0), dsu.find(3));
assert!(dsu.union(1, 3));
assert_eq!(dsu.set_size(0), 5);
for i in 1..5 {
assert_eq!(dsu.find(0), dsu.find(i));
}Implementations§
Source§impl Dsu
impl Dsu
Sourcepub fn find(&mut self, x: usize) -> usize
pub fn find(&mut self, x: usize) -> usize
Find the root of the set containing the element at index x.
This requires &mut self because it performs path compression, making later calls faster.
Sourcepub fn union(&mut self, a: usize, b: usize) -> bool
pub fn union(&mut self, a: usize, b: usize) -> bool
Merges the sets containing index a and b.
Returns true if the sets were merged, false if they were already in the same set.
Sourcepub fn set_size(&mut self, x: usize) -> usize
pub fn set_size(&mut self, x: usize) -> usize
Returns the size of the set containing the element at index x.
This requires &mut self because it performs path compression, making later calls faster.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Dsu
impl RefUnwindSafe for Dsu
impl Send for Dsu
impl Sync for Dsu
impl Unpin for Dsu
impl UnwindSafe for Dsu
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