-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdset.go
48 lines (41 loc) · 818 Bytes
/
dset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package ds
// Simple union find data struction
// Utilizing an int splice to store
// the data
// Extendable and deletable, but usually the size is set at the start
import (
"fmt"
)
type DisjointSet struct {
data []int
}
func MakeDJSet(num int) *DisjointSet {
var r DisjointSet
r.data = make([]int, num)
for x := 0; x < num; x++ {
r.data[x] = x
}
return &r
}
func (s DisjointSet) Find(num int) int {
return s.data[num]
}
func (s *DisjointSet) Union(a, b int) {
replace := s.Find(a)
with := s.Find(b)
if replace != with {
for val, _ := range s.data {
if s.data[val] == replace {
s.data[val] = with
}
}
}
return
}
func (s DisjointSet) Print() {
fmt.Println("Disjoint Set of Size: ", len(s.data))
for x := 0; x < len(s.data); x++ {
fmt.Print(s.data[x], ", ")
}
fmt.Println("")
}