From 8d81951dfb5236c932e5e75272cbec807ab9b5bf Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Sun, 25 Oct 2020 12:14:30 +0900 Subject: [PATCH 1/5] Update the documentation for `dsu` --- src/dsu.rs | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 121 insertions(+), 5 deletions(-) diff --git a/src/dsu.rs b/src/dsu.rs index 5809b9b..117433c 100644 --- a/src/dsu.rs +++ b/src/dsu.rs @@ -1,7 +1,49 @@ -/// Implement (union by size) + (path compression) -/// Reference: -/// Zvi Galil and Giuseppe F. Italiano, -/// Data structures and algorithms for disjoint set union problems +//! A Disjoint set union (DSU) with union by size and path compression. + +/// A Disjoint set union (DSU) with union by size and path compressione. +/// +/// See: [Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems](https://core.ac.uk/download/pdf/161439519.pdf) +/// +/// In the following documentation, let $n$ be size of the DSU. +/// +/// # Example +/// +/// ``` +/// use ac_library_rs::Dsu; +/// use proconio::{input, source::once::OnceSource}; +/// +/// input! { +/// from OnceSource::from( +/// "5\n\ +/// 3\n\ +/// 0 1\n\ +/// 2 3\n\ +/// 3 4\n", +/// ), +/// n: usize, +/// abs: [(usize, usize)], +/// } +/// +/// let mut dsu = Dsu::new(n); +/// for (a, b) in abs { +/// dsu.merge(a, b); +/// } +/// +/// assert!(dsu.same(0, 1)); +/// assert!(!dsu.same(1, 2)); +/// assert!(dsu.same(2, 4)); +/// +/// assert_eq!( +/// dsu.groups() +/// .into_iter() +/// .map(|mut group| { +/// group.sort_unstable(); +/// group +/// }) +/// .collect::>(), +/// [&[0, 1][..], &[2, 3, 4][..]], +/// ); +/// ``` pub struct Dsu { n: usize, // root node: -1 * component size @@ -10,13 +52,37 @@ pub struct Dsu { } impl Dsu { - // 0 <= size <= 10^8 is constrained. + /// Creates a new `Dsu`. + /// + /// # Constraints + /// + /// - $0 \leq n \leq 10^8$ + /// + /// # Complexity + /// + /// - $O(n)$ pub fn new(size: usize) -> Self { Self { n: size, parent_or_size: vec![-1; size], } } + + // `\textsc` does not work in KaTeX + /// Performs the `UNION` operation. + /// + /// # Constraints + /// + /// - $0 \leq a < n$ + /// - $0 \leq b < n$ + /// + /// # Panics + /// + /// Panics if the above constraints are not satisfied. + /// + /// # Complexity + /// + /// - $O(\alpha(n))$ amortized pub fn merge(&mut self, a: usize, b: usize) -> usize { assert!(a < self.n); assert!(b < self.n); @@ -32,11 +98,39 @@ impl Dsu { x } + /// Returns whether the vertices $a$ and $b$ are in the same connected component. + /// + /// # Constraints + /// + /// - $0 \leq a < n$ + /// - $0 \leq b < n$ + /// + /// # Panics + /// + /// Panics if the above constraint is not satisfied. + /// + /// # Complexity + /// + /// - $O(\alpha(n))$ amortized pub fn same(&mut self, a: usize, b: usize) -> bool { assert!(a < self.n); assert!(b < self.n); self.leader(a) == self.leader(b) } + + /// Performs the `FIND` operation. + /// + /// # Constraints + /// + /// - $0 \leq a < n$ + /// + /// # Panics + /// + /// Panics if the above constraint is not satisfied. + /// + /// # Complexity + /// + /// - $O(\alpha(n))$ amortized pub fn leader(&mut self, a: usize) -> usize { assert!(a < self.n); if self.parent_or_size[a] < 0 { @@ -45,11 +139,33 @@ impl Dsu { self.parent_or_size[a] = self.leader(self.parent_or_size[a] as usize) as i32; self.parent_or_size[a] as usize } + + /// Returns the size of the connected component that contains the vertex $a$. + /// + /// # Constraints + /// + /// - $0 \leq a < n$ + /// + /// # Panics + /// + /// Panics if the above constraint is not satisfied. + /// + /// # Complexity + /// + /// - $O(\alpha(n))$ amortized pub fn size(&mut self, a: usize) -> usize { assert!(a < self.n); let x = self.leader(a); -self.parent_or_size[x] as usize } + + /// Divides the graph into connected components. + /// + /// The result may not be ordered. + /// + /// # Complexity + /// + /// - $O(\alpha(n))$ amortized pub fn groups(&mut self) -> Vec> { let mut leader_buf = vec![0; self.n]; let mut group_size = vec![0; self.n]; From 09bb404c1a6ee36c58f3915e8aecfde3a8969233 Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Sun, 25 Oct 2020 13:41:21 +0900 Subject: [PATCH 2/5] Fix a typo --- src/dsu.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dsu.rs b/src/dsu.rs index 117433c..2857df1 100644 --- a/src/dsu.rs +++ b/src/dsu.rs @@ -1,6 +1,6 @@ //! A Disjoint set union (DSU) with union by size and path compression. -/// A Disjoint set union (DSU) with union by size and path compressione. +/// A Disjoint set union (DSU) with union by size and path compression. /// /// See: [Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems](https://core.ac.uk/download/pdf/161439519.pdf) /// From cdc10750d86fe2cdb054c1d8f474e17462907f8c Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Thu, 29 Oct 2020 19:59:02 +0900 Subject: [PATCH 3/5] Use "Latin Letter Small Capital" characters as substitutes for `\textsc` --- src/dsu.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dsu.rs b/src/dsu.rs index 2857df1..b5d4ad4 100644 --- a/src/dsu.rs +++ b/src/dsu.rs @@ -69,7 +69,7 @@ impl Dsu { } // `\textsc` does not work in KaTeX - /// Performs the `UNION` operation. + /// Performs the Uɴɪᴏɴ operation. /// /// # Constraints /// @@ -118,7 +118,7 @@ impl Dsu { self.leader(a) == self.leader(b) } - /// Performs the `FIND` operation. + /// Performs the Fɪɴᴅ operation. /// /// # Constraints /// From e5736c761ba5c28163003516bcc352a8a28f0d49 Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Sun, 6 Mar 2022 19:33:27 +0900 Subject: [PATCH 4/5] Fix a grammar error Co-authored-by: Hiroki Kobayashi <3303362+koba-e964@users.noreply.github.com> --- src/dsu.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dsu.rs b/src/dsu.rs index 7ccc6df..41db233 100644 --- a/src/dsu.rs +++ b/src/dsu.rs @@ -4,7 +4,7 @@ /// /// See: [Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems](https://core.ac.uk/download/pdf/161439519.pdf) /// -/// In the following documentation, let $n$ be size of the DSU. +/// In the following documentation, let $n$ be the size of the DSU. /// /// # Example /// From 3ec9768c3c720807279119d6cf1a7b39afdc6ffe Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Sun, 6 Mar 2022 19:34:58 +0900 Subject: [PATCH 5/5] Fix document Co-authored-by: Hiroki Kobayashi <3303362+koba-e964@users.noreply.github.com> --- src/dsu.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dsu.rs b/src/dsu.rs index 41db233..5e52880 100644 --- a/src/dsu.rs +++ b/src/dsu.rs @@ -165,7 +165,7 @@ impl Dsu { /// /// # Complexity /// - /// - $O(\alpha(n))$ amortized + /// - $O(n)$ pub fn groups(&mut self) -> Vec> { let mut leader_buf = vec![0; self.n]; let mut group_size = vec![0; self.n];