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.0.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.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ufds-0.2.0.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.0.tar.gz
Algorithm Hash digest
SHA256 0780acd2470275b2b2c00166fc4912681ae4884378626c263a6227d93469703b
MD5 429555c134179590f1257d5f710a0b8f
BLAKE2b-256 9acbcf42e6b3762932ffa2549790844597df5009978376a3fac3fbdcb8f7ea64

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ufds-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.8 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 10418bc047bef70e79d615e7fe11e9689d1b46813dcba8273d9585d5f96b64bd
MD5 0b15981164f89ed28ffb529db31de3e3
BLAKE2b-256 a490fdecaf5d91b5bf3d4ceb56baca99dd54665bcd7d445001f7c2b0c8b27c4d

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