Skip to main content

Union-Find (Disjoint Set) Data Structure

Project description

UFDS - Union-Find (Disjoint Set) Data Structure

Union-Find (aka Disjoint Set) Data Structure using the Forest-of-Trees implementation.

TL;DR

# import the user facing DisjointSet class
from ufds import DisjointSet

# create a new DisjointSet instance
ds = DisjointSet()

# create new trees
ds.find(1)
ds.find(2)

# link trees
ds.union(1, 2)

# DisjointSet contains single tree
#   1 -- 1
#     `- 2
ds  # displays DisjointSet(((1, 1), (1, 2)))

Implementation

DisjointSet is the user facing implementation of Union-Find data structure.

As the name suggests, it exposes two methods:

  • union(x, y): link (optionally create) trees containing x and y.
  • find(x): find root or create tree containing x.

I also exposes the following dunders:

  • __bool__: a DisjointSet is truthy if is contains at least one tree.
  • __eq__: two DisjointSet are equal if they contain the same sets.
  • __iter__: iterate over the sets in a DisjointSet set.
  • __repr__: represent a DisjointSet in a reproducible way.

DisjointSet inherits from BaseDisjointSet, a simple implementation which provides only the three base methods used to built Union-Find data structures.

These methods are protected (single underscore prefix), and assume their preconditions are met before execution.

The three base methods are:

  • _make_set(x)
    • effect: make a one node tree containing x.
    • precondition: no tree contains x.
  • _find_set(x)
    • effect: find root of the tree containing x.
    • precondition: a tree contains x.
  • _union(x)
    • effect: link the trees containing x and y into a single tree.
    • precondition: trees containing x and y are different.

Copyright and License

Copyright (c) 2023 Louis Cochen <louis.cochen@protonmail.ch>

Licensed under the Apache License version 2.0, see LICENSE file.

Contributing

At this time, this project is open source but not open contribution.

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

ufds-0.1.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

ufds-0.1.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file ufds-0.1.0.tar.gz.

File metadata

  • Download URL: ufds-0.1.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for ufds-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a75f406f89adedfdd60e2a1835e1bc2e619d1bb2a6b4f65455fbf6ba3e28bf66
MD5 1092a4f75eb1b356b8ea2fb2902456db
BLAKE2b-256 3530af2e71e1c4b5d8e5456f097b7c0a7c251491ae42fd30012978e06455ab76

See more details on using hashes here.

File details

Details for the file ufds-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ufds-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for ufds-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 90307483f3d47b895434f37c1cc641e6e92fd4c5aa70cb9b5706e57544fb9f9d
MD5 5378376f864ba5ec3768b2065b043dea
BLAKE2b-256 4437f0d90cd0ddb2d25bd1aef755eeddab1e84f5806a86d99425a4006172a404

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