Skip to main content

UnionFind

Struct UnionFind 

Source
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

Source

pub fn new(n: usize) -> Self

Creates a new Union-Find with n disjoint singleton sets {0}, {1}, ..., {n-1}.

§Complexity

O(n)

Source

pub fn len(&self) -> usize

Returns the number of elements.

Source

pub fn is_empty(&self) -> bool

Returns true if there are no elements.

Source

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

Finds the representative (root) of the set containing x.

Applies path compression: every node on the path from x to the root is made a direct child of the root.

§Complexity

Amortized O(α(n))

§Panics

Panics if x >= len().

Source

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().

Source

pub fn connected(&mut self, x: usize, y: usize) -> bool

Returns true if x and y are in the same set.

§Complexity

Amortized O(α(n))

Source

pub fn component_count(&self) -> usize

Returns the number of disjoint sets.

§Complexity

O(1)

Source

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

Returns the size of the set containing x.

§Complexity

Amortized O(α(n))

Trait Implementations§

Source§

impl Clone for UnionFind

Source§

fn clone(&self) -> UnionFind

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UnionFind

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V