pub struct UnionFind<K> { /* private fields */ }
Expand description
UnionFind<K>
is a disjoint-set data structure. It tracks set membership of n elements
indexed from 0 to n - 1. The scalar type is K
which must be an unsigned integer type.
http://en.wikipedia.org/wiki/Disjoint-set_data_structure
Too awesome not to quote:
“The amortized time per operation is O(α(n)) where α(n) is the inverse of f(x) = A(x, x) with A being the extremely fast-growing Ackermann function.”
Implementations§
Source§impl<K> UnionFind<K>where
K: IndexType,
impl<K> UnionFind<K>where
K: IndexType,
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if there are no elements in the union-find data-structure.
Sourcepub fn new_set(&mut self) -> K
pub fn new_set(&mut self) -> K
Adds a new disjoint set and returns the index of the new set.
The new disjoint set is always added to the end, so the returned index is the same as the number of elements before calling this function.
Time Complexity Takes amortized O(1) time.
Sourcepub fn try_find(&self, x: K) -> Option<K>
pub fn try_find(&self, x: K) -> Option<K>
Return the representative for x
or None
if x
is out of bounds.
Sourcepub fn find_mut(&mut self, x: K) -> K
pub fn find_mut(&mut self, x: K) -> K
Return the representative for x
.
Write back the found representative, flattening the internal datastructure in the process and quicken future lookups.
Panics if x
is out of bounds.
Sourcepub fn try_find_mut(&mut self, x: K) -> Option<K>
pub fn try_find_mut(&mut self, x: K) -> Option<K>
Return the representative for x
or None
if x
is out of bounds.
Write back the found representative, flattening the internal datastructure in the process and quicken future lookups.
Sourcepub fn equiv(&self, x: K, y: K) -> bool
pub fn equiv(&self, x: K, y: K) -> bool
Returns true
if the given elements belong to the same set, and returns
false
otherwise.
Panics if x
or y
is out of bounds.
Sourcepub fn try_equiv(&self, x: K, y: K) -> Result<bool, K>
pub fn try_equiv(&self, x: K, y: K) -> Result<bool, K>
Returns Ok(true)
if the given elements belong to the same set, and returns
Ok(false)
otherwise.
If x
or y
are out of bounds, it returns Err
with the first bad index found.
Sourcepub fn union(&mut self, x: K, y: K) -> bool
pub fn union(&mut self, x: K, y: K) -> bool
Unify the two sets containing x
and y
.
Return false
if the sets were already the same, true
if they were unified.
Panics if x
or y
is out of bounds.
Sourcepub fn try_union(&mut self, x: K, y: K) -> Result<bool, K>
pub fn try_union(&mut self, x: K, y: K) -> Result<bool, K>
Unify the two sets containing x
and y
.
Return Ok(false)
if the sets were already the same, Ok(true)
if they were unified.
If x
or y
are out of bounds, it returns Err
with first found bad index.
But if x == y
, the result will be Ok(false)
even if the indexes go out of bounds.
Sourcepub fn into_labeling(self) -> Vec<K>
pub fn into_labeling(self) -> Vec<K>
Return a vector mapping each element to its representative.
Source§impl<K> UnionFind<K>
impl<K> UnionFind<K>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Constructs a new, empty UnionFind<K>
with at least the specified capacity.
This acts similarly to Vec::with_capacity
.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the total number of elements the UnionFind
can hold without reallocating.
§Examples
use petgraph::unionfind::UnionFind;
let mut uf: UnionFind<u32> = UnionFind::with_capacity(10);
uf.new_set();
assert!(uf.capacity() >= 10);
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more elements to be inserted
in the given UnionFind<K>
. The collection may reserve more space to
speculatively avoid frequent reallocations. After calling reserve
,
capacity will be greater than or equal to self.len() + additional
.
Does nothing if capacity is already sufficient.
§Panics
Panics if the new capacity exceeds isize::MAX
bytes.
§Examples
use petgraph::unionfind::UnionFind;
let mut uf: UnionFind<u32> = UnionFind::new(3);
uf.reserve(10);
assert!(uf.capacity() >= 13);
Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for at least additional
more elements to
be inserted in the given UnionFind<K>
. Unlike reserve
, this will not
deliberately over-allocate to speculatively avoid frequent allocations.
After calling reserve_exact
, capacity will be greater than or equal to
self.len() + additional
. Does nothing if the capacity is already
sufficient.
Note that the allocator may give the collection more space than it
requests. Therefore, capacity can not be relied upon to be precisely
minimal. Prefer reserve
if future insertions are expected.
§Panics
Panics if the new capacity exceeds isize::MAX
bytes.
§Examples
use petgraph::unionfind::UnionFind;
let mut uf: UnionFind<u32> = UnionFind::new_empty();
uf.reserve_exact(10);
assert!(uf.capacity() >= 10);
Sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
Tries to reserve capacity for at least additional
more elements to be inserted
in the given UnionFind<K>
. The collection may reserve more space to speculatively avoid
frequent reallocations. After calling try_reserve
, capacity will be
greater than or equal to self.len() + additional
if it returns
Ok(())
. Does nothing if capacity is already sufficient. This method
preserves the contents even if an error occurs.
§Errors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
Sourcepub fn try_reserve_exact(
&mut self,
additional: usize,
) -> Result<(), TryReserveError>
pub fn try_reserve_exact( &mut self, additional: usize, ) -> Result<(), TryReserveError>
Tries to reserve the minimum capacity for at least additional
elements to be inserted in the given UnionFind<K>
. Unlike try_reserve
,
this will not deliberately over-allocate to speculatively avoid frequent
allocations. After calling try_reserve_exact
, capacity will be greater
than or equal to self.len() + additional
if it returns Ok(())
.
Does nothing if the capacity is already sufficient.
Note that the allocator may give the collection more space than it
requests. Therefore, capacity can not be relied upon to be precisely
minimal. Prefer try_reserve
if future insertions are expected.
§Errors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the UnionFind
as much as possible.
The behavior of this method depends on the allocator, which may either shrink the
collection in-place or reallocate. The resulting UnionFind
might still have some excess capacity, just as
is the case for with_capacity
. See Vec::shrink_to_fit
for more details, since the implementation is based on this method.
§Examples
use petgraph::unionfind::UnionFind;
let mut uf: UnionFind<u32> = UnionFind::with_capacity(10);
for _ in 0..3 {
uf.new_set();
}
assert!(uf.capacity() >= 10);
uf.shrink_to_fit();
assert!(uf.capacity() >= 3);
Sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the capacity of the UnionFind
with a lower bound.
The capacity will remain at least as large as both the length and the supplied value.
If the current capacity is less than the lower limit, this is a no-op.
§Examples
use petgraph::unionfind::UnionFind;
let mut uf: UnionFind<u32> = UnionFind::with_capacity(10);
for _ in 0..3 {
uf.new_set();
}
assert!(uf.capacity() >= 10);
uf.shrink_to(4);
assert!(uf.capacity() >= 4);
uf.shrink_to(0);
assert!(uf.capacity() >= 3);