dsu

package module
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2021 License: MIT Imports: 0 Imported by: 1

README

dsu

GoDoc Go Report Card Build Status codecov Mentioned in Awesome Go

Implementation of the Disjoint-Set data structure. The Disjoint-Set, Also called a Union-Find or Merge-Find set, is a data structure that stores a collection of disjoint (non-overlapping) sets. Equivalently, it stores a partition of a set into disjoint subsets. It provides operations for adding new sets, merging sets (replacing them by their union), and finding a representative member of a set. The last operation allows to find out efficiently if any two elements are in the same or different sets.

Installation

go get github.com/ihebu/dsu

Documentation

You can check the code documentation here

Usage Example


// Create a new disjoint-set
d := dsu.New()

// Add the elements 1, 2, 3 to the set
d.Add(1)
d.Add(2)
d.Add(3)

// The set is now {1}, {2}, {3}

// Unite the sets {1}, {2} 
d.Union(1, 2)

// The set is now {1, 2}, {3}

// Find the representative element of each set
d.Find(1) // returns 2
d.Find(2) // returns 2
d.Find(3) // returns 3

// Check the existence of an element in the set
d.Contains(2) // returns true
d.Contains(54) // returns false

// Note : you can add elements of different type in the set
// Example

d.Add("hello")
d.Add(34.5)

d.Union("hello", 34.5)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DSU

type DSU struct {
	// contains filtered or unexported fields
}

DSU is the type used to the Disjoint Set data structure. it maps from a value to a node pointer corresponding to the element in the set.

func New

func New() *DSU

New returns a pointer to an empty initialized DSU instance.

func (*DSU) Add

func (d *DSU) Add(x interface{}) bool

Add takes an element as a parameter and inserts it in the disjoint set. If the element already exists in the set, then nothing is done, and the return is false otherwise returns true

func (*DSU) Contains

func (d *DSU) Contains(x interface{}) bool

Contains checks if a given element is present in the disjoint set.

func (*DSU) Find

func (d *DSU) Find(x interface{}) interface{}

Find returns the root element that represents the set to which x belongs to. If the element doesn't exist in the set, Find returns the nil value.

func (*DSU) Union

func (d *DSU) Union(x, y interface{}) bool

Union replaces the set containing x and the set containing y with their union. Union uses Find to determine the roots of the trees containing x and y. If the roots are the same of one of the elements doesn't exist in the set, there is nothing more to do. and Union returns false Otherwise, the two sets get be merged. This is done by either setting the parent element of the element with the smaller size to the other parent and the return of the function in this case is true

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL