Skip to content

Commit 8935673

Browse files
authored
fix Dsu::leader to avoid multiple assert checks (#175)
1 parent eb19cd5 commit 8935673

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/dsu.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,7 @@ impl Dsu {
134134
/// - $O(\alpha(n))$ amortized
135135
pub fn leader(&mut self, a: usize) -> usize {
136136
assert!(a < self.n);
137-
if self.parent_or_size[a] < 0 {
138-
return a;
139-
}
140-
self.parent_or_size[a] = self.leader(self.parent_or_size[a] as usize) as i32;
141-
self.parent_or_size[a] as usize
137+
self._leader(a)
142138
}
143139

144140
/// Returns the size of the connected component that contains the vertex $a$.
@@ -186,6 +182,14 @@ impl Dsu {
186182
.filter(|x| !x.is_empty())
187183
.collect::<Vec<Vec<usize>>>()
188184
}
185+
186+
fn _leader(&mut self, a: usize) -> usize {
187+
if self.parent_or_size[a] < 0 {
188+
return a;
189+
}
190+
self.parent_or_size[a] = self._leader(self.parent_or_size[a] as usize) as i32;
191+
self.parent_or_size[a] as usize
192+
}
189193
}
190194

191195
#[cfg(test)]

0 commit comments

Comments
 (0)