-
Notifications
You must be signed in to change notification settings - Fork 0
/
Relation.java
352 lines (313 loc) · 10.4 KB
/
Relation.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import java.util.ArrayList;
// Java object representing a relation of a set of n elements. The relation is modeled via a matrix, where
// if the element in the ith row and jth column is 1, then i relates j, and if not, i does not relate j.
public class Relation {
// Rather than a 2D array of booleans, the matrix is instead an array of BitArray objects. BitArray is
// effectively a variable-length array of booleans, but stores values as bits of a 32-bit integer.
// This is because Java needs to use a byte to store a boolean value (sometimes more, depending on the
// implementation), so for large arrays of booleans memory usage increases dramatically.
private BitArray[] m;
// After calculating if the relation is reflexive/symmetric/antisymmetric/transitive, the relation
// remembers the results of that check to save processing time if that property is checked again
// before the relation is modified. If the relation is modified, it resets these fields to null.
private Boolean isReflexive;
private Boolean isSymmetric;
private Boolean isAntisymmetric;
private Boolean isTransitive;
// Similarly, it will also remember the equivalence classes and transitive closure after they are
// computed, so long as the relation is not changed.
private Integer[][] equivClasses;
private Relation transitiveClosure;
// Default constructor
public Relation(int nElements) {
m = new BitArray[nElements];
for(int i = 0; i < m.length; i++) {
m[i] = new BitArray(nElements);
}
}
// Copy constructor, initializes itself with the same values as the given relation.
public Relation(Relation r) {
m = new BitArray[r.m.length];
for(int i = 0; i < m.length; i++) {
m[i] = new BitArray(r.m[i]);
}
}
// Returns true if the relation is reflexive.
public boolean isReflexive() {
// If reflexiveness has already been checked, return the previous result.
if(isReflexive != null)
return isReflexive;
// Check reflexiveness by checking the diagonal entries of the matrix.
isReflexive = false;
for(int i = 0; i < m.length; i++) {
if(!m[i].get(i))
return false;
}
isReflexive = true;
return true;
}
// Returns true if the relation is symmetric.
public boolean isSymmetric() {
// If symmetry has not been checked, run the method to check it.
if(isSymmetric == null)
updateSymmetry();
return isSymmetric;
}
// Returns true if the relation is antisymmetric.
public boolean isAntisymmetric() {
// If symmetry has not been checked, run the method to check it.
if(isAntisymmetric == null)
updateSymmetry();
return isAntisymmetric;
}
// This is a helper method to update the values of isSymmetric and isAntisymmetric. The algorithm used
// to check either one is very similar, so it is more convenient to check them both at the same time.
private void updateSymmetry() {
isSymmetric = true;
isAntisymmetric = true;
for(int i = 0; i < m.length; i++) {
for(int j = 0; j <= i; j++) {
if(i != j) {
boolean b = m[i].get(j);
if(b ^ m[j].get(i)) {
isSymmetric = false;
if(!isAntisymmetric)
return;
} else if(b) {
isAntisymmetric = false;
if(!isSymmetric)
return;
}
}
}
}
}
// Returns true if the relation is transitive.
public boolean isTransitive() {
if(isTransitive != null)
return isTransitive;
transitiveClosure();
return isTransitive;
}
// Returns true if the relation is an equivalence matrix.
public boolean isEquivalence() {
// A relation is an equivalence matrix if and only if it is reflexive, symmetric, and transitive.
return isReflexive() && isSymmetric() && isTransitive();
}
// Returns true if the ath element relates the bth element.
public boolean relates(int a, int b) {
if(a >= m.length || b >= m.length)
return false;
return m[a].get(b);
}
// Sets the relation such that the ath element relates the bth element.
public void set(int a, int b, boolean value) {
resetProperties();
if(a >= m.length || b >= m.length)
return;
m[a].set(b, value);
}
// Returns a relation that is the transitive closure of this relation.
public Relation transitiveClosure() {
if(transitiveClosure != null)
return transitiveClosure;
transitiveClosure = new Relation(this);
if(isTransitive != null && isTransitive)
return transitiveClosure;
isTransitive = transitiveClosure.applyWarshall();
return transitiveClosure;
}
// This method applies Warshall's algorithm to this matrix.
// It does so by making a list of every column containing a one in the ith row and a list of every row
// containing a one in the ith column. It then sets every (row, column) pair from those lists to one.
// It repeats this for every value of i, where 0 < i < # of elements.
// This method returns true if no changes are made.
private boolean applyWarshall() {
boolean wasTransitive = true;
ArrayList<Integer> rows = new ArrayList<>();
ArrayList<Integer> columns = new ArrayList<>();
for(int i = 0; i < m.length; i++) {
for(int j = 0; j < m.length; j++) {
if(m[i].get(j))
columns.add(j);
if(m[j].get(i))
rows.add(j);
}
for(int row : rows) {
for(int column : columns) {
if(!m[row].get(column)) {
wasTransitive = false;
m[row].set(column, true);
}
}
}
rows.clear();
columns.clear();
}
isTransitive = true;
return wasTransitive;
}
// Returns every equivalence class of the given relation. If the relation is not an equivalence
// matrix, this returns null.
public Integer[][] equivClasses() {
if(equivClasses != null)
return equivClasses;
if(!isEquivalence())
return null;
// List of equivalence classes that have been found.
ArrayList<Integer[]> classesList = new ArrayList<>();
// List of values who have already had their equivalences checked.
ArrayList<Integer> foundValues = new ArrayList<>();
for(int i = 0; i < m.length; i++) {
if(!foundValues.contains(i)) {
// Initialize an ArrayList to store this new class, then fill it with all matching
// elements.
ArrayList<Integer> ar = new ArrayList<>();
ar.add(i);
findEquivalences(i, ar);
// Convert the class to an array of Integers, then add it to the list of classes.
classesList.add(ar.toArray(new Integer[ar.size()]));
// Add every value from the class into the list of found values.
foundValues.addAll(ar);
}
}
// Convert the list of classes to an array.
equivClasses = classesList.toArray(new Integer[classesList.size()][]);
return equivClasses;
}
// This method finds every one in the nth column of the matrix, and adds its column to the list of
// values that relate to the nth element.
private void findEquivalences(int n, ArrayList<Integer> ar) {
for(int i = n; i < m.length; i++) {
if(m[n].get(i)) {
// Check if the list already contains i, and if not, add it to the list and find all
// values related to i.
if(!ar.contains(i)) {
ar.add(i);
findEquivalences(i, ar);
}
}
}
}
// Convert the equivalence classes to a string for convenient printing.
public String equivClassesAsString() {
// Make sure the equivalence classes is initialized, and that it is, indeed, an equivalence
// relation.
equivClasses();
if(equivClasses == null)
return "(n/a)";
String s = "{";
for(int i = 0; i < equivClasses.length; i++) {
s += "{" + (equivClasses[i][0] + 1);
for(int j = 1; j < equivClasses[i].length; j++) {
s += "," + (equivClasses[i][j] + 1);
}
s += "}";
if(equivClasses.length - i > 1)
s += ",";
}
return s + "}";
}
// Reset the properties to null, so their respective methods know they need to be rechecked.
private void resetProperties() {
isReflexive = null;
isSymmetric = null;
isAntisymmetric = null;
isTransitive = null;
equivClasses = null;
transitiveClosure = null;
}
// Return the number of elements in the relation.
public int nElements() {
return m.length;
}
// Returns the relation's matrix as a string, using only ASCII characters (for compatibility).
private String toStringAscii() {
String s = " _";
String whitespace = " ";
for(int i = 0; i < m.length; i++) {
whitespace += " ";
}
s += whitespace + "_ ";
for(int i = 0; i < m.length; i++) {
if(i + 1 == m.length) {
s += "\n|_ ";
} else {
s += "\n| ";
}
for(int j = 0; j < m.length; j++) {
s += m[i].get(j) ? "1" : "0";
s += " ";
}
if(i + 1 == m.length) {
s += "_|";
} else {
s += " |";
}
}
return s;
}
// Returns the relation's matrix as a string, making use of Unicode box-drawing characters.
private String toStringUnicode() {
String s = "┌";
String whitespace = " ";
for(int i = 0; i < m.length; i++) {
whitespace += " ";
}
s += whitespace + "┐";
for(int i = 0; i < m.length; i++) {
s += "\n│ ";
for(int j = 0; j < m.length; j++) {
s += m[i].get(j) ? "1" : "0";
s += " ";
}
s += " │";
}
s += "\n└" + whitespace + "┘";
return s;
}
// Default toString method (uses Unicode by default).
public String toString() {
return toStringUnicode();
}
// Convert to string, with the given text format.
public String toString(boolean useUnicode) {
if(useUnicode)
return toStringUnicode();
return toStringAscii();
}
// This class is a way to store an array of boolean values in a more memory-efficient format, using
// bits of 32-bit integers to store data.
private class BitArray {
private int length;
private int[] data;
// Constructor
private BitArray(int length) {
this.length = length;
data = new int[length/32 + (length%32 == 0 ? 0 : 1)];
}
// Copy constructor
private BitArray(BitArray b) {
length = b.length;
data = b.data.clone();
}
// Get the ith boolean of the array
private boolean get(int i) {
if(i >= length)
return false;
return (data[i/32] & (1 << (i%32))) != 0;
}
// Set the ith boolean of the array to the given value
private boolean set(int i, boolean val) {
if(i >= length)
return false;
int n = 1 << (i%32);
if(val) {
data[i/32] = data[i/32] | n;
} else {
data[i/32] = data[i/32] & ~n;
}
return true;
}
}
}