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