DisjointSet
Disjoint Set (Union-Find) Data Structure for Python
A generic, type-safe Disjoint Set Union (Union-Find) data structure.
📦 Installation
Install from PyPI:
pip install disjointsetunion
Import the main class:
from disjointset import DisjointSet
🚀 Quick Start
from disjointset import DisjointSet
class Person:
def __init__(self, name: str):
self.name = name
ali = Person("Ali")
bob = Person("Bob")
tom = Person("Tom")
dsu = DisjointSet[int | str | Person]()
# make_set and make_set_many ---------------------------------------------------
dsu.make_set(1)
# (1)
dsu.make_set("Ali")
# (1, "Ali")
dsu.make_set(ali)
# (1, "Ali", ali)
dsu.make_set_many([2, "Bob", bob, 3, "Tom", tom])
# (1, "Ali", ali, 2, "Bob", bob, 3, "Tom", tom)
# union and union_many ---------------------------------------------------------
dsu.union(1, "Ali")
# ({1, "Ali"}, ali, 2, "Bob", bob, 3, "Tom", tom)
dsu.union("Ali", ali)
# ({1, "Ali", ali}, 2, "Bob", bob, 3, "Tom", tom)
dsu.union_many([2, "Bob", bob])
# ({1, "Ali", ali}, {2, "Bob", bob}, 3, "Tom", tom)
dsu.union_many([3, "Tom", tom])
# ({1, "Ali", ali}, {2, "Bob", bob}, {3, "Tom", tom})
# same_set and same_set_many ---------------------------------------------------
print(dsu.same_set(1, ali))
# True
print(dsu.same_set("Ali", 2))
# False
print(dsu.same_set(2, bob))
# True
print(dsu.same_set("Bob", tom))
# False
print(dsu.same_set_many([2, "Bob", bob]))
# True
print(dsu.same_set_many([3, "Tom", tom, 1]))
# False
# get_element_count -------------------------------------------------------------
print(dsu.get_element_count())
# 9 elements (1, "Ali", ali, 2, "Bob", bob, 3, "Tom", tom)
# get_component_count ----------------------------------------------------------
print(dsu.get_component_count())
# 3 components ({1, "Ali", ali}, {2, "Bob", bob}, {3, "Tom", tom})
# get_component_size -----------------------------------------------------------
print(dsu.get_component_size(1))
# 3 (the size of the component containing 1, which is {1, "Ali", ali})
print(dsu.get_component_size("Bob"))
# 3 (the size of the component containing "Bob", which is {2, "Bob", bob})
print(dsu.get_component_size(tom))
# 3 (the size of the component containing tom, which is {3, "Tom", tom})
🔧 Features
Core Operations
make_set(x)find(x)union(x, y)same_set(x, y)
Batch Helpers
make_set_many(iterable)find_many(iterable)union_many(iterable)same_set_many(iterable)
Metadata Helpers
get_element_count()get_component_count()get_component_size(x)
Fully Typed
Supports any hashable type:
# int, float, complex
# bool
# tuple¹, range
# str
# bytes, memoryview²
# frozenset
dsu = DisjointSet[int | float | complex | bool | str]()
Notes:
¹ tuple is hashable if all contained elements are hashable.
² memoryview is hashable if the underlying buffer is read-only.
📂 Project Structure
src/
disjointset/
disjointset.py
stats/
main.py
tests/
disjointset/
test_disjointset.py
🧪 Testing
Run the full test suite:
pytest
📝 License
MIT License
🔗 Links
Metadata
Release files for disjointsetunion 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| disjointsetunion-1.2.0.tar.gz | 5.7 kB | Without provenance | Details for disjointsetunion-1.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| disjointsetunion-1.2.0-py3-none-any.whl | Python 3 | none | any | Details for disjointsetunion-1.2.0-py3-none-any.whl |
Total release size: 11.2 kB
Release files / disjointsetunion-1.2.0.tar.gz
| Download URL | disjointsetunion-1.2.0.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
54f7e5eddd0c3872613ca36789f6d95dcdbbb2c4221522aa95396f53ba58e077
|
|
BLAKE2b-256 checksum How to use checksums |
de70236a3b8029cbbe58384543d62b2ea48d34d03ceca570c49d508626bbd55a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / disjointsetunion-1.2.0-py3-none-any.whl
| Download URL | disjointsetunion-1.2.0-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
52c3a0f95b9d9e0a86ae0acb474fb644ab27117c5206fd1a74921ae708a37f68
|
|
BLAKE2b-256 checksum How to use checksums |
138399d461e1c34d291e28e0e39c5c13c11b46df70c9abcaf526c3cfa04d1e61
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for disjointsetunion 1.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.