GithubHelp home page GithubHelp logo

theodesp / unionfind Goto Github PK

View Code? Open in Web Editor NEW
21.0 3.0 6.0 7 KB

An idiomatic implementation of a weighted Union Find data structure with path compression in Go.

Home Page: https://godoc.org/github.com/theodesp/unionfind

License: MIT License

Makefile 11.21% Go 88.79%
golang union-find data-structures algorithms

unionfind's Introduction

unionfind

An idiomatic implementation of a weighted Union Find data structure with path compression in go.

Install

$ go get github.com/theodesp/unionfind

Usage

// Initialize with size
uf := unionfind.New(10)

// Union a,b connects components at index a and b
uf.Union(1, 2)
uf.Union(2, 3)
uf.Union(5, 6)
uf.Union(4, 6)

fmt.PrintLn(uf.Find(2)) // Prints 1
fmt.PrintLn(uf.Connected(1, 2)) // Prints true
fmt.PrintLn(uf.Connected(1, 3)) // Prints false

API

uf := unionfind.New(N)

Create a new Union Find Structure with size N.

uf.Union(p, q)

Connects p and q components.

uf.Find(p)

Attempts to find the Root component of p. Returns -1 if p is out of bounds.

uf.Root(p, q)

Same as Find.

uf.Connected(p, q)

Checks if p and q are connected.

EXTRA

There is also a goroutine safe version of unionfind in the file safe-unionfind.

Licence

MIT - Theo Despoudis

unionfind's People

Contributors

carbocation avatar theodesp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

unionfind's Issues

Would it be possible to expose the struct field .root?

Howdy - experimenting with this package and have a need to use the root array, which appears to be inaccessible if I call uf.root. Would it be straightforward to implement this, or provide a pointer on how I might implement?

Make threadSafeUnionFind public

Both threadSafeUnionFind and threadSafeUnionFind's constructor are private, so can't be used. If you capitalize to ThreadSafeUnionFind or created a public constructor, then we could use it.

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.