Removing first swipe at the graph package, this will be replaced with
a rewritten version that is more functional and contract-based. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/graph/trunk@144592 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
1 parent
a214396735
commit
1c0222e787
50 files changed
-6304
No files matched your search
@@ -1,2 +0,0 @@
|
||||
*~
|
||||
target
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* 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 acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" 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"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Group.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
. Removing the "implements Constants" as it is a bad pattern. Interfaces
|
||||
should represent types. It also makes the UML diagrams very hard
|
||||
to follow. We're just going to use the fully qualified GraphConstants.{x}
|
||||
idiom.
|
||||
|
||||
. Added the Graphable interface to allow any arbitrary object to be
|
||||
manipulated like a vertex in a graph.
|
||||
|
||||
. Added a util package that has a dependency resolver that uses a depth first
|
||||
search to sort the dependencies for a set of objects that implement
|
||||
the Graphable interface. The resolver also works with Strings and
|
||||
lists and deps stated as lists of strings so I can resuse the test
|
||||
I wrote for the DependencyEngine in jjar.
|
||||
@@ -1,12 +0,0 @@
|
||||
--------------------------------------------------------------------------
|
||||
J A K A R T A C O M M O N S G R A P H P A C K A G E
|
||||
--------------------------------------------------------------------------
|
||||
|
||||
Abstract:
|
||||
|
||||
Todo
|
||||
|
||||
Initial commiters:
|
||||
|
||||
Markus Dahm
|
||||
Jason van Zyl
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
--------------------------------------------------------------------------
|
||||
J A K A R T A C O M M O N S U T I L I T I E S
|
||||
--------------------------------------------------------------------------
|
||||
|
||||
o Building
|
||||
|
||||
To build, you must have ant installed. Just type 'ant' -- the default
|
||||
target will build the jar.
|
||||
|
||||
There are also a few other targets like 'javadoc'. Look in build.xml
|
||||
|
||||
|
||||
o Use in your application
|
||||
|
||||
To use the Commons Utility package, build a commons-util.jar file,
|
||||
import the desired classes from org.apache.commons.util and and
|
||||
compile your code with cup.jar in the CLASSPATH.
|
||||
@@ -1,70 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
|
||||
<project name="Stratum" default="test" basedir=".">
|
||||
|
||||
<!-- Allow any user specific values to override the defaults -->
|
||||
<property file="${user.home}/build.properties" />
|
||||
<!-- Allow user defaults for this project -->
|
||||
<property file="build.properties" />
|
||||
<!-- Set default values for the build -->
|
||||
<property file="default.properties" />
|
||||
|
||||
<!-- Construct compile classpath -->
|
||||
<path id="classpath">
|
||||
<pathelement location="${junit.jar}"/>
|
||||
<pathelement location="${commons-collections.jar}"/>
|
||||
<pathelement location="${build.home}/class"/>
|
||||
</path>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- T E S T -->
|
||||
<!-- ================================================================== -->
|
||||
|
||||
<target
|
||||
name="test"
|
||||
depends="compile-test"
|
||||
description="runs (junit) unit tests">
|
||||
|
||||
<delete dir="${test.reportDirectory}"/>
|
||||
<mkdir dir="${test.reportDirectory}"/>
|
||||
|
||||
<echo>
|
||||
Running all JUnit tests
|
||||
</echo>
|
||||
|
||||
<junit printSummary="yes">
|
||||
<formatter type="plain"/>
|
||||
<classpath refid="classpath"/>
|
||||
<batchtest todir="${test.reportDirectory}">
|
||||
<fileset dir="${build.home}/class">
|
||||
<include name="**/*Test*.class"/>
|
||||
</fileset>
|
||||
</batchtest>
|
||||
</junit>
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- C O M P I L E T E S T S -->
|
||||
<!-- ================================================================== -->
|
||||
|
||||
<target
|
||||
name="compile-test"
|
||||
description="==> compiles the test source code">
|
||||
|
||||
<ant antfile="build.xml" target="clean"/>
|
||||
<ant antfile="build.xml" target="compile"/>
|
||||
|
||||
<javac srcdir="${test.home}"
|
||||
destdir="${build.home}/class"
|
||||
excludes="**/package.html"
|
||||
debug="${debug}"
|
||||
deprecation="${deprecation}"
|
||||
optimize="${optimize}">
|
||||
<classpath refid="classpath"/>
|
||||
<classpath>
|
||||
<pathelement path="${build.home}/class"/>
|
||||
</classpath>
|
||||
</javac>
|
||||
</target>
|
||||
|
||||
</project>
|
||||
@@ -1,172 +0,0 @@
|
||||
<project name="Jakarta Graph Utilities" default="jar" basedir=".">
|
||||
|
||||
<!--
|
||||
$Id: build.xml,v 1.3 2002/01/22 20:21:57 jvanzyl Exp $
|
||||
-->
|
||||
|
||||
<!-- ========== Initialize Properties =================================== -->
|
||||
|
||||
|
||||
<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>
|
||||
|
||||
|
||||
<!-- ========== Executable Targets ====================================== -->
|
||||
|
||||
<target name="init"
|
||||
description="Initialize and evaluate conditionals">
|
||||
<echo message="-------- ${component.name} ${component.version} --------"/>
|
||||
<filter token="name" value="${component.name}"/>
|
||||
<filter token="version" value="${component.version}"/>
|
||||
|
||||
<available
|
||||
classname="junit.framework.TestCase"
|
||||
property="junit.present"
|
||||
classpathref="test.classpath"/>
|
||||
</target>
|
||||
|
||||
|
||||
<target name="prepare" depends="init"
|
||||
description="Prepare build directory">
|
||||
<mkdir dir="${build.home}"/>
|
||||
<mkdir dir="${build.home}/class"/>
|
||||
<mkdir dir="${build.home}/conf"/>
|
||||
<mkdir dir="${build.home}/test"/>
|
||||
</target>
|
||||
|
||||
|
||||
<target name="static" depends="prepare"
|
||||
description="Copy static files to build directory">
|
||||
<tstamp/>
|
||||
</target>
|
||||
|
||||
<target name="compile" depends="static"
|
||||
description="Compile shareable components">
|
||||
|
||||
<javac srcdir="${source.home}/java"
|
||||
destdir="${build.home}/class"
|
||||
debug="${compile.debug}"
|
||||
deprecation="${compile.deprecation}"
|
||||
optimize="${compile.optimize}">
|
||||
<classpath refid="compile.classpath"/>
|
||||
</javac>
|
||||
|
||||
</target>
|
||||
|
||||
|
||||
<target name="clean"
|
||||
description="Clean build and distribution directories">
|
||||
<delete dir="${build.home}"/>
|
||||
<delete dir="${dist.home}"/>
|
||||
<delete file="${component.jar}"/>
|
||||
</target>
|
||||
|
||||
|
||||
<target name="all" depends="clean,compile"
|
||||
description="Clean and compile all components"/>
|
||||
|
||||
|
||||
<target name="javadoc" depends="compile"
|
||||
description="Create component Javadoc documentation">
|
||||
<mkdir dir="${dist.home}"/>
|
||||
<mkdir dir="${dist.home}/docs"/>
|
||||
<mkdir dir="${dist.home}/docs/api"/>
|
||||
<javadoc sourcepath="${source.home}/java"
|
||||
destdir="${dist.home}/docs/api"
|
||||
packagenames="org.apache.commons.*"
|
||||
author="true"
|
||||
private="true"
|
||||
version="true"
|
||||
doctitle="<h1>${component.title}</h1>"
|
||||
windowtitle="${component.title} (Version ${component.version})"
|
||||
bottom="Copyright (c) 2001 - Apache Software Foundation"/>
|
||||
</target>
|
||||
|
||||
|
||||
<target name="jar" depends="compile" description="Build jar">
|
||||
<jar jarfile="${component.jar}"
|
||||
basedir="${build.home}/class"
|
||||
manifest="${source.home}/conf/MANIFEST.MF" />
|
||||
</target>
|
||||
|
||||
|
||||
<target name="dist" depends="compile,javadoc"
|
||||
description="Create binary distribution">
|
||||
<mkdir dir="${dist.home}"/>
|
||||
<copy file="LICENSE" todir="${dist.home}"/>
|
||||
|
||||
<jar jarfile="${dist.home}/${component.jar}"
|
||||
basedir="${build.home}/class"
|
||||
manifest="${source.home}/conf/MANIFEST.MF"/>
|
||||
</target>
|
||||
|
||||
|
||||
<target name="install-jar" depends="jar"
|
||||
description="==> Installs .jar file in ${lib.repo}">
|
||||
<copy todir="${lib.repo}" filtering="no">
|
||||
<fileset dir="${build.dir}">
|
||||
<include name="${component.jar}"/>
|
||||
</fileset>
|
||||
</copy>
|
||||
</target>
|
||||
|
||||
|
||||
<target name="compile-tests" depends="prepare,compile" if="junit.present"
|
||||
description="==> compiles the test source code">
|
||||
<mkdir dir="${build.home}/test"/>
|
||||
|
||||
<javac srcdir="${test.home}"
|
||||
destdir="${build.home}/test"
|
||||
excludes="**/package.html"
|
||||
debug="${debug}"
|
||||
deprecation="${deprecation}"
|
||||
optimize="${optimize}">
|
||||
<classpath refid="test.classpath"/>
|
||||
<classpath>
|
||||
<pathelement path="${build.dest}"/>
|
||||
</classpath>
|
||||
</javac>
|
||||
</target>
|
||||
|
||||
|
||||
<!-- ========== Unit Test Targets ======================================= -->
|
||||
|
||||
<target name="run-tests" depends="prepare,compile-tests" if="junit.present"
|
||||
description="==> Runs the unit tests">
|
||||
|
||||
<taskdef name="junit"
|
||||
classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
|
||||
<classpath refid="test.classpath"/>
|
||||
</taskdef>
|
||||
|
||||
<junit printsummary="no" haltonfailure="yes">
|
||||
<classpath>
|
||||
<path refid="test.classpath"/>
|
||||
</classpath>
|
||||
<formatter type="plain" usefile="false"/>
|
||||
|
||||
<batchtest>
|
||||
<fileset dir="${test.home}">
|
||||
<include name="**/*Test.java"/>
|
||||
</fileset>
|
||||
</batchtest>
|
||||
</junit>
|
||||
</target>
|
||||
|
||||
</project>
|
||||
@@ -1,25 +0,0 @@
|
||||
# Component delcarations
|
||||
component.name = commons-graph
|
||||
component.title = Jakarta Graph Utilties
|
||||
component.version = 0.1-dev
|
||||
component.jar = ${component.name}.jar
|
||||
|
||||
# Paths
|
||||
build.home = target
|
||||
dist.home = dist
|
||||
source.home = src
|
||||
test.home = ${source.home}/test
|
||||
build.dir = .
|
||||
|
||||
# Compiler defaults
|
||||
compile.debug = true
|
||||
compile.deprecation = true
|
||||
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
|
||||
@@ -1,6 +0,0 @@
|
||||
Extension-Name: @name@
|
||||
Specification-Vendor: Apache Software Foundation
|
||||
Specification-Version: 1.0
|
||||
Implementation-Vendor: Apache Software Foundation
|
||||
Implementation-Title: commons-graph
|
||||
Implementation-Version: 0.1-dev
|
||||
@@ -1,155 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
import java.util.Vector;
|
||||
import org.apache.commons.graph.visitor.Visitor;
|
||||
|
||||
/**
|
||||
* Visit graph with breadth first search and compute the predecessor
|
||||
* during traversal for all vertices. The algorithm could be modified
|
||||
* so that it performs Dijkstra's shortest-path algorithm, for
|
||||
* instance, if you extend the VQueue class to a Priority Queue.
|
||||
*
|
||||
* Definition of BFS: A search algorithm which considers neighbors of a vertex,
|
||||
* that is, outgoing edges of the vertex's predecessor in the search, before any
|
||||
* outgoing edges of the vertex. Extremes are searched last. This is typically
|
||||
* implemented with a queue.
|
||||
*
|
||||
* @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 {
|
||||
private VQueue q;
|
||||
|
||||
/**
|
||||
* Breadth first search algorithm.
|
||||
*
|
||||
* @param v visitor object to apply when discovering and finishing a vertex
|
||||
*/
|
||||
public BFS(Visitor v) {
|
||||
this(v, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Breadth first search algorithm.
|
||||
*
|
||||
* @param v visitor object to apply when discovering and finishing a vertex
|
||||
* @param q queue to use during traversal
|
||||
*/
|
||||
public BFS(Visitor v, VQueue q) {
|
||||
super(v);
|
||||
this.q = q;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run breadth first search (BFS) algorithm.
|
||||
*
|
||||
* @param g graph to traverse
|
||||
* @param start vertex to start at (or null, starts at some vertex then)
|
||||
*/
|
||||
public void start(Graph g, Vertex start) {
|
||||
int size = g.getNoVertices();
|
||||
Vertex[] vertices = g.getVertexArray();
|
||||
|
||||
visitor.discoverGraph(g);
|
||||
|
||||
/* Initially mark all vertices as WHITE
|
||||
*/
|
||||
for(int i=0; i < size; i++) {
|
||||
vertices[i].setColor(GraphConstants.WHITE);
|
||||
vertices[i].setPredecessor(null);
|
||||
}
|
||||
|
||||
if(start == null || !g.contains(start)) // Start BFS at (white) start vertex
|
||||
start = vertices[0];
|
||||
|
||||
if(q == null) // No queue supplied
|
||||
q = new VQueue(size);
|
||||
|
||||
visitor.discoverVertex(start); // Initialization
|
||||
|
||||
start.setColor(GraphConstants.GRAY);
|
||||
q.enqueue(start);
|
||||
|
||||
while(!q.empty() && !stopped) {
|
||||
Vertex u = q.head();
|
||||
|
||||
vertices = g.getVertexArray(u); // Get adjacent vertices and edges
|
||||
Edge[] edges = g.getEdgeArray(u);
|
||||
size = vertices.length; // reuse var
|
||||
|
||||
for(int i=0; (i < size) && !stopped; i++) { // Explore all edges, same length
|
||||
visitor.discoverEdge(edges[i]);
|
||||
|
||||
Vertex v = vertices[i];
|
||||
if(v.getColor() == GraphConstants.WHITE) { // Unvisited vertex?
|
||||
visitor.discoverVertex(v);
|
||||
|
||||
v.setColor(GraphConstants.GRAY);
|
||||
q.enqueue(v);
|
||||
}
|
||||
|
||||
visitor.finishEdge(edges[i]);
|
||||
}
|
||||
|
||||
visitor.finishVertex(u);
|
||||
q.dequeue();
|
||||
u.setColor(GraphConstants.BLACK);
|
||||
}
|
||||
|
||||
visitor.finishGraph(g);
|
||||
}
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
import org.apache.commons.graph.visitor.Visitor;
|
||||
|
||||
/**
|
||||
* Visit graph with depth first search and mark discovery and finishing time in all
|
||||
* vertices.
|
||||
*
|
||||
* Definition of a DFS:
|
||||
* (1) Any search algorithm which considers outgoing edges of a vertex before
|
||||
* any neighbors of the vertex, that is, outgoing edges of the vertex's predecessor in
|
||||
* the search. Extremes are searched first. This is easily implemented with recursion.
|
||||
* (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.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 {
|
||||
/**
|
||||
* Depth first search algorithm.
|
||||
*
|
||||
* @param v visitor object to apply when discovering and finishing a vertex or edge
|
||||
*/
|
||||
public DFS(Visitor v) {
|
||||
super(v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Depth first search algorithm.
|
||||
*
|
||||
* @param v visitor object to apply when discovering and finishing a vertex or edge
|
||||
* @param traverse_all traverse the complete graph or stop after finishing all nodes reachable
|
||||
* from the start
|
||||
*/
|
||||
public DFS(Visitor v, boolean traverse_all) {
|
||||
super(v, traverse_all);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run depth first search (DFS) algorithm.
|
||||
*
|
||||
* @param g graph to traverse
|
||||
* @param start vertex to start at (or null, starts at some vertex then)
|
||||
*/
|
||||
public void start(Graph g, Vertex start) {
|
||||
int size = g.getNoVertices();
|
||||
Vertex[] vertices = g.getVertexArray();
|
||||
|
||||
graph = g;
|
||||
|
||||
visitor.discoverGraph(g);
|
||||
|
||||
// Initially mark all vertices as WHITE
|
||||
for(int i=0; i < size; i++)
|
||||
vertices[i].setColor(GraphConstants.WHITE);
|
||||
|
||||
if(start != null && g.contains(start)) // Start DFS at (white) start vertex
|
||||
DFS_Visit(start);
|
||||
|
||||
if(traverse_complete_graph) {
|
||||
for(int i=0; (i < size) && !stopped; i++) {
|
||||
Vertex v = vertices[i];
|
||||
|
||||
if(v.getColor() == GraphConstants.WHITE) // Unvisited vertex?
|
||||
DFS_Visit(v);
|
||||
}
|
||||
}
|
||||
|
||||
visitor.finishGraph(g);
|
||||
}
|
||||
|
||||
private final void DFS_Visit(Vertex u) {
|
||||
u.setColor(GraphConstants.GRAY); // Mark as visited
|
||||
visitor.discoverVertex(u);
|
||||
|
||||
Vertex[] vertices = graph.getVertexArray(u); // Get adjacent vertices
|
||||
Edge[] edges = graph.getEdgeArray(u); // and edges
|
||||
int size = vertices.length;
|
||||
|
||||
for(int i=0; (i < size) && !stopped; i++) { // Explore all edges
|
||||
visitor.discoverEdge(edges[i]);
|
||||
|
||||
Vertex v = vertices[i];
|
||||
|
||||
if(v.getColor() == GraphConstants.WHITE)
|
||||
{
|
||||
DFS_Visit(v);
|
||||
}
|
||||
|
||||
visitor.finishEdge(edges[i]);
|
||||
}
|
||||
|
||||
u.setColor(GraphConstants.BLACK); // Mark as finished
|
||||
visitor.finishVertex(u);
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
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 );
|
||||
}
|
||||
}
|
||||
@@ -1,150 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
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.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, Comparable
|
||||
{
|
||||
|
||||
/**
|
||||
* Unique identifer for this edge.
|
||||
*/
|
||||
private int ident;
|
||||
|
||||
/**
|
||||
* Counter that is incremented to give
|
||||
* this edge a unique identity.
|
||||
*/
|
||||
private static int counter;
|
||||
|
||||
/**
|
||||
* Create edge.
|
||||
*/
|
||||
public Edge() {
|
||||
ident = counter++;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ident, i.e. identifier of this node
|
||||
*/
|
||||
public final int getIdent() { return ident; }
|
||||
|
||||
/**
|
||||
* @return number of created edges
|
||||
*/
|
||||
public static final int getCounter() { return counter; }
|
||||
|
||||
/**
|
||||
* @return String representation.
|
||||
*/
|
||||
public String toString() {
|
||||
return toString(GraphConstants.PLAIN);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param verbose toggle output format
|
||||
* @return String representation.
|
||||
*/
|
||||
public String toString(int verbose) {
|
||||
if((verbose == GraphConstants.TALKATIVE) || (verbose == GraphConstants.V_TALKATIVE))
|
||||
{
|
||||
return "Edge(" + ident + ")";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Access for visitor patterns
|
||||
*/
|
||||
public void accept(Visitor v) { v.discoverEdge(this); }
|
||||
|
||||
/**
|
||||
* @return deep copy
|
||||
*/
|
||||
public Edge copy() {
|
||||
try {
|
||||
return (Edge)clone();
|
||||
} catch(CloneNotSupportedException e) {}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Check for equality.
|
||||
*/
|
||||
public boolean equals(Object o) {
|
||||
if(!(o instanceof Edge))
|
||||
return false;
|
||||
|
||||
Edge e = (Edge)o;
|
||||
return e.ident == ident;
|
||||
}
|
||||
|
||||
public int compareTo( Object o ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,709 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.io.*;
|
||||
|
||||
import org.apache.commons.graph.visitor.Visitor;
|
||||
import org.apache.commons.graph.visitor.DefaultVisitor;
|
||||
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.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 {
|
||||
|
||||
/**
|
||||
* Debugging flag
|
||||
*/
|
||||
private boolean debug = false;
|
||||
|
||||
private boolean directed = true;
|
||||
|
||||
/* Map Vertex to VertexWrapper and Edge to EdgeWrapper,
|
||||
* respectively. Wrapper objects carry the necessary information
|
||||
* about associations between vertices. VertexWrappers contain the
|
||||
* edges coming from and leading to a vertex (if the graph is undirected both
|
||||
* vertices "have" this edge, otherwise the source vertex only "has" it).
|
||||
* EdgeWrappers contain references to the source and target vertices.
|
||||
*/
|
||||
private Map vmap = new HashMap();
|
||||
private Map emap = new HashMap();
|
||||
|
||||
/**
|
||||
* Create empty directed graph.
|
||||
*/
|
||||
public Graph() {}
|
||||
|
||||
/**
|
||||
* Create directed graph initialized with a set of subgraphs.
|
||||
*/
|
||||
public Graph(Graph[] graphs) {
|
||||
for(int i=0; i < graphs.length; i++) {
|
||||
addGraph(graphs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create empty graph.
|
||||
* @param d graph is directed?
|
||||
*/
|
||||
public Graph(boolean d) {
|
||||
directed = d;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read graph from file using Serializable.
|
||||
*
|
||||
* @param name file name to read from
|
||||
*/
|
||||
public Graph(String name) throws IOException, ClassNotFoundException
|
||||
{
|
||||
this(new FileInputStream(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Read graph from input stream using Serializable.
|
||||
*
|
||||
* @param stream stream to read from
|
||||
*/
|
||||
public Graph(InputStream stream) throws IOException, ClassNotFoundException
|
||||
{
|
||||
ObjectInputStream oi = new ObjectInputStream(new GZIPInputStream(stream));
|
||||
Graph g = (Graph)oi.readObject();
|
||||
|
||||
this.directed = g.directed;
|
||||
this.vmap = g.vmap;
|
||||
this.emap = g.emap;
|
||||
}
|
||||
|
||||
/* Add sub graph to this graph.
|
||||
*/
|
||||
public void addGraph(Graph g) {
|
||||
Vertex[] vertices = g.getVertexArray();
|
||||
for(int j=0; j < vertices.length; j++)
|
||||
addVertex(vertices[j]);
|
||||
|
||||
Edge[] edges = g.getEdgeArray();
|
||||
for(int j=0; j < edges.length; j++)
|
||||
addEdge(g.getSource(edges[j]), g.getTarget(edges[j]), edges[j]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a vertex to the graph.
|
||||
* @param v vertex to add
|
||||
*/
|
||||
public void addVertex(Vertex v) {
|
||||
if(v == null)
|
||||
{
|
||||
throw new GraphError("Vertex must not be null");
|
||||
}
|
||||
|
||||
if (debug)
|
||||
{
|
||||
System.out.println("Adding vertex: " + v);
|
||||
}
|
||||
|
||||
vmap.put(v, new VertexWrapper(v));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a (default) edge between two vertices to the graph.
|
||||
*
|
||||
* @param u source vertex.
|
||||
* @param v target vertex.
|
||||
*/
|
||||
public void addEdge(Vertex u, Vertex v) {
|
||||
addEdge(u, v, new Edge());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an edge between two vertices to the graph.
|
||||
*
|
||||
* @param u source vertex.
|
||||
* @param v target vertex.
|
||||
* @param e edge between
|
||||
*/
|
||||
public void addEdge(Vertex u, Vertex v, Edge e) {
|
||||
VertexWrapper uw = getWrapper(u);
|
||||
VertexWrapper vw = getWrapper(v);
|
||||
|
||||
if(e == null)
|
||||
{
|
||||
throw new GraphError("Edge must not be null");
|
||||
}
|
||||
|
||||
emap.put(e, new EdgeWrapper(e, u, v));
|
||||
uw.addEdge(e);
|
||||
|
||||
if(!directed)
|
||||
{
|
||||
// Add edge to the other side, too
|
||||
vw.addEdge(e);
|
||||
}
|
||||
|
||||
vw.addPredecessorEdge(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove given vertex and the edges that connect it to other vertices.
|
||||
*
|
||||
* @param v abandoned vertex.
|
||||
*/
|
||||
public void removeVertex(Vertex v) {
|
||||
VertexWrapper vw = getWrapper(v);
|
||||
|
||||
Edge[] edges = vw.getEdges(); // Remove all adjacent edges
|
||||
|
||||
if(!directed) { // Inform other side of the edge that it has to be removed
|
||||
for(int i=0; i < edges.length; i++) {
|
||||
EdgeWrapper ew = getWrapper(edges[i]);
|
||||
Vertex u = (ew.src == v)? ew.target : ew.src;
|
||||
VertexWrapper uw = getWrapper(u);
|
||||
uw.removeEdge(edges[i]);
|
||||
}
|
||||
} else {
|
||||
for(int i=0; i < edges.length; i++) {
|
||||
VertexWrapper uw = getWrapper(getWrapper(edges[i]).target);
|
||||
uw.removePredecessorEdge(edges[i]);
|
||||
}
|
||||
}
|
||||
|
||||
vmap.remove(v); // Remove vertex from the hash map
|
||||
|
||||
// Finally remove the edges from the hash map
|
||||
for(int i=0; i < edges.length; i++)
|
||||
emap.remove(edges[i]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove given edge from the graph.
|
||||
*
|
||||
* @param e abandoned edge.
|
||||
*/
|
||||
public void removeEdge(Edge e) {
|
||||
EdgeWrapper ew = getWrapper(e);
|
||||
emap.remove(e); // Remove edge from the hash map
|
||||
|
||||
// Update source vertex
|
||||
VertexWrapper vw = getWrapper(ew.src);
|
||||
vw.removeEdge(e);
|
||||
|
||||
if(!directed) { // Inform other node, too
|
||||
vw = getWrapper(ew.target);
|
||||
vw.removeEdge(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the graph so that it can be used over again.
|
||||
*/
|
||||
public void reset()
|
||||
{
|
||||
vmap.clear();
|
||||
emap.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get source vertex of edge. Order between source and target doesn't
|
||||
* really matter if this graph is undirected.
|
||||
*
|
||||
* @param e edge.
|
||||
*/
|
||||
public final Vertex getSource(Edge e) { return getWrapper(e).src; }
|
||||
|
||||
/**
|
||||
* Get target vertex of edge. Order between source and target doesn't
|
||||
* really matter if this graph is undirected.
|
||||
*
|
||||
* @param e edge.
|
||||
*/
|
||||
public final Vertex getTarget(Edge e) { return getWrapper(e).target; }
|
||||
|
||||
/**
|
||||
* @return number of vertices.
|
||||
*/
|
||||
public final int getNoVertices() { return vmap.size(); }
|
||||
|
||||
/**
|
||||
* @return number of edges.
|
||||
*/
|
||||
public final int getNoEdges() { return emap.size(); }
|
||||
|
||||
/**
|
||||
* @return all vertices of the graph.
|
||||
*/
|
||||
public Collection getVertices() { return vmap.keySet(); }
|
||||
|
||||
/**
|
||||
* @return all edges of the graph.
|
||||
*/
|
||||
public Collection getEdges() { return emap.keySet(); }
|
||||
|
||||
/**
|
||||
* @return all vertices of the graph.
|
||||
*/
|
||||
public final Vertex[] getVertexArray() {
|
||||
return (Vertex[])getVertices().toArray(new Vertex[getNoVertices()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return all edges of the graph.
|
||||
*/
|
||||
public final Edge[] getEdgeArray() {
|
||||
return (Edge[])getEdges().toArray(new Edge[getNoEdges()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return all edges between source and target
|
||||
*/
|
||||
public Edge[] getEdgeArray(Vertex source, Vertex target) {
|
||||
Collection c = getEdges(source, target);
|
||||
return (Edge[])c.toArray(new Edge[c.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return all edges between source and target
|
||||
*/
|
||||
public Collection getEdges(Vertex source, Vertex target) {
|
||||
Edge[] edges = getEdgeArray(source);
|
||||
List list = new ArrayList();
|
||||
|
||||
for(int i=0; i < edges.length; i++)
|
||||
if(getTarget(edges[i]) == target)
|
||||
list.add(edges[i]);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return all vertices adjacent to v.
|
||||
*/
|
||||
public Vertex[] getVertexArray(Vertex v) {
|
||||
Edge[] edges = getEdgeArray(v);
|
||||
int size = edges.length;
|
||||
Vertex[] vertices = new Vertex[size];
|
||||
|
||||
/* Take the other side of the edge, no matter if directed or not
|
||||
*/
|
||||
for(int i=0; i < size; i++) {
|
||||
EdgeWrapper ew = getWrapper(edges[i]);
|
||||
vertices[i] = (ew.src == v)? ew.target : ew.src;
|
||||
}
|
||||
|
||||
return vertices;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return number of vertices adjacent to v.
|
||||
*/
|
||||
public int getNoVertices(Vertex v) {
|
||||
VertexWrapper vw = getWrapper(v);
|
||||
return vw.edges.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return number of edges adjacent to v.
|
||||
*/
|
||||
public final int getNoEdges(Vertex v) {
|
||||
return getNoVertices(v);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return all edges adjacent to v.
|
||||
*/
|
||||
public Edge[] getEdgeArray(Vertex v) {
|
||||
VertexWrapper vw = getWrapper(v);
|
||||
return vw.getEdges();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return all vertices adjacent to v.
|
||||
*/
|
||||
public Collection getVertices(Vertex v) {
|
||||
return Arrays.asList(getVertexArray(v));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return all edges adjacent to v.
|
||||
*/
|
||||
public Collection getEdges(Vertex v) {
|
||||
return Arrays.asList(getEdgeArray(v));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all vertices from which v may be reached directly, in an undirected
|
||||
* graph, this is identical to getVertexArray(v)
|
||||
*
|
||||
* @return all predecessor vertices of v
|
||||
*/
|
||||
public Vertex[] getPredecessorArray(Vertex v) {
|
||||
if(!directed)
|
||||
return getVertexArray(v);
|
||||
|
||||
Collection c = getPredecessors(v);
|
||||
return (Vertex[])c.toArray(new Vertex[c.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all vertices from which v may be reached directly, in an undirected
|
||||
* graph, this is identical to getVertices(v)
|
||||
*
|
||||
* @return all predecessor vertices of v
|
||||
*/
|
||||
public Collection getPredecessors(Vertex v) {
|
||||
if(!directed)
|
||||
return getVertices(v);
|
||||
|
||||
List list = new ArrayList();
|
||||
Collection edges = getWrapper(v).pred_edges;
|
||||
|
||||
for(Iterator i = edges.iterator(); i.hasNext(); )
|
||||
list.add(getWrapper(((Edge)i.next())).src);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public Edge[] getPredecessorEdgeArray(Vertex v) {
|
||||
if(!directed)
|
||||
return getEdgeArray(v);
|
||||
|
||||
Collection edges = getWrapper(v).pred_edges;
|
||||
|
||||
return (Edge[])edges.toArray(new Edge[edges.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all edges pointing to v, in an undirected
|
||||
* graph, this is identical to getEdges(v)
|
||||
*
|
||||
* @return all predecessor vertices of v
|
||||
*/
|
||||
public Collection getPredecessorEdges(Vertex v) {
|
||||
if(!directed)
|
||||
return getEdges(v);
|
||||
|
||||
return (Collection)((ArrayList)getWrapper(v).pred_edges).clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Is v in this graph?
|
||||
*/
|
||||
public final boolean contains(Vertex v) {
|
||||
return vmap.containsKey(v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is e in this graph?
|
||||
*/
|
||||
public final boolean contains(Edge e) {
|
||||
return emap.containsKey(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check, if v is a vertex of graph g.
|
||||
*/
|
||||
public static final boolean contains(Graph g, Vertex v) {
|
||||
return g.contains(v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check, if e is an edge of graph g.
|
||||
*/
|
||||
public static final boolean contains(Graph g, Edge e) {
|
||||
return g.contains(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check, if g is a subgraph of super_g, i.e. if all vertices and edges of g are
|
||||
* contained in super_g, too.
|
||||
*
|
||||
* @param super_g the super graph
|
||||
* @param g the subgraph
|
||||
*/
|
||||
public static final boolean contains(Graph super_g, Graph g) {
|
||||
for(Iterator i = g.getVertices().iterator(); i.hasNext();) {
|
||||
Vertex v = (Vertex)i.next();
|
||||
if(!contains(super_g, v))
|
||||
return false;
|
||||
}
|
||||
|
||||
for(Iterator i = g.getEdges().iterator(); i.hasNext();) {
|
||||
Edge e = (Edge)i.next();
|
||||
if(!contains(super_g, e))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check, if g is a subgraph of this graph. Tests for object equivalence (==).
|
||||
*/
|
||||
public final boolean contains(Graph g) {
|
||||
return contains(this, g);
|
||||
}
|
||||
|
||||
/**
|
||||
* Access for visitor patterns
|
||||
*/
|
||||
public void accept(Visitor v) { v.discoverGraph(this); }
|
||||
|
||||
/**
|
||||
* Is this graph directed?
|
||||
*/
|
||||
public final boolean isDirected() { return directed; }
|
||||
|
||||
/**
|
||||
* @return wrapper object corresponding to vertex or throw an exception.
|
||||
*/
|
||||
private final VertexWrapper getWrapper(Vertex v) {
|
||||
VertexWrapper vw = (VertexWrapper)vmap.get(v);
|
||||
|
||||
if(vw == null)
|
||||
throw new GraphError("Vertex not contained in this graph: " + v);
|
||||
|
||||
return vw;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return wrapper object corresponding to edge or throw an exception.
|
||||
*/
|
||||
private final EdgeWrapper getWrapper(Edge e) {
|
||||
EdgeWrapper ew = (EdgeWrapper)emap.get(e);
|
||||
|
||||
if(ew == null)
|
||||
throw new GraphError("Edge not contained in this graph: " + e);
|
||||
|
||||
return ew;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return toString(GraphConstants.PLAIN);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return graph contents traversed with DFS
|
||||
*/
|
||||
public String toString(int verbose) {
|
||||
if(verbose == GraphConstants.PLAIN)
|
||||
return printArray(getVertexArray());
|
||||
else {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
PrintWriter out = new PrintWriter(bos);
|
||||
|
||||
new DFS(new PrintVisitor(out, GraphConstants.VERBOSE)).start(this);
|
||||
out.close();
|
||||
|
||||
return bos.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All vertices and edges are copied deeply, i.e. copy() will be called on
|
||||
* them recursively.
|
||||
*
|
||||
* @return deep copy of this graph.
|
||||
*/
|
||||
public Graph copy() {
|
||||
Graph g = new Graph(directed);
|
||||
|
||||
for(Iterator i = getVertices().iterator(); i.hasNext();) {
|
||||
Vertex v = ((Vertex)i.next()).copy();
|
||||
g.addVertex(v);
|
||||
}
|
||||
|
||||
for(Iterator i = getEdges().iterator(); i.hasNext();) {
|
||||
Edge e = (Edge)i.next();
|
||||
EdgeWrapper ew = getWrapper(e);
|
||||
|
||||
g.addEdge(ew.src, ew.target, e.copy());
|
||||
}
|
||||
|
||||
return g;
|
||||
}
|
||||
|
||||
public Graph copy(Vertex start) {
|
||||
final Graph g = new Graph(directed);
|
||||
|
||||
new DFS(new DefaultVisitor() {
|
||||
public void discoverVertex(Vertex v) {
|
||||
g.addVertex(v);
|
||||
}
|
||||
|
||||
public void finishEdge(Edge e) {
|
||||
EdgeWrapper ew = getWrapper(e);
|
||||
|
||||
g.addEdge(ew.src, ew.target, e);
|
||||
}
|
||||
}, false).start(this, start);
|
||||
|
||||
return g;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return shallow copy of this graph, i.e. all vertices and edges keep their
|
||||
* object identity
|
||||
*/
|
||||
public Object clone() {
|
||||
Graph g = new Graph(directed);
|
||||
|
||||
for(Iterator i = getVertices().iterator(); i.hasNext();) {
|
||||
Vertex v = ((Vertex)i.next());
|
||||
g.addVertex(v);
|
||||
}
|
||||
|
||||
for(Iterator i = getEdges().iterator(); i.hasNext();) {
|
||||
Edge e = (Edge)i.next();
|
||||
EdgeWrapper ew = getWrapper(e);
|
||||
|
||||
g.addEdge(ew.src, ew.target, e);
|
||||
}
|
||||
|
||||
return g;
|
||||
}
|
||||
|
||||
/* Wrap association information into class, s.a.
|
||||
*/
|
||||
private static final class VertexWrapper implements Serializable {
|
||||
Vertex vertex;
|
||||
List edges = new ArrayList(3);
|
||||
List pred_edges = new ArrayList(3);
|
||||
|
||||
final void addEdge(Edge e) { edges.add(e); }
|
||||
final void addPredecessorEdge(Edge e) { pred_edges.add(e); }
|
||||
final void removeEdge(Edge e) { edges.remove(e); }
|
||||
final void removePredecessorEdge(Edge e) { pred_edges.remove(e); }
|
||||
|
||||
final Edge[] getEdges() {
|
||||
return (Edge[])edges.toArray(new Edge[edges.size()]);
|
||||
}
|
||||
|
||||
VertexWrapper(Vertex v) {
|
||||
vertex = v;
|
||||
}
|
||||
}
|
||||
|
||||
/* Wrap association information into class, s.a.
|
||||
*/
|
||||
private static final class EdgeWrapper implements Serializable {
|
||||
Edge edge;
|
||||
Vertex src, target;
|
||||
|
||||
EdgeWrapper(Edge e, Vertex u, Vertex v) {
|
||||
edge = e;
|
||||
src = u;
|
||||
target = v;
|
||||
}
|
||||
}
|
||||
|
||||
/** Clear all vertex attributes like color, distance, ...
|
||||
*/
|
||||
public void clearVertices() {
|
||||
for(Iterator i = getVertices().iterator(); i.hasNext();) {
|
||||
Vertex v = ((Vertex)i.next());
|
||||
v.setColor(-1);
|
||||
v.setDistance(-1);
|
||||
v.setDiscoveryTime(-1);
|
||||
v.setFinishingTime(-1);
|
||||
v.setPredecessor(null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump graph to file using Serializable.
|
||||
*
|
||||
* @param name file name to dump to
|
||||
*/
|
||||
public void dump(String name) throws IOException {
|
||||
dump(new FileOutputStream(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump graph to output stream using Serializable.
|
||||
*
|
||||
* @param stream stream to dump to
|
||||
*/
|
||||
public void dump(OutputStream stream) throws IOException {
|
||||
ObjectOutputStream oo = new ObjectOutputStream(new GZIPOutputStream(stream));
|
||||
oo.writeObject(this);
|
||||
oo.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the given array using toString() where appropiate.
|
||||
*/
|
||||
private static final String printArray(Object[] obj) {
|
||||
StringBuffer buf = new StringBuffer("{");
|
||||
|
||||
for(int i=0; i < obj.length; i++) {
|
||||
if(obj[i] != null)
|
||||
buf.append(obj[i].toString());
|
||||
else
|
||||
buf.append("null");
|
||||
|
||||
if(i < obj.length - 1)
|
||||
buf.append(", ");
|
||||
}
|
||||
buf.append('}');
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,195 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
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.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
|
||||
{
|
||||
/**
|
||||
* Compute the inverse of a graph, i.e. with all edges reverted
|
||||
*/
|
||||
public static final Graph transposeGraph(DirectedGraph g) {
|
||||
DirectedGraph graph = new DirectedGraph();
|
||||
|
||||
for(Iterator i=g.getVertices().iterator(); i.hasNext();)
|
||||
graph.addVertex((Vertex)i.next());
|
||||
|
||||
for(Iterator i=g.getEdges().iterator(); i.hasNext();) {
|
||||
Edge f = (Edge)i.next();
|
||||
|
||||
graph.addEdge(g.getTarget(f), g.getSource(f), f);
|
||||
}
|
||||
|
||||
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.
|
||||
*
|
||||
* @param super_g super graph containing the subset of vertices
|
||||
* @param g subset of super_g (g must be SCC of super_g)
|
||||
*/
|
||||
public static final boolean isLoop(Graph super_g, Graph loop) {
|
||||
return findLoopEntry(super_g, loop) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test whether the SCC g is a loop.
|
||||
*
|
||||
* @param super_g super graph containing the subset of vertices
|
||||
* @param g subset of super_g (g must be SCC of super_g)
|
||||
* @return root node of loop, i.e. the entry point, or null if this is no loop
|
||||
*/
|
||||
public static final Vertex findLoopEntry(Graph super_g, Graph loop) {
|
||||
switch(loop.getNoVertices()) {
|
||||
case 0:
|
||||
return null; // certainly not a loop
|
||||
|
||||
case 1: // singleton must point to itself, i.e. be a self-loop
|
||||
Vertex v = loop.getVertexArray()[0];
|
||||
Vertex[] edges = loop.getVertexArray(v);
|
||||
|
||||
for(int i=0; i < edges.length; i++)
|
||||
if(edges[i] == v)
|
||||
return v;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/* Make sure that the root node is the only entry point to the
|
||||
* SCC, i.e. there is no way from a node v \in (super_g \ g) into
|
||||
* a node in g except via the root node.
|
||||
*
|
||||
* So we have to find the root node first ...
|
||||
*/
|
||||
Vertex root = null;
|
||||
Vertex[] vertices = super_g.getVertexArray();
|
||||
|
||||
for(int i=0; i < vertices.length; i++) {
|
||||
Vertex v = vertices[i];
|
||||
|
||||
if(loop.contains(v)) // v is in the SCC, no check needed
|
||||
continue;
|
||||
|
||||
Vertex[] edges = super_g.getVertexArray(v); // All nodes reachable from v
|
||||
for(int j=0; j < edges.length; j++) {
|
||||
Vertex u = edges[j];
|
||||
|
||||
if(loop.contains(u))
|
||||
if(root == null)
|
||||
root = u;
|
||||
else
|
||||
return null; // Can't be a loop then, i.e. has more than one root node
|
||||
}
|
||||
}
|
||||
|
||||
return root; // Has loop a root node at all !?
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* GraphConstants useful for graph algorithms.
|
||||
*
|
||||
* @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>
|
||||
*/
|
||||
|
||||
public interface GraphConstants
|
||||
{
|
||||
public static final int WHITE = 0;
|
||||
public static final int GRAY = 1;
|
||||
public static final int BLACK = 2;
|
||||
|
||||
public static final int INFINITE = Integer.MAX_VALUE;
|
||||
|
||||
/**
|
||||
* Modes for toString(int verbose), three seem sufficient, but you can use your own
|
||||
*/
|
||||
public static final int PLAIN = 0; // Simple output
|
||||
public static final int VERBOSE = 1; // Verbose output
|
||||
public static final int TALKATIVE = 2; // Even more verbose output
|
||||
public static final int V_PLAIN = 3; // Simple output for graph layout
|
||||
public static final int V_VERBOSE = 4; // Verbose output for graph layout
|
||||
public static final int V_TALKATIVE = 5; // Even more verbose output for graph layout
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @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 {
|
||||
public GraphError(String s) { super(s); }
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
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();
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Describes a labeled edge between two vertices of a graph.
|
||||
*
|
||||
* @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 {
|
||||
private String label;
|
||||
|
||||
/**
|
||||
* Create edge with label.
|
||||
*/
|
||||
public LabeledEdge(String l) {
|
||||
label = l;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param verbose toggle output format
|
||||
* @return String representation.
|
||||
*/
|
||||
public String toString(int verbose) {
|
||||
return label;
|
||||
}
|
||||
|
||||
public final String getLabel() { return label; }
|
||||
public final void setLabel(String l) { label = l; }
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Describes a labeled vertex of a graph.
|
||||
*
|
||||
* @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 {
|
||||
private String label;
|
||||
|
||||
/**
|
||||
* Create vertex with label l
|
||||
*/
|
||||
public LabeledVertex(String l) {
|
||||
label = l;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param verbose toggle output format
|
||||
* @return String representation.
|
||||
*/
|
||||
public String toString(int verbose) {
|
||||
return label;
|
||||
}
|
||||
|
||||
public final String getLabel() { return label; }
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
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.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
|
||||
extends TraversalOrder
|
||||
{
|
||||
/**
|
||||
* Depth first search algorithm.
|
||||
*
|
||||
* @param v visitor object to apply when discovering and finishing a vertex or edge
|
||||
*/
|
||||
public Sequential(Visitor v) {
|
||||
super(v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Visit graph in sequential order
|
||||
*
|
||||
* @param g graph to traverse
|
||||
* @param start vertex to start at (or null, starts at first vertex then)
|
||||
*/
|
||||
public void start(Graph g, Vertex start) {
|
||||
int size = g.getNoVertices();
|
||||
Vertex[] vertices = g.getVertexArray();
|
||||
int index = 0;
|
||||
|
||||
visitor.discoverGraph(g);
|
||||
|
||||
/* Initially mark all vertices as WHITE
|
||||
*/
|
||||
for(int i=0; i < size; i++) {
|
||||
vertices[i].setColor(GraphConstants.WHITE);
|
||||
if(vertices[i] == start)
|
||||
index = i;
|
||||
}
|
||||
|
||||
for(int i=0; (i < size) && !stopped; i++) {
|
||||
Vertex v = vertices[index++ % size];
|
||||
|
||||
visitor.discoverVertex(v);
|
||||
v.setColor(GraphConstants.GRAY);
|
||||
|
||||
Edge[] edges = g.getEdgeArray(v);
|
||||
for(int j=0; j < edges.length; j++) {
|
||||
visitor.discoverEdge(edges[j]);
|
||||
visitor.finishEdge(edges[j]);
|
||||
}
|
||||
|
||||
visitor.finishVertex(v);
|
||||
v.setColor(GraphConstants.BLACK);
|
||||
}
|
||||
|
||||
visitor.finishGraph(g);
|
||||
}
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
import org.apache.commons.graph.visitor.Visitor;
|
||||
|
||||
/**
|
||||
* Class takes a visitor "piggy-backed" and applies it to all nodes
|
||||
* and edges. The traversal order implemented by children of this
|
||||
* class may be Depth-first or Breadth-first search, e.g..
|
||||
*
|
||||
* This relates to the "Strategy" design pattern.
|
||||
*
|
||||
* @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
|
||||
* @see Sequential
|
||||
*/
|
||||
public abstract class TraversalOrder
|
||||
{
|
||||
protected Visitor visitor;
|
||||
protected Graph graph;
|
||||
protected boolean stopped;
|
||||
protected boolean traverse_complete_graph;
|
||||
|
||||
/**
|
||||
* Depth first search algorithm.
|
||||
*
|
||||
* @param v visitor object to apply when discovering and finishing a vertex or edge
|
||||
* @param traverse_all traverse the complete graph or stop after finishing all nodes reachable
|
||||
* from the start
|
||||
*/
|
||||
public TraversalOrder(Visitor v, boolean traverse_all) {
|
||||
visitor = v;
|
||||
traverse_complete_graph = traverse_all;
|
||||
}
|
||||
|
||||
public TraversalOrder(Visitor v) {
|
||||
this(v, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start traversal.
|
||||
*
|
||||
* @param g graph to traverse
|
||||
* @param start vertex to start at (or null, starts at some vertex then)
|
||||
* @param all_vertices visit all vertices or just those reache from start
|
||||
*/
|
||||
public abstract void start(Graph g, Vertex start);
|
||||
|
||||
/** May be called to stop traversal, e.g. if a searched node has been found.
|
||||
*/
|
||||
public void stop() {
|
||||
stopped = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Depth first search algorithm starting at some vertex.
|
||||
*
|
||||
* @param g graph to traverse
|
||||
*/
|
||||
public void start(Graph g) {
|
||||
start(g, null);
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
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 );
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements a simple first-in, first-out queue of vertices of fixed size.
|
||||
* 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.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 {
|
||||
protected Vertex[] vertices;
|
||||
protected int left, right;
|
||||
|
||||
public VQueue(int s) {
|
||||
vertices = new Vertex[s];
|
||||
}
|
||||
|
||||
public void enqueue(Vertex v) { vertices[right++] = v; }
|
||||
public Vertex dequeue() { return vertices[left++]; }
|
||||
public Vertex head() { return vertices[left]; }
|
||||
public boolean empty() { return right <= left; }
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements a simple first-in, last-out stack of vertices of fixed size.
|
||||
*
|
||||
* @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 {
|
||||
private Vertex[] vertices;
|
||||
private int top;
|
||||
|
||||
public VStack(int s) {
|
||||
vertices = new Vertex[s];
|
||||
}
|
||||
|
||||
public final void push(Vertex v) { vertices[top++] = v; }
|
||||
public final Vertex pop() { return vertices[--top]; }
|
||||
public final Vertex top() { return vertices[top - 1]; }
|
||||
public final boolean empty() { return top <= 0; }
|
||||
}
|
||||
@@ -1,199 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.apache.commons.graph.visitor.Visitor;
|
||||
|
||||
/**
|
||||
* Describes a vertex of a graph.
|
||||
*
|
||||
* @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
|
||||
implements Graphable,Cloneable,Serializable
|
||||
{
|
||||
private int ident; // unique number, i.e. identifier
|
||||
private static int counter; // produce default numbers
|
||||
|
||||
/** Useful variables for graph algorithms, calculated by DFS.
|
||||
* @see DFS
|
||||
*/
|
||||
private int dtime, ftime;
|
||||
private int color; // Color of vertex during traversal
|
||||
private Vertex pred; // Predecessor of vertex
|
||||
private int distance; // (Shortest) path to given root node (BFS)
|
||||
|
||||
/**
|
||||
* Create vertex with default identifier.
|
||||
*/
|
||||
public Vertex() {
|
||||
ident = counter++;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ident, i.e. identifier of this node
|
||||
*/
|
||||
public final int getIdent() { return ident; }
|
||||
|
||||
/**
|
||||
* @return number of created vertices
|
||||
*/
|
||||
public static final int getCounter() { return counter; }
|
||||
|
||||
/**
|
||||
* @return String representation.
|
||||
*/
|
||||
public String toString() {
|
||||
return toString(GraphConstants.PLAIN);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return name to be used in output.
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param verbose toggle output format
|
||||
* @return String representation.
|
||||
*/
|
||||
public String toString(int verbose) {
|
||||
switch(verbose) {
|
||||
case GraphConstants.VERBOSE: case GraphConstants.TALKATIVE:
|
||||
return "Vertex(" + ident + ", pred=" +
|
||||
((pred == null)? "null" : ("" + pred.ident)) +
|
||||
", discovery=" + dtime + ", finishing=" + ftime +
|
||||
", distance=" + distance + ")";
|
||||
|
||||
case GraphConstants.V_VERBOSE: case GraphConstants.V_TALKATIVE:
|
||||
return "Vertex(" + ident + ")\npred=" +
|
||||
((pred == null)? "null" : ("" + pred.ident)) +
|
||||
"\ndiscovery=" + dtime + "\nfinishing=" + ftime +
|
||||
"\ndistance=" + distance;
|
||||
|
||||
case GraphConstants.PLAIN: case GraphConstants.V_PLAIN: default:
|
||||
return "Vertex(" + ident + ")";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Computed by DFS, BFS or other visitors
|
||||
* @see Visitor
|
||||
*/
|
||||
public int getDiscoveryTime() { return dtime; }
|
||||
|
||||
/**
|
||||
* Computed by DFS, BFS or other visitors
|
||||
* @see Visitor
|
||||
*/
|
||||
public int getFinishingTime() { return ftime; }
|
||||
|
||||
/**
|
||||
* Computed by DFS, BFS or other visitors
|
||||
* @see Visitor
|
||||
* @deprecated use Graph.getPredecessors
|
||||
*/
|
||||
public Vertex getPredecessor() { return pred; }
|
||||
|
||||
/**
|
||||
* Computed by DFS, BFS or other visitors
|
||||
* @see Visitor
|
||||
* @return distance from start vertex (No. edges in between)
|
||||
* @see BFSVisitor
|
||||
*/
|
||||
public int getDistance() { return distance; }
|
||||
|
||||
/**
|
||||
* May be set only by classes of this package, Visitor in particular.
|
||||
*/
|
||||
public void setDistance(int d) { distance = d; }
|
||||
public void setDiscoveryTime(int d) { dtime = d; }
|
||||
public void setFinishingTime(int f) { ftime = f; }
|
||||
public void setColor(int c) { color = c; }
|
||||
public int getColor() { return color; }
|
||||
public void setPredecessor(Vertex p) { pred = p; }
|
||||
|
||||
/**
|
||||
* Access for visitors
|
||||
*/
|
||||
public void accept(Visitor v) { v.discoverVertex(this); }
|
||||
|
||||
/**
|
||||
* @return deep copy
|
||||
*/
|
||||
public Vertex copy() {
|
||||
try {
|
||||
return (Vertex)clone();
|
||||
}
|
||||
catch(CloneNotSupportedException e) {}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Check for equality.
|
||||
*/
|
||||
public boolean equals(Object o) {
|
||||
if(!(o instanceof Vertex))
|
||||
return false;
|
||||
|
||||
Vertex v = (Vertex)o;
|
||||
return v.ident == ident;
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
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/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Describes an edge with a weight.
|
||||
*
|
||||
* @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;
|
||||
|
||||
/**
|
||||
* Create edge with a weight.
|
||||
*/
|
||||
public WeightedEdge(int w) {
|
||||
weight = w;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param verbose toggle output format
|
||||
* @return String representation.
|
||||
*/
|
||||
public String toString(int verbose) {
|
||||
switch(verbose) {
|
||||
case GraphConstants.TALKATIVE: case GraphConstants.V_TALKATIVE:
|
||||
return "WeightedEdge(" + getIdent() + "\nweight=" + weight + ")";
|
||||
default:
|
||||
return "" + weight;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
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 );
|
||||
}
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
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 );
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
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 );
|
||||
}
|
||||
}
|
||||
@@ -1,252 +0,0 @@
|
||||
package org.apache.commons.graph.util;
|
||||
|
||||
/**
|
||||
* What is transitive closure?
|
||||
*
|
||||
* Consider a directed graph G=(V,E), where V is the set of vertices and E is the
|
||||
* set of edges. The transitive closure of G is a graph G+ = (V,E+) such
|
||||
* that for all v,w in V there is an edge (v,w) in E+ if and only if
|
||||
* there is a non-null path from v to w in G.
|
||||
*/
|
||||
|
||||
// V = { a, b, c, d, e, f }
|
||||
// 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;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.graph.DFS;
|
||||
import org.apache.commons.graph.Graph;
|
||||
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
|
||||
{
|
||||
/**
|
||||
* Graph of the entities for which the dependencies
|
||||
* must be resolved.
|
||||
*/
|
||||
private Graph graph;
|
||||
|
||||
private Map vertices;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*/
|
||||
public DependencyResolver()
|
||||
{
|
||||
// Create a directed graph.
|
||||
graph = new Graph(true);
|
||||
|
||||
// Create a map to collapse duplicate vertices.
|
||||
// This should be handled in the graph package but
|
||||
// I will take care of it here for now.
|
||||
vertices = new HashMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of sorted dependencies given a starting
|
||||
* vertex.
|
||||
*
|
||||
* @param g Starting vertex in the form of a Graphable object.
|
||||
* @return A list of Graphable objects in sorted dependency order.
|
||||
*/
|
||||
public List getSortedDependencies(Graphable g)
|
||||
{
|
||||
// Create the DFS
|
||||
DependencyVisitor visitor = new DependencyVisitor();
|
||||
DFS dfs = new DFS(visitor);
|
||||
dfs.start(graph,(Vertex)g);
|
||||
return visitor.getSortedDependencies();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of sorted dependencies given a starting
|
||||
* vertex.
|
||||
*
|
||||
* @param g Starting vertex in the form of a String object.
|
||||
* @return A list of Strings in sorted dependency order.
|
||||
*/
|
||||
public List getSortedDependencies(String g)
|
||||
{
|
||||
LabeledVertex start = (LabeledVertex) vertices.get(g);
|
||||
List l = getSortedDependencies(start);
|
||||
ArrayList sl = new ArrayList();
|
||||
|
||||
for (Iterator i = l.iterator(); i.hasNext();)
|
||||
{
|
||||
sl.add(((Graphable)i.next()).getName());
|
||||
}
|
||||
return sl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all the vertices and edges from the graph.
|
||||
*/
|
||||
public void reset()
|
||||
{
|
||||
graph.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a String representation of all the vertices
|
||||
* present in the graph.
|
||||
*
|
||||
* @return String representation of the vertices in the graph.
|
||||
*/
|
||||
public String getVerticesString()
|
||||
{
|
||||
Vertex[] va = graph.getVertexArray();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
for (int i = 0; i < va.length; i++)
|
||||
{
|
||||
sb.append(va[i].toString());
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(String g, List dependencies)
|
||||
{
|
||||
LabeledVertex startingVertex = (LabeledVertex) vertices.get(g);
|
||||
if (startingVertex == null)
|
||||
{
|
||||
// Create a labeled vertex with our starting vertex g.
|
||||
startingVertex = new LabeledVertex(g);
|
||||
vertices.put(g, startingVertex);
|
||||
graph.addVertex(startingVertex);
|
||||
}
|
||||
|
||||
for (Iterator i = dependencies.iterator(); i.hasNext();)
|
||||
{
|
||||
// Create a vertex and add to the graph.
|
||||
String dependencyName = (String) 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);
|
||||
if (dependency == null)
|
||||
{
|
||||
dependency = new LabeledVertex(dependencyName);
|
||||
vertices.put(dependencyName, dependency);
|
||||
graph.addVertex(dependency);
|
||||
}
|
||||
|
||||
// Create an edge between the starting vertex and
|
||||
// the dependency.
|
||||
graph.addEdge(startingVertex, dependency);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
LabeledVertex a = new LabeledVertex("A");
|
||||
LabeledVertex b = new LabeledVertex("B");
|
||||
LabeledVertex c = new LabeledVertex("C");
|
||||
LabeledVertex d = new LabeledVertex("D");
|
||||
LabeledVertex e = new LabeledVertex("E");
|
||||
LabeledVertex f = new LabeledVertex("F");
|
||||
|
||||
graph.addVertex(a);
|
||||
graph.addVertex(b);
|
||||
graph.addVertex(c);
|
||||
graph.addVertex(d);
|
||||
graph.addVertex(e);
|
||||
graph.addVertex(f);
|
||||
|
||||
graph.addEdge(c,f);
|
||||
|
||||
graph.addEdge(d,a);
|
||||
graph.addEdge(d,b);
|
||||
|
||||
graph.addEdge(e,b);
|
||||
graph.addEdge(e,c);
|
||||
graph.addEdge(e,d);
|
||||
|
||||
graph.addEdge(f,d);
|
||||
|
||||
DependencyVisitor visitor = new DependencyVisitor();
|
||||
DFS dfs = new DFS(visitor);
|
||||
dfs.start(graph,(Vertex)f);
|
||||
List l = visitor.getSortedDependencies();
|
||||
|
||||
for (Iterator i = l.iterator(); i.hasNext();)
|
||||
{
|
||||
System.out.println(i.next());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
package org.apache.commons.graph.visitor;
|
||||
|
||||
/* ====================================================================
|
||||
* 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.BFS;
|
||||
import org.apache.commons.graph.GraphConstants;
|
||||
import org.apache.commons.graph.Edge;
|
||||
import org.apache.commons.graph.Graph;
|
||||
import org.apache.commons.graph.Vertex;
|
||||
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.3 2002/01/16 20:38:29 jvanzyl Exp $
|
||||
* @author <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
|
||||
* @see BFS
|
||||
*/
|
||||
public class BFSVisitor extends DefaultVisitor implements GraphConstants {
|
||||
private VStack stack; // Stack to remember predecessors
|
||||
|
||||
public BFSVisitor() {}
|
||||
|
||||
public void discoverGraph(Graph g) {
|
||||
int size = g.getNoVertices();
|
||||
Vertex[] vertices = g.getVertexArray();
|
||||
|
||||
for(int i=0; i < size; i++) {
|
||||
vertices[i].setDistance(INFINITE);
|
||||
vertices[i].setPredecessor(null);
|
||||
}
|
||||
|
||||
stack = new VStack(size);
|
||||
}
|
||||
|
||||
public void discoverVertex(Vertex v) {
|
||||
if(stack.empty()) { // At start vertex, v == start
|
||||
v.setPredecessor(null);
|
||||
v.setDistance(0);
|
||||
}
|
||||
else {
|
||||
Vertex pred = stack.top();
|
||||
v.setPredecessor(pred);
|
||||
v.setDistance(pred.getDistance() + 1);
|
||||
}
|
||||
|
||||
stack.push(v);
|
||||
}
|
||||
|
||||
public void finishVertex(Vertex v) {
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
public void visit(Graph g, Vertex start) {
|
||||
new BFS(this).start(g, start);
|
||||
}
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
package org.apache.commons.graph.visitor;
|
||||
|
||||
/* ====================================================================
|
||||
* 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.Edge;
|
||||
import org.apache.commons.graph.Graph;
|
||||
import org.apache.commons.graph.Vertex;
|
||||
import org.apache.commons.graph.VStack;
|
||||
|
||||
/**
|
||||
* Visit graph with DFS and compute predecessors during traversal as
|
||||
* well as discovery and finishing time of all vertices. Instances of
|
||||
* this class (and of course its descendants) may be passed to a DFS
|
||||
* object.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public class DFSVisitor extends DefaultVisitor {
|
||||
|
||||
/**
|
||||
* Global time.
|
||||
*/
|
||||
private int time;
|
||||
|
||||
/**
|
||||
* Stack to remember predecessors.
|
||||
*/
|
||||
private VStack stack;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public DFSVisitor()
|
||||
{
|
||||
}
|
||||
|
||||
public void discoverGraph(Graph g) {
|
||||
int size = g.getNoVertices();
|
||||
Vertex[] vertices = g.getVertexArray();
|
||||
|
||||
for(int i=0; i < size; i++)
|
||||
{
|
||||
vertices[i].setPredecessor(null);
|
||||
}
|
||||
|
||||
time = 0; // Reset timer
|
||||
stack = new VStack(size);
|
||||
}
|
||||
|
||||
public void discoverVertex(Vertex v) {
|
||||
if(stack.empty()) // First node
|
||||
{
|
||||
v.setPredecessor(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
v.setPredecessor(stack.top());
|
||||
}
|
||||
|
||||
stack.push(v);
|
||||
v.setDiscoveryTime(time++);
|
||||
}
|
||||
|
||||
public void finishVertex(Vertex v) {
|
||||
v.setFinishingTime(time++);
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package org.apache.commons.graph.visitor;
|
||||
|
||||
/* ====================================================================
|
||||
* 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.DFS;
|
||||
import org.apache.commons.graph.Edge;
|
||||
import org.apache.commons.graph.Graph;
|
||||
import org.apache.commons.graph.Vertex;
|
||||
|
||||
/**
|
||||
* Empty Visitor, i.e. all visit methods do nothing.
|
||||
*
|
||||
* @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 {
|
||||
protected DFS dfs;
|
||||
|
||||
public DefaultVisitor() {}
|
||||
|
||||
public void discoverGraph(Graph g) {}
|
||||
public void finishGraph(Graph g) {}
|
||||
public void discoverVertex(Vertex v) {}
|
||||
public void finishVertex(Vertex v) { }
|
||||
public void discoverEdge(Edge e) {}
|
||||
public void finishEdge(Edge e) {}
|
||||
|
||||
public void visit(Graph g, Vertex start) {
|
||||
dfs = new DFS(this);
|
||||
dfs.start(g, start);
|
||||
}
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
package org.apache.commons.graph.visitor;
|
||||
|
||||
/* ====================================================================
|
||||
* 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.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.graph.Vertex;
|
||||
|
||||
/**
|
||||
* Visit graph with DFS and compute predecessors during traversal as
|
||||
* well as discovery and finishing time of all vertices. Instances of
|
||||
* this class (and of course its descendants) may be passed to a DFS
|
||||
* object.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public class DependencyVisitor
|
||||
extends DFSVisitor
|
||||
{
|
||||
/**
|
||||
* Dependencies in sorted order. The order in which
|
||||
* the vertices are finished is the order in which
|
||||
* they must be processed by the mechanism using
|
||||
* the dependency list.
|
||||
*/
|
||||
LinkedList sortedDependencies;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public DependencyVisitor()
|
||||
{
|
||||
sortedDependencies = new LinkedList();
|
||||
}
|
||||
|
||||
public void finishVertex(Vertex v)
|
||||
{
|
||||
super.finishVertex(v);
|
||||
sortedDependencies.addLast(v);
|
||||
}
|
||||
|
||||
public List getSortedDependencies()
|
||||
{
|
||||
return sortedDependencies;
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package org.apache.commons.graph.visitor;
|
||||
|
||||
/* ====================================================================
|
||||
* 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.io.PrintWriter;
|
||||
|
||||
import org.apache.commons.graph.DFS;
|
||||
import org.apache.commons.graph.Edge;
|
||||
import org.apache.commons.graph.Graph;
|
||||
import org.apache.commons.graph.Vertex;
|
||||
|
||||
/**
|
||||
* Visit graph with depth first search and simply prints the current
|
||||
* 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.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 {
|
||||
private PrintWriter out;
|
||||
private int verbose;
|
||||
|
||||
/**
|
||||
* Simply print vertex and edge contents, when visiting them.
|
||||
*
|
||||
* @param o stream to print to
|
||||
* @param v toggle verbosity of output
|
||||
* @see GraphConstants
|
||||
*/
|
||||
public PrintVisitor(PrintWriter o, int v) {
|
||||
out = o;
|
||||
verbose = v;
|
||||
}
|
||||
|
||||
public void discoverGraph(Graph g) {}
|
||||
public void finishGraph(Graph g) {}
|
||||
public void finishVertex(Vertex v) {}
|
||||
public void finishEdge(Edge e) {}
|
||||
|
||||
public void discoverVertex(Vertex v) { out.println(v.toString(verbose)); }
|
||||
public void discoverEdge(Edge e) { out.println(e.toString(verbose)); }
|
||||
|
||||
public void visit(Graph g, Vertex start) {
|
||||
new DFS(this).start(g, start);
|
||||
}
|
||||
}
|
||||
@@ -1,175 +0,0 @@
|
||||
package org.apache.commons.graph.visitor;
|
||||
|
||||
/* ====================================================================
|
||||
* 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.Vector;
|
||||
|
||||
import org.apache.commons.graph.DFS;
|
||||
import org.apache.commons.graph.Edge;
|
||||
import org.apache.commons.graph.Graph;
|
||||
import org.apache.commons.graph.Vertex;
|
||||
import org.apache.commons.graph.VStack;
|
||||
|
||||
/**
|
||||
* Visit graph computing its "Strongly Connected Components" (SCC).
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public class SCCVisitor implements Visitor {
|
||||
private int time; // Global time
|
||||
private VStack stack; // Stack to remember members of a SCC
|
||||
private Vector sccs; // Forest of SCCs
|
||||
private Graph graph;
|
||||
private Graph[] graphs;
|
||||
private int size;
|
||||
|
||||
public SCCVisitor() {}
|
||||
|
||||
public void discoverGraph(Graph g) {
|
||||
graph = g;
|
||||
size = g.getNoVertices();
|
||||
Vertex[] vertices = g.getVertexArray();
|
||||
|
||||
for(int i=0; i < size; i++) {
|
||||
vertices[i].setDiscoveryTime(0);
|
||||
/* Should rather be setMyMinTime(), "abused" to remember local minimum
|
||||
*/
|
||||
vertices[i].setFinishingTime(size * 2); // Invalidate
|
||||
}
|
||||
|
||||
time = 0; // Reset timer
|
||||
stack = new VStack(size);
|
||||
sccs = new Vector();
|
||||
}
|
||||
|
||||
|
||||
public void discoverVertex(Vertex v) {
|
||||
int min = time++; // Clock tick
|
||||
v.setDiscoveryTime(min); // Remember time of discovery
|
||||
v.setFinishingTime(min); // Remember local minimum
|
||||
stack.push(v); // Push current node on stack
|
||||
}
|
||||
|
||||
public void discoverEdge(Edge e) {}
|
||||
|
||||
public void finishEdge(Edge e) {
|
||||
Vertex src = graph.getSource(e);
|
||||
Vertex target = graph.getTarget(e);
|
||||
int m = target.getFinishingTime();
|
||||
|
||||
/* After recursion, test if the (white) vertex just visited has now a smaller
|
||||
* minium than the local one, i.e. if target.min < src.min && !target.isBlack().
|
||||
*/
|
||||
if((m < src.getFinishingTime()) && (target.getDiscoveryTime() < size + 1))
|
||||
src.setFinishingTime(m);
|
||||
}
|
||||
|
||||
public void finishVertex(Vertex v) {
|
||||
/* If the local minimum hasn't changed, i.e. if no vertices with smaller minimums
|
||||
* have been reached, this must be the root vertex of a SCC.
|
||||
*/
|
||||
if(v.getFinishingTime() == v.getDiscoveryTime()) { // Root of SCC?
|
||||
Graph scc = new Graph(); // Gather nodes in subgraph
|
||||
Vertex p;
|
||||
|
||||
do { // Pop all nodes belonging to this SCC subgraph
|
||||
p = stack.pop();
|
||||
scc.addVertex(p); // May have no edges, so put it first in there
|
||||
p.setDiscoveryTime(size * 2); // Invalidate, i.e. mark vertex as black
|
||||
} while(p != v);
|
||||
|
||||
Vertex[] vertices = scc.getVertexArray();
|
||||
for(int j=0; j < vertices.length; j++) {
|
||||
p = vertices[j];
|
||||
|
||||
Edge[] edges = graph.getEdgeArray(p);
|
||||
for(int i=0; i < edges.length; i++) { // Build up subgraph
|
||||
Edge e = edges[i];
|
||||
Vertex src = graph.getSource(e);
|
||||
Vertex target = graph.getTarget(e);
|
||||
|
||||
// Add only edges that are within the subgraph
|
||||
if(scc.contains(src) && scc.contains(target))
|
||||
scc.addEdge(src, target, e);
|
||||
}
|
||||
}
|
||||
|
||||
sccs.addElement(scc); // Add subgraph vectorto forest of subgraphs
|
||||
}
|
||||
}
|
||||
|
||||
public void finishGraph(Graph g) {
|
||||
graphs = new Graph[sccs.size()];
|
||||
sccs.copyInto(graphs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return all strongly connected components found during traversal
|
||||
*/
|
||||
public final Graph[] getSCCs() { return graphs; }
|
||||
|
||||
/**
|
||||
* Visit graph with DFS and find all its strongly connected components
|
||||
* which may be obtained with getSCCs().
|
||||
*
|
||||
* @param g graph to traverse
|
||||
* @param start where to start traversal
|
||||
*/
|
||||
public void visit(Graph g, Vertex start) {
|
||||
new DFS(this).start(g, start);
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package org.apache.commons.graph.visitor;
|
||||
|
||||
/* ====================================================================
|
||||
* 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.Edge;
|
||||
import org.apache.commons.graph.Graph;
|
||||
import org.apache.commons.graph.Vertex;
|
||||
|
||||
/**
|
||||
* Interface for visitor patterns.
|
||||
*
|
||||
* @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 {
|
||||
public void discoverGraph(Graph g); // may be also used as "visitGraph"
|
||||
public void finishGraph(Graph g);
|
||||
|
||||
public void discoverVertex(Vertex v); // same as above
|
||||
public void finishVertex(Vertex v);
|
||||
|
||||
public void discoverEdge(Edge e); // same as above
|
||||
public void finishEdge(Edge e);
|
||||
|
||||
public void visit(Graph g, Vertex v); // Start visiting graph g beginning at start
|
||||
}
|
||||
@@ -1,171 +0,0 @@
|
||||
package org.apache.commons.graph.visualize;
|
||||
|
||||
/* ====================================================================
|
||||
* 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.io.OutputStream;
|
||||
|
||||
import org.apache.commons.graph.GraphConstants;
|
||||
import org.apache.commons.graph.Edge;
|
||||
import org.apache.commons.graph.Graph;
|
||||
import org.apache.commons.graph.Sequential;
|
||||
import org.apache.commons.graph.Vertex;
|
||||
|
||||
/**
|
||||
* Visualizes directed graphs into the
|
||||
* <A HREF="http://www.informatik.uni-bremen.de/~inform/forschung/daVinci/daVinci.html">
|
||||
* DaVinci</A> format. It can only be used for directed graphs and does not support
|
||||
* labeled edges very well, unfortunately. They have to be simulated via dummy vertices.
|
||||
*
|
||||
* Traverses graph with the Sequential visitor, since it uses invariants of that algorithm.
|
||||
*
|
||||
* @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 {
|
||||
private Graph graph;
|
||||
private int no_edges, edges;
|
||||
private int no_vertices, vertices;
|
||||
|
||||
/**
|
||||
* @param out stream to print output to
|
||||
* @param verbose toggle verbosity of output
|
||||
*/
|
||||
public DaVinciVisualizer(OutputStream out, int verbose) {
|
||||
super(out, verbose);
|
||||
}
|
||||
|
||||
public void discoverGraph(Graph g) {
|
||||
graph = g;
|
||||
|
||||
no_vertices = g.getNoVertices();
|
||||
vertices = 0;
|
||||
out.println("[");
|
||||
}
|
||||
|
||||
public void finishGraph(Graph g) {
|
||||
out.println("]");
|
||||
out.close();
|
||||
}
|
||||
|
||||
public void discoverVertex(Vertex v) {
|
||||
String label = convertString(v.toString(verbose));
|
||||
//System.out.println("vertex:" + v.getIdent() + ":" + v);
|
||||
|
||||
println(1, "l(\"" + v.getIdent() + "\",");
|
||||
println(1, "n(\"anything\", [");
|
||||
println(2, "a(\"OBJECT\", \"" + label + "\"),");
|
||||
println(2, "a(\"FONTSTYLE\", \"bold\"),");
|
||||
println(2, "a(\"FONTFAMILY\", \"courier\")],");
|
||||
println(1, "["); // array of successor nodes
|
||||
|
||||
no_edges = graph.getNoEdges(v);
|
||||
edges = 0;
|
||||
}
|
||||
|
||||
public void finishVertex(Vertex v) {
|
||||
String str = (vertices < no_vertices - 1)? "]))," : "]))";
|
||||
println(1, str);
|
||||
vertices++;
|
||||
}
|
||||
|
||||
private static int label_counter=0;
|
||||
|
||||
private final String getEdgeAttribute() {
|
||||
if(graph.isDirected())
|
||||
return "";
|
||||
else
|
||||
return "a(\"_DIR\", \"none\"";
|
||||
}
|
||||
|
||||
public void discoverEdge(Edge e) {
|
||||
int u = graph.getSource(e).getIdent();
|
||||
int v = graph.getTarget(e).getIdent();
|
||||
String label = convertString(e.toString(verbose));
|
||||
|
||||
if(label.equals("")) {
|
||||
print(2, "l(\"" + u + "->" + v + "\", e(\"\", [" + getEdgeAttribute() +
|
||||
"], r(\"" + v + "\")))");
|
||||
} else { // Simulate labeled edge with intermediate node
|
||||
String dummy = "label" + label_counter++; // Dummy name for intermediate node
|
||||
|
||||
print(2, "l(\"" + u + "->" + dummy + "\", e(\"\", [a(\"_DIR\", \"none\")], " +
|
||||
"l(\"" + dummy + "\", n(\"anything\",[a(\"OBJECT\", \"" + label +
|
||||
"\"), a(\"_GO\", \"text\")], [l(\"" + dummy + "->" + v +
|
||||
"\", e(\"\",[" + getEdgeAttribute() + "], r(\"" + v + "\")))]))))");
|
||||
//System.out.println(graph.getTarget(e).getIdent() + ":" + graph.getTarget(e));
|
||||
}
|
||||
}
|
||||
|
||||
public void finishEdge(Edge e) {
|
||||
if(edges < no_edges - 1)
|
||||
out.println(", ");
|
||||
else
|
||||
out.print('\n');
|
||||
|
||||
edges++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Visualize graph g with this Visualizer.
|
||||
*
|
||||
* @param g graph to visualize
|
||||
* @param start node to start at
|
||||
*/
|
||||
public void visualize(Graph g, Vertex start) {
|
||||
new Sequential(this).start(g);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,163 +0,0 @@
|
||||
package org.apache.commons.graph.visualize;
|
||||
|
||||
/* ====================================================================
|
||||
* 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.io.OutputStream;
|
||||
|
||||
import org.apache.commons.graph.GraphConstants;
|
||||
import org.apache.commons.graph.DFS;
|
||||
import org.apache.commons.graph.Edge;
|
||||
import org.apache.commons.graph.Graph;
|
||||
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.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 {
|
||||
private Graph graph;
|
||||
|
||||
/**
|
||||
* @param out stream to print output to
|
||||
* @param verbose toggle verbosity of output
|
||||
*/
|
||||
public GMLVisualizer(OutputStream out, int verbose) {
|
||||
super(out, verbose);
|
||||
}
|
||||
|
||||
public void discoverGraph(Graph g) {
|
||||
String d = g.isDirected()? "1" : "0";
|
||||
|
||||
graph = g;
|
||||
|
||||
println(0, "graph [");
|
||||
println(1, "Creator\t \"GMLVisualizer\"");
|
||||
println(1, "id\t " + Vertex.getCounter()); // Make sure this is a unique number
|
||||
println(1, "directed\t " + d + "\n");
|
||||
}
|
||||
|
||||
public void finishGraph(Graph g) {
|
||||
println(0, "]\n");
|
||||
out.close();
|
||||
}
|
||||
|
||||
public void discoverVertex(Vertex v) {
|
||||
println(1, "node [");
|
||||
println(2, "id\t " + v.getIdent());
|
||||
println(2, "label");
|
||||
|
||||
out.println("\"" + v.toString(verbose) + "\"");
|
||||
|
||||
println(2, "graphics [");
|
||||
println(3, "type\t\t \"rectangle\"");
|
||||
println(3, "fill\t\t \"white\"");
|
||||
println(3, "outline\t\t \"black\"");
|
||||
println(3, "background\t \"blue\"");
|
||||
println(3, "foreground\t \"green\"");
|
||||
println(2, "]\n");
|
||||
|
||||
println(2, "LabelGraphics [");
|
||||
println(3, "type\t \"text\"");
|
||||
println(3, "fill\t \"black\"");
|
||||
println(3, "justify\t \"left\"");
|
||||
println(2, "]\n");
|
||||
|
||||
println(1, "]\n");
|
||||
}
|
||||
|
||||
public void finishVertex(Vertex v) {}
|
||||
|
||||
public void discoverEdge(Edge e) {
|
||||
int src = graph.getSource(e).getIdent();
|
||||
int target = graph.getTarget(e).getIdent();
|
||||
|
||||
println(1, "edge [");
|
||||
println(2, "source\t " + src);
|
||||
println(2, "target\t " + target);
|
||||
|
||||
println(2, "label");
|
||||
out.println("\"" + e.toString(verbose) + "\"");
|
||||
|
||||
println(2, "graphics [");
|
||||
|
||||
println(3, "fill\t \"black\"");
|
||||
//println(3, "fill\t\t \"white\"");
|
||||
|
||||
println(3, "arrow\t \"last\"");
|
||||
println(2, "]\n");
|
||||
|
||||
println(2, "LabelGraphics [");
|
||||
println(3, "type\t \"text\"");
|
||||
println(3, "fill\t \"black\"");
|
||||
println(2, "]\n");
|
||||
|
||||
println(1, "]\n");
|
||||
}
|
||||
|
||||
public void finishEdge(Edge e) {}
|
||||
|
||||
/**
|
||||
* Visualize graph g with this Visualizer.
|
||||
*
|
||||
* @param g graph to visualize
|
||||
* @param start node to start at
|
||||
*/
|
||||
public void visualize(Graph g, Vertex start) {
|
||||
new DFS(this).start(g, start);
|
||||
}
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
package org.apache.commons.graph.visualize;
|
||||
|
||||
/* ====================================================================
|
||||
* 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.io.OutputStream;
|
||||
|
||||
import org.apache.commons.graph.GraphConstants;
|
||||
import org.apache.commons.graph.DFS;
|
||||
import org.apache.commons.graph.Edge;
|
||||
import org.apache.commons.graph.Graph;
|
||||
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.3 2002/01/16 20:38:30 jvanzyl Exp $
|
||||
* @author <a href="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
|
||||
*/
|
||||
public class GraphMLVisualizer
|
||||
extends GraphVisualizer
|
||||
{
|
||||
private Graph graph;
|
||||
|
||||
/**
|
||||
* @param out stream to print output to
|
||||
* @param verbose toggle verbosity of output
|
||||
*/
|
||||
public GraphMLVisualizer(OutputStream out, int verbose) {
|
||||
super(out, verbose);
|
||||
}
|
||||
|
||||
public void discoverGraph(Graph g) {
|
||||
String d = g.isDirected()? "true" : "false";
|
||||
|
||||
graph = g;
|
||||
|
||||
println(0, "<GraphXML>");
|
||||
println(0, "<graph isDirected=\"" + d + "\" id=\"" + Vertex.getCounter() + "\">");
|
||||
}
|
||||
|
||||
public void finishGraph(Graph g)
|
||||
{
|
||||
println(0, "</graph>");
|
||||
println(0, "</GraphXML>");
|
||||
out.close();
|
||||
}
|
||||
|
||||
public void discoverVertex(Vertex v)
|
||||
{
|
||||
println(0, "<node name=\"" + v + "\"/>");
|
||||
}
|
||||
|
||||
public void finishVertex(Vertex v) {}
|
||||
|
||||
public void discoverEdge(Edge e)
|
||||
{
|
||||
String src = graph.getSource(e).toString();
|
||||
String target = graph.getTarget(e).toString();
|
||||
|
||||
println(0, "<edge source=\"" + src + "\" target=\"" + target + "\"/>");
|
||||
}
|
||||
|
||||
public void finishEdge(Edge e) {}
|
||||
|
||||
/**
|
||||
* Visualize graph g with this Visualizer.
|
||||
*
|
||||
* @param g graph to visualize
|
||||
* @param start node to start at
|
||||
*/
|
||||
public void visualize(Graph g, Vertex start) {
|
||||
new DFS(this).start(g, start);
|
||||
}
|
||||
}
|
||||
@@ -1,171 +0,0 @@
|
||||
package org.apache.commons.graph.visualize;
|
||||
|
||||
/* ====================================================================
|
||||
* 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.io.*;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import org.apache.commons.graph.GraphConstants;
|
||||
import org.apache.commons.graph.Edge;
|
||||
import org.apache.commons.graph.Graph;
|
||||
import org.apache.commons.graph.Vertex;
|
||||
import org.apache.commons.graph.visitor.Visitor;
|
||||
|
||||
/**
|
||||
* Abstract super class for drivers that visualize graphs
|
||||
* in different graph formats such as
|
||||
* <A HREF="http://www.fmi.uni-passau.de/Graphlet/">GML</A> or
|
||||
* <A HREF="http://www.informatik.uni-bremen.de/~inform/forschung/daVinci/daVinci.html">
|
||||
* daVinci</A> or <A HREF="http://www.cs.uni-sb.de/RW/users/sander/html/gsvcg1.html">
|
||||
* VCG</A>.
|
||||
*
|
||||
* By default graphs are dumped in the native Java format, i.e. by using the
|
||||
* Serialization feature.
|
||||
*
|
||||
* @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
|
||||
* @see VCGVisualizer
|
||||
*/
|
||||
public class GraphVisualizer implements Visitor {
|
||||
protected OutputStream orig_out;
|
||||
protected PrintWriter out;
|
||||
protected int verbose;
|
||||
|
||||
/**
|
||||
* @param out stream to print output to
|
||||
* @param verbose toggle verbosity
|
||||
*/
|
||||
public GraphVisualizer(OutputStream out, int verbose) {
|
||||
orig_out = out;
|
||||
this.out = new PrintWriter(out, true);
|
||||
this.verbose = verbose;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print string using ident tab as prefix, used for nested calls
|
||||
*
|
||||
* @param indent how many tabs
|
||||
* @param s string to print
|
||||
*/
|
||||
protected final void print(int indent, String s) {
|
||||
for(int i=0; i < indent; i++)
|
||||
out.print('\t');
|
||||
|
||||
out.print(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print string using ident tab as prefix, used for nested calls
|
||||
*
|
||||
* @param indent how many tabs
|
||||
* @param s string to print
|
||||
*/
|
||||
protected final void println(int indent, String s) {
|
||||
print(indent, s);
|
||||
out.print('\n');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Escape all occurences of newline chars '\n' and quotes \"
|
||||
*/
|
||||
protected static final String convertString(String label) {
|
||||
char[] ch = label.toCharArray();
|
||||
StringBuffer buf = new StringBuffer();
|
||||
|
||||
for(int i=0; i < ch.length; i++) {
|
||||
switch(ch[i]) {
|
||||
case '\n':
|
||||
buf.append("\\n"); break;
|
||||
case '\"':
|
||||
buf.append("\\\""); break;
|
||||
case '\\':
|
||||
buf.append("\\\\"); break;
|
||||
default:
|
||||
buf.append(ch[i]); break;
|
||||
}
|
||||
}
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Visualize graph g with this Visualizer.
|
||||
*
|
||||
* @param g graph to visualize
|
||||
* @param start node to start at
|
||||
*/
|
||||
public void visualize(Graph g, Vertex start) {
|
||||
try {
|
||||
g.dump(orig_out);
|
||||
} catch(IOException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
out.close();
|
||||
}
|
||||
|
||||
public void discoverGraph(Graph g) {}
|
||||
public void finishGraph(Graph g) {}
|
||||
public void discoverVertex(Vertex v) {}
|
||||
public void finishVertex(Vertex v) {}
|
||||
public void discoverEdge(Edge e) {}
|
||||
public void finishEdge(Edge e) {}
|
||||
|
||||
public void visit(Graph g, Vertex start) {
|
||||
visualize(g, start);
|
||||
}
|
||||
}
|
||||
@@ -1,144 +0,0 @@
|
||||
package org.apache.commons.graph.visualize;
|
||||
|
||||
/* ====================================================================
|
||||
* 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.io.OutputStream;
|
||||
|
||||
import org.apache.commons.graph.GraphConstants;
|
||||
import org.apache.commons.graph.DFS;
|
||||
import org.apache.commons.graph.Edge;
|
||||
import org.apache.commons.graph.Graph;
|
||||
import org.apache.commons.graph.Vertex;
|
||||
|
||||
/**
|
||||
* Visualizes graphs into the <A
|
||||
* HREF="http://www.cs.uni-sb.de/RW/users/sander/html/gsvcg1.html">VCG</A>
|
||||
* format.
|
||||
*
|
||||
* @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 {
|
||||
private Graph graph;
|
||||
|
||||
/**
|
||||
* @param out stream to print output to
|
||||
* @param verbose toggle verbosity of output
|
||||
*/
|
||||
public VCGVisualizer(OutputStream out, int verbose) {
|
||||
super(out, verbose);
|
||||
}
|
||||
|
||||
public void discoverGraph(Graph g) {
|
||||
String d = g.isDirected()? "1" : "0";
|
||||
|
||||
graph = g;
|
||||
|
||||
println(0, "/* Created by VCGVisualizer */\n");
|
||||
println(0, "graph: {");
|
||||
println(1, "display_edge_labels: yes");
|
||||
println(1, "port_sharing: no");
|
||||
println(1, "manhattan_edges: yes");
|
||||
println(1, "straight_phase: yes");
|
||||
println(1, "priority_phase: yes");
|
||||
println(1, "node.color: white");
|
||||
println(1, "node.bordercolor: black");
|
||||
println(1, "node.textcolor: black");
|
||||
println(1, "node.shape: box");
|
||||
println(1, "node.textmode: left_justify");
|
||||
println(1, "edge.textcolor: black");
|
||||
println(1, "edge.color: black\n");
|
||||
}
|
||||
|
||||
public void finishGraph(Graph g) {
|
||||
println(0, "}");
|
||||
out.close();
|
||||
}
|
||||
|
||||
public void discoverVertex(Vertex v) {
|
||||
String label = convertString(v.toString(verbose));
|
||||
|
||||
println(1, "node: {");
|
||||
println(2, "title: \"" + v.getIdent() + "\"");
|
||||
println(2, "label: \"" + label + "\"");
|
||||
println(1, "}");
|
||||
}
|
||||
|
||||
public void finishVertex(Vertex v) {}
|
||||
|
||||
public void discoverEdge(Edge e) {
|
||||
int src = graph.getSource(e).getIdent();
|
||||
int target = graph.getTarget(e).getIdent();
|
||||
String label = convertString(e.toString(verbose));
|
||||
|
||||
println(1, "edge: {");
|
||||
println(2, "sourcename: \"" + src + "\"");
|
||||
println(2, "targetname: \"" + target + "\"");
|
||||
if(!label.equals(""))
|
||||
println(2, "label: \"" + label + "\"");
|
||||
println(1, "}");
|
||||
}
|
||||
|
||||
public void finishEdge(Edge e) {}
|
||||
|
||||
/**
|
||||
* Visualize graph g with this Visualizer.
|
||||
*
|
||||
* @param g graph to visualize
|
||||
* @param start node to start at
|
||||
*/
|
||||
public void visualize(Graph g, Vertex start) {
|
||||
new DFS(this).start(g, start);
|
||||
}
|
||||
}
|
||||
@@ -1,215 +0,0 @@
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,346 +0,0 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,166 +0,0 @@
|
||||
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() );
|
||||
}
|
||||
}
|
||||
@@ -1,197 +0,0 @@
|
||||
package org.apache.commons.graph.util;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
|
||||
* @version $Id: TestDependencyResolver.java,v 1.3 2001/12/02 19:25:44 jvanzyl Exp $
|
||||
*/
|
||||
public class TestDependencyResolver
|
||||
extends TestCase
|
||||
{
|
||||
private DependencyResolver d;
|
||||
|
||||
public TestDependencyResolver(String testName)
|
||||
{
|
||||
super(testName);
|
||||
}
|
||||
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite(TestDependencyResolver.class);
|
||||
}
|
||||
|
||||
public void setUp()
|
||||
{
|
||||
d = new DependencyResolver();
|
||||
}
|
||||
|
||||
public void testCookieList()
|
||||
{
|
||||
String compare = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
try
|
||||
{
|
||||
addAlphabetProjects(d);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
public void testReset()
|
||||
{
|
||||
String compare = "";
|
||||
|
||||
try
|
||||
{
|
||||
addAlphabetProjects(d);
|
||||
d.reset();
|
||||
assertEquals(compare,getList(d.getSortedDependencies("")));
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
fail();
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
public void testSingleProjectDependency()
|
||||
{
|
||||
// tests the engine with
|
||||
//
|
||||
// A B C
|
||||
// ^ ^ ^\
|
||||
// \/ \/ |
|
||||
// D <-E |
|
||||
// ^ /
|
||||
// \ /
|
||||
// F <-
|
||||
|
||||
String compare = "ABDFCE";
|
||||
|
||||
try
|
||||
{
|
||||
d.reset();
|
||||
d.addGraphable("A", deps(null));
|
||||
d.addGraphable("B", deps(null));
|
||||
d.addGraphable("C", deps("F"));
|
||||
d.addGraphable("D", deps("A,B"));
|
||||
d.addGraphable("E", deps("B,C,D"));
|
||||
d.addGraphable("F", deps("D"));
|
||||
|
||||
// Get the dependencies where we exclude the
|
||||
// specified target.
|
||||
assertEquals(compare,getList(d.getSortedDependencies("F")));
|
||||
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
public void testMultiProjectDependency()
|
||||
{
|
||||
String compare = "12BCDA3";
|
||||
|
||||
try
|
||||
{
|
||||
d.reset();
|
||||
d.addGraphable("A", deps("B,C,D"));
|
||||
d.addGraphable("B", deps("1,2"));
|
||||
d.addGraphable("C", deps(null));
|
||||
d.addGraphable("D", deps(null));
|
||||
d.addGraphable("1", deps(null));
|
||||
d.addGraphable("2", deps(null));
|
||||
d.addGraphable("3", deps("C"));
|
||||
|
||||
ArrayList projects = new ArrayList();
|
||||
projects.add("A");
|
||||
projects.add("3");
|
||||
|
||||
// We do not want to exclude the target. This is simulating
|
||||
// maven where all dependencies are built including the
|
||||
// target package.
|
||||
assertEquals(compare,getList(d.getSortedDependencies("")));
|
||||
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
fail();
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
private void addAlphabetProjects(DependencyResolver d)
|
||||
throws Exception
|
||||
{
|
||||
d.addGraphable( "Z", deps("Y"));
|
||||
d.addGraphable( "Y", deps("X"));
|
||||
d.addGraphable( "X", deps("W"));
|
||||
d.addGraphable( "W", deps("V"));
|
||||
d.addGraphable( "V", deps("U"));
|
||||
d.addGraphable( "U", deps("T"));
|
||||
d.addGraphable( "T", deps("S"));
|
||||
d.addGraphable( "S", deps("R"));
|
||||
d.addGraphable( "R", deps("Q"));
|
||||
d.addGraphable( "Q", deps("P"));
|
||||
d.addGraphable( "P", deps("O"));
|
||||
d.addGraphable( "O", deps("N"));
|
||||
d.addGraphable( "N", deps("M"));
|
||||
d.addGraphable( "M", deps("L"));
|
||||
d.addGraphable( "L", deps("K"));
|
||||
d.addGraphable( "K", deps("J"));
|
||||
d.addGraphable( "J", deps("I"));
|
||||
d.addGraphable( "I", deps("H"));
|
||||
d.addGraphable( "H", deps("G"));
|
||||
d.addGraphable( "G", deps("F"));
|
||||
d.addGraphable( "F", deps("E"));
|
||||
d.addGraphable( "E", deps("D"));
|
||||
d.addGraphable( "D", deps("C"));
|
||||
d.addGraphable( "C", deps("B"));
|
||||
d.addGraphable( "B", deps("A"));
|
||||
d.addGraphable( "A", deps(null));
|
||||
}
|
||||
|
||||
private String getList(List list)
|
||||
throws Exception
|
||||
{
|
||||
StringBuffer sb = new StringBuffer();
|
||||
Iterator i = list.iterator();
|
||||
while(i.hasNext())
|
||||
{
|
||||
String item = (String) i.next();
|
||||
sb.append(item);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private List deps(String project)
|
||||
{
|
||||
ArrayList deps = new ArrayList();
|
||||
|
||||
if (project != null)
|
||||
{
|
||||
StringTokenizer st = new StringTokenizer(project, ",");
|
||||
|
||||
while(st.hasMoreTokens())
|
||||
{
|
||||
deps.add(st.nextToken());
|
||||
}
|
||||
}
|
||||
|
||||
return deps;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user