-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathunion_find.go
87 lines (71 loc) · 2.19 KB
/
union_find.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
/*
Date: 2024-07-28
Description:
Implement disjoint sets(union find) data structure.
Disjoint sets are useful to maintain groups, check for membership and update
membership
Approach:
Implemented using arrays
- parent array keeps parent of an element
- rank array keeps rank of subset
Find is used to find which group a element belongs to, returns parent of that
group. This function also does path compression step
Union is used to merge 2 elements to a single group if already not in the
same group otherwise merges them to single group using rank
References:
https://www.geeksforgeeks.org/disjoint-set-data-structures/
https://github.com/williamfiset/Algorithms/blob/master/src/main/java/com/williamfiset/algorithms/datastructures/unionfind/UnionFind.java
Complexity:
Find: O(1) amortised
Union: O(1)
Space: O(N)
*/
package main
import "fmt"
type UnionFind struct {
rank []int
parent []int
}
func newUnionFind(n int) *UnionFind {
u := &UnionFind{}
for i := 0; i < n; i++ {
u.rank = append(u.rank, 0)
u.parent = append(u.parent, i) // All element are parent of itself
}
return u
}
// Recursive method of find with path compression. This is same as iterative
// approach implemented here: Level-2/graph_cycle_detection_union_find.py.
// However recursive is more intuitive and readable.
func (u *UnionFind) find(x int) int {
if u.parent[x] != x {
u.parent[x] = u.find(u.parent[x]) // Path compression
}
return u.parent[x]
}
// Merges element x and y to same group not already in same.
func (u *UnionFind) union(x, y int) {
xset, yset := u.find(x), u.find(y)
if xset == yset { // Both elements in same set
return
}
// Put smaller ranked item under bigger ranked item if ranks are different
if u.rank[xset] < u.rank[yset] {
u.parent[xset] = yset
} else if u.rank[xset] > u.rank[yset] {
u.parent[yset] = xset
} else {
// If ranks are same, then move y under x (doesn't matter which
// one goes where) and increment rank of x's tree
u.parent[yset] = xset
u.rank[xset] += 1
}
}
func main() {
u := newUnionFind(5)
u.union(0, 2)
u.union(4, 2)
u.union(3, 1)
fmt.Printf("4->%d, 0->%d\n", u.find(4), u.find(0)) // 4->0, 0->0
fmt.Printf("1->%d, 0->%d\n", u.find(1), u.find(0)) // 1->3, 0->0
}