-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathEquivClasses.java
68 lines (59 loc) · 1.57 KB
/
EquivClasses.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
public class EquivClasses {
static int N = 7;
static int A[] = {1,2,2,3,5,6};
static int B[] = {2,4,1,0,2,0};
// @include
/*
* A and B encode pairwise equivalences on a cardinality N set whose elements
* are indexed by 0,1,2,...,N-1
*
* For example A[5] = 6 and B[5] = 0 indicates that the sixth and
* zeroth elements are to be grouped into the same equivalence class
*
* We return the weakest equivalence relation implied by A and B in an array
* F of length N; F[i] holds the smallest index of all the elements that
* i is equivalent to.
*/
public static int [] ComputeEquivalenceClasses(int N, int A[], int B[]) {
int[] F = new int[N];
int numEquivalentPairs = A.length;
for (int i = 0 ; i < F.length; ++i) {
F[i] = i;
}
for (int k = 0 ; k < numEquivalentPairs; ++k) {
int i = A[k];
while (F[i] != i) {
i = F[i];
}
int j = B[k];
while (F[j] != j) {
j = F[j];
}
if (i != j) {
if (i < j) {
F[j] = i;
} else {
F[i] = j;
}
}
}
for (int i = 0 ; i < N; ++i) {
while (F[i] != F[F[i]]) {
F[i] = F[F[i]];
}
}
return F;
}
// @exclude
public static void main(String[] args) {
int F[] = ComputeEquivalenceClasses(N, A, B);
for (int i = 0 ; i < N; i++)
System.out.println("F[" + i + "] = " + F[i] );
}
static void print(String s, int [] F) {
System.out.println(s);
for (int i = 0 ; i < N; i++) {
System.out.println("F[" + i + "] = " + F[i] );
}
}
}