Giter Club home page Giter Club logo

disjoint_set's Introduction

ds:disjoint_set: c++14 disjoint-set data structure Actions Status

A simple implementation of disjoint-set data structure with union-by-size and path halving. Works with any integral, unsigned type that fits in a std::size_t.

A disjoint-set data structure, also known as union-find or merge-find data structure, can help you store and quary and manipulate a collection of non-overlaping (disjoin) subsets of a set.

For example, you can use a disjoint-set data structure to determine connected components of a graph: start from every node being in it's own set. For each edge in the network, merge (union) the two sets the two ends of the edge belong to. The final collection of sets each detemine a connected component.

Installation

This package relies on cmake. Make sure a moderatly recent version (3.14 or newer) is already installed. You should also have a C++ compiler with C++14 support. We regularly test this with GCC 8.4 so anything more recent should do.

Here is how to build and run the tests:

$ git clone https://github.com/arashbm/disjoint_set.git
$ cd disjoint_set
$ mkdir build  # make directory to build in
$ cd build
$ cmake ..
$ cmake --build . --target disjoint_set_tests  # build the tests
$ ./disjoint_set_tests

At this point you should see "All tests passed" if all the steps are successful. You can continue by install the library on your system:

cmake --build . --target install

This final step might require admin previlages. It basically just copies the include/ directoy to its proper location on your system, e.g. to /usr/include on linux. It also copies the cmake files require by the cmake command find_package to their proper location, e.g. to /usr/share.

Example

// in "example.cpp"

#include <iostream>
#include <ds/disjoint_set.hpp>

int main() {
  ds::disjoint_set<std::size_t> dis(10);

  dis.merge(1, 2);
  dis.merge(3, 2);
  dis.merge(0, 5);

  // get all the sets with their representative
  std::unordered_map<
    std::size_t,
    std::vector<std::size_t>> sets = dis.sets();
  std::cout << sets.size() << " disjoint sets" << std::endl;

  // get all the sets with their representative, except singleton sets
  auto sets_no_sing = dis.sets(false);
  std::cout << sets_no_sing.size() <<
    " sets without the singletons" << std::endl;

  // get representative of the set that 1 belongs to
  size_t rep = dis.rep(1);

  std::cout << "Members of the set containing 1:" << std::endl;
  for (auto&& i: sets[rep])
    std::cout << "- " << i << std::endl;

  return 0;
}

Assuming you cloned this library in /path/to/disjoint_set you can compile example.cpp with:

$ g++ -std=c++14 -I/path/to/disjoint_set/include -o example example.cpp

If you have installed the library as instructed previously, you skip -I option:

$ g++ -std=c++14 -o example example.cpp
$ ./example
7 disjoint sets
2 sets without the singletons
Members of the set containing 1:
- 1
- 2
- 3

Check out the tests (in src/disjoint_tests.cpp) for more examples.

disjoint_set's People

Contributors

arashbm avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  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.