-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnionFind.java
150 lines (128 loc) · 4.36 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
public class UnionFind {
int numOfObjects;
int[] componentOfObject;
int count = 0;
File file;
public UnionFind() {
System.out.println("=== Union Find ===");
}
void setNumOfObjects(int i) {
numOfObjects = i;
componentOfObject = new int[numOfObjects];
System.out.printf("There are %d objects.\n", numOfObjects);
}
boolean connected(int p, int q) {
boolean isConnected;
int result = componentOfObject[p] * componentOfObject[q];
if (result == 0) {
isConnected = false;
} else if (componentOfObject[p] == componentOfObject[q]) {
isConnected = true;
} else {
isConnected = false;
}
return isConnected;
}
void union(int p, int q) {
if (componentOfObject[p] == 0) {
if (componentOfObject[q] == 0) {
count++;
componentOfObject[p] = count;
componentOfObject[q] = count;
} else {
componentOfObject[p] = componentOfObject[q];
}
} else {
if (componentOfObject[q] == 0) {
componentOfObject[q] = componentOfObject[p];
} else {
int oldIndex, newIndex;
if (componentOfObject[p] <= componentOfObject[q]) {
newIndex = componentOfObject[p];
oldIndex = componentOfObject[q];
} else {
newIndex = componentOfObject[q];
oldIndex = componentOfObject[p];
}
for (int i = 0; i < numOfObjects; i++) {
if (componentOfObject[i] == oldIndex) {
componentOfObject[i] = newIndex;
}
}
if (newIndex != oldIndex) {
count--;
}
}
}
}
void listConnectedComponent() {
System.out.printf("\nThere are %d connected components.\n", count);
for (int i = 1; i <= count; i ++) {
System.out.print("{");
boolean isFirst = true;
for (int j = 0; j < numOfObjects; j++) {
if (componentOfObject[j] == i) {
if (!isFirst) {
System.out.printf(", ");
}
System.out.printf("%d", j);
isFirst = false;
}
}
System.out.println("}");
}
}
void getFile(String fileName) {
URL fileUrl;
fileUrl = ClassLoader.getSystemResource(fileName);
if (fileUrl == null) {
System.out.printf("File (%s) doesn't exist!\n", fileName);
System.exit(0);
}
file = new File(fileUrl.getFile());
}
void runUnionFind() {
FileReader fileReader;
BufferedReader bufferedReader;
String line;
String[] pairOfIntegers;
int p, q;
try {
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
setNumOfObjects(Integer.parseInt(bufferedReader.readLine()));
while ((line = bufferedReader.readLine()) != null) {
pairOfIntegers = line.trim().split("\\s+");
if (pairOfIntegers.length != 2) {
System.out.println("Pair should have two objects.");
System.exit(0);
}
p = Integer.parseInt(pairOfIntegers[0]);
q = Integer.parseInt(pairOfIntegers[1]);
System.out.printf("\nconnected(%d, %d)\n", p, q);
if (!connected(p, q)) {
union(p, q);
System.out.printf("Not connected!\nunion(%d, %d)\n", p, q);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Please specify input file.");
System.exit(0);
}
String fileName = args[0];
UnionFind uf = new UnionFind();
uf.getFile(fileName);
uf.runUnionFind();
uf.listConnectedComponent();
}
}