-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCliqueTest.java
78 lines (72 loc) · 2.9 KB
/
CliqueTest.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
import org.junit.Test;
import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;
import java.util.zip.CRC32;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;
public class CliqueTest {
private boolean[][] createRandomAdjacencyMatrix(int n, int edges, Random rng) {
ArrayList<Integer> indices = new ArrayList<>();
for(int i = 0; i < n; i++) {
indices.add(i);
}
boolean[][] adjacencyMatrix = new boolean[n][n];
int pct = rng.nextInt(40) + 40;
while(edges > 0) {
Collections.shuffle(indices, rng);
int upTo = 1;
while(upTo < n && rng.nextInt(100) < pct) { upTo++; }
for(int i = 1; i < upTo; i++) {
int u = indices.get(i);
for(int j = 0; j < i; j++) {
int v = indices.get(j);
if(!adjacencyMatrix[u][v]) {
edges--;
adjacencyMatrix[u][v] = true;
adjacencyMatrix[v][u] = true;
}
}
}
}
return adjacencyMatrix;
}
@Test public void testFindFirstCliqueOneHundred() {
testClique(100, 2899382865L);
}
@Test public void testFindFirstCliqueFiveHundred() {
testClique(500, 1058405530L);
}
@Test public void testFindFirstCliqueOneThousand() {
testClique(1000, 2204406524L);
}
private void testClique(int trials, long expected) {
Random rng = new Random(12345 + trials);
CRC32 check = new CRC32();
int n = 3, count = 0, goal = 1;
for(int i = 0; i < trials; i++) {
int ee = (n*n) / 4;
int edges = ee + rng.nextInt(1 + ee/2);
boolean[][] adjacencyMatrix = createRandomAdjacencyMatrix(n, edges, rng);
int[] clique = Clique.findFirstClique(adjacencyMatrix);
assertNotNull(clique);
// Verify that the found clique actually is a clique.
for(int ii = 0; ii < clique.length; ii++) {
for(int jj = ii+1; jj < clique.length; jj++) {
// All nodes in the clique are distinct.
assertNotEquals(clique[ii], clique[jj]);
// All nodes in the clique are connected by an edge.
assertTrue(adjacencyMatrix[clique[ii]][clique[jj]]);
}
}
// Update the checksum with the elements of the found clique.
for(int u: clique) { check.update(u); }
// Increment the problem size counter.
if(++count == goal) { count = 0; goal++; n++; }
}
// Just to make sure that you always returned the lexicographically first clique.
assertEquals(expected, check.getValue());
}
}