GithubHelp home page GithubHelp logo

jogru0 / disjoint Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 74 KB

Fast and safe implementation of the disjoint-set data structure.

Home Page: https://docs.rs/disjoint/latest/disjoint/index.html

License: Apache License 2.0

Rust 100.00%

disjoint's Introduction

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.

disjoint's People

Contributors

jogru0 avatar andriydev avatar

Stargazers

Matthew Archer avatar Rob Patro avatar

Watchers

 avatar  avatar

Forkers

andriydev

disjoint's Issues

all_sets

Hi,

I'm looking here as a replacement for the now defunct partitions.

Small thing first, in the docs it says

// Adding 'd', not joined to anything.
dsv.push('c'); // {'a', 'b'}, {'c'}

You are pushing c, not d.

But to the real question, is there a way to extract the sets, similar to the all_sets() in partitions?

Expose `root_of`.

Currently, there is no way to get the "index" of a group. That is, given two elements in the disjoint sets, there is no way to give an ambiguous index to the whole group. Of course internally, we know that there is an unambiguous index for a group - root_of. This is only "unambiguous" provided that no union occurs involving any elements of the group.

My use case

I have a triangulation of a polygon (with holes). I want to merge these triangles together into convex polygons (to make a navigation mesh). To do this I'm using something like the Hertel-Mehlhorn Algorithm.

Essentially:

  1. Sort all shared edges by their length (longest-first).
  2. for each edge, check if merging the polygons on either side would be convex. If so, merge those polygons.
  3. Repeat until no more edges left.

The way I'm doing this is I represent each triangle as an element in the disjoint sets. Whenever I merge two polygons, I union them in the disjoint sets. However, I still need to refer to the same polygon if I try to "dissolve" a different edge. So I need a way for the triangles that have been merged into a polygon to "refer" to the polygon itself. I can do this by always re-associating the polygon with the root_of after union-ing.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.