Repository navigation
Expand file tree
/
Copy pathDisjointSets.java
More file actions
70 lines (61 loc) · 1.8 KB
/
Copy pathDisjointSets.java
File metadata and controls
70 lines (61 loc) · 1.8 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
/*Copyright (c) Dec 25, 2014 CareerMonk Publications and others.
* E-Mail : info@careermonk.com
* Creation Date : 2015-01-10 06:15:46
* Last modification : 2006-05-31
by : Narasimha Karumanchi
* File Name : DisjointSets.java
* Book Title : Data Structures And Algorithms Made In Java
* Warranty : This software is provided "as is" without any
* warranty; without even the implied warranty of
* merchantability or fitness for a particular purpose.
*
*/
package chapter08disjointsets;
public class DisjointSets {
private int[] S;
public DisjointSets(int numElements) {
S = new int [numElements];
for (int i = 0; i < S.length; i++) {
S[i] = -1;
}
}
public void union(int root1, int root2) {
if (S[root2] < S[root1]) {
S[root1] = root2;
} else {
if (S[root1] == S[root2]) {
S[root1]--;
}
S[root2] = root1;
}
}
// Path Compression
public int find(int x) {
if (S[x] < 0) {
return x;
} else {
S[x] = find(S[x]);
return S[x];
}
}
public static void main(String[] args) {
int NumElements = 97;
int NumInSameSet = 14;
DisjointSets s = new DisjointSets(NumElements);
int set1, set2;
for (int k = 1; k < NumInSameSet; k *= 2) {
for (int j = 0; j + k < NumElements; j += 2 * k) {
set1 = s.find(j);
set2 = s.find(j + k);
s.union(set1, set2);
}
}
for (int i = 0; i < NumElements; i++) {
System.out.print(s.find(i) + "*");
if (i % NumInSameSet == NumInSameSet - 1) {
System.out.println();
}
}
System.out.println();
}
}