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_elemnt_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)

Fully Typed

Supports any hashable type:

dsu = DisjointSet[str]()

📂 Project Structure

src/
    disjointset/
        disjointset.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.1.0.tar.gz (4.4 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.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for disjointsetunion-1.1.0.tar.gz
Algorithm Hash digest
SHA256 c68add33b8d0a0768a2f5172904819f2a43c0c77a4f257105199900c128b19a5
MD5 aa39c96a3aa9a69362ad5ee03a4f81b4
BLAKE2b-256 42371f26ee345b8fbf629f2a7fe4157c6b7f919671ec0e7f1d6b820f9d8cbace

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for disjointsetunion-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5443784555580e978af3b081444501750368f56b7fba5391bf4feef0d18d649
MD5 8817acc11e0b0b95641504b4c8cb1d93
BLAKE2b-256 f08c4915b2bdca593f8b7ef7f1bb9faa44d47b332f3fa6af3b30bd9629e78b9e

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