pub struct DisjointSet<T: Clone + Hash + Eq> { /* private fields */ }Expand description
Disjoint Set (Union-Find) data structure
This data structure maintains a collection of disjoint sets and supports efficient union and find operations. It’s commonly used in clustering algorithms for tracking connected components.
§Examples
use scirs2_cluster::hierarchy::DisjointSet;
let mut ds = DisjointSet::new();
// Add some elements
ds.make_set(1);
ds.make_set(2);
ds.make_set(3);
ds.make_set(4);
// Union some sets
ds.union(1, 2);
ds.union(3, 4);
// Check connectivity
assert_eq!(ds.find(&1), ds.find(&2)); // 1 and 2 are connected
assert_eq!(ds.find(&3), ds.find(&4)); // 3 and 4 are connected
assert_ne!(ds.find(&1), ds.find(&3)); // 1 and 3 are in different setsImplementations§
Source§impl<T: Clone + Hash + Eq> DisjointSet<T>
impl<T: Clone + Hash + Eq> DisjointSet<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty disjoint set
§Examples
use scirs2_cluster::hierarchy::DisjointSet;
let ds: DisjointSet<i32> = DisjointSet::new();Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new disjoint set with a specified capacity
This can improve performance when you know approximately how many elements you’ll be adding.
§Arguments
capacity- Expected number of elements
Sourcepub fn find(&mut self, x: &T) -> Option<T>
pub fn find(&mut self, x: &T) -> Option<T>
Find the representative (root) of the set containing the given element
Uses path compression for optimization: all nodes on the path to the root are made to point directly to the root.
§Arguments
x- Element to find the representative for
§Returns
Some(representative)if the element exists in the structureNoneif the element doesn’t exist
§Examples
use scirs2_cluster::hierarchy::DisjointSet;
let mut ds = DisjointSet::new();
ds.make_set(1);
ds.make_set(2);
ds.union(1, 2);
let root1 = ds.find(&1).unwrap();
let root2 = ds.find(&2).unwrap();
assert_eq!(root1, root2); // Same representativeSourcepub fn union(&mut self, x: T, y: T) -> bool
pub fn union(&mut self, x: T, y: T) -> bool
Union two sets containing the given elements
Uses union by rank: the root of the tree with smaller rank becomes a child of the root with larger rank.
§Arguments
x- Element from first sety- Element from second set
§Returns
trueif the sets were successfully unioned (they were different sets)falseif the elements were already in the same set or don’t exist
§Examples
use scirs2_cluster::hierarchy::DisjointSet;
let mut ds = DisjointSet::new();
ds.make_set(1);
ds.make_set(2);
assert!(ds.union(1, 2)); // Successfully unioned
assert!(!ds.union(1, 2)); // Already in same setSourcepub fn connected(&mut self, x: &T, y: &T) -> bool
pub fn connected(&mut self, x: &T, y: &T) -> bool
Check if two elements are in the same set
§Arguments
x- First elementy- Second element
§Returns
trueif both elements exist and are in the same setfalseif they’re in different sets or don’t exist
§Examples
use scirs2_cluster::hierarchy::DisjointSet;
let mut ds = DisjointSet::new();
ds.make_set(1);
ds.make_set(2);
ds.make_set(3);
ds.union(1, 2);
assert!(ds.connected(&1, &2)); // Connected
assert!(!ds.connected(&1, &3)); // Not connectedSourcepub fn num_sets(&self) -> usize
pub fn num_sets(&self) -> usize
Get the number of disjoint sets
§Returns
The number of disjoint sets currently in the structure
§Examples
use scirs2_cluster::hierarchy::DisjointSet;
let mut ds = DisjointSet::new();
assert_eq!(ds.num_sets(), 0);
ds.make_set(1);
ds.make_set(2);
assert_eq!(ds.num_sets(), 2);
ds.union(1, 2);
assert_eq!(ds.num_sets(), 1);Sourcepub fn get_set_members(&mut self, x: &T) -> Option<Vec<T>>
pub fn get_set_members(&mut self, x: &T) -> Option<Vec<T>>
Get all elements in the same set as the given element
§Arguments
x- Element to find set members for
§Returns
Some(Vec<T>)containing all elements in the same setNoneif the element doesn’t exist
§Examples
use scirs2_cluster::hierarchy::DisjointSet;
let mut ds = DisjointSet::new();
ds.make_set(1);
ds.make_set(2);
ds.make_set(3);
ds.union(1, 2);
let set_members = ds.get_set_members(&1).unwrap();
assert_eq!(set_members.len(), 2);
assert!(set_members.contains(&1));
assert!(set_members.contains(&2));
assert!(!set_members.contains(&3));Sourcepub fn get_all_sets(&mut self) -> Vec<Vec<T>>
pub fn get_all_sets(&mut self) -> Vec<Vec<T>>
Get all disjoint sets as a vector of vectors
§Returns
A vector where each inner vector contains the elements of one set
§Examples
use scirs2_cluster::hierarchy::DisjointSet;
let mut ds = DisjointSet::new();
ds.make_set(1);
ds.make_set(2);
ds.make_set(3);
ds.union(1, 2);
let all_sets = ds.get_all_sets();
assert_eq!(all_sets.len(), 2); // Two disjoint setsTrait Implementations§
Source§impl<T: Clone + Clone + Hash + Eq> Clone for DisjointSet<T>
impl<T: Clone + Clone + Hash + Eq> Clone for DisjointSet<T>
Source§fn clone(&self) -> DisjointSet<T>
fn clone(&self) -> DisjointSet<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<T> Freeze for DisjointSet<T>
impl<T> RefUnwindSafe for DisjointSet<T>where
T: RefUnwindSafe,
impl<T> Send for DisjointSet<T>where
T: Send,
impl<T> Sync for DisjointSet<T>where
T: Sync,
impl<T> Unpin for DisjointSet<T>where
T: Unpin,
impl<T> UnwindSafe for DisjointSet<T>where
T: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.