kk-min / go-uf

Union-Find / Disjoint Set data structure implementation for Go.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Go Union Find / Disjoint Set

Union-Find / Disjoint Set data structure implementation for Go.

Features

  • Path Compression: FindSet(n) will point the parent value of n and it's intermediate parents to the root
  • Ranking via Size: The set with a bigger size will act as the representative during a Union

Usage

// Create 5 initial disjoint sets
sets := uf.CreateSets(5)

// Union the sets based on edge (connection) information
edges := [][]int{{0,1}, {2,3}, {3,4}}
for _, edge := range edges {
	sets.Union(edge[0], edge[1])
}

// Get root (parent set) of a number
sets.FindSet(0) // 0
sets.FindSet(1) // 0
sets.FindSet(2) // 2
sets.FindSet(3) // 2
sets.FindSet(4) // 2

About

Union-Find / Disjoint Set data structure implementation for Go.

License:MIT License


Languages

Language:Go 100.0%