Giter VIP home page Giter VIP logo

dsu_data_structure's Introduction

DSU - disjoint-set-union

Info

See the disjoint set union data structure description at the link.

This implementation is built on sets and not references to parents. Therefore, the complexity of the operation is different from the article. Also added some useful methods.

Basic methods and their complexity:

  • same O(m) - check items from the same set;
  • union O(m + n) - union sets into one set based on their items;
  • make O(m) - make a new set for an item or separate this item from its set;
  • remove_items O(m) - remove an item from a structure;
  • remove_sets O(m + n) - remove a set from a structure based on its item.

Where:

  • m - number of method parameters (items), usually 1-2;
  • n - number of items in the DSU.

Additional methods allow:

  • get sets based on their items;
  • get all items from sets;
  • get a copy DSU;
  • clear the DSU of all items;
  • check the DSU for emptiness;
  • get number of items;
  • get string representation;
  • compare two structures DSU;
  • check whether an item is contained in one of the sets;
  • get iterator over all items.

Prerequisites

Language version starting from 3.10+

$ python --version
Python 3.10.4

Installation

pip install dsu_data_structure

Usage

Open the folder to view details.

>>> from dsu_data_structure import DSU

>>> dsu = DSU(range(6))
>>> print(dsu)
[{0}, {1}, {2}, {3}, {4}, {5}]

>>> print(dsu.items())
{0, 1, 2, 3, 4, 5}

>>> dsu.union(0, 1, 2, 3)
>>> print(dsu)
[{0, 1, 2, 3}, {4}, {5}]

>>> dsu.same(0, 2)
True
>>> dsu.same(0, 5)
False

>>> dsu.make(0, 10)
>>> print(dsu)
[{0}, {1, 2, 3}, {4}, {5}, {10}]

>>> dsu.remove_items(0, 2)
>>> print(dsu)
[{1, 3}, {4}, {5}, {10}]

>>> dsu.remove_sets(3)
>>> print(dsu)
[{4}, {5}, {10}]

Authors


License

MIT License - see the LICENSE file for details


dsu_data_structure's People

Contributors

d1mav0lk0v avatar

Watchers

 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.