Skip to content

Commit 8ec851b

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

File tree

1 file changed

+76
-5
lines changed

1 file changed

+76
-5
lines changed

src/dsu.rs

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
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).
2+
3+
/// A Disjoint set union (DSU).
4+
///
5+
/// Given an undirected graph, it processes the following queries in $O(\alpha(n))$ time (amortized).
6+
///
7+
/// - Edge addition
8+
/// - Deciding whether given two vertices are in the same connected component
9+
///
10+
/// Each connected component internally has a representative vertex.
11+
///
12+
/// When two connected components are merged by edge addition, one of the two representatives of these connected components becomes the representative of the new connected component.
13+
///
14+
/// In the following documentation, let $n$ be size of the DSU.
15+
// Implement (union by size) + (path compression)
16+
// Reference:
17+
// Zvi Galil and Giuseppe F. Italiano,
18+
// Data structures and algorithms for disjoint set union problems
519
pub struct Dsu {
620
n: usize,
721
// root node: -1 * component size
@@ -10,13 +24,32 @@ pub struct Dsu {
1024
}
1125

1226
impl Dsu {
13-
// 0 <= size <= 10^8 is constrained.
27+
/// Creates a new `Dsu`.
28+
///
29+
/// # Constraints
30+
///
31+
/// - $0 \leq n \leq 10^8$
32+
///
33+
/// # Complexity
34+
///
35+
/// - $O(n)$
1436
pub fn new(size: usize) -> Self {
1537
Self {
1638
n: size,
1739
parent_or_size: vec![-1; size],
1840
}
1941
}
42+
43+
/// Adds an edge $(a, b)$.
44+
///
45+
/// # Constraints
46+
///
47+
/// - $0 \leq a < n$
48+
/// - $0 \leq b < n$
49+
///
50+
/// # Complexity
51+
///
52+
/// - $O(\alpha(n))$ amortized
2053
pub fn merge(&mut self, a: usize, b: usize) -> usize {
2154
assert!(a < self.n);
2255
assert!(b < self.n);
@@ -32,11 +65,31 @@ impl Dsu {
3265
x
3366
}
3467

68+
/// Returns whether the vertices $a$ and $b$ are in the same connected component.
69+
///
70+
/// # Constraints
71+
///
72+
/// - $0 \leq a < n$
73+
/// - $0 \leq b < n$
74+
///
75+
/// # Complexity
76+
///
77+
/// - $O(\alpha(n))$ amortized
3578
pub fn same(&mut self, a: usize, b: usize) -> bool {
3679
assert!(a < self.n);
3780
assert!(b < self.n);
3881
self.leader(a) == self.leader(b)
3982
}
83+
84+
/// Returns the representative of the connected component that contains the vertex $a$.
85+
///
86+
/// # Constraints
87+
///
88+
/// - $0 \leq a < n$
89+
///
90+
/// # Complexity
91+
///
92+
/// - $O(\alpha(n))$ amortized
4093
pub fn leader(&mut self, a: usize) -> usize {
4194
assert!(a < self.n);
4295
if self.parent_or_size[a] < 0 {
@@ -45,11 +98,29 @@ impl Dsu {
4598
self.parent_or_size[a] = self.leader(self.parent_or_size[a] as usize) as i32;
4699
self.parent_or_size[a] as usize
47100
}
101+
102+
/// Returns the size of the connected component that contains the vertex $a$.
103+
///
104+
/// # Constraints
105+
///
106+
/// - $0 \leq a < n$
107+
///
108+
/// # Complexity
109+
///
110+
/// - $O(\alpha(n))$ amortized
48111
pub fn size(&mut self, a: usize) -> usize {
49112
assert!(a < self.n);
50113
let x = self.leader(a);
51114
-self.parent_or_size[x] as usize
52115
}
116+
117+
/// Divides the graph into connected components.
118+
///
119+
/// The result may not be ordered.
120+
///
121+
/// # Complexity
122+
///
123+
/// - $O(\alpha(n))$ amortized
53124
pub fn groups(&mut self) -> Vec<Vec<usize>> {
54125
let mut leader_buf = vec![0; self.n];
55126
let mut group_size = vec![0; self.n];

0 commit comments

Comments
 (0)