Skip to main content

Disjoint Set - Data Structure Implementation

Project description

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

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

disjointsetunion-1.2.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

disjointsetunion-1.2.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file disjointsetunion-1.2.0.tar.gz.

File metadata

  • Download URL: disjointsetunion-1.2.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for disjointsetunion-1.2.0.tar.gz
Algorithm Hash digest
SHA256 54f7e5eddd0c3872613ca36789f6d95dcdbbb2c4221522aa95396f53ba58e077
MD5 b2ff4aa03619d3a6b8c5a7fb17676a36
BLAKE2b-256 de70236a3b8029cbbe58384543d62b2ea48d34d03ceca570c49d508626bbd55a

See more details on using hashes here.

File details

Details for the file disjointsetunion-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for disjointsetunion-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 52c3a0f95b9d9e0a86ae0acb474fb644ab27117c5206fd1a74921ae708a37f68
MD5 887635097731f5a2ab1cc2157e5fa7b0
BLAKE2b-256 138399d461e1c34d291e28e0e39c5c13c11b46df70c9abcaf526c3cfa04d1e61

See more details on using hashes here.

Supported by

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