Skip to content

Commit 8d81951

Browse files
committed
Update the documentation for dsu
1 parent 19509cd commit 8d81951

File tree

1 file changed

+121
-5
lines changed

1 file changed

+121
-5
lines changed

src/dsu.rs

+121-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
1-
/// Implement (union by size) + (path compression)
2-
/// Reference:
3-
/// Zvi Galil and Giuseppe F. Italiano,
4-
/// Data structures and algorithms for disjoint set union problems
1+
//! A Disjoint set union (DSU) with union by size and path compression.
2+
3+
/// A Disjoint set union (DSU) with union by size and path compressione.
4+
///
5+
/// See: [Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems](https://core.ac.uk/download/pdf/161439519.pdf)
6+
///
7+
/// In the following documentation, let $n$ be size of the DSU.
8+
///
9+
/// # Example
10+
///
11+
/// ```
12+
/// use ac_library_rs::Dsu;
13+
/// use proconio::{input, source::once::OnceSource};
14+
///
15+
/// input! {
16+
/// from OnceSource::from(
17+
/// "5\n\
18+
/// 3\n\
19+
/// 0 1\n\
20+
/// 2 3\n\
21+
/// 3 4\n",
22+
/// ),
23+
/// n: usize,
24+
/// abs: [(usize, usize)],
25+
/// }
26+
///
27+
/// let mut dsu = Dsu::new(n);
28+
/// for (a, b) in abs {
29+
/// dsu.merge(a, b);
30+
/// }
31+
///
32+
/// assert!(dsu.same(0, 1));
33+
/// assert!(!dsu.same(1, 2));
34+
/// assert!(dsu.same(2, 4));
35+
///
36+
/// assert_eq!(
37+
/// dsu.groups()
38+
/// .into_iter()
39+
/// .map(|mut group| {
40+
/// group.sort_unstable();
41+
/// group
42+
/// })
43+
/// .collect::<Vec<_>>(),
44+
/// [&[0, 1][..], &[2, 3, 4][..]],
45+
/// );
46+
/// ```
547
pub struct Dsu {
648
n: usize,
749
// root node: -1 * component size
@@ -10,13 +52,37 @@ pub struct Dsu {
1052
}
1153

1254
impl Dsu {
13-
// 0 <= size <= 10^8 is constrained.
55+
/// Creates a new `Dsu`.
56+
///
57+
/// # Constraints
58+
///
59+
/// - $0 \leq n \leq 10^8$
60+
///
61+
/// # Complexity
62+
///
63+
/// - $O(n)$
1464
pub fn new(size: usize) -> Self {
1565
Self {
1666
n: size,
1767
parent_or_size: vec![-1; size],
1868
}
1969
}
70+
71+
// `\textsc` does not work in KaTeX
72+
/// Performs the `UNION` operation.
73+
///
74+
/// # Constraints
75+
///
76+
/// - $0 \leq a < n$
77+
/// - $0 \leq b < n$
78+
///
79+
/// # Panics
80+
///
81+
/// Panics if the above constraints are not satisfied.
82+
///
83+
/// # Complexity
84+
///
85+
/// - $O(\alpha(n))$ amortized
2086
pub fn merge(&mut self, a: usize, b: usize) -> usize {
2187
assert!(a < self.n);
2288
assert!(b < self.n);
@@ -32,11 +98,39 @@ impl Dsu {
3298
x
3399
}
34100

101+
/// Returns whether the vertices $a$ and $b$ are in the same connected component.
102+
///
103+
/// # Constraints
104+
///
105+
/// - $0 \leq a < n$
106+
/// - $0 \leq b < n$
107+
///
108+
/// # Panics
109+
///
110+
/// Panics if the above constraint is not satisfied.
111+
///
112+
/// # Complexity
113+
///
114+
/// - $O(\alpha(n))$ amortized
35115
pub fn same(&mut self, a: usize, b: usize) -> bool {
36116
assert!(a < self.n);
37117
assert!(b < self.n);
38118
self.leader(a) == self.leader(b)
39119
}
120+
121+
/// Performs the `FIND` operation.
122+
///
123+
/// # Constraints
124+
///
125+
/// - $0 \leq a < n$
126+
///
127+
/// # Panics
128+
///
129+
/// Panics if the above constraint is not satisfied.
130+
///
131+
/// # Complexity
132+
///
133+
/// - $O(\alpha(n))$ amortized
40134
pub fn leader(&mut self, a: usize) -> usize {
41135
assert!(a < self.n);
42136
if self.parent_or_size[a] < 0 {
@@ -45,11 +139,33 @@ impl Dsu {
45139
self.parent_or_size[a] = self.leader(self.parent_or_size[a] as usize) as i32;
46140
self.parent_or_size[a] as usize
47141
}
142+
143+
/// Returns the size of the connected component that contains the vertex $a$.
144+
///
145+
/// # Constraints
146+
///
147+
/// - $0 \leq a < n$
148+
///
149+
/// # Panics
150+
///
151+
/// Panics if the above constraint is not satisfied.
152+
///
153+
/// # Complexity
154+
///
155+
/// - $O(\alpha(n))$ amortized
48156
pub fn size(&mut self, a: usize) -> usize {
49157
assert!(a < self.n);
50158
let x = self.leader(a);
51159
-self.parent_or_size[x] as usize
52160
}
161+
162+
/// Divides the graph into connected components.
163+
///
164+
/// The result may not be ordered.
165+
///
166+
/// # Complexity
167+
///
168+
/// - $O(\alpha(n))$ amortized
53169
pub fn groups(&mut self) -> Vec<Vec<usize>> {
54170
let mut leader_buf = vec![0; self.n];
55171
let mut group_size = vec![0; self.n];

0 commit comments

Comments
 (0)