#disjoint-set #union-find #safe #merge-find

disjoint

Fast and safe implementation of the disjoint-set data structure

8 releases (breaking)

0.8.0 Jul 13, 2024
0.7.0 Feb 6, 2024
0.6.0 Apr 23, 2023
0.5.0 Apr 14, 2023
0.1.0 Apr 11, 2023

#290 in Data structures

Download history 111/week @ 2024-06-24 138/week @ 2024-07-01 190/week @ 2024-07-08 65/week @ 2024-07-15 128/week @ 2024-07-22 115/week @ 2024-07-29 1/week @ 2024-08-05 15/week @ 2024-08-12 25/week @ 2024-08-19 51/week @ 2024-08-26 163/week @ 2024-09-02 38/week @ 2024-09-09 45/week @ 2024-09-16 43/week @ 2024-09-23 43/week @ 2024-09-30 29/week @ 2024-10-07

162 downloads per month
Used in 5 crates (2 directly)

MIT/Apache

39KB
373 lines

disjoint

Tests Coverage Crate Docs

This crate provides fast disjoint-set data structure implementations in 100% safe Rust.

DisjointSet is a very lightweight disjoint-set data structure, with no additional data attached to the set elements. Use this if you manage the data associated to the elements yourself, and just want to keep track which elements are joined.

DisjointSetVec<T> combines a DisjointSet with a Vec<T>, so it manages contiguous data entries T and keeps track of which entries are joined. Use this if you want the disjoint-set data structure to contain some additional data T for each element.

Examples

Disjoint set data structures can be applied to find the minimal spanning forest of an undirected edge-weighted graph. Let's assume we work with the following graph interface:

    trait Edge : Copy {
        fn first_vertex(&self) -> usize;
        fn second_vertex(&self) -> usize;
    }
    
    trait Graph {
        type E : Edge;
        fn edges_ordered_by_weight(&self) -> Vec<Self::E>;
        fn number_vertices(&self) -> usize;
        fn new(edges: Vec<Self::E>) -> Self;
    }

Then it's very straight-forward to use the provided DisjointSet struct and its methods is_joined and join to implement Kruskal’s algorithm to find the minimum spanning forest.

use disjoint::DisjointSet;

fn minimum_spanning_forest<G : Graph>(graph: &G) -> G {
    let mut result_edges = Vec::new();
    let mut vertices = DisjointSet::new(graph.number_vertices());

    for edge in graph.edges_ordered_by_weight() {
        if !vertices.is_joined(edge.first_vertex(), edge.second_vertex()) {
            vertices.join(edge.first_vertex(), edge.second_vertex());
            result_edges.push(edge);
        }
    }
    
    Graph::new(result_edges)
}

We can even use the fact that join returns true if the elements have not been joined already, to further simplify the algorithm (this variation is sometimes called Quick-Union):

use disjoint::DisjointSet;

fn minimum_spanning_forest_quick_find<G : Graph>(graph: &G) -> G {
    let mut result_edges = Vec::new();
    let mut vertices = DisjointSet::new(graph.number_vertices());

    for edge in graph.edges_ordered_by_weight() {
        if vertices.join(edge.first_vertex(), edge.second_vertex()) {
            result_edges.push(edge);
        }
    }
    
    Graph::new(result_edges)
}

See the documentation for more details on how to use this crate.

Changelog

This crate maintains a changelog.

License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps