-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnionFind.java
178 lines (145 loc) · 4.22 KB
/
UnionFind.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Implementation of the Union-Find ADT.
*/
/*
Assignment number : 8
File Name : UnionFind.java
Name: Ran Zaaroor
Student ID : 209374040
Email : [email protected]
*/
public class UnionFind {
private static final int ROOT = 0;
private int up[];
private int weight[];
int numSets;
/**
* Constructor - initializes up and weight arrays.
*
* @param numElements initial number of singleton sets.
*/
public UnionFind (int numElements) {
up = new int[numElements + 1];
for (int i = 1; i < up.length; i++)
up[i] = ROOT;
weight = new int[numElements + 1];
for (int i = 1; i < weight.length; i++)
weight[i] = 1;
numSets = numElements;
}
/**
* Unites two sets using weigthed union.
* Assumes that the given elements are representatives of their sets.
*
* @param i representative of first set.
* @param j representative of second set.
*/
public void union (int i, int j) {
if (up[i] != 0 || up[j] != 0)
throw new IllegalArgumentException ("i and j must be set representatives");
if (i == j)
return;
numSets--;
int newWeight = weight[i] + weight[j];
if (weight[i] < weight[j]) {
up[i] = j;
weight[j] = newWeight;
}
else {
up[j] = i;
weight[i] = newWeight;
}
}
/**
* Finds the set representative, and applies path compression.
*
* @param i the element to search.
* @return the representative of the group that contains i.
*/
public int find (int i) {
int r = i;
while (up[r] != ROOT)
r = up[r];
// Compress path.
if (i != r) {
int k = up[i];
while (k != r) {
up[i] = r;
i = k;
k = up[k];
}
}
return r;
}
/**
* Find the current number of sets.
*
* @return the number of set.
*/
public int getNumSets() {
return numSets;
}
/**
* Prints the contents of the up array.
*/
public void debugPrintUp() {
System.out.print ("up: ");
for (int i = 1; i < up.length; i++)
System.out.print (up[i] + " ");
System.out.println ("");
}
/**
* Prints the results of running find on all elements.
*/
public void debugPrintFind() {
System.out.print ("find: ");
for (int i = 1; i < up.length; i++)
System.out.print (find (i) + " ");
System.out.println ("");
}
/**
* Prints the contents of the weight array.
*/
public void debugPrintWeight() {
System.out.print ("weight: ");
for (int i = 1; i < weight.length; i++)
System.out.print (weight[i] + " ");
System.out.println ("");
}
/**
* Various tests for the Union-Find functionality.
*
* @param args command line arguments - not used.
*/
public static void main (String[] args) {
UnionFind uf = new UnionFind (10);
uf.debugPrintUp();
uf.debugPrintFind();
uf.debugPrintWeight();
System.out.println ("Number of sets: " + uf.getNumSets());
System.out.println ("");
uf.union (2, 1);
uf.union (3, 2);
uf.union (4, 2);
uf.union (5, 2);
uf.debugPrintUp();
uf.debugPrintFind();
uf.debugPrintWeight();
System.out.println ("Number of sets: " + uf.getNumSets());
System.out.println();
uf.union (6, 7);
uf.union (8, 9);
uf.union (6, 8);
uf.debugPrintUp();
uf.debugPrintFind();
uf.debugPrintWeight();
System.out.println ("Number of sets: " + uf.getNumSets());
System.out.println();
uf.find (8);
uf.debugPrintUp();
uf.debugPrintFind();
uf.debugPrintWeight();
System.out.println ("Number of sets: " + uf.getNumSets());
System.out.println();
}
}