TarjanGraphSearchTest.java
4.92 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
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
package org.onlab.graph;
import org.junit.Test;
import static com.google.common.collect.ImmutableSet.of;
import static org.junit.Assert.assertEquals;
import static org.onlab.graph.TarjanGraphSearch.SCCResult;
/**
* Tarjan graph search tests.
*/
public class TarjanGraphSearchTest extends GraphTest {
private void validate(SCCResult<TestVertex, TestEdge> result, int cc) {
System.out.println("Cluster count: " + result.clusterVertexes().size());
System.out.println("Clusters: " + result.clusterVertexes());
assertEquals("incorrect cluster count", cc, result.clusterCount());
}
private void validate(SCCResult<TestVertex, TestEdge> result,
int i, int vc, int ec) {
assertEquals("incorrect cluster count", vc, result.clusterVertexes().get(i).size());
assertEquals("incorrect edge count", ec, result.clusterEdges().get(i).size());
}
@Test
public void basic() {
graph = new AdjacencyListsGraph<>(vertexes(), edges());
TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
SCCResult<TestVertex, TestEdge> result = gs.search(graph, null);
validate(result, 6);
}
@Test
public void singleCluster() {
graph = new AdjacencyListsGraph<>(vertexes(),
of(new TestEdge(A, B, 1),
new TestEdge(B, C, 1),
new TestEdge(C, D, 1),
new TestEdge(D, E, 1),
new TestEdge(E, F, 1),
new TestEdge(F, G, 1),
new TestEdge(G, H, 1),
new TestEdge(H, A, 1)));
TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
SCCResult<TestVertex, TestEdge> result = gs.search(graph, null);
validate(result, 1);
validate(result, 0, 8, 8);
}
@Test
public void twoUnconnectedCluster() {
graph = new AdjacencyListsGraph<>(vertexes(),
of(new TestEdge(A, B, 1),
new TestEdge(B, C, 1),
new TestEdge(C, D, 1),
new TestEdge(D, A, 1),
new TestEdge(E, F, 1),
new TestEdge(F, G, 1),
new TestEdge(G, H, 1),
new TestEdge(H, E, 1)));
TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
SCCResult<TestVertex, TestEdge> result = gs.search(graph, null);
validate(result, 2);
validate(result, 0, 4, 4);
validate(result, 1, 4, 4);
}
@Test
public void twoWeaklyConnectedClusters() {
graph = new AdjacencyListsGraph<>(vertexes(),
of(new TestEdge(A, B, 1),
new TestEdge(B, C, 1),
new TestEdge(C, D, 1),
new TestEdge(D, A, 1),
new TestEdge(E, F, 1),
new TestEdge(F, G, 1),
new TestEdge(G, H, 1),
new TestEdge(H, E, 1),
new TestEdge(B, E, 1)));
TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
SCCResult<TestVertex, TestEdge> result = gs.search(graph, null);
validate(result, 2);
validate(result, 0, 4, 4);
validate(result, 1, 4, 4);
}
@Test
public void twoClustersConnectedWithIgnoredEdges() {
graph = new AdjacencyListsGraph<>(vertexes(),
of(new TestEdge(A, B, 1),
new TestEdge(B, C, 1),
new TestEdge(C, D, 1),
new TestEdge(D, A, 1),
new TestEdge(E, F, 1),
new TestEdge(F, G, 1),
new TestEdge(G, H, 1),
new TestEdge(H, E, 1),
new TestEdge(B, E, -1),
new TestEdge(E, B, -1)));
TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
SCCResult<TestVertex, TestEdge> result = gs.search(graph, weight);
validate(result, 2);
validate(result, 0, 4, 4);
validate(result, 1, 4, 4);
}
}