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)
dsu.make_set("Ali")
dsu.make_set(ali)
dsu.make_set_many([2, 3, "Bob", "Tom", bob, tom])

# union and union_many
dsu.union(1, 2)
dsu.union("Ali", ali)
dsu.union(1, ali)
dsu.union_many(["Bob", bob])
dsu.union_many([3, "Tom", tom])

# True
print(dsu.same_set(1, ali))
print(dsu.same_set(2, "Ali"))
print(dsu.same_set("Bob", bob))
print(dsu.same_set_many([3, "Tom", tom]))
print(dsu.same_set_many([1, 2, "Ali", ali]))

🔧 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.0.0.tar.gz (3.6 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.0.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for disjointsetunion-1.0.0.tar.gz
Algorithm Hash digest
SHA256 816641071184ce08a9674031a385fe5c5a84d9d5e47ee681a0ddb1d707cb6aea
MD5 f85fed90e160d208a630bae2a5841754
BLAKE2b-256 6c2d752d72364b4d9568b12a03b5399e054092ffaa76b0395340532a8f34563e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for disjointsetunion-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 93a311f732c55fbaca34441a28b5a2d58148f51d69c405b9a228a221e240edb9
MD5 6fe543ae25dcf949c7f6e4588c48ae13
BLAKE2b-256 23afe753c74b41c0ca36dc4f8be44b305bb0aa0a2a4c03ebf437fdcc07d191ef

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