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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
/* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ------------------------------
* CycleDetectorTest.java
* ------------------------------
* (C) Copyright 2003-2008, by John V. Sichi and Contributors.
*
* Original Author: John V. Sichi
* Contributor(s): Khanh Vu
*
* $Id: CycleDetectorTest.java 745 2011-06-07 05:50:57Z perfecthash $
*
* Changes
* -------
* 16-Sept-2004 : Initial revision (JVS);
*
*/
package org.jgrapht.alg;
import java.util.*;
import junit.framework.*;
import org.jgrapht.*;
import org.jgrapht.graph.*;
/**
* .
*
* @author John V. Sichi
*/
public class CycleDetectorTest
extends TestCase
{
//~ Static fields/initializers ---------------------------------------------
private static final String V1 = "v1";
private static final String V2 = "v2";
private static final String V3 = "v3";
private static final String V4 = "v4";
private static final String V5 = "v5";
private static final String V6 = "v6";
private static final String V7 = "v7";
//~ Methods ----------------------------------------------------------------
/**
* .
*
* @param g
*/
public void createGraph(Graph<String, DefaultEdge> g)
{
g.addVertex(V1);
g.addVertex(V2);
g.addVertex(V3);
g.addVertex(V4);
g.addVertex(V5);
g.addVertex(V6);
g.addVertex(V7);
g.addEdge(V1, V2);
g.addEdge(V2, V3);
g.addEdge(V3, V4);
g.addEdge(V4, V1);
g.addEdge(V4, V5);
g.addEdge(V5, V6);
g.addEdge(V1, V6);
// test an edge which leads into a cycle, but where the source
// is not itself part of a cycle
g.addEdge(V7, V1);
}
/**
* .
*/
public void testDirectedWithCycle()
{
DirectedGraph<String, DefaultEdge> g =
new DefaultDirectedGraph<String, DefaultEdge>(
DefaultEdge.class);
createGraph(g);
Set<String> cyclicSet = new HashSet<String>();
cyclicSet.add(V1);
cyclicSet.add(V2);
cyclicSet.add(V3);
cyclicSet.add(V4);
Set<String> acyclicSet = new HashSet<String>();
acyclicSet.add(V5);
acyclicSet.add(V6);
acyclicSet.add(V7);
runTest(g, cyclicSet, acyclicSet);
}
/**
* .
*/
public void testDirectedWithDoubledCycle()
{
DirectedGraph<String, DefaultEdge> g =
new DefaultDirectedGraph<String, DefaultEdge>(
DefaultEdge.class);
// build the graph: vertex order is chosen specifically
// to exercise old bug-cases in CycleDetector
g.addVertex(V2);
g.addVertex(V1);
g.addVertex(V3);
g.addEdge(V1, V2);
g.addEdge(V2, V3);
g.addEdge(V3, V1);
g.addEdge(V2, V1);
Set<String> cyclicSet = new HashSet<String>();
cyclicSet.add(V1);
cyclicSet.add(V2);
cyclicSet.add(V3);
Set<String> acyclicSet = new HashSet<String>();
runTest(g, cyclicSet, acyclicSet);
}
/**
* .
*/
@SuppressWarnings("unchecked")
public void testDirectedWithoutCycle()
{
DirectedGraph<String, DefaultEdge> g =
new DefaultDirectedGraph<String, DefaultEdge>(
DefaultEdge.class);
createGraph(g);
g.removeVertex(V2);
Set<String> cyclicSet = Collections.EMPTY_SET; // hb: I would like
// EMPTY_SET to be typed
// as well...
Set<String> acyclicSet = g.vertexSet();
runTest(g, cyclicSet, acyclicSet);
}
private void runTest(
DirectedGraph<String, DefaultEdge> g,
Set<String> cyclicSet,
Set<String> acyclicSet)
{
CycleDetector<String, DefaultEdge> detector =
new CycleDetector<String, DefaultEdge>(g);
Set emptySet = Collections.EMPTY_SET;
assertEquals(!cyclicSet.isEmpty(), detector.detectCycles());
assertEquals(cyclicSet, detector.findCycles());
for (String v : cyclicSet) {
assertEquals(true, detector.detectCyclesContainingVertex(v));
assertEquals(cyclicSet, detector.findCyclesContainingVertex(v));
}
for (String v : acyclicSet) {
assertEquals(false, detector.detectCyclesContainingVertex(v));
assertEquals(emptySet, detector.findCyclesContainingVertex(v));
}
}
public void testVertexEquals()
{
DefaultDirectedGraph<String, DefaultEdge> graph =
new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
assertEquals(0, graph.edgeSet().size());
String vertexA = "A";
String vertexB = "B";
String vertexC = new StringBuffer("A").toString();
assertNotSame(vertexA, vertexC);
graph.addVertex(vertexA);
graph.addVertex(vertexB);
graph.addEdge(vertexA, vertexB);
graph.addEdge(vertexB, vertexC);
assertEquals(2, graph.edgeSet().size());
assertEquals(2, graph.vertexSet().size());
CycleDetector<String, DefaultEdge> cycleDetector =
new CycleDetector<String, DefaultEdge>(graph);
Set<String> cycleVertices = cycleDetector.findCycles();
boolean foundCycle =
cycleDetector.detectCyclesContainingVertex(vertexA);
boolean foundVertex = graph.containsVertex(vertexA);
Set<String> subCycle =
cycleDetector.findCyclesContainingVertex(vertexA);
assertEquals(2, cycleVertices.size());
assertEquals(2, subCycle.size()); // fails with zero items
assertTrue(foundCycle); // fails with no cycle found which includes
// vertexA
assertTrue(foundVertex);
}
}
// End CycleDetectorTest.java
|