Disjoint set data structure implementation for Python
Project description
disjoint-set
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
Release history Release notifications | RSS feed
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)
Built Distribution
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce37119ca6be07671c4e51410a28511193ec168580ff373b0819d31b34b47728 |
|
MD5 | 240405f143d41fbc6f11fd0343477764 |
|
BLAKE2b-256 | c0fe6fa6f037d6ca2b40485423cbfd53e4ed2dadfc8666d92982f18bc421de43 |
File details
Details for the file disjoint_set-0.8.0-py3-none-any.whl
.
File metadata
- Download URL: disjoint_set-0.8.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.8.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f802cd3cd03c651921bde18fa73d316c22104b09e5eab157e30ba322a7aeeae |
|
MD5 | 6168d47cfef2a3b0e507d8889d69b9fe |
|
BLAKE2b-256 | a27daf80d6cc77c0e3506de6fc61c2c540e3bd7143474c30a9bd3047cc9496ee |