Implementation of disjoint set in Go(>= 1.18 with generic features). For the algorithm, see https://en.wikipedia.org/wiki/Disjoint-set_data_structure
package main
import (
"fmt"
"github.com/rickonono3/ufset"
)
func main() {
sets := ufset.New[int]()
sets.Union(0, 1)
sets.Union(2, 1)
sets.Union(0, 0)
sets.Union(3, 4)
sets.Union(5, 3)
fmt.Println(sets.InSameSet(0, 2))
fmt.Println(sets.InSameSet(0, 3))
setsList := make([]int, 6)
for i := 0; i < 6; i++ {
setsList[i] = sets.Find(i)
}
fmt.Println(setsList)
sets2 := ufset.New[string]()
sets2.Union("hello", "world")
sets2.Union("world", "peace")
sets2.Union("foo", "bar")
fmt.Println(sets2.Find("hello"), sets2.Find("peace"), sets2.Find("bar"))
}
output:
true
false
[0, 0, 0, 3, 3, 3]
["hello", "hello", "foo"]
Instantiates a new sets struct.
Unions two sets contain the provided keys, keys not exist will be created automatically as single-key sets.
Returns the root key of a set contains the provided key.
Returns whether the set contains k1
is exactly the set contains k2
. Equals to Find(k1) == Find(k2)
.