Code Monkey home page Code Monkey logo

unionfind's Introduction

UnionFind

Language License Build Status

This repository contains a single-file header-only C++17 disjoint-set structure with union-by-size and path compression.

Example:

#include <unionfind/unionfind.hpp>
auto main(int argc, char **argv)-> int
{
    // create a disjoint-set structure for 5 sets.
    unionfind::UnionFind uf{10};
    
    // merge sets 0 and 1
    uf.merge(0, 1);
    
    // merge sets 0 and 2
    uf.merge(0, 2);
    
    // find returns an std::optional which is std::nullopt if the passed argument is greater
    // than the number of elements with which the UnionFind structure was created.
    std::optional<std::size_t> zero_opt = uf.find(0);
    
    assert(uf.find(0) == uf.find(1));
    assert(uf.find(1) == uf.find(2));
    
    // if it is known that the passed argument is in 
    // a valid range UnionFind::findUnsafe can be used instead
    std::size_t zero = uf.findUnsafe(0);

    // UnionFind::numberOfSets returns the number of disjoint sets in the datastructure
    std::size_t sets = uf.numberOfSets();
    
    // uf contains 8 sets: {0, 1, 2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}
    assert(sets == 8);
    
    // it is also possible to call merge with any number of parameters
    uf.merge(5, 6, 7, 8, 9);
    
    // size_opt contains the number of elements the set in which 5 is an element of
    // if the passed argument is not an element size_opt would be std::nullopt
    std::optional<std::size_t> size_opt = uf.sizeOfSetContaining(5);
    assert(size_opt.value() == 5);
    
    // there also exists an unsave variant
    std::size_t size = uf.sizeOfSetContainingUnsafe(5);
    assert(size == 5);
}

unionfind's People

Contributors

darkwingmcquack avatar

Watchers

 avatar  avatar

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.