blob: adc66a5e2e5b51737c6710e64fc7204362a53c5c [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tom0633d682014-09-10 12:10:03 -070019package org.onlab.graph;
20
21import org.junit.Test;
22
23import static com.google.common.collect.ImmutableSet.of;
24import static org.junit.Assert.assertEquals;
25import static org.onlab.graph.TarjanGraphSearch.SCCResult;
26
27/**
28 * Tarjan graph search tests.
29 */
30public class TarjanGraphSearchTest extends GraphTest {
31
32 private void validate(SCCResult<TestVertex, TestEdge> result, int cc) {
33 System.out.println("Cluster count: " + result.clusterVertexes().size());
34 System.out.println("Clusters: " + result.clusterVertexes());
35 assertEquals("incorrect cluster count", cc, result.clusterCount());
36 }
37
38 private void validate(SCCResult<TestVertex, TestEdge> result,
39 int i, int vc, int ec) {
40 assertEquals("incorrect cluster count", vc, result.clusterVertexes().get(i).size());
41 assertEquals("incorrect edge count", ec, result.clusterEdges().get(i).size());
42 }
43
44 @Test
45 public void basic() {
46 graph = new AdjacencyListsGraph<>(vertexes(), edges());
47 TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
48 SCCResult<TestVertex, TestEdge> result = gs.search(graph, null);
49 validate(result, 6);
50 }
51
52 @Test
53 public void singleCluster() {
54 graph = new AdjacencyListsGraph<>(vertexes(),
55 of(new TestEdge(A, B, 1),
56 new TestEdge(B, C, 1),
57 new TestEdge(C, D, 1),
58 new TestEdge(D, E, 1),
59 new TestEdge(E, F, 1),
60 new TestEdge(F, G, 1),
61 new TestEdge(G, H, 1),
62 new TestEdge(H, A, 1)));
63
64 TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
65 SCCResult<TestVertex, TestEdge> result = gs.search(graph, null);
66 validate(result, 1);
67 validate(result, 0, 8, 8);
68 }
69
70 @Test
71 public void twoUnconnectedCluster() {
72 graph = new AdjacencyListsGraph<>(vertexes(),
73 of(new TestEdge(A, B, 1),
74 new TestEdge(B, C, 1),
75 new TestEdge(C, D, 1),
76 new TestEdge(D, A, 1),
77 new TestEdge(E, F, 1),
78 new TestEdge(F, G, 1),
79 new TestEdge(G, H, 1),
80 new TestEdge(H, E, 1)));
81 TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
82 SCCResult<TestVertex, TestEdge> result = gs.search(graph, null);
83 validate(result, 2);
84 validate(result, 0, 4, 4);
85 validate(result, 1, 4, 4);
86 }
87
88 @Test
89 public void twoWeaklyConnectedClusters() {
90 graph = new AdjacencyListsGraph<>(vertexes(),
91 of(new TestEdge(A, B, 1),
92 new TestEdge(B, C, 1),
93 new TestEdge(C, D, 1),
94 new TestEdge(D, A, 1),
95 new TestEdge(E, F, 1),
96 new TestEdge(F, G, 1),
97 new TestEdge(G, H, 1),
98 new TestEdge(H, E, 1),
99 new TestEdge(B, E, 1)));
100 TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
101 SCCResult<TestVertex, TestEdge> result = gs.search(graph, null);
102 validate(result, 2);
103 validate(result, 0, 4, 4);
104 validate(result, 1, 4, 4);
105 }
106
107 @Test
108 public void twoClustersConnectedWithIgnoredEdges() {
109 graph = new AdjacencyListsGraph<>(vertexes(),
110 of(new TestEdge(A, B, 1),
111 new TestEdge(B, C, 1),
112 new TestEdge(C, D, 1),
113 new TestEdge(D, A, 1),
114 new TestEdge(E, F, 1),
115 new TestEdge(F, G, 1),
116 new TestEdge(G, H, 1),
117 new TestEdge(H, E, 1),
118 new TestEdge(B, E, -1),
119 new TestEdge(E, B, -1)));
120
121 TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
122 SCCResult<TestVertex, TestEdge> result = gs.search(graph, weight);
123 validate(result, 2);
124 validate(result, 0, 4, 4);
125 validate(result, 1, 4, 4);
126 }
127
128}