-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathKruskalsAlgorithm.java
More file actions
68 lines (65 loc) · 1.89 KB
/
KruskalsAlgorithm.java
File metadata and controls
68 lines (65 loc) · 1.89 KB
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
class Solution {
static int spanningTree(int V, int E, List<List<int[]>> adj) {
// Code Here.
//(u, v, weight)
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<>(){
public int compare(int pair1[], int pair2[]){
return pair1[2] - pair2[2];
}
});
// insert edges in pq
for(int i=0;i<V;i++){
for(int edges[] : adj.get(i)){
pq.offer(new int[]{i,edges[0],edges[1]});
}
}
DisjointSet dsu = new DisjointSet(V);
int sum=0;
while(!pq.isEmpty()){
int object[] = pq.poll();
int u = object[0];
int v = object[1];
int weight = object[2];
if(dsu.unionBySize(u,v)){
sum += weight;
}
}
return sum;
}
}
class DisjointSet {
int parent[];
int size[];
DisjointSet(int nodes){
this.parent = new int[nodes];
this.size = new int[nodes];
for(int i=0;i<nodes;i++){
this.parent[i] = i;
this.size[i] = 1;
}
}
public int findRootParent(int node){
if(node == parent[node]){
return node;
}
parent[node] = findRootParent(parent[node]);
return parent[node];
}
public boolean unionBySize(int node1, int node2){
//1. find the root parent
int rootParent1 = findRootParent(node1);
int rootParent2 = findRootParent(node2);
if(rootParent1==rootParent2){
return false;
}
// 2, union of components
if(size[rootParent1]<size[rootParent2]){
parent[rootParent1] = rootParent2;
size[rootParent2] += size[rootParent1];
}else {
parent[rootParent2] = rootParent1;
size[rootParent1] += size[rootParent2];
}
return true;
}
}