Skip to main content

Disjoint Set (Union-Find) Data Structure

This repository contains a Python implementation of the Disjoint Set (also known as Union-Find) data structure. The implementation includes optimizations such as path compression and union by rank to ensure efficient operations.

Features

  • Find: Determine the root of the set containing a given element.
  • Union: Merge two sets into one.
  • Connected: Check if two elements are in the same set.
  • Path Compression: Flattens the tree during find operations for faster future queries.
  • Union by Rank: Keeps the tree balanced by attaching smaller trees to larger ones.

Code Overview

DisjointSet Class

Attributes

  • parent: A list where each element points to its parent in the set.
  • rank: A list representing the rank (approximate depth) of each set.

Methods

  • __init__(size: int): Initializes the disjoint-set with size elements.
  • find(x: int) -> int: Finds the root of the set containing x with path compression.
  • union(x: int, y: int): Merges the sets containing x and y using union by rank.
  • connected(x: int, y: int) -> bool: Checks if x and y are in the same set.

Example Usage

# Create a disjoint-set with 10 elements
ds = DisjointSet(10)

# Perform some union operations
ds.union(1, 2)
ds.union(3, 4)
ds.union(1, 3)

# Check if elements are connected
print(ds.connected(1, 4))  # Output: True
print(ds.connected(1, 5))  # Output: False

Metadata

Release files for disjoint-set-struct 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for disjoint-set-struct 0.0.1
File Size Uploaded
disjoint_set_struct-0.0.1.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for disjoint-set-struct 0.0.1
File Interpreter ABI Platform
disjoint_set_struct-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.2 kB

Release files / disjoint_set_struct-0.0.1.tar.gz

Download URL disjoint_set_struct-0.0.1.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
db398c062b4f7c5c32ccc159da200076a2184ba790cf4a02777e1dea6fccae38
BLAKE2b-256 checksum
How to use checksums
6551e81db28e28fc5487fdfc061f20e29e14a089ccc9ccc44b639be70b2d30a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release files / disjoint_set_struct-0.0.1-py3-none-any.whl

Download URL disjoint_set_struct-0.0.1-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fbcb69d15370a39e15ec82f2ad0341b06e99110e5217fd174653a549a3641d62
BLAKE2b-256 checksum
How to use checksums
7178db77aeff27d46fc019ce50d28b2966a35f70cd96afb9c84738a0a76102fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page