Skip to main content

DisjointSet

Struct DisjointSet 

Source
pub struct DisjointSet { /* private fields */ }
Expand description

Kesishmaydigan to’plamlar strukturasi: elementlarni guruhlarga birlashtiradi va “bu ikkovi bir guruhdami?” savoliga deyarli O(1) da javob beradi.

G’oya: har bir guruh — daraxt, daraxtning ildizi guruh “vakili”. Ikki optimallashtirish uni juda tez qiladi:

  1. Path compression (find da): yo’ldagi hamma tugunni to’g’ridan-to’g’ri ildizga ulaymiz — keyingi so’rovlar bir qadamda tugaydi.
  2. Union by rank: kichik daraxtni kattasiga ulaymiz — daraxt bo’yi o’smaydi.

Ikkalasi birga — amortizatsiyalangan O(α(n)), bu yerda α — teskari Akkerman funksiyasi. Amalda α(n) ≤ 4 (koinotdagi atomlar soni uchun ham).

Qayerda ishlatiladi: Kruskal MST, tarmoqdagi bog’liq komponentalar, “do’stlar do’stim” tipidagi masalalar, rasm segmentatsiyasi.

§Misol

use rust_algorithms::data_structures::DisjointSet;

let mut ds = DisjointSet::new(5); // 0..4
ds.union(0, 1);
ds.union(3, 4);
assert!(ds.connected(0, 1));
assert!(!ds.connected(1, 3));
assert_eq!(ds.count(), 3);        // {0,1}, {2}, {3,4}

ds.union(1, 4);
assert!(ds.connected(0, 3));
assert_eq!(ds.count(), 2);

Implementations§

Source§

impl DisjointSet

Source

pub fn new(n: usize) -> Self

n ta alohida element (0..n) bilan boshlaydi.

Source

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

x ning guruh vakilini (ildizini) topadi — yo’lni siqish bilan.

Source

pub fn union(&mut self, a: usize, b: usize) -> bool

a va b guruhlarini birlashtiradi. Qaytadi: true — haqiqatan birlashtirildi, false — allaqachon bir guruhda edi.

Source

pub fn connected(&mut self, a: usize, b: usize) -> bool

a va b bir guruhdami?

Source

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

x turgan guruhdagi elementlar soni.

Source

pub fn count(&self) -> usize

Nechta alohida guruh bor.

Source

pub fn len(&self) -> usize

Umumiy elementlar soni.

Source

pub fn is_empty(&self) -> bool

Elementlar bormi?

Trait Implementations§

Source§

impl Clone for DisjointSet

Source§

fn clone(&self) -> DisjointSet

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for DisjointSet

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.