-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkruskal-minimum-spanning.go
53 lines (53 loc) · 1.57 KB
/
kruskal-minimum-spanning.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
// https://leetcode.com/problems/connecting-cities-with-minimum-cost/
func unionFindCompress (parent []int, x int) int {
// Base case
if (parent[x] == x) {
return x;
}
// As a manager, assign task to worker node to get root
root := unionFindCompress(parent, parent[x]);
// update root for current node to compressed it
parent[x] = root;
return root;
}
func kruskalMinimumSpanningTree (N int, connections [][]int) int {
// Sort given connections
sort.Slice(connections, func (i int, j int ) bool {
return connections[i][2] < connections[j][2]
});
components := N;
cost := 0;
parent := make([] int, N);
size := make([] int, N);
for i, _ := range(parent) {
parent[i] = i;
size[i] = 1;
}
// Iterate each connection
for _, connection := range(connections) {
u := connection[0] - 1;
v := connection[1] - 1;
w := connection[2];
// find parent for each group
uRoot := unionFindCompress(parent, u);
vRoot := unionFindCompress(parent, v);
// if parent is same then ignore
if (uRoot != vRoot) {
// Add lower size into bigger set
if(size[uRoot] < size[vRoot]) {
parent[uRoot] = vRoot;
size[vRoot] += size[uRoot];
} else {
parent[vRoot] = uRoot;
size[uRoot] += size[vRoot]
}
// recude component
components--;
cost += w;
}
}
if components == 1 {
return cost;
}
return -1;
}