DisjointSet

Struct DisjointSet 

Source
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 sets

Implementations§

Source§

impl<T: Clone + Hash + Eq> DisjointSet<T>

Source

pub fn new() -> Self

Create a new empty disjoint set

§Examples
use scirs2_cluster::hierarchy::DisjointSet;
let ds: DisjointSet<i32> = DisjointSet::new();
Source

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
Source

pub fn make_set(&mut self, x: T)

Add a new element as its own singleton set

If the element already exists, this operation has no effect.

§Arguments
  • x - Element to add
§Examples
use scirs2_cluster::hierarchy::DisjointSet;
let mut ds = DisjointSet::new();
ds.make_set(42);
assert!(ds.contains(&42));
Source

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 structure
  • None if 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 representative
Source

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 set
  • y - Element from second set
§Returns
  • true if the sets were successfully unioned (they were different sets)
  • false if 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 set
Source

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

Check if two elements are in the same set

§Arguments
  • x - First element
  • y - Second element
§Returns
  • true if both elements exist and are in the same set
  • false if 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 connected
Source

pub fn contains(&self, x: &T) -> bool

Check if an element exists in the disjoint set

§Arguments
  • x - Element to check
§Returns
  • true if the element exists
  • false otherwise
Source

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);
Source

pub fn size(&self) -> usize

Get the total number of elements

§Returns

The total number of elements in all sets

Source

pub fn is_empty(&self) -> bool

Check if the disjoint set is empty

§Returns
  • true if no elements have been added
  • false otherwise
Source

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 set
  • None if 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));
Source

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 sets
Source

pub fn clear(&mut self)

Clear all elements from the disjoint set

§Examples
use scirs2_cluster::hierarchy::DisjointSet;
let mut ds = DisjointSet::new();
ds.make_set(1);
ds.make_set(2);

assert_eq!(ds.size(), 2);
ds.clear();
assert_eq!(ds.size(), 0);

Trait Implementations§

Source§

impl<T: Clone + Clone + Hash + Eq> Clone for DisjointSet<T>

Source§

fn clone(&self) -> DisjointSet<T>

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<T: Debug + Clone + Hash + Eq> Debug for DisjointSet<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Clone + Hash + Eq> Default for DisjointSet<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto 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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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