15 releases

Uses old Rust 2015

0.4.2 May 30, 2018
0.4.1 May 30, 2018
0.3.5 May 30, 2018
0.3.4 Jan 30, 2018
0.1.1 Jun 12, 2016

#444 in Data structures

Download history 255/week @ 2023-12-16 269/week @ 2023-12-23 237/week @ 2023-12-30 456/week @ 2024-01-06 499/week @ 2024-01-13 478/week @ 2024-01-20 520/week @ 2024-01-27 516/week @ 2024-02-03 722/week @ 2024-02-10 624/week @ 2024-02-17 561/week @ 2024-02-24 804/week @ 2024-03-02 701/week @ 2024-03-09 632/week @ 2024-03-16 654/week @ 2024-03-23 623/week @ 2024-03-30

2,730 downloads per month
Used in 10 crates (9 directly)

MIT/Apache

48KB
826 lines

disjoint-sets: three union-find implementations

Build Status Crates.io License: MIT License: Apache 2.0

The variants are:

structure element type data? concurrent?
UnionFind vector small integer no no
UnionFindNode tree tree node yes no
AUnionFind array usize no yes

All three perform rank-balanced path compression à la Tarjan, using interior mutability.

Usage

It’s on crates.io, so add this to your Cargo.toml:

[dependencies]
disjoint-sets = "0.4.2"

And add this to your crate root:

extern crate disjoint_sets;

This crate supports Rust version 1.15 and later.

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)
    };
}

Dependencies

~180KB