Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shiritai/rooted-disjoint-set
Python implementation of a (rooted) disjoint set, elegant, efficient and user-friendly.
https://github.com/shiritai/rooted-disjoint-set
algorithm data-structure disjoint-set disjoint-set-union python python-dict union-find
Last synced: 22 days ago
JSON representation
Python implementation of a (rooted) disjoint set, elegant, efficient and user-friendly.
- Host: GitHub
- URL: https://github.com/shiritai/rooted-disjoint-set
- Owner: Shiritai
- License: mit
- Created: 2024-11-18T02:42:05.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2024-11-18T02:44:16.000Z (about 2 months ago)
- Last Synced: 2024-11-18T03:33:01.075Z (about 2 months ago)
- Topics: algorithm, data-structure, disjoint-set, disjoint-set-union, python, python-dict, union-find
- Language: Python
- Homepage:
- Size: 0 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# (Rooted) Disjoint set
> [!NOTE]
> 可參閱[中文文檔](./README.zh_TW.md)。> [!IMPORTANT]
> This is perhaps the most elegant, user-friendly and Python-styled implementation I've ever seen!## TL;DR
A disjointed set (`dict[key, root_of_key]`) that
* behaves completely the same as a Python `dict`
* has `dict` based and `disjoint-set` based methods for accessing
* with complete the same effect
* reserve the root-child relation of the keys
* optimized with flattening## Usage
### Instantiate
```python
ds = RootedDisjointSet()# or
ds = RootedDisjointSet({ 1: 1, 2: 1, 3: 2 })
# 1 <- 2 <-3# or
ds = RootedDisjointSet({ 1: '1', '2': '1', 3: 1 })
# '1' <- 1 <- 3
# '1' <- '2'
# note that key can be any hashable type
```### Set dependent (Merge)
```python
ds[key] = value
# or
ds.set_dependent(key, value)
```### Find root (Find)
```python
ds[key]
# or
ds.find_root(key)
```### Has same root (Check)
```python
ds[key1] == ds[key2]
# or
ds.has_same_root(key1, key2)
```### Is same disjoint set
```python
ds1 = RootedDisjointSet()
ds2 = RootedDisjointSet()
ds1 == ds2
```## Note
Welcome to give any kinds of comment, open issues or make PR ;)