forked from theodesp/unionfind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unionfind.go
101 lines (85 loc) · 3.02 KB
/
unionfind.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package unionfind
/*
Open Source Initiative OSI - The MIT License (MIT):Licensing
The MIT License (MIT)
Copyright (c) 2017 Theo Despoudis ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// Package unionfind implements a weighted Union Find data structure with path compression.
// Union-Find is used to determine the connected components in a graph.
// We can determine whether 2 nodes are in the same connected component or not in the graph.
// We can also determine that by adding an edge between 2 nodes whether it leads to cycle in the graph or not.
//// Any type that implements unionfind.Interface can be used as
//type Interface interface {
// Less(i, j int) bool
// GetItem(i int) interface{}
// SetItem(i int)
//}
//
type UnionFind struct {
root []int
size []int
}
// New returns an initialized list of size
func New(size int) *UnionFind {
return new(UnionFind).init(size)
}
// Constructor initializes root and size arrays
func (uf *UnionFind) init(size int) *UnionFind {
uf = new(UnionFind)
uf.root = make([]int, size)
uf.size = make([]int, size)
for i := 0; i < size; i++ {
uf.root[i] = i
uf.size[i] = 1
}
return uf
}
// Union connects p and q by finding their roots and comparing their respective
// size arrays to keep the tree flat
func (uf *UnionFind) Union(p int, q int) {
qRoot := uf.Root(q)
pRoot := uf.Root(p)
if uf.size[qRoot] < uf.size[pRoot] {
uf.root[qRoot] = uf.root[pRoot]
uf.size[pRoot] += uf.size[qRoot]
} else {
uf.root[pRoot] = uf.root[qRoot]
uf.size[qRoot] += uf.size[pRoot]
}
}
// Root or Find traverses each parent element while compressing the
// levels to find the root element of p
// If we attempt to access an element outside the array it returns -1
func (uf *UnionFind) Root(p int) int {
if p > len(uf.root)-1 {
return -1
}
for uf.root[p] != p {
uf.root[p] = uf.root[uf.root[p]]
p = uf.root[p]
}
return p
}
// Root or Find
func (uf *UnionFind) Find(p int) int {
return uf.Root(p)
}
// Check if items p,q are connected
func (uf *UnionFind) Connected(p int, q int) bool {
return uf.Root(p) == uf.Root(q)
}