- fixing the lic

- applying patches submitted by David Dixon-Peugh:

I've moved some over.  (Its still in the org/apache/commons/graph
package.)

I added a package "algo" off of the graph package.  This is designed
to hold various graph algorithms.  Graphs have lots of neet things
you can do with them, unfortunately, they don't really fit neatly
into a nice OO design.  (At least I can't see a good connection.)

Changes:

Edge.java
WeightedEdge.java
(org/apache/commons/graph)

These two classes were changed to implement Comparable.  This allows
them to be sorted, and to be dropped into a PriorityQueue.

New Files:

DirectedGraph.java
UndirectedGraph.java
(org/apache/commons/graph)

There are some algorithms which demand either Directed or Undirected
graphs.  These classes allow us to have static type checking for those
algorithms.


Label.java
(org/apache/commons/graph)

There are many algorithms in graph theory which require a marking of
groups.  This label basically has some pointers which it snaps whenever
it is referenced.  It brings running times of some algorithms down close
to O(V + E) rather than O(VlgV + E).


MinimumSpanningForest.java
(org/apache/commons/graph/algo)

This file implements a MinimumSpanningForest algorithm over any graphs.
It uses a PriorityQueue from org/apache/commons/collection.


Sort.java
(org/apache/commons/graph/algo)

This file implements a Topological Sort on directed graphs.  It has some
problems if there is a back edge in the graph.  (Currently it ignores
back edges.  I hope to have it throw a BackEdgeException in the future.)


BackEdges.java
(org/apache/commons/graph/algo)

This file identifies back edges in a Directed Graph, when starting from
a particular vertex.


GraphException.java
BackEdgeException.java
(org/apache/commons/graph/exception)

These are basic exceptions that can be thrown from the graph package.


TestBackEdges.java
TestSort.java
TestMinimumSpanningTree.java

These are JUnit tests for the algorithms provided in this package.
No tests are provided for the Exceptions, Label, Directed and Undirected
classes  (Although Label really should have one.  If Label isn't
working, you get some really nasty bugs.)

--------------------------

Well, this should keep you busy for a while.

Let me know if there are any changes you would like me to make.

DDP

PR:
Obtained from:
Submitted by:
Reviewed by:


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/graph/trunk@144589 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
jvanzyl committed 2002-01-16 20:38:30 +00:00
1 parent 32e1c5f233
commit 22c091063c
43 files changed
+1801 -92

No files matched your search

+5 -1
View File
@@ -12,6 +12,7 @@
<!-- Construct compile classpath -->
<path id="classpath">
<pathelement location="${junit.jar}"/>
<pathelement location="${commons-collections.jar}"/>
<pathelement location="${build.home}/class"/>
</path>
@@ -24,6 +25,9 @@
depends="compile-test"
description="runs (junit) unit tests">
<delete dir="${test.reportDirectory}"/>
<mkdir dir="${test.reportDirectory}"/>
<echo>
Running all JUnit tests
</echo>
@@ -31,7 +35,7 @@
<junit printSummary="yes">
<formatter type="plain"/>
<classpath refid="classpath"/>
<batchtest todir=".">
<batchtest todir="${test.reportDirectory}">
<fileset dir="${build.home}/class">
<include name="**/*Test*.class"/>
</fileset>
+6 -4
View File
@@ -1,27 +1,29 @@
<project name="Jakarta Graph Utilities" default="jar" basedir=".">
<!--
$Id: build.xml,v 1.1 2001/11/12 16:05:17 jvanzyl Exp $
$Id: build.xml,v 1.2 2002/01/16 20:38:28 jvanzyl Exp $
-->
<!-- ========== Initialize Properties =================================== -->
<property file="./build.properties"/> <!-- Component local -->
<property file="../build.properties"/> <!-- Commons local -->
<property file="${user.home}/build.properties"/> <!-- User local -->
<property file="${user.home}/build.properties"/>
<property file="build.properties"/>
<property file="default.properties"/>
<!-- ========== Classpaths ============================================== -->
<path id="compile.classpath">
<pathelement location="${build.home}/class"/>
<pathelement location="${commons-collections.jar}"/>
</path>
<path id="test.classpath">
<pathelement location="${build.home}/class"/>
<pathelement location="${build.home}/test"/>
<pathelement location="${junit.jar}"/>
<pathelement location="${commons-collections.jar}"/>
</path>
+4
View File
@@ -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
+3 -3
View File
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public class BFS extends TraversalOrder {
+3 -3
View File
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public class DFS extends TraversalOrder {
@@ -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
* <http://www.apache.org/>.
*/
/**
* DirectedGraph
*
* This is a graph for which the edges only flow one way.
*/
public class DirectedGraph extends Graph
{
public DirectedGraph()
{
super( true );
}
}
+9 -4
View File
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
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;
}
}
+3 -3
View File
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public class Graph implements Cloneable, Serializable {
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
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.
*
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public class GraphError extends RuntimeException {
@@ -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
* <http://www.apache.org/>.
*/
/**
* 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 <a href="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
* @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();
}
@@ -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
* <http://www.apache.org/>.
*/
/**
* 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();
}
}
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public class LabeledEdge extends Edge {
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public class LabeledVertex extends Vertex {
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public final class Sequential
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
* @see DFS
* @see BFS
@@ -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
* <http://www.apache.org/>.
*/
/**
* UndirectedGraph
*
* This represents graphs where edges go both ways.
*/
public class UndirectedGraph
extends Graph
{
public UndirectedGraph() {
super( false );
}
}
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public class VQueue {
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public final class VStack {
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public class Vertex
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
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;
}
}
@@ -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
* <http://www.apache.org/>.
*/
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;
}
}
@@ -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
* <http://www.apache.org/>.
*/
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 );
}
}
@@ -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
* <http://www.apache.org/>.
*/
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;
}
}
@@ -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
* <http://www.apache.org/>.
*/
/**
* 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 );
}
}
@@ -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
* <http://www.apache.org/>.
*/
/**
* 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 );
}
}
@@ -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());
}
}
}
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
* @see BFS
*/
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
* @see DFS
*/
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public class DefaultVisitor implements Visitor {
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
* @see DFS
*/
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public class PrintVisitor implements Visitor {
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
* @see DFS
*/
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public interface Visitor {
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public class DaVinciVisualizer extends GraphVisualizer implements GraphConstants {
@@ -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 <A HREF="http://www.fmi.uni-passau.de/Graphlet/">GML</A>
* 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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public class GMLVisualizer extends GraphVisualizer {
@@ -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 <A HREF="http://www.fmi.uni-passau.de/Graphlet/">GML</A>
* 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 <a href="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
*/
public class GraphMLVisualizer
@@ -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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
* @see DaVinciVisualizer
* @see GMLVisualizer
@@ -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</A>
* 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 <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
*/
public class VCGVisualizer extends GraphVisualizer {
@@ -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
* <http://www.apache.org/>.
*/
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.
*
* ---------------------
*
* <Nothing>
* 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));
}
}
@@ -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
* <http://www.apache.org/>.
*/
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.
*
* <No V, No E>
*
* ------------------
*
* *
*
* ------------------
*
* *----*
*
* ------------------
*
* * *
*
* ------------------
*
* *
* |
* *-*-*
*
* ------------------
*
* *
* / \
* * *
* \ /
* *
*
* -----------------
*
* *
* /
* *---*
* \ /
* *
*
* ----------------
*
* * *
* | |
* * *
*
* -----------------
*
* *
*
* *---*
* \ /
* *
*
* -----------------
*
* *
* /|\
* *-+-*
* \|/
* *
*
*/
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);
}
}
@@ -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
* <http://www.apache.org/>.
*/
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.
*
* ---------------------
*
* <Nothing>
* 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() );
}
}