uf_rush 0.2.1

A lock-free, thread-safe implementation of the Union-Find (Disjoint-Set) data structure.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::sync::atomic::{AtomicUsize, Ordering};

/// Constant defining the number of `rank` bits in a node represented as a `usize`.
const RANK_BITS: u32 = usize::BITS.ilog2();

/// Constant defining the number of `parent` bits in a node represented as a `usize`.
const PARENT_BITS: u32 = usize::BITS - RANK_BITS;

/// Maximum allowable size of a lock-free union-find data structure.
pub const MAX_SIZE: usize = usize::MAX >> RANK_BITS;

/// Thread-safe and lock-free implementation of a union-find (also known as disjoint set) data
/// structure.
///
/// This implementation is based on the algorithm presented in
///
/// > "Wait-free Parallel Algorithms for the Union-Find Problem" \
/// > by Richard J. Anderson and Heather Woll.
pub struct UFRush {
    /// List of nodes in the union-find structure, represented by atomic unsigned integers.
    nodes: Vec<AtomicUsize>,
}

/// Implementation block for the UFRush struct.
impl UFRush {
    /// Creates a new union-find data structure with a specified number of elements.
    ///
    /// # Arguments
    /// * `size` - Number of elements in the union-find structure.
    ///
    /// # Returns
    /// An instance of [`UFRush`].
    ///
    /// # Panics
    /// This method will panic if the `size` exceeds the [`MAX_SIZE`].
    pub fn new(size: usize) -> Self {
        assert!(size <= MAX_SIZE);

        Self {
            nodes: (0..size).map(AtomicUsize::new).collect(),
        }
    }

    /// Returns the total number of elements in the union-find structure.
    ///
    /// # Returns
    /// The total number of elements.
    pub fn size(&self) -> usize {
        self.nodes.len()
    }

    /// Determines whether the elements `x` and `y` belong to the same subset.
    ///
    /// # Arguments
    /// * `x` - The first element.
    /// * `y` - The second element.
    ///
    /// # Returns
    /// [`true`] if `x` and `y` belong to the same subset; [`false`] otherwise.
    ///
    /// # Panics
    /// This method will panic if `x` or `y` are out of bounds.
    ///
    /// # Note
    /// The same operation checks whether two elements belong to the same subset. In a sequential
    /// scenario, this operation could be considered redundant, as it can be constructed from a pair
    /// of find operations. However, when it comes to concurrent environments, providing same as a
    /// basic operation is crucial. This is because in such scenarios, the identifiers of subsets
    /// might change dynamically due to concurrent union operations, making it challenging to reliably
    /// determine if a pair of elements belong to the same subset solely based on the outcomes of
    /// individual `find` operations.
    pub fn same(&self, x: usize, y: usize) -> bool {
        loop {
            let x_rep = self.find(x);
            let y_rep = self.find(y);
            if x_rep == y_rep {
                return true;
            }
            let x_node = self.nodes[x_rep].load(Ordering::Relaxed);
            if x_rep == parent(x_node) {
                return false;
            }
        }
    }

    /// Finds the representative of the subset that `x` belongs to.
    ///
    /// # Arguments
    /// * `x` - The element to find the representative for.
    ///
    /// # Returns
    /// The representative element of the subset that contains `x`.
    ///
    /// # Panics
    /// This method will panic if `x` is out of bounds.
    ///
    /// # Note
    /// The find operation uses the "path halving" technique, an intermediate strategy between full
    /// path compression and no compression at all.
    ///
    /// In the path halving technique, instead of making every node in the path point directly to the
    /// root as in full path compression, we only change the parent of every other node in the path to
    /// point to its grandparent. This is achieved by skipping over the parent node on each iteration
    /// during the find operation. Despite not fully compressing the path, this strategy is still
    /// effective in flattening the tree structure over time, thus accelerating future operations.
    ///
    /// The advantage of path halving is that it achieves a good balance between the speed of the find
    /// operation and the amount of modification it makes to the tree structure, avoiding a potential
    /// slowdown due to excessively frequent writes in highly concurrent scenarios. Therefore, it is
    /// particularly suitable for lock-free data structures like [`UFRush`], where minimizing
    /// write contention is crucial for performance.
    pub fn find(&self, mut x: usize) -> usize {
        assert!(x < self.size());

        let mut x_node = self.nodes[x].load(Ordering::Relaxed);
        while x != parent(x_node) {
            let x_parent = parent(x_node);
            let x_parent_node = self.nodes[x_parent].load(Ordering::Relaxed);
            let x_parent_parent = parent(x_parent_node);

            let x_new_node = encode(x_parent_parent, rank(x_node));
            let _ = self.nodes[x].compare_exchange_weak(
                x_node,
                x_new_node,
                Ordering::Release,
                Ordering::Relaxed,
            );

            x = x_parent_parent;
            x_node = self.nodes[x].load(Ordering::Relaxed);
        }
        x
    }

    /// Unites the subsets that contain `x` and `y`.
    ///
    /// If `x` and `y` are already in the same subset, no action is performed.
    ///
    /// # Arguments
    /// * `x` - The first element.
    /// * `y` - The second element.
    ///
    /// # Returns
    /// [`true`] if `x` and `y` were in different subsets and a union operation was performed;
    /// [`false`] if `x` and `y` were already in the same subset.
    ///
    /// # Panics
    /// This method will panic if `x` or `y` are out of bounds.
    ///
    /// # Note
    /// The unite operation utilizes a Union-Find algorithm that adopts the "union by rank"
    /// strategy for its union operation.
    ///
    /// In "union by rank", each node holds a rank, and when two sets are united, the set with the
    /// smaller rank becomes a subset of the set with the larger rank. If both sets have the same
    /// rank, either one can become a subset of the other, but the rank of the new root is incremented
    /// by one. This strategy ensures that the tree representing the set does not become excessively
    /// deep, which helps keep the operation's time complexity nearly constant.
    pub fn unite(&self, x: usize, y: usize) -> bool {
        loop {
            // Load representative for x and y
            let mut x_rep = self.find(x);
            let mut y_rep = self.find(y);

            // If they are already part of the same set, return false
            if x_rep == y_rep {
                return false;
            }

            // Load the encoded representation of the representatives
            let x_node = self.nodes[x_rep].load(Ordering::Relaxed);
            let y_node = self.nodes[y_rep].load(Ordering::Relaxed);

            let mut x_rank = rank(x_node);
            let mut y_rank = rank(y_node);

            // Swap the elements around to always make x the smaller one
            if x_rank > y_rank || (x_rank == y_rank && x_rep > y_rep) {
                std::mem::swap(&mut x_rep, &mut y_rep);
                std::mem::swap(&mut x_rank, &mut y_rank);
            }

            // x_rep is a root
            let cur_value = encode(x_rep, x_rank);
            // assign the new root to be y
            let new_value = encode(y_rep, x_rank);
            // change the value of the smaller subtree root to point to the other one
            if self.nodes[x_rep]
                .compare_exchange(cur_value, new_value, Ordering::Release, Ordering::Acquire)
                .is_ok()
            {
                // x_repr now points to y_repr
                // If the subtrees has the same height, increase the rank of the new root
                if x_rank == y_rank {
                    let cur_value = encode(y_rep, y_rank);
                    let new_value = encode(y_rep, y_rank + 1);
                    let _ = self.nodes[y_rep].compare_exchange_weak(
                        cur_value,
                        new_value,
                        Ordering::Release,
                        Ordering::Relaxed,
                    );
                }
                return true;
            }
            // A different thread has already merged modified the value of x_repr -> repeat
        }
    }

    /// Clears the union-find structure, making every element a separate subset.
    pub fn clear(&mut self) {
        self.nodes
            .iter_mut()
            .enumerate()
            .for_each(|(i, node)| node.store(i, Ordering::Relaxed));
    }
}

/// This unsafe implementation indicate that [`UFRush`] can safely be shared
/// across threads (`Sync`).
unsafe impl Sync for UFRush {}

/// This unsafe implementation indicate that [`UFRush`] is safe to transfer
/// the ownership between threads (`Send`).
unsafe impl Send for UFRush {}

/// Encodes the parent node and rank into a single `usize`.
fn encode(parent: usize, rank: usize) -> usize {
    parent | (rank << PARENT_BITS)
}

/// Retrieves the parent node from an encoded `usize`.
fn parent(n: usize) -> usize {
    n & MAX_SIZE
}

/// Retrieves the rank from an encoded `usize`.
fn rank(n: usize) -> usize {
    n >> PARENT_BITS
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::prelude::*;
    use std::collections::HashSet;
    use std::sync::Arc;
    use std::thread;

    #[test]
    fn test_new() {
        let uf = UFRush::new(10);
        assert_eq!(uf.size(), 10);
    }

    #[test]
    fn test_find() {
        let uf = UFRush::new(10);
        assert_eq!(uf.find(5), 5);
    }

    #[test]
    fn test_same() {
        let uf = UFRush::new(10);
        assert!(!uf.same(1, 2));
    }

    #[test]
    fn test_unite() {
        let uf = UFRush::new(10);
        assert!(!uf.same(1, 2));
        assert!(uf.unite(1, 2));
        assert!(uf.same(1, 2));
    }

    #[test]
    fn test_unite_already_united() {
        let uf = UFRush::new(10);
        assert!(uf.unite(1, 2));
        assert!(!uf.unite(1, 2));
    }

    #[test]
    fn test_clear() {
        let mut uf = UFRush::new(10);
        assert!(uf.unite(1, 2));
        assert!(uf.same(1, 2));
        uf.clear();
        assert!(!uf.same(1, 2));
    }

    #[test]
    fn test_multithreaded_build_cyclic_graph() {
        let vertices = 100;
        let uf = Arc::new(UFRush::new(vertices));

        // Spawn threads, one thread per nodes
        let handles: Vec<_> = (0..vertices)
            .map(|n| {
                let uf = Arc::clone(&uf);
                thread::spawn(move || {
                    // Unite the current node with the next one, creating a cycle
                    uf.unite(n, (n + 1) % vertices);
                })
            })
            .collect();

        // Wait for all threads to finish
        for handle in handles {
            handle.join().unwrap();
        }

        // Check results - all nodes should be in the same subset
        for n in 0..vertices - 1 {
            assert!(uf.same(n, (n + 1) % vertices));
        }
    }

    #[test]
    fn test_multithreaded_cyclic_graph() {
        assert!(is_cyclic(3, [(0, 1), (1, 2), (2, 0)]));
    }

    #[test]
    fn test_multithreaded_acyclic_graph() {
        assert!(!is_cyclic(4, [(0, 1), (1, 2), (2, 3)]));
    }

    #[test]
    fn stress_test() {
        let vertices = 100;
        let mut edges = HashSet::with_capacity(5 * vertices);
        let mut rng = rand::thread_rng();

        // Add edges to form a cycle
        edges.extend((0..vertices).map(|n| (n, (n + 1) % vertices)));

        // Add some extra random edges
        for _ in edges.len()..edges.capacity() {
            let u = rng.gen_range(0..vertices);
            let v = rng.gen_range(0..vertices);
            if u != v {
                edges.insert((u, v));
            }
        }

        // Validate the number of successful unite actions for some random arrangement of edges
        let mut edges: Vec<_> = edges.into_iter().collect();
        for _ in 0..100 {
            let uf = Arc::new(UFRush::new(vertices));

            // Spawn threads, one thread per edge
            let handles: Vec<_> = edges
                .iter()
                .map(|&(u, v)| {
                    let uf = Arc::clone(&uf);
                    thread::spawn(move || uf.unite(u, v))
                })
                .collect();

            // Wait for all threads to finish; count the number of united edges
            let total_united = handles
                .into_iter()
                .map(|handle| handle.join().unwrap())
                .filter(|&united| united)
                .count();

            // Ensure the number of successful unite actions is as expected
            assert_eq!(total_united, vertices - 1);

            // Shuffle the edges for the next iteration
            edges.shuffle(&mut rng);
        }
    }

    fn is_cyclic<I>(vertices: usize, edges: I) -> bool
    where
        I: IntoIterator<Item = (usize, usize)>,
    {
        let uf = Arc::new(UFRush::new(vertices));

        // Spawn threads, one thread per edge
        let handles: Vec<_> = edges
            .into_iter()
            .map(|(u, v)| {
                let uf = Arc::clone(&uf);
                thread::spawn(move || {
                    if uf.same(u, v) {
                        // If two nodes are in the same set, we've found a cycle
                        true
                    } else {
                        // Otherwise, unite them and continue
                        uf.unite(u, v);
                        false
                    }
                })
            })
            .collect();

        // Wait for all threads to finish and return if there was any cycle
        handles
            .into_iter()
            .map(|handle| handle.join().unwrap())
            .any(|cyclic| cyclic)
    }
}