diff --git a/build-test.xml b/build-test.xml index 731004a..0d0c74c 100644 --- a/build-test.xml +++ b/build-test.xml @@ -12,6 +12,7 @@ + @@ -24,6 +25,9 @@ depends="compile-test" description="runs (junit) unit tests"> + + + Running all JUnit tests @@ -31,7 +35,7 @@ - + diff --git a/build.xml b/build.xml index be1ed97..f675014 100644 --- a/build.xml +++ b/build.xml @@ -1,27 +1,29 @@ - - - + + + + + diff --git a/build.properties b/default.properties similarity index 76% rename from build.properties rename to default.properties index 4fdc8ee..8efa67d 100644 --- a/build.properties +++ b/default.properties @@ -19,3 +19,7 @@ compile.optimize = true # Test defaults test.failonerror = true test.runner = junit.textui.TestRunner +test.reportDirectory = test-reports + +commons-collections.jar = ${lib.repo}/commons-collections.jar +junit.jar = ${lib.repo}/junit-3.7.jar diff --git a/src/java/org/apache/commons/graph/BFS.java b/src/java/org/apache/commons/graph/BFS.java index b60a0b0..341d36a 100644 --- a/src/java/org/apache/commons/graph/BFS.java +++ b/src/java/org/apache/commons/graph/BFS.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -68,7 +68,7 @@ import org.apache.commons.graph.visitor.Visitor; * outgoing edges of the vertex. Extremes are searched last. This is typically * implemented with a queue. * - * @version $Id: BFS.java,v 1.4 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: BFS.java,v 1.5 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ public class BFS extends TraversalOrder { diff --git a/src/java/org/apache/commons/graph/DFS.java b/src/java/org/apache/commons/graph/DFS.java index 329a5ac..c09c66d 100644 --- a/src/java/org/apache/commons/graph/DFS.java +++ b/src/java/org/apache/commons/graph/DFS.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -67,7 +67,7 @@ import org.apache.commons.graph.visitor.Visitor; * (2) An algorithm which marks all vertices in a directed graph in the order they are * discovered and finished, partitioning the graph into a forest. * - * @version $Id: DFS.java,v 1.4 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: DFS.java,v 1.5 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ public class DFS extends TraversalOrder { diff --git a/src/java/org/apache/commons/graph/DirectedGraph.java b/src/java/org/apache/commons/graph/DirectedGraph.java new file mode 100644 index 0000000..5e55d15 --- /dev/null +++ b/src/java/org/apache/commons/graph/DirectedGraph.java @@ -0,0 +1,68 @@ +package org.apache.commons.graph; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Commons" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Commons", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * DirectedGraph + * + * This is a graph for which the edges only flow one way. + */ +public class DirectedGraph extends Graph +{ + public DirectedGraph() + { + super( true ); + } +} diff --git a/src/java/org/apache/commons/graph/Edge.java b/src/java/org/apache/commons/graph/Edge.java index 4364b74..b2560b3 100644 --- a/src/java/org/apache/commons/graph/Edge.java +++ b/src/java/org/apache/commons/graph/Edge.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -62,11 +62,11 @@ import org.apache.commons.graph.visitor.Visitor; * Describes an edge between two vertices of a graph, i.e. a relation between * two arbitrary vertices. * - * @version $Id: Edge.java,v 1.3 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: Edge.java,v 1.4 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ public class Edge - implements Cloneable,Serializable + implements Cloneable,Serializable, Comparable { /** @@ -142,4 +142,9 @@ public class Edge Edge e = (Edge)o; return e.ident == ident; } + + public int compareTo( Object o ) { + return 0; + } + } diff --git a/src/java/org/apache/commons/graph/Graph.java b/src/java/org/apache/commons/graph/Graph.java index 2b78f78..0131dd2 100644 --- a/src/java/org/apache/commons/graph/Graph.java +++ b/src/java/org/apache/commons/graph/Graph.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -65,7 +65,7 @@ import org.apache.commons.graph.visitor.PrintVisitor; /** * Describes a generic graph G = (V, E), i.e. a set of vertices and edges. * - * @version $Id: Graph.java,v 1.4 2001/12/02 19:23:40 jvanzyl Exp $ + * @version $Id: Graph.java,v 1.5 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ public class Graph implements Cloneable, Serializable { diff --git a/src/java/org/apache/commons/graph/GraphAlgorithms.java b/src/java/org/apache/commons/graph/GraphAlgorithms.java index a93b44a..d290c7c 100644 --- a/src/java/org/apache/commons/graph/GraphAlgorithms.java +++ b/src/java/org/apache/commons/graph/GraphAlgorithms.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -55,11 +55,13 @@ package org.apache.commons.graph; */ import java.util.*; +import org.apache.commons.graph.algo.*; +import org.apache.commons.graph.exception.*; /** * Collection of graph algorithms such as transposeGraph. * - * @version $Id: GraphAlgorithms.java,v 1.2 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: GraphAlgorithms.java,v 1.3 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ public abstract class GraphAlgorithms @@ -67,11 +69,8 @@ public abstract class GraphAlgorithms /** * Compute the inverse of a graph, i.e. with all edges reverted */ - public static final Graph transposeGraph(Graph g) { - if(!g.isDirected()) - throw new GraphError("Can only transpose directed graphs"); - - Graph graph = new Graph(true); + public static final Graph transposeGraph(DirectedGraph g) { + DirectedGraph graph = new DirectedGraph(); for(Iterator i=g.getVertices().iterator(); i.hasNext();) graph.addVertex((Vertex)i.next()); @@ -85,6 +84,52 @@ public abstract class GraphAlgorithms return graph; } + /** + * Get a Minimum Spanning Forest from a graph. + */ + public static MinimumSpanningForest + getMinimumSpanningForest( Graph g ) { + return MinimumSpanningForest.getMSF( g ); + } + + public static MinimumSpanningForest + getMinimumSpanningForest( Graph g, boolean isMin ) { + return MinimumSpanningForest.getMSF( g, isMin ); + } + + public static MinimumSpanningForest + getMSF( Graph g ) { + return MinimumSpanningForest.getMSF( g ); + } + + public static MinimumSpanningForest + getMSF( Graph g, boolean isMin ) { + return MinimumSpanningForest.getMSF( g, isMin ); + } + + /** + * Perform a Topological Sort on a Directed + * graph. + * + * Should throw BackEdgeException if it is not a DAG. + */ + public static List topoSort( DirectedGraph g ) { + return Sort.getInstance().topologicalSort( g ); + } + public static List topologicalSort( DirectedGraph g ) { + return Sort.getInstance().topologicalSort( g ); + } + + /** + * Identify all backedges in a directed graph + * when starting from a particular vertex. + */ + public static Set getBackEdges( DirectedGraph g, Vertex start ) + throws BackEdgeException + { + return BackEdges.getInstance().getBackEdges( g, start ); + } + /** * Test whether the SCC g is a loop. * diff --git a/src/java/org/apache/commons/graph/GraphConstants.java b/src/java/org/apache/commons/graph/GraphConstants.java index c6284b7..f86c6c2 100644 --- a/src/java/org/apache/commons/graph/GraphConstants.java +++ b/src/java/org/apache/commons/graph/GraphConstants.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -57,7 +57,7 @@ package org.apache.commons.graph; /** * GraphConstants useful for graph algorithms. * - * @version $Id: GraphConstants.java,v 1.1 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: GraphConstants.java,v 1.2 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ diff --git a/src/java/org/apache/commons/graph/GraphError.java b/src/java/org/apache/commons/graph/GraphError.java index c2f3cfc..93e090b 100644 --- a/src/java/org/apache/commons/graph/GraphError.java +++ b/src/java/org/apache/commons/graph/GraphError.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -55,7 +55,7 @@ package org.apache.commons.graph; */ /** - * @version $Id: GraphError.java,v 1.1 2001/11/12 16:05:18 jvanzyl Exp $ + * @version $Id: GraphError.java,v 1.2 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ public class GraphError extends RuntimeException { diff --git a/src/java/org/apache/commons/graph/Graphable.java b/src/java/org/apache/commons/graph/Graphable.java new file mode 100644 index 0000000..c469771 --- /dev/null +++ b/src/java/org/apache/commons/graph/Graphable.java @@ -0,0 +1,77 @@ +package org.apache.commons.graph; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Commons" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Commons", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * Interface that defines a standard Graphable type, implement + * the Graphable interface when you want an arbitrary object type + * to be manipulated like a vertex in a graph. + * + * @author Jason van Zyl + * @version $Id: Graphable.java,v 1.1 2002/01/16 20:38:28 jvanzyl Exp $ + */ + +public interface Graphable +{ + /** + * Get the name of the Graphable object. We are not + * using toString() because what might be returned + * might not be suitable in a visual graph. For example, + * an XML type object's toString() method might return + * an XML document. A simple getName() might not be + * enough either but it will do for now. + */ + String getName(); +} diff --git a/src/java/org/apache/commons/graph/Label.java b/src/java/org/apache/commons/graph/Label.java new file mode 100644 index 0000000..bd5de6a --- /dev/null +++ b/src/java/org/apache/commons/graph/Label.java @@ -0,0 +1,125 @@ +package org.apache.commons.graph; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Commons" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Commons", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * Label + * + * This is a special Label. + * + * You can either set it to a string, or you can + * set it to another label. + * + * If you set it to another label, it will snap the + * pointers, and really set it to the root. + */ +public class Label extends Object { + private Label root = null; + private String name = null; + + public Label( String name ) { + this.name = name; + } + + public Label( Label root ) { + this.root = root.getRoot(); + } + + public Label getRoot() { + if (root == null) { + return this; + } else { + root = root.getRoot(); + return root; + } + } + + public void setRoot(Label root) { + if ( this.root == null ) { + this.root = root.getRoot(); + } else { + if ( root != this.root ) { + this.root.setRoot( root.getRoot() ); + this.root = root.getRoot(); + } + } + } + + public String getName() { + if (root != null) { + root = root.getRoot(); + return root.getName(); + } else { + return name; + } + } + + public void setName(String name) { + if (root != null) { + root.setName( name ); + } else { + this.name = name; + } + } + + public String toString() { + return getName(); + } + + public boolean equals( Object rhs ) { + Label r = (Label) rhs; + return r.getName() == this.getName(); + } +} diff --git a/src/java/org/apache/commons/graph/LabeledEdge.java b/src/java/org/apache/commons/graph/LabeledEdge.java index b3e920e..06e52c9 100644 --- a/src/java/org/apache/commons/graph/LabeledEdge.java +++ b/src/java/org/apache/commons/graph/LabeledEdge.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -57,7 +57,7 @@ package org.apache.commons.graph; /** * Describes a labeled edge between two vertices of a graph. * - * @version $Id: LabeledEdge.java,v 1.1 2001/11/12 16:05:19 jvanzyl Exp $ + * @version $Id: LabeledEdge.java,v 1.2 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ public class LabeledEdge extends Edge { diff --git a/src/java/org/apache/commons/graph/LabeledVertex.java b/src/java/org/apache/commons/graph/LabeledVertex.java index dd7b70d..9259b7c 100644 --- a/src/java/org/apache/commons/graph/LabeledVertex.java +++ b/src/java/org/apache/commons/graph/LabeledVertex.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -57,7 +57,7 @@ package org.apache.commons.graph; /** * Describes a labeled vertex of a graph. * - * @version $Id: LabeledVertex.java,v 1.1 2001/11/12 16:05:19 jvanzyl Exp $ + * @version $Id: LabeledVertex.java,v 1.2 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ public class LabeledVertex extends Vertex { diff --git a/src/java/org/apache/commons/graph/Sequential.java b/src/java/org/apache/commons/graph/Sequential.java index f85433a..173cf4e 100644 --- a/src/java/org/apache/commons/graph/Sequential.java +++ b/src/java/org/apache/commons/graph/Sequential.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -59,7 +59,7 @@ import org.apache.commons.graph.visitor.Visitor; /** * Visit graph in sequential order, i.e. in the order returned by Graph.getVertices(). * - * @version $Id: Sequential.java,v 1.3 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: Sequential.java,v 1.4 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ public final class Sequential diff --git a/src/java/org/apache/commons/graph/TraversalOrder.java b/src/java/org/apache/commons/graph/TraversalOrder.java index 2d26d03..c8674d9 100644 --- a/src/java/org/apache/commons/graph/TraversalOrder.java +++ b/src/java/org/apache/commons/graph/TraversalOrder.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -63,7 +63,7 @@ import org.apache.commons.graph.visitor.Visitor; * * This relates to the "Strategy" design pattern. * - * @version $Id: TraversalOrder.java,v 1.3 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: TraversalOrder.java,v 1.4 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm * @see DFS * @see BFS diff --git a/src/java/org/apache/commons/graph/UndirectedGraph.java b/src/java/org/apache/commons/graph/UndirectedGraph.java new file mode 100644 index 0000000..760dd8a --- /dev/null +++ b/src/java/org/apache/commons/graph/UndirectedGraph.java @@ -0,0 +1,68 @@ +package org.apache.commons.graph; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Commons" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Commons", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * UndirectedGraph + * + * This represents graphs where edges go both ways. + */ +public class UndirectedGraph + extends Graph +{ + public UndirectedGraph() { + super( false ); + } +} diff --git a/src/java/org/apache/commons/graph/VQueue.java b/src/java/org/apache/commons/graph/VQueue.java index e00e692..a5ecdd0 100644 --- a/src/java/org/apache/commons/graph/VQueue.java +++ b/src/java/org/apache/commons/graph/VQueue.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -59,7 +59,7 @@ package org.apache.commons.graph; * Can be extended, e.g., to implement a priority queue, so that the traversal * order of the BFS algorithm may be influenced. * - * @version $Id: VQueue.java,v 1.1 2001/11/12 16:05:20 jvanzyl Exp $ + * @version $Id: VQueue.java,v 1.2 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ public class VQueue { diff --git a/src/java/org/apache/commons/graph/VStack.java b/src/java/org/apache/commons/graph/VStack.java index 7607071..9cd3068 100644 --- a/src/java/org/apache/commons/graph/VStack.java +++ b/src/java/org/apache/commons/graph/VStack.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -57,7 +57,7 @@ package org.apache.commons.graph; /** * Implements a simple first-in, last-out stack of vertices of fixed size. * - * @version $Id: VStack.java,v 1.2 2001/11/29 14:17:33 jvanzyl Exp $ + * @version $Id: VStack.java,v 1.3 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ public final class VStack { diff --git a/src/java/org/apache/commons/graph/Vertex.java b/src/java/org/apache/commons/graph/Vertex.java index dd4410d..0e11093 100644 --- a/src/java/org/apache/commons/graph/Vertex.java +++ b/src/java/org/apache/commons/graph/Vertex.java @@ -26,12 +26,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -61,7 +61,7 @@ import org.apache.commons.graph.visitor.Visitor; /** * Describes a vertex of a graph. * - * @version $Id: Vertex.java,v 1.3 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: Vertex.java,v 1.4 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ public class Vertex diff --git a/src/java/org/apache/commons/graph/WeightedEdge.java b/src/java/org/apache/commons/graph/WeightedEdge.java index a51f9cc..b4ca1f9 100644 --- a/src/java/org/apache/commons/graph/WeightedEdge.java +++ b/src/java/org/apache/commons/graph/WeightedEdge.java @@ -1,5 +1,6 @@ package org.apache.commons.graph; + /* ==================================================================== * The Apache Software License, Version 1.1 * @@ -26,12 +27,12 @@ package org.apache.commons.graph; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -57,11 +58,12 @@ package org.apache.commons.graph; /** * Describes an edge with a weight. * - * @version $Id: WeightedEdge.java,v 1.2 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: WeightedEdge.java,v 1.3 2002/01/16 20:38:28 jvanzyl Exp $ * @author M. Dahm */ public class WeightedEdge extends Edge + implements Comparable { private int weight; @@ -88,4 +90,10 @@ public class WeightedEdge public void addToWeight(int w) { weight += w; } public void setWeight(int weight) { this.weight = weight; } public int getWeight() { return weight; } + + public int compareTo( Object o ) { + if (getWeight() > ((WeightedEdge) o).getWeight()) return 1; + if (getWeight() < ((WeightedEdge) o).getWeight()) return -1; + return 0; + } } diff --git a/src/java/org/apache/commons/graph/algo/BackEdges.java b/src/java/org/apache/commons/graph/algo/BackEdges.java new file mode 100644 index 0000000..e702a48 --- /dev/null +++ b/src/java/org/apache/commons/graph/algo/BackEdges.java @@ -0,0 +1,114 @@ +package org.apache.commons.graph.algo; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Commons" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Commons", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.commons.graph.*; +import org.apache.commons.graph.visitor.DefaultVisitor; +import org.apache.commons.graph.exception.BackEdgeException; + +import java.util.Set; +import java.util.HashSet; + +/** + * FindBackEdges + * + * This algorithm takes a directed graph, and determines + * where the Graph edges are. + * + * It uses the Visitor pattern. + */ +public class BackEdges +{ + private static BackEdges _instance = new BackEdges(); + + private BackEdges() { } + + private class BackEdgeCollector + extends DefaultVisitor + { + private Graph visitingGraph = null; + private Set backEdges = new HashSet(); + + public BackEdgeCollector() { } + + public void discoverGraph( Graph g ) { + visitingGraph = g; + } + + public void discoverEdge( Edge e ) { + Vertex target = visitingGraph.getTarget( e ); + if (target.getColor() != GraphConstants.WHITE) { + backEdges.add( e ); + } + } + + public Set getBackEdges() { + return backEdges; + } + } + + public Set getBackEdges( DirectedGraph g, + Vertex start) + throws BackEdgeException + { + BackEdgeCollector bec = new BackEdgeCollector(); + bec.visit( g, start ); + return bec.getBackEdges(); + } + + public static BackEdges getInstance() { + return _instance; + } +} diff --git a/src/java/org/apache/commons/graph/algo/MinimumSpanningForest.java b/src/java/org/apache/commons/graph/algo/MinimumSpanningForest.java new file mode 100644 index 0000000..257dc63 --- /dev/null +++ b/src/java/org/apache/commons/graph/algo/MinimumSpanningForest.java @@ -0,0 +1,151 @@ +package org.apache.commons.graph.algo; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Commons" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Commons", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.commons.graph.*; +import org.apache.commons.collections.*; + +import java.util.Map; +import java.util.List; +import java.util.Set; +import java.util.HashMap; +import java.util.ArrayList; +import java.util.Iterator; + +public class MinimumSpanningForest + extends UndirectedGraph +{ + private Map roots = new HashMap(); // LBL.getName() X V + private Map vertexLabels = new HashMap(); // V X LBL + private boolean isMin = true; + private Graph original = null; + + private MinimumSpanningForest() { + super(); + } + + private MinimumSpanningForest( boolean isMinimum ) { + isMin = isMinimum; + } + + private void calculate( Graph g ) { + PriorityQueue edgeHeap = + new BinaryHeap(g.getEdges().size(), isMin); + + Iterator i = g.getEdges().iterator(); + while (i.hasNext()) { + edgeHeap.insert( (Comparable) i.next()); + } + + Vertex[] vertices = g.getVertexArray(); + for (int j = 0; j < vertices.length; j++) { + Label newLabel = new Label("" + j); + + roots.put( newLabel.getName(), newLabel ); + vertexLabels.put( vertices[j], newLabel ); + addVertex( vertices[j] ); + } + + while (!edgeHeap.isEmpty()) { + Edge nextEdge = (Edge) edgeHeap.pop(); + Vertex source = g.getSource( nextEdge ); + Vertex target = g.getTarget( nextEdge ); + Label srcLabel = (Label) vertexLabels.get( source ); + Label tgtLabel = (Label) vertexLabels.get( target ); + + // System.err.println("TRYING: " + nextEdge + + // " FROM " + srcLabel + + // " TO " + tgtLabel ); + + if (!srcLabel.equals( tgtLabel )) { + // System.err.println("UNIONING."); + // Add the edge to the forrest. + String oldLabel = tgtLabel.getName(); + roots.remove( oldLabel ); + tgtLabel.setRoot( srcLabel ); + + addEdge( source, target, nextEdge ); + } + } + } + + public Set getTreeRoots() { + return roots.entrySet(); + } + + public static MinimumSpanningForest + getMinimumSpanningForest( Graph g ) + { + return getMinimumSpanningForest( g, true ); + } + + public static MinimumSpanningForest + getMinimumSpanningForest( Graph g, + boolean isMin) { + MinimumSpanningForest msf = + new MinimumSpanningForest( isMin ); + msf.calculate( g ); + return msf; + } + + public static MinimumSpanningForest getMSF( Graph g ) { + return getMinimumSpanningForest( g ); + } + + public static MinimumSpanningForest getMSF( Graph g, + boolean isMin) { + return getMinimumSpanningForest( g, isMin ); + } +} diff --git a/src/java/org/apache/commons/graph/algo/Sort.java b/src/java/org/apache/commons/graph/algo/Sort.java new file mode 100644 index 0000000..8fcd074 --- /dev/null +++ b/src/java/org/apache/commons/graph/algo/Sort.java @@ -0,0 +1,109 @@ +package org.apache.commons.graph.algo; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Commons" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Commons", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.commons.graph.*; +import org.apache.commons.graph.visitor.DefaultVisitor; + +import java.util.List; +import java.util.LinkedList; + +/** + * Sort + * + * This class holds things such as a Topological Sort. + * If there are any other good sorts needed for graphs, + * maybe they should be put in here. + */ +public class Sort +{ + private static Sort _instance = new Sort(); + + private class TopoVisitor extends DefaultVisitor { + private List vertices = new LinkedList(); + + public TopoVisitor() { } + + public void discoverGraph( Graph g ) { + vertices = new LinkedList(); + } + + public void finishVertex( Vertex v ) { + vertices.add( 0, v ); + } + + public void visit( DirectedGraph g ) { + DFS dfs = new DFS( this, true ); + dfs.start( g, null ); + } + + public List getVertices() { + return vertices; + } + } + + public List topologicalSort( DirectedGraph dg ) { + TopoVisitor visitor = new TopoVisitor(); + visitor.visit( dg ); + return visitor.getVertices(); + } + + private Sort() { } + + public static Sort getInstance() { + return _instance; + } + +} diff --git a/src/java/org/apache/commons/graph/exception/BackEdgeException.java b/src/java/org/apache/commons/graph/exception/BackEdgeException.java new file mode 100644 index 0000000..95e2ec8 --- /dev/null +++ b/src/java/org/apache/commons/graph/exception/BackEdgeException.java @@ -0,0 +1,75 @@ +package org.apache.commons.graph.exception; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Commons" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Commons", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * BackEdgeException + * + * This exception is thrown when an algorithm is expecting + * a DAG, and it has a Back Edge on it. + * + * Try to remove the Back Edge, and do it again. + */ +public class BackEdgeException + extends GraphException +{ + public BackEdgeException() { + super("Not a DAG. Back Edge detected."); + } + + public BackEdgeException( String msg ) { + super( msg ); + } +} diff --git a/src/java/org/apache/commons/graph/exception/GraphException.java b/src/java/org/apache/commons/graph/exception/GraphException.java new file mode 100644 index 0000000..56b05a0 --- /dev/null +++ b/src/java/org/apache/commons/graph/exception/GraphException.java @@ -0,0 +1,72 @@ +package org.apache.commons.graph.exception; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Commons" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Commons", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * GraphException + * + * This is a generic exception + * which superclasses all Graph exceptions. + */ +public class GraphException extends Exception +{ + public GraphException() { + super(); + } + + public GraphException( String msg ) { + super( msg ); + } +} diff --git a/src/java/org/apache/commons/graph/util/DependencyResolver.java b/src/java/org/apache/commons/graph/util/DependencyResolver.java index 9a51097..99f5ff2 100644 --- a/src/java/org/apache/commons/graph/util/DependencyResolver.java +++ b/src/java/org/apache/commons/graph/util/DependencyResolver.java @@ -13,6 +13,8 @@ package org.apache.commons.graph.util; // E = { (a,b), (c,d), (e,f) } // G = (V,E) +import java.io.FileOutputStream; + import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -25,6 +27,7 @@ import org.apache.commons.graph.Graphable; import org.apache.commons.graph.LabeledVertex; import org.apache.commons.graph.Vertex; import org.apache.commons.graph.visitor.DependencyVisitor; +import org.apache.commons.graph.visualize.GraphMLVisualizer; public class DependencyResolver { @@ -155,6 +158,56 @@ public class DependencyResolver } } + /** + * Given a Graphable object which is our starting vertex + * and a list of dependencies, a List of Graphables, we + * will create the requisite graph. + * + +* @param g The starting vertex + * @param dependencies List of Graphable dependencies + */ + public void addGraphable(Graphable g, List dependencies) + { + LabeledVertex startingVertex = (LabeledVertex) vertices.get(g.getName()); + if (startingVertex == null) + { + // Create a labeled vertex with our starting vertex g. + startingVertex = new LabeledVertex(g.getName()); + vertices.put(g.getName(), startingVertex); + graph.addVertex(startingVertex); + } + + for (Iterator i = dependencies.iterator(); i.hasNext();) + { + // Create a vertex and add to the graph. + Graphable dependencyName = (Graphable) i.next(); + + // Look to see if this vertex is already contained in + // the graph and only add it if it isn't. We need to + // add some code to the graph object to deal with this + // automatically. + LabeledVertex dependency = (LabeledVertex)vertices.get(dependencyName.getName()); + if (dependency == null) + { + dependency = new LabeledVertex(dependencyName.getName()); + vertices.put(dependencyName.getName(), dependency); + graph.addVertex(dependency); + } + + // Create an edge between the starting vertex and + // the dependency. + graph.addEdge(startingVertex, dependency); + } + } + + public void printGraph(String file, Graphable start) + throws Exception + { + GraphMLVisualizer v = new GraphMLVisualizer(new FileOutputStream(file),1); + v.visualize(graph, (LabeledVertex)vertices.get(start.getName())); + } + public static void main(String[] args) { Graph graph = new Graph(true); @@ -194,4 +247,6 @@ public class DependencyResolver System.out.println(i.next()); } } + + } diff --git a/src/java/org/apache/commons/graph/visitor/BFSVisitor.java b/src/java/org/apache/commons/graph/visitor/BFSVisitor.java index df98b63..4fc7b3e 100644 --- a/src/java/org/apache/commons/graph/visitor/BFSVisitor.java +++ b/src/java/org/apache/commons/graph/visitor/BFSVisitor.java @@ -26,12 +26,12 @@ package org.apache.commons.graph.visitor; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -65,7 +65,7 @@ import org.apache.commons.graph.VStack; * Visit graph with BFS and compute predecessors during traversal as * well as the "distance" to the source node (number of edges in between). * - * @version $Id: BFSVisitor.java,v 1.2 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: BFSVisitor.java,v 1.3 2002/01/16 20:38:29 jvanzyl Exp $ * @author M. Dahm * @see BFS */ diff --git a/src/java/org/apache/commons/graph/visitor/DFSVisitor.java b/src/java/org/apache/commons/graph/visitor/DFSVisitor.java index 2f09274..da73a15 100644 --- a/src/java/org/apache/commons/graph/visitor/DFSVisitor.java +++ b/src/java/org/apache/commons/graph/visitor/DFSVisitor.java @@ -26,12 +26,12 @@ package org.apache.commons.graph.visitor; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -65,7 +65,7 @@ import org.apache.commons.graph.VStack; * this class (and of course its descendants) may be passed to a DFS * object. * - * @version $Id: DFSVisitor.java,v 1.2 2001/12/01 19:12:14 jvanzyl Exp $ + * @version $Id: DFSVisitor.java,v 1.3 2002/01/16 20:38:29 jvanzyl Exp $ * @author M. Dahm * @see DFS */ diff --git a/src/java/org/apache/commons/graph/visitor/DefaultVisitor.java b/src/java/org/apache/commons/graph/visitor/DefaultVisitor.java index f09f340..c577e65 100644 --- a/src/java/org/apache/commons/graph/visitor/DefaultVisitor.java +++ b/src/java/org/apache/commons/graph/visitor/DefaultVisitor.java @@ -26,12 +26,12 @@ package org.apache.commons.graph.visitor; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -62,7 +62,7 @@ import org.apache.commons.graph.Vertex; /** * Empty Visitor, i.e. all visit methods do nothing. * - * @version $Id: DefaultVisitor.java,v 1.1 2001/11/29 14:17:34 jvanzyl Exp $ + * @version $Id: DefaultVisitor.java,v 1.2 2002/01/16 20:38:29 jvanzyl Exp $ * @author M. Dahm */ public class DefaultVisitor implements Visitor { diff --git a/src/java/org/apache/commons/graph/visitor/DependencyVisitor.java b/src/java/org/apache/commons/graph/visitor/DependencyVisitor.java index 557d9e0..f202808 100644 --- a/src/java/org/apache/commons/graph/visitor/DependencyVisitor.java +++ b/src/java/org/apache/commons/graph/visitor/DependencyVisitor.java @@ -26,12 +26,12 @@ package org.apache.commons.graph.visitor; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -65,7 +65,7 @@ import org.apache.commons.graph.Vertex; * this class (and of course its descendants) may be passed to a DFS * object. * - * @version $Id: DependencyVisitor.java,v 1.2 2001/12/02 19:23:40 jvanzyl Exp $ + * @version $Id: DependencyVisitor.java,v 1.3 2002/01/16 20:38:29 jvanzyl Exp $ * @author M. Dahm * @see DFS */ diff --git a/src/java/org/apache/commons/graph/visitor/PrintVisitor.java b/src/java/org/apache/commons/graph/visitor/PrintVisitor.java index 9050f21..5e81fd3 100644 --- a/src/java/org/apache/commons/graph/visitor/PrintVisitor.java +++ b/src/java/org/apache/commons/graph/visitor/PrintVisitor.java @@ -26,12 +26,12 @@ package org.apache.commons.graph.visitor; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -66,7 +66,7 @@ import org.apache.commons.graph.Vertex; * vertices and edges as they are discovered. May be used with DFS, BFS, * Sequential or any other visitor. Uses DFS by default. * - * @version $Id: PrintVisitor.java,v 1.2 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: PrintVisitor.java,v 1.3 2002/01/16 20:38:29 jvanzyl Exp $ * @author M. Dahm */ public class PrintVisitor implements Visitor { diff --git a/src/java/org/apache/commons/graph/visitor/SCCVisitor.java b/src/java/org/apache/commons/graph/visitor/SCCVisitor.java index c913252..b9bd200 100644 --- a/src/java/org/apache/commons/graph/visitor/SCCVisitor.java +++ b/src/java/org/apache/commons/graph/visitor/SCCVisitor.java @@ -26,12 +26,12 @@ package org.apache.commons.graph.visitor; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -65,7 +65,7 @@ import org.apache.commons.graph.VStack; /** * Visit graph computing its "Strongly Connected Components" (SCC). * - * @version $Id: SCCVisitor.java,v 1.1 2001/11/29 14:17:34 jvanzyl Exp $ + * @version $Id: SCCVisitor.java,v 1.2 2002/01/16 20:38:29 jvanzyl Exp $ * @author M. Dahm * @see DFS */ diff --git a/src/java/org/apache/commons/graph/visitor/Visitor.java b/src/java/org/apache/commons/graph/visitor/Visitor.java index 8f992e5..f0a5a83 100644 --- a/src/java/org/apache/commons/graph/visitor/Visitor.java +++ b/src/java/org/apache/commons/graph/visitor/Visitor.java @@ -26,12 +26,12 @@ package org.apache.commons.graph.visitor; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -61,7 +61,7 @@ import org.apache.commons.graph.Vertex; /** * Interface for visitor patterns. * - * @version $Id: Visitor.java,v 1.1 2001/11/29 14:17:34 jvanzyl Exp $ + * @version $Id: Visitor.java,v 1.2 2002/01/16 20:38:29 jvanzyl Exp $ * @author M. Dahm */ public interface Visitor { diff --git a/src/java/org/apache/commons/graph/visualize/DaVinciVisualizer.java b/src/java/org/apache/commons/graph/visualize/DaVinciVisualizer.java index fbb90dd..d508a3b 100644 --- a/src/java/org/apache/commons/graph/visualize/DaVinciVisualizer.java +++ b/src/java/org/apache/commons/graph/visualize/DaVinciVisualizer.java @@ -26,12 +26,12 @@ package org.apache.commons.graph.visualize; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -70,7 +70,7 @@ import org.apache.commons.graph.Vertex; * * Traverses graph with the Sequential visitor, since it uses invariants of that algorithm. * - * @version $Id: DaVinciVisualizer.java,v 1.2 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: DaVinciVisualizer.java,v 1.3 2002/01/16 20:38:30 jvanzyl Exp $ * @author M. Dahm */ public class DaVinciVisualizer extends GraphVisualizer implements GraphConstants { diff --git a/src/java/org/apache/commons/graph/visualize/GMLVisualizer.java b/src/java/org/apache/commons/graph/visualize/GMLVisualizer.java index e761a04..381a7df 100644 --- a/src/java/org/apache/commons/graph/visualize/GMLVisualizer.java +++ b/src/java/org/apache/commons/graph/visualize/GMLVisualizer.java @@ -26,12 +26,12 @@ package org.apache.commons.graph.visualize; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -66,7 +66,7 @@ import org.apache.commons.graph.Vertex; * Visualizes graphs into the GML * format. * - * @version $Id: GMLVisualizer.java,v 1.2 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: GMLVisualizer.java,v 1.3 2002/01/16 20:38:30 jvanzyl Exp $ * @author M. Dahm */ public class GMLVisualizer extends GraphVisualizer { diff --git a/src/java/org/apache/commons/graph/visualize/GraphMLVisualizer.java b/src/java/org/apache/commons/graph/visualize/GraphMLVisualizer.java index 897ddd6..6afdc13 100644 --- a/src/java/org/apache/commons/graph/visualize/GraphMLVisualizer.java +++ b/src/java/org/apache/commons/graph/visualize/GraphMLVisualizer.java @@ -26,12 +26,12 @@ package org.apache.commons.graph.visualize; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -66,7 +66,7 @@ import org.apache.commons.graph.Vertex; * Visualizes graphs into the GML * format. * - * @version $Id: GraphMLVisualizer.java,v 1.2 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: GraphMLVisualizer.java,v 1.3 2002/01/16 20:38:30 jvanzyl Exp $ * @author Jason van Zyl */ public class GraphMLVisualizer diff --git a/src/java/org/apache/commons/graph/visualize/GraphVisualizer.java b/src/java/org/apache/commons/graph/visualize/GraphVisualizer.java index 052e57c..9c0ee39 100644 --- a/src/java/org/apache/commons/graph/visualize/GraphVisualizer.java +++ b/src/java/org/apache/commons/graph/visualize/GraphVisualizer.java @@ -26,12 +26,12 @@ package org.apache.commons.graph.visualize; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -73,7 +73,7 @@ import org.apache.commons.graph.visitor.Visitor; * By default graphs are dumped in the native Java format, i.e. by using the * Serialization feature. * - * @version $Id: GraphVisualizer.java,v 1.2 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: GraphVisualizer.java,v 1.3 2002/01/16 20:38:30 jvanzyl Exp $ * @author M. Dahm * @see DaVinciVisualizer * @see GMLVisualizer diff --git a/src/java/org/apache/commons/graph/visualize/VCGVisualizer.java b/src/java/org/apache/commons/graph/visualize/VCGVisualizer.java index 2372d45..1d3d42e 100644 --- a/src/java/org/apache/commons/graph/visualize/VCGVisualizer.java +++ b/src/java/org/apache/commons/graph/visualize/VCGVisualizer.java @@ -26,12 +26,12 @@ package org.apache.commons.graph.visualize; * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and - * "Apache BCEL" must not be used to endorse or promote products + * "Commons" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", - * "Apache BCEL", nor may "Apache" appear in their name, without + * "Commons", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED @@ -66,7 +66,7 @@ import org.apache.commons.graph.Vertex; * HREF="http://www.cs.uni-sb.de/RW/users/sander/html/gsvcg1.html">VCG * format. * - * @version $Id: VCGVisualizer.java,v 1.2 2001/12/02 18:07:18 jvanzyl Exp $ + * @version $Id: VCGVisualizer.java,v 1.3 2002/01/16 20:38:30 jvanzyl Exp $ * @author M. Dahm */ public class VCGVisualizer extends GraphVisualizer { diff --git a/src/test/org/apache/commons/graph/algo/TestBackEdges.java b/src/test/org/apache/commons/graph/algo/TestBackEdges.java new file mode 100644 index 0000000..c6044e9 --- /dev/null +++ b/src/test/org/apache/commons/graph/algo/TestBackEdges.java @@ -0,0 +1,215 @@ +package org.apache.commons.graph.algo; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Commons" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Commons", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import junit.framework.*; +import org.apache.commons.graph.*; +import org.apache.commons.graph.algo.BackEdges; +import java.util.Set; +import java.util.Iterator; + +/** + * TestBackEdges + * + * JUnit test for testing the BackEdgeCollector + * visitor. + * + * We need to test it on several types of + * directed graphs. + * + * --------------------- + * + * + * Can't test. Can't call DFS without a start + * vertex. + * --------------------- + * + * * + * + * --------------------- + * + * *->* + * + * -------------------- + * + * *->*->* + * + * ------------------- + * + * *-->*-->* + * ^ | + * \-/ + * + * ------------------- + * + * *-->* + * ^ | + * \-/ + * + * ------------------- + * + * That should be enough + * for now. + */ +public class TestBackEdges + extends TestCase +{ + public TestBackEdges( String testName ) { + super( testName ); + } + + private Vertex A = new LabeledVertex("A"); + private Vertex B = new LabeledVertex("B"); + private Vertex C = new LabeledVertex("C"); + + private Edge AB = new LabeledEdge("AB"); + private Edge AC = new LabeledEdge("AC"); + + private Edge BA = new LabeledEdge("BA"); + private Edge BC = new LabeledEdge("BC"); + + private Edge CA = new LabeledEdge("CA"); + private Edge CB = new LabeledEdge("CB"); + + private BackEdges BACK = BackEdges.getInstance(); + + public void testNoEdges() throws Throwable { + DirectedGraph g = new DirectedGraph(); + g.addVertex( A ); + + Set backEdges = BACK.getBackEdges(g, A); + + assert( "Edges not empty.", + backEdges.size() == 0 ); + } + + public void testOneEdges() throws Throwable { + DirectedGraph g = new DirectedGraph(); + + g.addVertex( A ); + g.addVertex( B ); + + g.addEdge( A, B, AB ); + + Set backEdges = BACK.getBackEdges(g, A); + + assert( "Edges not empty.", + backEdges.size() == 0 ); + } + + public void testCycle1() throws Throwable { + DirectedGraph g = new DirectedGraph(); + + g.addVertex( A ); + g.addVertex( B ); + + g.addEdge( A, B, AB ); + g.addEdge( B, A, BA ); + + Set backEdges = BACK.getBackEdges(g, A); + assert( "BA not present", + backEdges.contains(BA)); + } + + public void testCycle2() throws Throwable { + DirectedGraph g = new DirectedGraph(); + + g.addVertex( A ); + g.addVertex( B ); + g.addVertex( C ); + + g.addEdge( A, B, AB ); + g.addEdge( B, C, BC ); + g.addEdge( C, A, CA ); + + Set backEdges = BACK.getBackEdges(g, A); + assert( "CA not present", + backEdges.contains(CA)); + } + + public void testCycle3() throws Throwable { + DirectedGraph g = new DirectedGraph(); + + g.addVertex( A ); + g.addVertex( B ); + g.addVertex( C ); + + g.addEdge( A, B, AB ); + g.addEdge( B, C, BC ); + g.addEdge( C, A, CA ); + + Set backEdges = BACK.getBackEdges(g, C); + assert( "BC not present", + backEdges.contains(BC)); + } + + public void testCycle4() throws Throwable { + DirectedGraph g = new DirectedGraph(); + + g.addVertex( A ); + g.addVertex( B ); + g.addVertex( C ); + + g.addEdge( A, B, AB ); + g.addEdge( B, C, BC ); + g.addEdge( C, B, CB ); + + Set backEdges = BACK.getBackEdges(g, A); + assert( "CB not present", + backEdges.contains(CB)); + } + + +} diff --git a/src/test/org/apache/commons/graph/algo/TestMinimumSpanningForest.java b/src/test/org/apache/commons/graph/algo/TestMinimumSpanningForest.java new file mode 100644 index 0000000..0138d98 --- /dev/null +++ b/src/test/org/apache/commons/graph/algo/TestMinimumSpanningForest.java @@ -0,0 +1,346 @@ +package org.apache.commons.graph.algo; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Commons" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Commons", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.util.List; +import java.util.Iterator; + +import org.apache.commons.graph.*; +import junit.framework.*; + +/** + * TestMinimumSpanningForrest + * + * This class tests the implementation of the MinimumSpanning + * forrest. + * + * We are going to test the following graphs. + * + * + * + * ------------------ + * + * * + * + * ------------------ + * + * *----* + * + * ------------------ + * + * * * + * + * ------------------ + * + * * + * | + * *-*-* + * + * ------------------ + * + * * + * / \ + * * * + * \ / + * * + * + * ----------------- + * + * * + * / + * *---* + * \ / + * * + * + * ---------------- + * + * * * + * | | + * * * + * + * ----------------- + * + * * + * + * *---* + * \ / + * * + * + * ----------------- + * + * * + * /|\ + * *-+-* + * \|/ + * * + * + */ +public class TestMinimumSpanningForest + extends TestCase +{ + private Vertex A = new LabeledVertex("A"); + private Vertex B = new LabeledVertex("B"); + private Vertex C = new LabeledVertex("C"); + private Vertex D = new LabeledVertex("D"); + + private Edge AB = new LabeledEdge("AB"); + private Edge AC = new LabeledEdge("AC"); + private Edge AD = new LabeledEdge("AD"); + private Edge BC = new LabeledEdge("BC"); + private Edge BD = new LabeledEdge("BD"); + private Edge CD = new LabeledEdge("CD"); + + public TestMinimumSpanningForest( String name ) { + super( name ); + } + + public void setUp() { + + } + + public void testNoVertex() throws Throwable { + Graph g = new Graph(); + MinimumSpanningForest msf = + MinimumSpanningForest.getMSF( g ); + + assert("Wrong number of vertexes: " + msf.getNoVertices(), + msf.getNoVertices() == 0); + assert("Wrong number of edges: " + msf.getNoEdges(), + msf.getNoEdges() == 0); + } + + public void testOneVertex() throws Throwable + { + Graph g = new Graph(); + g.addVertex( A ); // A + + MinimumSpanningForest msf = + MinimumSpanningForest.getMSF( g ); + assert("Wrong number of vertexes: " + msf.getNoVertices(), + msf.getNoVertices() == 1); + assert("Wrong number of edges: " + msf.getNoEdges(), + msf.getNoEdges() == 0); + + } + + public void testTwoVertexNoEdge() throws Throwable + { + Graph g = new Graph(); + g.addVertex( A ); + g.addVertex( B ); // A B + + MinimumSpanningForest msf = + MinimumSpanningForest.getMSF( g ); + assert("Wrong number of vertexes: " + msf.getNoVertices(), + msf.getNoVertices() == 2); + assert("Wrong number of edges: " + msf.getNoEdges(), + msf.getNoEdges() == 0); + + assert("Wrong number of roots: " + msf.getTreeRoots().size(), + msf.getTreeRoots().size() == 2); + + } + + public void testTwoVertexOneEdge() throws Throwable + { + Graph g = new Graph(); + + g.addVertex( A ); + g.addVertex( B ); // A---B + g.addEdge( A, B, AB ); + + MinimumSpanningForest msf = + MinimumSpanningForest.getMSF( g ); + assert("Wrong number of vertexes: " + msf.getNoVertices(), + msf.getNoVertices() == 2); + assert("Wrong number of edges: " + msf.getNoEdges(), + msf.getNoEdges() == 1); + + assert("Wrong number of roots: " + msf.getTreeRoots().size(), + msf.getTreeRoots().size() == 1); + } + + public void testCycle1() throws Throwable + { + Graph g = new Graph(); + + g.addVertex( A ); + g.addVertex( B ); + g.addVertex( C ); + g.addVertex( D ); + + g.addEdge( A, B, AB ); // A A + g.addEdge( B, C, BC ); // / \ / \ + g.addEdge( C, D, CD ); // B D ===> B D + g.addEdge( A, D, AD ); // \ / \ + // C C + + MinimumSpanningForest msf = + MinimumSpanningForest.getMSF( g ); + assert("Wrong number of vertexes: " + msf.getNoVertices(), + msf.getNoVertices() == 4); + assert("Wrong number of edges: " + msf.getNoEdges(), + msf.getNoEdges() == 3); + + assert("Wrong number of roots: " + msf.getTreeRoots().size(), + msf.getTreeRoots().size() == 1); + } + + public void testCycle2() throws Throwable + { + Graph g = new Graph(); + + g.addVertex( A ); + g.addVertex( B ); + g.addVertex( C ); + g.addVertex( D ); + + g.addEdge( A, B, AB ); // A---D A---D + g.addEdge( A, C, AC ); // / \ =====> / \ + g.addEdge( B, C, BC ); // B---C B C + g.addEdge( A, D, AD ); + + MinimumSpanningForest msf = + MinimumSpanningForest.getMSF( g ); + assert("Wrong number of vertexes: " + msf.getNoVertices(), + msf.getNoVertices() == 4); + assert("Wrong number of edges: " + msf.getNoEdges(), + msf.getNoEdges() == 3); + + assert("Wrong number of roots: " + msf.getTreeRoots().size(), + msf.getTreeRoots().size() == 1); + } + + public void testTwoTrees() throws Throwable + { + Graph g = new Graph(); + + g.addVertex( A ); + g.addVertex( B ); + g.addVertex( C ); + g.addVertex( D ); + + g.addEdge( A, B, AB ); // A---B + g.addEdge( C, D, CD ); // + // C---D + + MinimumSpanningForest msf = + MinimumSpanningForest.getMSF( g ); + assert("Wrong number of vertexes: " + msf.getNoVertices(), + msf.getNoVertices() == 4); + assert("Wrong number of edges: " + msf.getNoEdges(), + msf.getNoEdges() == 2); + + assert("Wrong number of roots: " + msf.getTreeRoots().size(), + msf.getTreeRoots().size() == 2); + } + + public void testTwoTreesOneCycle() throws Throwable + { + Graph g = new Graph(); + + g.addVertex( A ); + g.addVertex( B ); + g.addVertex( C ); + g.addVertex( D ); + + g.addEdge( A, B, AB ); // A D A D + g.addEdge( B, C, BC ); // / \ ==> / \ + g.addEdge( A, C, AC ); // B---C B C + + MinimumSpanningForest msf = + MinimumSpanningForest.getMSF( g ); + + assert("Wrong number of vertexes: " + msf.getNoVertices(), + msf.getNoVertices() == 4); + assert("Wrong number of edges: " + msf.getNoEdges(), + msf.getNoEdges() == 2); + + assert("Wrong number of roots: " + msf.getTreeRoots().size(), + msf.getTreeRoots().size() == 2); + } + + public void testConnected() throws Throwable + { + Graph g = new Graph(); + + g.addVertex( A ); + g.addVertex( B ); + g.addVertex( C ); + g.addVertex( D ); + + g.addEdge( A, B, AB ); // A A + g.addEdge( A, C, AC ); // /|\ /|\ + g.addEdge( A, D, AD ); // B-+-C ==> B D C + // \|/ + // D + + g.addEdge( B, C, BC ); + g.addEdge( B, D, BD ); + + g.addEdge( C, D, CD ); + + MinimumSpanningForest msf = + MinimumSpanningForest.getMSF( g ); + assert("Wrong number of vertexes: " + msf.getNoVertices(), + msf.getNoVertices() == 4); + assert("Wrong number of edges: " + msf.getNoEdges(), + msf.getNoEdges() == 3); + + assert("Wrong number of roots: " + msf.getTreeRoots().size(), + msf.getTreeRoots().size() == 1); + } + +} diff --git a/src/test/org/apache/commons/graph/algo/TestSort.java b/src/test/org/apache/commons/graph/algo/TestSort.java new file mode 100644 index 0000000..f2de41d --- /dev/null +++ b/src/test/org/apache/commons/graph/algo/TestSort.java @@ -0,0 +1,166 @@ +package org.apache.commons.graph.algo; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Commons" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Commons", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import junit.framework.*; +import org.apache.commons.graph.*; + +import java.util.List; +import java.util.Iterator; + +/** + * TestSort + * + * JUnit test for testing the Sort class. + * (or Topological Sort) + * + * We need to test it on several types of + * directed graphs. + * + * --------------------- + * + * + * Can't test. Can't call DFS without a start + * vertex. + * --------------------- + * + * A + * + * --------------------- + * + * A->B + * (Start at A) + * + * -------------------- + * + * A-->B + * (Start at B) + * + * -------------------- + * + * A->B->C + * + * ------------------- + * + * A-->B-->C + * ^ | + * \-/ + * + * ------------------- + * + * A-->B + * ^ | + * \-/ + * + * ------------------- + * + * A-->B + * + * C-->D + * + * ------------------ + * + * A--->B + * --^ + * / + * C--->D + * + * ------------------ + * That should be enough + * for now. + */ +public class TestSort + extends TestCase +{ + public TestSort( String testName ) { + super( testName ); + } + + private Vertex A = new LabeledVertex("A"); + private Vertex B = new LabeledVertex("B"); + private Vertex C = new LabeledVertex("C"); + private Vertex D = new LabeledVertex("D"); + + private Edge AB = new LabeledEdge("AB"); + private Edge AC = new LabeledEdge("AC"); + private Edge AD = new LabeledEdge("AD"); + + private Edge BA = new LabeledEdge("BA"); + private Edge BC = new LabeledEdge("BC"); + private Edge BD = new LabeledEdge("BD"); + + private Edge CA = new LabeledEdge("CA"); + private Edge CB = new LabeledEdge("CB"); + private Edge CD = new LabeledEdge("CD"); + + private Edge DA = new LabeledEdge("DA"); + private Edge DB = new LabeledEdge("DB"); + private Edge DC = new LabeledEdge("DC"); + + private Sort SORT = Sort.getInstance(); + + public void testOneVertex() throws Throwable { + DirectedGraph g = new DirectedGraph(); + + g.addVertex( A ); + + List vertices = SORT.topologicalSort( g ); + Iterator i = vertices.iterator(); + + assert( "Wrong number of vertexes: " + vertices.size(), + vertices.size() == 1 ); + assert( "A not in proper place.", A == i.next() ); + } +}