Note
可參閱中文文檔。
Important
This is perhaps the most elegant, user-friendly and Python-styled implementation I've ever seen!
A disjointed set (dict[key, root_of_key]
) that
- behaves completely the same as a Python
dict
- has
dict
based anddisjoint-set
based methods for accessing - with complete the same effect
- has
- reserve the root-child relation of the keys
- optimized with flattening
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
ds[key] = value
# or
ds.set_dependent(key, value)
ds[key]
# or
ds.find_root(key)
ds[key1] == ds[key2]
# or
ds.has_same_root(key1, key2)
ds1 = RootedDisjointSet()
ds2 = RootedDisjointSet()
ds1 == ds2
Welcome to give any kinds of comment, open issues or make PR ;)