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:
- Path compression (
findda): yo’ldagi hamma tugunni to’g’ridan-to’g’ri ildizga ulaymiz — keyingi so’rovlar bir qadamda tugaydi. - 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
impl DisjointSet
Trait Implementations§
Source§impl Clone for DisjointSet
impl Clone for DisjointSet
Source§fn clone(&self) -> DisjointSet
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for DisjointSet
impl RefUnwindSafe for DisjointSet
impl Send for DisjointSet
impl Sync for DisjointSet
impl Unpin for DisjointSet
impl UnsafeUnpin for DisjointSet
impl UnwindSafe for DisjointSet
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
Mutably borrows from an owned value. Read more