Skip to main content

Disjoint set data structure implementation for Python

Project description

disjoint-set

PyPI - Python Version PyPI Coveralls PyPI - License

DisjointSet (a.k.a. union–find data structure or merge–find set) implementation for Python.

Prerequisites

The only requirement is using Python 3.8+. You can verify this by running:

$ python --version
Python 3.8.18

Installation

pip install disjoint-set

You can verify you're running the latest package version by running:

>>> import disjoint_set
>>> disjoint_set.__version__
'0.8.0'

Usage

Import & instantiate

>>> from disjoint_set import DisjointSet
>>> DisjointSet()
DisjointSet({})

>>> DisjointSet({1: 1})
DisjointSet({1: 1})

>>> DisjointSet.from_iterable([1,2,3])
DisjointSet({1: 1, 2: 2, 3: 3})

Perform find & union operations

>>> ds = DisjointSet()
>>> ds.find(1)
1

>>> ds.union(1,2)
>>> ds.find(1)
2

>>> ds.find(2)
2

Check if values belong to the same set

>>> ds = DisjointSet({1: 2, 2: 2, 3: 3})
>>> ds.connected(1,2)
True

>>> ds.connected(1,3)
False

Check if values are present within the data structure

>>> ds = DisjointSet()
>>> "a" in ds
False

>>> ds.find("a")
'a'

>>> "a" in ds
True

List elements and sets within the disjoint set

>>> ds = DisjointSet({1: 2, 2: 2, 3: 3})
>>> list(ds)
[(1, 2), (2, 2), (3, 3)]

>>> ds = DisjointSet({1: 2, 2: 2, 3: 3})
>>> list(ds.itersets())
[{1, 2}, {3}]

Contributing

Feel free to open any issues on github.

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

disjoint_set-0.8.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

disjoint_set-0.8.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file disjoint_set-0.8.0.tar.gz.

File metadata

  • Download URL: disjoint_set-0.8.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.18

File hashes

Hashes for disjoint_set-0.8.0.tar.gz
Algorithm Hash digest
SHA256 ce37119ca6be07671c4e51410a28511193ec168580ff373b0819d31b34b47728
MD5 240405f143d41fbc6f11fd0343477764
BLAKE2b-256 c0fe6fa6f037d6ca2b40485423cbfd53e4ed2dadfc8666d92982f18bc421de43

See more details on using hashes here.

File details

Details for the file disjoint_set-0.8.0-py3-none-any.whl.

File metadata

File hashes

Hashes for disjoint_set-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f802cd3cd03c651921bde18fa73d316c22104b09e5eab157e30ba322a7aeeae
MD5 6168d47cfef2a3b0e507d8889d69b9fe
BLAKE2b-256 a27daf80d6cc77c0e3506de6fc61c2c540e3bd7143474c30a9bd3047cc9496ee

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page