-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionFind.java
More file actions
89 lines (68 loc) · 1.68 KB
/
UnionFind.java
File metadata and controls
89 lines (68 loc) · 1.68 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
82
83
84
85
86
87
88
89
import java.util.*;
public class UnionFind<V>{
public static class Node<V>{
public V value;
public Node(V v){
value= v;
}
}
private HashMap<Node<V>,Node<V>> parents;
private HashMap<V,Node<V>> nodes;
private HashMap<Node<V>,Integer> sizeMap;
public UnionFind(List<V> arr){
parents = new HashMap<Node<V>,Node<V>>();
nodes = new HashMap<V,Node<V>>();
sizeMap = new HashMap<Node<V>,Integer>();
for (V cur : arr ) {
Node<V> n = new Node<V>(cur);
nodes.put(cur,n);
parents.put(n,n);
sizeMap.put(n,1);
}
}
private Node<V> Find(V node){
Stack<Node<V>> stack = new Stack<Node<V>>();
Node<V> cur = nodes.get(node);
while(cur != parents.get(cur)){
stack.push(cur);
cur = parents.get(cur);
}
while(!stack.isEmpty()){
parents.put(stack.pop(),cur);
}
return cur;
}
public void Union(V a,V b){
Node<V> fa = Find(a);
Node<V> fb = Find(b);
if(fa != fb){
int sa = sizeMap.get(fa);
int sb = sizeMap.get(fb);
Node<V> big = sa>=sb ? fa : fb;
Node<V> small = big==fa ? fb : fa;
parents.put(small,big);
sizeMap.remove(small);
}
}
public int size(){
return sizeMap.size();
}
public static void main(String[] args){
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(1);
arr.add(11);
arr.add(111);
arr.add(1111);
arr.add(21);
arr.add(31);
arr.add(41);
UnionFind<Integer> unionFind = new UnionFind<Integer>(arr);
System.out.println("size ="+ unionFind.size());
unionFind.Union(1,11);
System.out.println("size ="+ unionFind.size());
unionFind.Union(1111,11);
System.out.println("size ="+ unionFind.size());
unionFind.Union(1,1111);
System.out.println("size ="+ unionFind.size());
}
}