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.2.1.tar.gz (13.9 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.2.1-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ufds-0.2.1.tar.gz
Algorithm Hash digest
SHA256 f9edc5359a03a138762034f088bf307b5e540660ef65b459f35bbe68de06bc6a
MD5 3d31330bd9e83e077766bd58ee1b64db
BLAKE2b-256 0f4961fbc7ceef3d1e84ac6b6896d58240e2cfadcc0c5113714a9f65f34835b3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ufds-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.9 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.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 19fa8250d16bda2f05672b5ad2ec661bf4602be9dbe425a491dbc7fb6b7c2130
MD5 eb4b4ebc608a36fa88fd62df7536d34e
BLAKE2b-256 1f0713bad3f098bb8176bfd61a22eea1606282d4fc886cd08180e41e1f2fbd2f

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