Skip to main content

A Disjoint-Set data structure (aka Union-Find w/ Rank)

What is Union-Find?

Suppose you have a collection S of elements e1, e2, ..., en, and wish to group them into different collections using operations:

  • "put ei and ej into the same group" (union),
  • "give me a representative of the group ei belongs to" (find).

Then a Union-Find data structure helps to store the underlying groups very efficiently and implements this API.

Note: The variant implemented uses Path Compression to further improve the performance.

(Some) Applications

  • Detect Cycles in Graph: Given a graph G, we can put the endpoints of edges into the same group (same connected component) unless there is a pair of endpoints (ei, ej) that share a group representative. If that happens, there was already a path existing between them, and adding this edge will add multiple paths, which cannot be the case for acyclic graphs.

  • Number of connected components in Graph: Given a graph G, put the endpoints of edges into the same group (same connected component). Once all nodes are exhausted, the number of groups formed is the number of connected components in G.

Some interesting lecture notes regarding Union-Find.

Usage

Setup

Using any of the package installers, install pyreunion from the PyPi.

For example, pip install pyreunion.

API

Example 1

Task: Create a UnionFind data structure of arbitrary size that contains usize at its elements. Then, union a few elements and capture the state of the data structure after that.

Solution:

import pyreunion


def main():

    # Create an empty UnionFind data structure.
    uf = pyreunion.UnionFind()

    print("Initial state:", uf.str())
    print("All elements form their own group (singletons).")
    print(uf.subsets())

    uf.union(2, 1)
    print("After combining the groups that contains 2 and 1:", uf.str())

    uf.union(4, 3)
    print("After combining the groups that contains 4 and 3:", uf.str())

    uf.union(6, 5)
    print("After combining the groups that contains 6 and 5:", uf.str())

    hs1 = {1, 2}
    hs2 = {3, 4}
    hs3 = {5, 6}

    subsets = uf.subsets()
    assert (len(subsets) == 3)

    assert (hs1 in subsets)
    assert (hs2 in subsets)
    assert (hs3 in subsets)

    uf.union(1, 5)

    print("After combining the groups that contains 1 and 5", uf.str())

    subsets = uf.subsets()
    assert (len(subsets) == 2)

    for x in hs1:
        hs3.add(x)

    assert (hs3 in subsets)
    assert (hs2 in subsets)

    # It is possible to iterate over the subsets.
    for partition in uf.subsets():
        print(partition)


if __name__ == '__main__':
    main()

Performance

The underlying implementation uses Path Compression and is written in Rust. The implementation and some performance statistics are available here.

Metadata

Release files for pyreunion 0.1.3

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

Source distribution (sdist)

Source distribution for pyreunion 0.1.3
File Size Uploaded
pyreunion-0.1.3.tar.gz 3.9 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for pyreunion 0.1.3
File Interpreter ABI Platform
pyreunion-0.1.3-cp39-cp39-manylinux_2_24_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.24+ x86-64 Details
pyreunion-0.1.3-cp38-cp38-manylinux_2_24_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.24+ x86-64 Details

Total release size: 356.4 kB

Release files / pyreunion-0.1.3.tar.gz

Download URL pyreunion-0.1.3.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f043962e0e146d4615c4334f2f25be88a8295729da055f8199191e74e0d304b5
BLAKE2b-256 checksum
How to use checksums
495b506a35509c3140646b60c380deff8e220d14b0c8b6f9bc3ddef1f05269ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.11.3

Release files / pyreunion-0.1.3-cp39-cp39-manylinux_2_24_x86_64.whl

Download URL pyreunion-0.1.3-cp39-cp39-manylinux_2_24_x86_64.whl
Size 176.2 kB
Tags CPython 3.9 Linux glibc 2.24+ x86-64
SHA-256 checksum
How to use checksums
826316fddbe6f884d0e7bf8ca45813827497ec9635c810a7c9f342a9d39a8218
BLAKE2b-256 checksum
How to use checksums
2b0f63a5c0af7db5d990a26eec40d361f5c90ea151e79ae6e72385d91899d897
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.11.3

Release files / pyreunion-0.1.3-cp38-cp38-manylinux_2_24_x86_64.whl

Download URL pyreunion-0.1.3-cp38-cp38-manylinux_2_24_x86_64.whl
Size 176.2 kB
Tags CPython 3.8 Linux glibc 2.24+ x86-64
SHA-256 checksum
How to use checksums
3b827431aafd6eb39e9c86e30b5b159312b2e94de41d902f599012f5a5f16c4a
BLAKE2b-256 checksum
How to use checksums
628439407486a424b2878d2e1aa2bfbfa5000e571e8acc443ab7e93fc23072eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.11.3

Release history Release notifications | RSS feed

This release

0.1.3 This release

3 release files

0.1.2

3 release files

0.1.1

2 release files

0.1.0

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