-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionFindRankCompression.java
More file actions
81 lines (77 loc) · 1.98 KB
/
UnionFindRankCompression.java
File metadata and controls
81 lines (77 loc) · 1.98 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
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.*;
class Edge{
int v1,v2,w;
Edge(int v1,int v2,int w){
this.v1 = v1;
this.v2 = v2;
this.w = w;
}
@Override
public String toString(){
return v1 < v2 ? v1+" "+v2+" "+w : v2+" "+v1+" "+w;
}
}
class A implements Comparator<Edge>{
public int compare(Edge o1,Edge o2){
return o1.w - o2.w;
}
}
class Par{
int id,rank;
Par(int id,int rank){
this.id = id;
this.rank = rank;
}
@Override
public String toString(){
return this.id+" "+this.rank;
}
}
public class UnionFindRankCompression {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int V = sc.nextInt();
int E = sc.nextInt();
Edge edges[] = new Edge[E];
for(int i=0;i<E;i++){
edges[i] = new Edge(sc.nextInt(), sc.nextInt(),sc.nextInt());
}
Arrays.sort(edges,new A());
Edge arr[] = MST(edges,V);
for(Edge x:arr)
System.out.println(x);
}
static void makeParent(Par parent[]){
for(int i =0;i<parent.length;i++)
parent[i] = new Par(i,0);
}
static Par findParent(int v,Par parent[]){
if(parent[v].id == v)
return parent[v];
return findParent(parent[v].id,parent);
}
static Edge[] MST(Edge edges[],int V){
Par parent[] = new Par[V] ;
int count = 0, i = 0;
Edge[] ans = new Edge[V-1];
makeParent(parent);
while(count != V-1){
Edge temp = edges[i++];
Par a = findParent(temp.v1,parent) , b = findParent(temp.v2,parent);
parent[temp.v1] = a;
parent[temp.v2] = b;
if(a.id == b.id)
continue;
if(a.rank <= b.rank){
parent[a.id] = b;
if(a.rank == b.rank)
b.rank++;
}
else{
parent[b.id] = a;
}
ans[count++] = temp;
}
return ans;
}
}