Dsu

Struct Dsu 

Source
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

Source

pub fn new(n: usize) -> Self

Creates a new disjoint-set union data structure with n elements.

Source

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.

Source

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.

Source

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.

Source

pub fn root_size(&self, x: usize) -> usize

Returns the size of the set with root x.

This function will panic if x is not a root.

Source

pub fn roots(&self) -> impl Iterator<Item = usize> + '_

Returns an iterator over the roots of the disjoint-set.

Trait Implementations§

Source§

impl Debug for Dsu

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.