-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDisjointSet.java
102 lines (91 loc) · 2.14 KB
/
DisjointSet.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.util.*;
class DisjointSet<T>
{
private HashMap<T,Node> map=new HashMap<T,Node>();
private HashSet<T> collection=new HashSet<T>();
private class Node
{
int rank;
T data;
int size;
Node parent;
Node(T a)
{
rank=0;
data=a;
parent=this;
size=1;
}
}
public boolean isASet(T data)
{
return map.containsKey(data);
}
public int numberOfSets()
{
return collection.size();
}
public boolean isGroupRepresentative(T data)
{
if(map.containsKey(data))
{
Node node=map.get(data);
return collection.contains(node.data);
}
return false;
}
public Iterator<T> getCollection()
{
return collection.iterator();
}
public boolean union(T data1,T data2)
{
Node node1=map.get(data1);
Node node2=map.get(data2);
Node parent1=findSet(node1);
Node parent2=findSet(node2);
if(parent1.data.equals(parent2.data))
return false;
if(parent1.rank>=parent2.rank)
{
parent1.rank=parent1.rank==parent2.rank?parent1.rank+1:parent1.rank;
parent2.parent=parent1;
parent1.size+=parent2.size;
collection.remove(parent2.data);
}
else
{
parent1.parent=parent2;
parent2.size+=parent1.size;
collection.remove(parent1.data);
}
return true;
}
public T findSet(T data)
{
return findSet(map.get(data)).data;
}
public int getSize(T data)
{
return findSet(map.get(data)).size;
}
private Node findSet(Node node)
{
Node parent=node.parent;
if(parent==node)
return parent;
else
{
node.parent=findSet(node.parent);
return node.parent;
}
}
public void makeSet(T data)
{
if(map.containsKey(data))
return;
Node node=new Node(data);
map.put(data,node);
collection.add(node.data);
}
}