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
//! Three union-find implementations //! //! The variants are: //! //! - [`UnionFind`](struct.UnionFind.html): An array-based union-find //! where clients represent elements as small unsigned integers. //! //! - [`UnionFindNode`](struct.UnionFindNode.html): A tree-based //! union-find where each set can have associated data, and where //! clients represent elements as opaque tree nodes. //! //! - [`AUnionFind`](struct.AUnionFind.html): Like `UnionFind`, but //! it’s `Sync` for sharing between threads. (I’m not an expert on //! concurrent programming, so I would take this API with a grain of //! salt.) //! //! All three perform rank-balanced path compression à la Tarjan, //! using interior mutability. //! //! # Usage //! //! It’s [on crates.io](https://crates.io/crates/disjoint-sets), so add //! this to your `Cargo.toml`: //! //! ```toml //! [dependencies] //! disjoint-sets = "*" //! ``` //! //! And add this to your crate root: //! //! ``` //! extern crate disjoint_sets; //! ``` //! //! # Examples //! //! Kruskal’s algorithm to find the minimum spanning tree of a graph: //! //! ``` //! use disjoint_sets::UnionFind; //! //! type Node = usize; //! type Weight = usize; //! //! struct Edge { //! dst: Node, //! weight: Weight, //! } //! //! type Graph = Vec<Vec<Edge>>; //! //! fn edges_by_weight(graph: &Graph) -> Vec<(Node, Node, Weight)> { //! let mut edges = vec![]; //! //! for (src, dsts) in graph.iter().enumerate() { //! for edge in dsts { //! edges.push((src, edge.dst, edge.weight)); //! } //! } //! //! edges.sort_by_key(|&(_, _, weight)| weight); //! edges //! } //! //! fn mst(graph: &Graph) -> Vec<(Node, Node)> { //! let mut result = vec![]; //! let mut uf = UnionFind::new(graph.len()); //! //! for (src, dst, _) in edges_by_weight(graph) { //! if !uf.equiv(src, dst) { //! uf.union(src, dst); //! result.push((src, dst)); //! } //! } //! //! result //! } //! //! fn main() { //! // Graph to use: //! // //! // 0 ------ 1 ------ 2 //! // | 6 | 5 | //! // | 8 | 1 | 4 //! // | | | //! // 3 ------ 4 ------ 5 //! // | 7 | 2 | //! // | 3 | 12 | 11 //! // | | | //! // 6 ------ 7 ------ 8 //! // 9 10 //! let graph = vec![ //! // Node 0 //! vec![ Edge { dst: 1, weight: 6 }, //! Edge { dst: 3, weight: 8 }, ], //! // Node 1 //! vec![ Edge { dst: 2, weight: 5 }, //! Edge { dst: 4, weight: 1 }, ], //! // Node 2 //! vec![ Edge { dst: 5, weight: 4 }, ], //! // Node 3 //! vec![ Edge { dst: 4, weight: 7 }, //! Edge { dst: 6, weight: 3 }, ], //! // Node 4 //! vec![ Edge { dst: 5, weight: 2 }, //! Edge { dst: 7, weight: 12 }, ], //! // Node 5 //! vec![ Edge { dst: 8, weight: 11 }, ], //! // Node 6 //! vec![ Edge { dst: 7, weight: 9 }, ], //! // Node 7 //! vec![ Edge { dst: 8, weight: 10 }, ], //! // Node 8 //! vec![ ], //! ]; //! //! assert_eq! { //! vec![ (1, 4), (4, 5), (3, 6), (2, 5), //! (0, 1), (3, 4), (6, 7), (7, 8), ], //! mst(&graph) //! }; //! } //! ``` #![warn(missing_docs)] #![cfg_attr(feature = "clippy", feature(plugin))] #![cfg_attr(feature = "clippy", plugin(clippy))] mod traits; mod array; mod tree; mod async; pub use traits::*; pub use array::*; pub use tree::*; pub use async::*;