GithubHelp home page GithubHelp logo

ufset's Introduction

Generic Disjoint Sets (Union-Find Sets)

Go

Implementation of disjoint set in Go(>= 1.18 with generic features). For the algorithm, see https://en.wikipedia.org/wiki/Disjoint-set_data_structure

Example

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"]

Methods

ufset.New[K]() (*DisjointSets[K comparable])

Instantiates a new sets struct.

(*DisjointSets[K]) Union(k1, k2 K)

Unions two sets contain the provided keys, keys not exist will be created automatically as single-key sets.

(*DisjointSets[K]) Find(key K) K

Returns the root key of a set contains the provided key.

(*DisjointSets[K]) InSameSet(k1, k2 K) bool

Returns whether the set contains k1 is exactly the set contains k2. Equals to Find(k1) == Find(k2).

ufset's People

Contributors

rickonono3 avatar

Stargazers

 avatar Token2019 avatar Watson avatar  avatar  avatar  avatar Kimmy Sun avatar  avatar  avatar Zhida Chen avatar ZouR Ma avatar 可爱垃圾 avatar Austin Liubov avatar  avatar

Watchers

 avatar  avatar

Forkers

reyx3 cerviny

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.