libjgrapht0.6-java-0.6.0/ 0000755 0001751 0001751 00000000000 11251315107 014664 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/etc/ 0000755 0001751 0001751 00000000000 11251311101 015425 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/etc/checkstyle-settings.xml 0000644 0001751 0001751 00000031107 10266566752 022202 0 ustar moeller moeller
* Used by greedy algorithms that need to sort vertices by their degree. Two * vertices are considered equal if their degrees are equal. *
* * @author Linda Buisman * * @since Nov 6, 2003 */ public class VertexDegreeComparator implements java.util.Comparator { /** The graph that contains the vertices to be compared. */ private UndirectedGraph m_graph; /** * The sort order for vertex degree.true
for ascending degree
* order (smaller degrees first), false
for descending.
*/
private boolean m_ascendingOrder;
/**
* Creates a comparator for comparing the degrees of vertices in the
* specified graph. The comparator compares in ascending order of degrees
* (lowest first).
*
* @param g graph with respect to which the degree is calculated.
*/
public VertexDegreeComparator( UndirectedGraph g ) {
this( g, true );
}
/**
* Creates a comparator for comparing the degrees of vertices in the
* specified graph.
*
* @param g graph with respect to which the degree is calculated.
* @param ascendingOrder true - compares in ascending order of degrees
* (lowest first), false - compares in descending order of degrees
* (highest first).
*/
public VertexDegreeComparator( UndirectedGraph g, boolean ascendingOrder ) {
m_graph = g;
m_ascendingOrder = ascendingOrder;
}
/**
* Compare the degrees of v1
and v2
, taking into
* account whether ascending or descending order is used.
*
* @param v1 the first vertex to be compared.
* @param v2 the second vertex to be compared.
*
* @return -1 if v1
comes before v2
, +1 if
* v1
comes after v2
, 0 if equal.
*/
public int compare( Object v1, Object v2 ) {
int degree1 = m_graph.degreeOf( v1 );
int degree2 = m_graph.degreeOf( v2 );
if( ( degree1 < degree2 && m_ascendingOrder )
|| ( degree1 > degree2 && !m_ascendingOrder ) ) {
return -1;
}
else if( ( degree1 > degree2 && m_ascendingOrder )
|| ( degree1 < degree2 && !m_ascendingOrder ) ) {
return 1;
}
else {
return 0;
}
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/alg/util/package.html 0000644 0001751 0001751 00000000174 10266566752 024571 0 ustar moeller moeller
Utilities used by JGraphT algorithms.
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/alg/ConnectivityInspector.java 0000644 0001751 0001751 00000023512 10266566752 026544 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* --------------------------
* ConnectivityInspector.java
* --------------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): John V. Sichi
*
* $Id: ConnectivityInspector.java,v 1.11 2005/04/23 08:09:28 perfecthash Exp $
*
* Changes
* -------
* 06-Aug-2003 : Initial revision (BN);
* 10-Aug-2003 : Adaptation to new event model (BN);
*
*/
package org._3pq.jgrapht.alg;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org._3pq.jgrapht.DirectedGraph;
import org._3pq.jgrapht.Graph;
import org._3pq.jgrapht.UndirectedGraph;
import org._3pq.jgrapht.event.ConnectedComponentTraversalEvent;
import org._3pq.jgrapht.event.GraphEdgeChangeEvent;
import org._3pq.jgrapht.event.GraphListener;
import org._3pq.jgrapht.event.GraphVertexChangeEvent;
import org._3pq.jgrapht.event.TraversalListenerAdapter;
import org._3pq.jgrapht.event.VertexTraversalEvent;
import org._3pq.jgrapht.graph.AsUndirectedGraph;
import org._3pq.jgrapht.traverse.BreadthFirstIterator;
/**
* Allows obtaining various connectivity aspects of a graph. The inspected
* graph is specified at construction time and cannot be modified.
* Currently, the inspector supports connected components for an undirected
* graph and weakly connected components for a directed graph. To find
* strongly connected components, use {@link StrongConnectivityInspector}
* instead.
*
* * The inspector methods work in a lazy fashion: no computation is performed * unless immediately necessary. Computation are done once and results and * cached within this class for future need. *
* ** The inspector is also a {@link org._3pq.jgrapht.event.GraphListener}. If * added as a listener to the inspected graph, the inspector will amend * internal cached results instead of recomputing them. It is efficient when a * few modifications are applied to a large graph. If many modifications are * expected it will not be efficient due to added overhead on graph update * operations. If inspector is added as listener to a graph other than the one * it inspects, results are undefined. *
* * @author Barak Naveh * @author John V. Sichi * * @since Aug 6, 2003 */ public class ConnectivityInspector implements GraphListener { List m_connectedSets; Map m_vertexToConnectedSet; private Graph m_graph; /** * Creates a connectivity inspector for the specified undirected graph. * * @param g the graph for which a connectivity inspector to be created. */ public ConnectivityInspector( UndirectedGraph g ) { init( ); m_graph = g; } /** * Creates a connectivity inspector for the specified directed graph. * * @param g the graph for which a connectivity inspector to be created. */ public ConnectivityInspector( DirectedGraph g ) { init( ); m_graph = new AsUndirectedGraph( g ); } /** * Test if the inspected graph is connected. An empty graph is not * considered connected. * * @returntrue
if and only if inspected graph is connected.
*/
public boolean isGraphConnected( ) {
return lazyFindConnectedSets( ).size( ) == 1;
}
/**
* Returns a set of all vertices that are in the maximally connected
* component together with the specified vertex. For more on maximally
* connected component, see
* http://www.nist.gov/dads/HTML/maximallyConnectedComponent.html.
*
* @param vertex the vertex for which the connected set to be returned.
*
* @return a set of all vertices that are in the maximally connected
* component together with the specified vertex.
*/
public Set connectedSetOf( Object vertex ) {
Set connectedSet = (Set) m_vertexToConnectedSet.get( vertex );
if( connectedSet == null ) {
connectedSet = new HashSet( );
BreadthFirstIterator i =
new BreadthFirstIterator( m_graph, vertex );
while( i.hasNext( ) ) {
connectedSet.add( i.next( ) );
}
m_vertexToConnectedSet.put( vertex, connectedSet );
}
return connectedSet;
}
/**
* Returns a list of Set
s, where each set contains all
* vertices that are in the same maximally connected component. All graph
* vertices occur in exactly one set. For more on maximally connected
* component, see
* http://www.nist.gov/dads/HTML/maximallyConnectedComponent.html.
*
* @return Returns a list of Set
s, where each set contains all
* vertices that are in the same maximally connected component.
*/
public List connectedSets( ) {
return lazyFindConnectedSets( );
}
/**
* @see GraphListener#edgeAdded(GraphEdgeChangeEvent)
*/
public void edgeAdded( GraphEdgeChangeEvent e ) {
init( ); // for now invalidate cached results, in the future need to amend them.
}
/**
* @see GraphListener#edgeRemoved(GraphEdgeChangeEvent)
*/
public void edgeRemoved( GraphEdgeChangeEvent e ) {
init( ); // for now invalidate cached results, in the future need to amend them.
}
/**
* Tests if there is a path from the specified source vertex to the
* specified target vertices. For a directed graph, direction is ignored
* for this interpretation of path.
*
* * Note: Future versions of this method might not ignore edge directions * for directed graphs. *
* * @param sourceVertex one end of the path. * @param targetVertex another end of the path. * * @returntrue
if and only if there is a path from the source
* vertex to the target vertex.
*/
public boolean pathExists( Object sourceVertex, Object targetVertex ) {
/*
* TODO: Ignoring edge direction for directed graph may be
* confusing. For directed graphs, consider Dijkstra's algorithm.
*/
Set sourceSet = connectedSetOf( sourceVertex );
return sourceSet.contains( targetVertex );
}
/**
* @see org._3pq.jgrapht.event.VertexSetListener#vertexAdded(GraphVertexChangeEvent)
*/
public void vertexAdded( GraphVertexChangeEvent e ) {
init( ); // for now invalidate cached results, in the future need to amend them.
}
/**
* @see org._3pq.jgrapht.event.VertexSetListener#vertexRemoved(GraphVertexChangeEvent)
*/
public void vertexRemoved( GraphVertexChangeEvent e ) {
init( ); // for now invalidate cached results, in the future need to amend them.
}
private void init( ) {
m_connectedSets = null;
m_vertexToConnectedSet = new HashMap( );
}
private List lazyFindConnectedSets( ) {
if( m_connectedSets == null ) {
m_connectedSets = new ArrayList( );
Set vertexSet = m_graph.vertexSet( );
if( vertexSet.size( ) > 0 ) {
BreadthFirstIterator i =
new BreadthFirstIterator( m_graph, null );
i.addTraversalListener( new MyTraversalListener( ) );
while( i.hasNext( ) ) {
i.next( );
}
}
}
return m_connectedSets;
}
/**
* A traversal listener that groups all vertices according to to their
* containing connected set.
*
* @author Barak Naveh
*
* @since Aug 6, 2003
*/
private class MyTraversalListener extends TraversalListenerAdapter {
private Set m_currentConnectedSet;
/**
* @see TraversalListenerAdapter#connectedComponentFinished(ConnectedComponentTraversalEvent)
*/
public void connectedComponentFinished(
ConnectedComponentTraversalEvent e ) {
m_connectedSets.add( m_currentConnectedSet );
}
/**
* @see TraversalListenerAdapter#connectedComponentStarted(ConnectedComponentTraversalEvent)
*/
public void connectedComponentStarted(
ConnectedComponentTraversalEvent e ) {
m_currentConnectedSet = new HashSet( );
}
/**
* @see TraversalListenerAdapter#vertexTraversed(Object)
*/
public void vertexTraversed( VertexTraversalEvent e ) {
Object v = e.getVertex( );
m_currentConnectedSet.add( v );
m_vertexToConnectedSet.put( v, m_currentConnectedSet );
}
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/alg/CycleDetector.java 0000644 0001751 0001751 00000013222 10266566752 024725 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ------------------
* CycleDetector.java
* ------------------
* (C) Copyright 2004, by John V. Sichi and Contributors.
*
* Original Author: John V. Sichi
* Contributor(s): -
*
* $Id: CycleDetector.java,v 1.4 2005/04/23 08:09:28 perfecthash Exp $
*
* Changes
* -------
* 16-Sept-2004 : Initial revision (JVS);
*
*/
package org._3pq.jgrapht.alg;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org._3pq.jgrapht.DirectedGraph;
import org._3pq.jgrapht.Edge;
import org._3pq.jgrapht.Graph;
import org._3pq.jgrapht.traverse.DepthFirstIterator;
/**
* Performs cycle detection on a graph. The inspected graph is specified
* at construction time and cannot be modified. Currently, the detector
* supports only directed graphs.
*
* @author John V. Sichi
*
* @since Sept 16, 2004
*/
public class CycleDetector {
/** Graph on which cycle detection is being performed. */
Graph m_graph;
/**
* Creates a cycle detector for the specified graph. Currently only
* directed graphs are supported.
*
* @param graph the DirectedGraph in which to detect cycles
*/
public CycleDetector( DirectedGraph graph ) {
m_graph = graph;
}
/**
* Performs yes/no cycle detection on the entire graph.
*
* @return true iff the graph contains at least one cycle
*/
public boolean detectCycles( ) {
try {
execute( null, null );
}
catch( CycleDetectedException ex ) {
return true;
}
return false;
}
/**
* Performs yes/no cycle detection on an individual vertex.
*
* @param v the vertex to test
*
* @return true if v is on at least one cycle
*/
public boolean detectCyclesContainingVertex( Object v ) {
try {
execute( null, v );
}
catch( CycleDetectedException ex ) {
return true;
}
return false;
}
/**
* Finds the vertex set for the subgraph of all cycles.
*
* @return set of all vertices which participate in at least one cycle in
* this graph
*/
public Set findCycles( ) {
Set set = new HashSet( );
execute( set, null );
return set;
}
/**
* Finds the vertex set for the subgraph of all cycles which contain a
* particular vertex.
*
* @param v the vertex to test
*
* @return set of all vertices reachable from v via at least one cycle
*/
public Set findCyclesContainingVertex( Object v ) {
Set set = new HashSet( );
execute( set, v );
return set;
}
private void execute( Set s, Object v ) {
ProbeIterator iter = new ProbeIterator( s, v );
while( iter.hasNext( ) ) {
iter.next( );
}
}
/**
* Exception thrown internally when a cycle is detected during a yes/no
* cycle test. Must be caught by top-level detection method.
*/
private static class CycleDetectedException extends RuntimeException {
private static final long serialVersionUID = 3834305137802950712L;
}
/**
* Version of DFS which maintains a backtracking path used to probe for
* cycles.
*/
private class ProbeIterator extends DepthFirstIterator {
private List m_path;
private Set m_cycleSet;
ProbeIterator( Set cycleSet, Object startVertex ) {
super( m_graph, startVertex );
m_cycleSet = cycleSet;
m_path = new ArrayList( );
}
/**
* {@inheritDoc}
*/
protected void encounterVertexAgain( Object vertex, Edge edge ) {
super.encounterVertexAgain( vertex, edge );
int i = m_path.indexOf( vertex );
if( i > -1 ) {
if( m_cycleSet == null ) {
// we're doing yes/no cycle detection
throw new CycleDetectedException( );
}
for( ; i < m_path.size( ); ++i ) {
m_cycleSet.add( m_path.get( i ) );
}
}
}
/**
* {@inheritDoc}
*/
protected Object provideNextVertex( ) {
Object v = super.provideNextVertex( );
// backtrack
for( int i = m_path.size( ) - 1; i >= 0; --i ) {
if( m_graph.containsEdge( m_path.get( i ), v ) ) {
break;
}
m_path.remove( i );
}
m_path.add( v );
return v;
}
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/alg/DijkstraShortestPath.java 0000644 0001751 0001751 00000011322 10266566752 026317 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -------------------------
* DijkstraShortestPath.java
* -------------------------
* (C) Copyright 2003, by John V. Sichi and Contributors.
*
* Original Author: John V. Sichi
*
* $Id: DijkstraShortestPath.java,v 1.3 2005/05/30 05:37:28 perfecthash Exp $
*
* Changes
* -------
* 02-Sep-2003 : Initial revision (JVS);
* 29-May-2005 : Make non-static and add radius support (JVS);
*
*/
package org._3pq.jgrapht.alg;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org._3pq.jgrapht.Edge;
import org._3pq.jgrapht.Graph;
import org._3pq.jgrapht.traverse.ClosestFirstIterator;
/**
* An implementation of Dijkstra's
* shortest path algorithm using ClosestFirstIterator
.
*
* @author John V. Sichi
*
* @since Sep 2, 2003
*/
public final class DijkstraShortestPath {
private List m_edgeList;
private double m_pathLength;
/**
* Creates and executes a new DijkstraShortestPath algorithm instance. An
* instance is only good for a single search; after construction, it can
* be accessed to retrieve information about the path found.
*
* @param graph the graph to be searched
* @param startVertex the vertex at which the path should start
* @param endVertex the vertex at which the path should end
* @param radius limit on path length, or Double.POSITIVE_INFINITY for
* unbounded search
*/
public DijkstraShortestPath( Graph graph, Object startVertex,
Object endVertex, double radius ) {
ClosestFirstIterator iter =
new ClosestFirstIterator( graph, startVertex, radius );
while( iter.hasNext( ) ) {
Object vertex = iter.next( );
if( vertex.equals( endVertex ) ) {
createEdgeList( iter, endVertex );
m_pathLength = iter.getShortestPathLength( endVertex );
return;
}
}
m_edgeList = null;
m_pathLength = Double.POSITIVE_INFINITY;
}
/**
* Return the edges making up the path found.
*
* @return List of Edges, or null if no path exists
*/
public List getPathEdgeList( ) {
return m_edgeList;
}
/**
* Return the length of the path found.
*
* @return path length, or Double.POSITIVE_INFINITY if no path exists
*/
public double getPathLength( ) {
return m_pathLength;
}
/**
* Convenience method to find the shortest path via a single static method
* call. If you need a more advanced search (e.g. limited by radius, or
* computation of the path length), use the constructor instead.
*
* @param graph the graph to be searched
* @param startVertex the vertex at which the path should start
* @param endVertex the vertex at which the path should end
*
* @return List of Edges, or null if no path exists
*/
public static List findPathBetween( Graph graph, Object startVertex,
Object endVertex ) {
DijkstraShortestPath alg =
new DijkstraShortestPath( graph, startVertex, endVertex,
Double.POSITIVE_INFINITY );
return alg.getPathEdgeList( );
}
private void createEdgeList( ClosestFirstIterator iter, Object endVertex ) {
m_edgeList = new ArrayList( );
while( true ) {
Edge edge = iter.getSpanningTreeEdge( endVertex );
if( edge == null ) {
break;
}
m_edgeList.add( edge );
endVertex = edge.oppositeVertex( endVertex );
}
Collections.reverse( m_edgeList );
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/alg/StrongConnectivityInspector.java 0000644 0001751 0001751 00000024761 10266566752 027750 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* --------------------------
* StrongConnectivityInspector.java
* --------------------------
* (C) Copyright 2005, by Christian Soltenborn and Contributors.
*
* Original Author: Christian Soltenborn
*
* $Id: StrongConnectivityInspector.java,v 1.6 2005/07/17 05:33:25 perfecthash Exp $
*
* Changes
* -------
* 2-Feb-2005 : Initial revision (CS);
*
*/
package org._3pq.jgrapht.alg;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.Vector;
import org._3pq.jgrapht.DirectedGraph;
import org._3pq.jgrapht.GraphHelper;
import org._3pq.jgrapht.edge.DirectedEdge;
import org._3pq.jgrapht.graph.DefaultDirectedGraph;
import org._3pq.jgrapht.graph.DirectedSubgraph;
/**
* * Complements the {@link org._3pq.jgrapht.alg.ConnectivityInspector} class * with the capability to compute the strongly connected components of a * directed graph. The algorithm is implemented after "Corman et al: * Introduction to agorithms", Chapter 25.2. It has a running time of O(V + * E). *
* ** Unlike {@link org._3pq.jgrapht.alg.ConnectivityInspector}, this class does * not implement incremental inspection. The full algorithm is executed at the * first call of {@link StrongConnectivityInspector#stronglyConnectedSets()} * or {@link StrongConnectivityInspector#isStronglyConnected()}. *
* * @author Christian Soltenborn * * @since Feb 2, 2005 */ public class StrongConnectivityInspector { // the graph to compute the strongly connected sets for private final DirectedGraph m_graph; // stores the vertices, ordered by their finishing time in first dfs private LinkedList m_orderedVertices; // the result of the computation, cached for future calls private List m_stronglyConnectedSets; // the result of the computation, cached for future calls private List m_stronglyConnectedSubgraphs; // maps vertices to their VertexData object private Map m_vertexToVertexData; /** * The constructor of the StrongConnectivityInspector class. * * @param directedGraph the graph to inspect * * @throws IllegalArgumentException */ public StrongConnectivityInspector( DirectedGraph directedGraph ) { if( directedGraph == null ) { throw new IllegalArgumentException( "null not allowed for graph!" ); } m_graph = directedGraph; m_vertexToVertexData = null; m_orderedVertices = null; m_stronglyConnectedSets = null; m_stronglyConnectedSubgraphs = null; } /** * Returns the graph inspected by the StrongConnectivityInspector. * * @return the graph inspected by this StrongConnectivityInspector */ public DirectedGraph getGraph( ) { return m_graph; } /** * Returns true if the graph of this *StronglyConnectivityInspector
instance is strongly
* connected.
*
* @return true if the graph is strongly connected, false otherwise
*/
public boolean isStronglyConnected( ) {
return stronglyConnectedSets( ).size( ) == 1;
}
/**
* Computes a {@link List} of {@link Set}s, where each set contains
* vertices which together form a strongly connected component within the
* given graph.
*
* @return List
of Set
s containing the strongly
* connected components
*/
public List stronglyConnectedSets( ) {
if( m_stronglyConnectedSets == null ) {
m_orderedVertices = new LinkedList( );
m_stronglyConnectedSets = new Vector( );
// create VertexData objects for all vertices, store them
createVertexData( );
// perform the first round of DFS, result is an ordering
// of the vertices by decreasing finishing time
Iterator iter = m_vertexToVertexData.values( ).iterator( );
while( iter.hasNext( ) ) {
VertexData data = (VertexData) iter.next( );
if( !data.m_discovered ) {
dfsVisit( m_graph, data, null );
}
}
// calculate inverse graph (i.e. every edge is reversed)
DirectedGraph inverseGraph = new DefaultDirectedGraph( );
GraphHelper.addGraphReversed( inverseGraph, m_graph );
// get ready for next dfs round
resetVertexData( );
// second dfs round: vertices are considered in decreasing
// finishing time order; every tree found is a strongly
// connected set
iter = m_orderedVertices.iterator( );
while( iter.hasNext( ) ) {
VertexData data = (VertexData) iter.next( );
if( !data.m_discovered ) {
// new strongly connected set
Set set = new HashSet( );
m_stronglyConnectedSets.add( set );
dfsVisit( inverseGraph, data, set );
}
}
// clean up for garbage collection
m_orderedVertices = null;
m_vertexToVertexData = null;
}
return m_stronglyConnectedSets;
}
/**
* * Computes a list of {@link DirectedSubgraph}s of the given graph. Each * subgraph will represent a strongly connected component and will contain * all vertices of that component. The subgraph will have an edge (u,v) * iff u and v are contained in the strongly connected component. *
* ** NOTE: Calling this method will first execute {@link * StrongConnectivityInspector#stronglyConnectedSets()}. If you don't need * subgraphs, use that method. *
* * @return a list of subgraphs representing the strongly connected * components */ public List stronglyConnectedSubgraphs( ) { if( m_stronglyConnectedSubgraphs == null ) { List sets = stronglyConnectedSets( ); m_stronglyConnectedSubgraphs = new Vector( sets.size( ) ); Iterator iter = sets.iterator( ); while( iter.hasNext( ) ) { m_stronglyConnectedSubgraphs.add( new DirectedSubgraph( m_graph, (Set) iter.next( ), null ) ); } } return m_stronglyConnectedSubgraphs; } /* * Creates a VertexData object for every vertex in the graph and stores them * in a HashMap. */ private void createVertexData( ) { m_vertexToVertexData = new HashMap( m_graph.vertexSet( ).size( ) ); Iterator iter = m_graph.vertexSet( ).iterator( ); while( iter.hasNext( ) ) { Object vertex = iter.next( ); m_vertexToVertexData.put( vertex, new VertexData( vertex, false, false ) ); } } /* * The subroutine of DFS. NOTE: the set is used to distinguish between 1st * and 2nd round of DFS. set == null: finished vertices are stored (1st * round). set != null: all vertices found will be saved in the set (2nd * round) */ private void dfsVisit( DirectedGraph graph, VertexData vertexData, Set vertices ) { Stack stack = new Stack( ); stack.push( vertexData ); while( !stack.isEmpty( ) ) { VertexData data = (VertexData) stack.pop( ); if( !data.m_discovered ) { data.m_discovered = true; if( vertices != null ) { vertices.add( data.m_vertex ); } // TODO: other way to identify when this vertex is finished!? stack.push( new VertexData( data, true, true ) ); // follow all edges Iterator iter = graph.outgoingEdgesOf( data.m_vertex ).iterator( ); while( iter.hasNext( ) ) { DirectedEdge edge = (DirectedEdge) iter.next( ); VertexData targetData = (VertexData) m_vertexToVertexData.get( edge.getTarget( ) ); if( !targetData.m_discovered ) { // the "recursion" stack.push( targetData ); } } } else if( data.m_finished ) { if( vertices == null ) { // see TODO above m_orderedVertices.addFirst( data.m_vertex ); } } } } /* * Resets all VertexData objects. */ private void resetVertexData( ) { Iterator iter = m_vertexToVertexData.values( ).iterator( ); while( iter.hasNext( ) ) { VertexData data = (VertexData) iter.next( ); data.m_discovered = false; data.m_finished = false; } } /* * Lightweight class storing some data vor every vertex. */ private final class VertexData { private final Object m_vertex; private boolean m_discovered; private boolean m_finished; private VertexData( Object vertex, boolean discovered, boolean finished ) { m_vertex = vertex; m_discovered = discovered; m_finished = finished; } } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/alg/VertexCovers.java 0000644 0001751 0001751 00000012363 10266566752 024640 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ----------------- * VertexCovers.java * ----------------- * (C) Copyright 2003, by Linda Buisman and Contributors. * * Original Author: Linda Buisman * Contributor(s): Barak Naveh * * $Id: VertexCovers.java,v 1.4 2004/11/18 20:46:24 barak_naveh Exp $ * * Changes * ------- * 06-Nov-2003 : Initial revision (LB); * */ package org._3pq.jgrapht.alg; import java.util.Collections; import java.util.HashSet; import java.util.Set; import org._3pq.jgrapht.Edge; import org._3pq.jgrapht.Graph; import org._3pq.jgrapht.UndirectedGraph; import org._3pq.jgrapht.alg.util.VertexDegreeComparator; import org._3pq.jgrapht.graph.Subgraph; import org._3pq.jgrapht.graph.UndirectedSubgraph; /** * Algorithms to find a vertex cover for a graph. A vertex cover is a set of * vertices that touches all the edges in the graph. The graph's vertex set is * a trivial cover. However, a minimal vertex set (or at least an * approximation for it) is usually desired. Finding a true minimal vertex * cover is an NP-Complete problem. For more on the vertex cover problem, see * * http://mathworld.wolfram.com/VertexCover.html * * @author Linda Buisman * * @since Nov 6, 2003 */ public class VertexCovers { /** * Finds a 2-approximation for a minimal vertex cover of the specified * graph. The algorithm promises a cover that is at most double the size * of a minimal cover. The algorithm takes O(|E|) time. * ** For more details see Jenny Walter, CMPU-240: Lecture notes for Language * Theory and Computation, Fall 2002, Vassar College, * * http://www.cs.vassar.edu/~walter/cs241index/lectures/PDF/approx.pdf. *
* * @param g the graph for which vertex cover approximation is to be found. * * @return a set of vertices which is a vertex cover for the specified * graph. */ public Set find2ApproximationCover( Graph g ) { // C <-- {} Set cover = new HashSet( ); // G'=(V',E') <-- G(V,E) Subgraph sg = new Subgraph( g, null, null ); // while E' is non-empty while( sg.edgeSet( ).size( ) > 0 ) { // let (u,v) be an arbitrary edge of E' Edge e = (Edge) sg.edgeSet( ).iterator( ).next( ); // C <-- C U {u,v} Object u = e.getSource( ); Object v = e.getTarget( ); cover.add( u ); cover.add( v ); // remove from E' every edge incident on either u or v sg.removeVertex( u ); sg.removeVertex( v ); } return cover; // return C } /** * Finds a greedy approximation for a minimal vertex cover of a specified * graph. At each iteration, the algorithm picks the vertex with the * highest degree and adds it to the cover, until all edges are covered. * ** The algorithm works on undirected graphs, but can also work on directed * graphs when their edge-directions are ignored. To ignore edge * directions you can use {@link * org._3pq.jgrapht.GraphHelper#undirectedGraph(Graph)} or {@link * org._3pq.jgrapht.graph.AsUndirectedGraph}. *
* * @param g the graph for which vertex cover approximation is to be found. * * @return a set of vertices which is a vertex cover for the specified * graph. */ public Set findGreedyCover( UndirectedGraph g ) { // C <-- {} Set cover = new HashSet( ); // G' <-- G UndirectedGraph sg = new UndirectedSubgraph( g, null, null ); // compare vertices in descending order of degree VertexDegreeComparator comp = new VertexDegreeComparator( sg ); // while G' != {} while( sg.edgeSet( ).size( ) > 0 ) { // v <-- vertex with maximum degree in G' Object v = Collections.max( sg.vertexSet( ), comp ); // C <-- C U {v} cover.add( v ); // remove from G' every edge incident on v, and v itself sg.removeVertex( v ); } return cover; } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/alg/package.html 0000644 0001751 0001751 00000000177 10266566752 023617 0 ustar moeller moeller Algorithms provided with JGraphT. libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/demo/ 0000755 0001751 0001751 00000000000 11251311101 021455 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/demo/HelloJGraphT.java 0000644 0001751 0001751 00000007567 10266566752 024657 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ----------------- * HelloJGraphT.java * ----------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: HelloJGraphT.java,v 1.4 2004/09/17 07:24:12 perfecthash Exp $ * * Changes * ------- * 27-Jul-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.demo; import java.net.MalformedURLException; import java.net.URL; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.UndirectedGraph; import org._3pq.jgrapht.graph.DefaultDirectedGraph; import org._3pq.jgrapht.graph.SimpleGraph; /** * A simple introduction to using JGraphT. * * @author Barak Naveh * * @since Jul 27, 2003 */ public final class HelloJGraphT { private HelloJGraphT( ) {} // ensure non-instantiability. /** * The starting point for the demo. * * @param args ignored. */ public static void main( String[] args ) { UndirectedGraph stringGraph = createStringGraph( ); // note undirected edges are printed as: {* NOTE: To run this demo you may need to increase the JVM max mem size. In * Sun's JVM it is done using the "-Xmx" switch. Specify "-Xmx300M" to set it * to 300MB. *
* ** WARNING: Don't run this demo as-is on machines with less than 512MB memory. * Your machine will start paging severely. You need to first modify it to * have fewer graph elements. This is easily done by changing the loop * counters below. *
* * @author Barak Naveh * * @since Aug 10, 2003 */ public final class PerformanceDemo { /** * The starting point for the demo. * * @param args ignored. */ public static void main( String[] args ) { long time = System.currentTimeMillis( ); reportPerformanceFor( "starting at", time ); Graph g = new Pseudograph( ); Object prev; Object curr; curr = prev = new Object( ); g.addVertex( prev ); int numVertices = 10000; int numEdgesPerVertex = 200; int numElements = numVertices * ( 1 + numEdgesPerVertex ); System.out.println( "\n" + "allocating graph with " + numElements + " elements (may take a few tens of seconds)..." ); for( int i = 0; i < numVertices; i++ ) { curr = new Object( ); g.addVertex( curr ); for( int j = 0; j < numEdgesPerVertex; j++ ) { g.addEdge( prev, curr ); } prev = curr; } reportPerformanceFor( "graph allocation", time ); time = System.currentTimeMillis( ); for( Iterator i = new BreadthFirstIterator( g ); i.hasNext( ); ) { i.next( ); } reportPerformanceFor( "breadth traversal", time ); time = System.currentTimeMillis( ); for( Iterator i = new DepthFirstIterator( g ); i.hasNext( ); ) { i.next( ); } reportPerformanceFor( "depth traversal", time ); System.out.println( "\n" + "Paused: graph is still in memory (to check mem consumption)." ); System.out.print( "press any key to free memory and finish..." ); try { System.in.read( ); } catch( IOException e ) { e.printStackTrace( ); } System.out.println( "done." ); } private static void reportPerformanceFor( String msg, long refTime ) { double time = ( System.currentTimeMillis( ) - refTime ) / 1000.0; double mem = usedMemory( ) / ( 1024.0 * 1024.0 ); mem = Math.round( mem * 100 ) / 100.0; System.out.println( msg + " (" + time + " sec, " + mem + "MB)" ); } private static long usedMemory( ) { Runtime rt = Runtime.getRuntime( ); return rt.totalMemory( ) - rt.freeMemory( ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/demo/package.html 0000644 0001751 0001751 00000000223 10266566752 023770 0 ustar moeller moeller Demo programs that help to get started with JGraphT. libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/edge/ 0000755 0001751 0001751 00000000000 11251311101 021435 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/edge/DefaultEdge.java 0000644 0001751 0001751 00000007166 10266566752 024520 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ---------------- * DefaultEdge.java * ---------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: DefaultEdge.java,v 1.5 2005/04/23 08:09:29 perfecthash Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 10-Aug-2003 : General edge refactoring (BN); * */ package org._3pq.jgrapht.edge; import java.io.Serializable; import org._3pq.jgrapht.Edge; /** * A skeletal implementation of the Edge interface, to minimize the * effort required to implement the interface. * * @author Barak Naveh * * @since Jul 14, 2003 */ public class DefaultEdge implements Edge, Cloneable, Serializable { private static final long serialVersionUID = 3258408452177932855L; private Object m_source; private Object m_target; /** * Constructor for DefaultEdge. * * @param sourceVertex source vertex of the edge. * @param targetVertex target vertex of the edge. */ public DefaultEdge( Object sourceVertex, Object targetVertex ) { m_source = sourceVertex; m_target = targetVertex; } /** * @see org._3pq.jgrapht.Edge#getSource() */ public Object getSource( ) { return m_source; } /** * @see org._3pq.jgrapht.Edge#getTarget() */ public Object getTarget( ) { return m_target; } /** * @see org._3pq.jgrapht.Edge#setWeight(double) */ public void setWeight( double weight ) { throw new UnsupportedOperationException( ); } /** * @see org._3pq.jgrapht.Edge#getWeight() */ public double getWeight( ) { return DEFAULT_EDGE_WEIGHT; } /** * @see Edge#clone() */ public Object clone( ) { try { return super.clone( ); } catch( CloneNotSupportedException e ) { // shouldn't happen as we are Cloneable throw new InternalError( ); } } /** * @see org._3pq.jgrapht.Edge#containsVertex(java.lang.Object) */ public boolean containsVertex( Object v ) { return m_source.equals( v ) || m_target.equals( v ); } /** * @see org._3pq.jgrapht.Edge#oppositeVertex(java.lang.Object) */ public Object oppositeVertex( Object v ) { if( v.equals( m_source ) ) { return m_target; } else if( v.equals( m_target ) ) { return m_source; } else { throw new IllegalArgumentException( "no such vertex" ); } } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/edge/DirectedEdge.java 0000644 0001751 0001751 00000004353 10266566752 024652 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ----------------- * DirectedEdge.java * ----------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: DirectedEdge.java,v 1.5 2004/11/18 21:44:24 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 10-Aug-2003 : General edge refactoring (BN); * */ package org._3pq.jgrapht.edge; /** * A implementation of directed edge. * * @author Barak Naveh * * @since Jul 14, 2003 */ public class DirectedEdge extends DefaultEdge { private static final long serialVersionUID = 3258689927188134195L; /** * @see DefaultEdge#DefaultEdge(Object, Object) */ public DirectedEdge( Object sourceVertex, Object targetVertex ) { super( sourceVertex, targetVertex ); } /** * Returns a string representation of this directed edge. The * representation is a parenthesized pair (v1,v2) where v1 is the source * vertex of this edge and v2 is the target vertex of this edge. * * @return a string representation of this directed edge. */ public String toString( ) { return "(" + getSource( ) + "," + getTarget( ) + ")"; } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/edge/DirectedWeightedEdge.java 0000644 0001751 0001751 00000005207 10266566752 026332 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------- * DirectedWeightedEdge.java * ------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: DirectedWeightedEdge.java,v 1.9 2004/11/18 21:44:24 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 10-Aug-2003 : General edge refactoring (BN); * */ package org._3pq.jgrapht.edge; /** * An implementation of directed weighted edge. * * @author Barak Naveh * * @since Jul 14, 2003 */ public class DirectedWeightedEdge extends DirectedEdge { private static final long serialVersionUID = 3689070664137257523L; private double m_weight = DEFAULT_EDGE_WEIGHT; /** * @see DirectedEdge#DirectedEdge(Object, Object) */ public DirectedWeightedEdge( Object sourceVertex, Object targetVertex ) { super( sourceVertex, targetVertex ); } /** * Constructor for DirectedWeightedEdge. * * @param sourceVertex source vertex of the new edge. * @param targetVertex target vertex of the new edge. * @param weight the weight of the new edge. */ public DirectedWeightedEdge( Object sourceVertex, Object targetVertex, double weight ) { super( sourceVertex, targetVertex ); m_weight = weight; } /** * @see org._3pq.jgrapht.Edge#setWeight(double) */ public void setWeight( double weight ) { m_weight = weight; } /** * @see org._3pq.jgrapht.Edge#getWeight() */ public double getWeight( ) { return m_weight; } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/edge/EdgeFactories.java 0000644 0001751 0001751 00000010426 10266566752 025044 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------ * EdgeFactories.java * ------------------ * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: EdgeFactories.java,v 1.6 2004/11/18 21:44:24 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 04-Aug-2003 : Renamed from EdgeFactoryFactory & made utility class (BN); * 03-Nov-2003 : Made edge factories serializable (BN); * */ package org._3pq.jgrapht.edge; import java.io.Serializable; import org._3pq.jgrapht.Edge; import org._3pq.jgrapht.EdgeFactory; /** * This utility class is a container of various {@link * org._3pq.jgrapht.EdgeFactory} classes. * ** Classes included here do not have substantial logic. They are grouped * together in this container in order to avoid clutter. *
* * @author Barak Naveh * * @since Jul 16, 2003 */ public final class EdgeFactories { private EdgeFactories( ) {} // ensure non-instantiability. /** * An EdgeFactory for producing directed edges. * * @author Barak Naveh * * @since Jul 14, 2003 */ public static class DirectedEdgeFactory extends AbstractEdgeFactory { private static final long serialVersionUID = 3618135658586388792L; /** * @see EdgeFactory#createEdge(Object, Object) */ public Edge createEdge( Object source, Object target ) { return new DirectedEdge( source, target ); } } /** * An EdgeFactory for producing directed edges with weights. * * @author Barak Naveh * * @since Jul 14, 2003 */ public static class DirectedWeightedEdgeFactory extends AbstractEdgeFactory { private static final long serialVersionUID = 3257002163870775604L; /** * @see EdgeFactory#createEdge(Object, Object) */ public Edge createEdge( Object source, Object target ) { return new DirectedWeightedEdge( source, target ); } } /** * An EdgeFactory for producing undirected edges. * * @author Barak Naveh * * @since Jul 14, 2003 */ public static class UndirectedEdgeFactory extends AbstractEdgeFactory { private static final long serialVersionUID = 3257007674431189815L; /** * @see EdgeFactory#createEdge(Object, Object) */ public Edge createEdge( Object source, Object target ) { return new UndirectedEdge( source, target ); } } /** * An EdgeFactory for producing undirected edges with weights. * * @author Barak Naveh * * @since Jul 14, 2003 */ public static class UndirectedWeightedEdgeFactory extends AbstractEdgeFactory { private static final long serialVersionUID = 4048797883346269237L; /** * @see EdgeFactory#createEdge(Object, Object) */ public Edge createEdge( Object source, Object target ) { return new UndirectedWeightedEdge( source, target ); } } /** * A base class for edge factories. * * @author Barak Naveh * * @since Nov 3, 2003 */ abstract static class AbstractEdgeFactory implements EdgeFactory, Serializable {} } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/edge/UndirectedEdge.java 0000644 0001751 0001751 00000004341 10266566752 025212 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------- * UndirectedEdge.java * ------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: UndirectedEdge.java,v 1.5 2004/11/18 21:44:24 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 10-Aug-2003 : General edge refactoring (BN); * */ package org._3pq.jgrapht.edge; /** * A implementation for an undirected edge. * * @author Barak Naveh * * @since Jul 14, 2003 */ public class UndirectedEdge extends DefaultEdge { private static final long serialVersionUID = 3257563988526380337L; /** * @see DefaultEdge#DefaultEdge(Object, Object) */ public UndirectedEdge( Object sourceVertex, Object targetVertex ) { super( sourceVertex, targetVertex ); } /** * Returns a string representation of this undirected edge. The * representation is a curly-braced pair {v1,v2} where v1,v2 are the two * endpoint vertices of this edge. * * @return a string representation of this directed edge. */ public String toString( ) { return "{" + getSource( ) + "," + getTarget( ) + "}"; } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/edge/UndirectedWeightedEdge.java 0000644 0001751 0001751 00000005237 10266566752 026700 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------------------- * UndirectedWeightedEdge.java * --------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: UndirectedWeightedEdge.java,v 1.9 2004/11/18 21:44:24 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 10-Aug-2003 : General edge refactoring (BN); * */ package org._3pq.jgrapht.edge; /** * An implementation of undirected weighted edge. * * @author Barak Naveh * * @since Jul 16, 2003 */ public class UndirectedWeightedEdge extends UndirectedEdge { private static final long serialVersionUID = 4120853256903012915L; private double m_weight = DEFAULT_EDGE_WEIGHT; /** * @see UndirectedEdge#UndirectedEdge(Object, Object) */ public UndirectedWeightedEdge( Object sourceVertex, Object targetVertex ) { super( sourceVertex, targetVertex ); } /** * Constructor for UndirectedWeightedEdge. * * @param sourceVertex source vertex of the new edge. * @param targetVertex target vertex of the new edge. * @param weight the weight of the new edge. */ public UndirectedWeightedEdge( Object sourceVertex, Object targetVertex, double weight ) { super( sourceVertex, targetVertex ); m_weight = weight; } /** * @see org._3pq.jgrapht.Edge#setWeight(double) */ public void setWeight( double weight ) { m_weight = weight; } /** * @see org._3pq.jgrapht.Edge#getWeight() */ public double getWeight( ) { return m_weight; } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/edge/package.html 0000644 0001751 0001751 00000000203 10266566752 023746 0 ustar moeller moeller Implementations of various edge interfaces. libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/event/ 0000755 0001751 0001751 00000000000 11251311101 021652 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/event/ConnectedComponentTraversalEvent.java 0000644 0001751 0001751 00000004763 10266566752 031237 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------------------- * ConnectedComponentTraversalEvent.java * ------------------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: ConnectedComponentTraversalEvent.java,v 1.2 2004/11/18 21:55:00 barak_naveh Exp $ * * Changes * ------- * 11-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.event; import java.util.EventObject; /** * A traversal event with respect to a connected component. * * @author Barak Naveh * * @since Aug 11, 2003 */ public class ConnectedComponentTraversalEvent extends EventObject { private static final long serialVersionUID = 3834311717709822262L; /** Connected component traversal started event. */ public static final int CONNECTED_COMPONENT_STARTED = 31; /** Connected component traversal finished event. */ public static final int CONNECTED_COMPONENT_FINISHED = 32; /** The type of this event. */ private int m_type; /** * Creates a new ConnectedComponentTraversalEvent. * * @param eventSource the source of the event. * @param type the type of event. */ public ConnectedComponentTraversalEvent( Object eventSource, int type ) { super( eventSource ); m_type = type; } /** * Returns the event type. * * @return the event type. */ public int getType( ) { return m_type; } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/event/EdgeTraversalEvent.java 0000644 0001751 0001751 00000004265 10266566752 026313 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ----------------------- * EdgeTraversalEvent.java * ----------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: EdgeTraversalEvent.java,v 1.2 2004/11/18 21:55:00 barak_naveh Exp $ * * Changes * ------- * 11-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.event; import java.util.EventObject; import org._3pq.jgrapht.Edge; /** * A traversal event for a graph edge. * * @author Barak Naveh * * @since Aug 11, 2003 */ public class EdgeTraversalEvent extends EventObject { private static final long serialVersionUID = 4050768173789820979L; /** The traversed edge. */ protected Edge m_edge; /** * Creates a new EdgeTraversalEvent. * * @param eventSource the source of the event. * @param edge the traversed edge. */ public EdgeTraversalEvent( Object eventSource, Edge edge ) { super( eventSource ); m_edge = edge; } /** * Returns the traversed edge. * * @return the traversed edge. */ public Edge getEdge( ) { return m_edge; } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/event/GraphChangeEvent.java 0000644 0001751 0001751 00000004331 10266566752 025724 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------------- * GraphChangeEvent.java * --------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: GraphChangeEvent.java,v 1.3 2004/11/18 21:55:00 barak_naveh Exp $ * * Changes * ------- * 10-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.event; import java.util.EventObject; /** * An event which indicates that a graph has changed. This class is a root for * graph change events. * * @author Barak Naveh * * @since Aug 10, 2003 */ public class GraphChangeEvent extends EventObject { private static final long serialVersionUID = 3834592106026382391L; /** The type of graph change this event indicates. */ protected int m_type; /** * Creates a new graph change event. * * @param eventSource the source of the event. * @param type the type of event. */ public GraphChangeEvent( Object eventSource, int type ) { super( eventSource ); m_type = type; } /** * Returns the event type. * * @return the event type. */ public int getType( ) { return m_type; } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/event/GraphEdgeChangeEvent.java 0000644 0001751 0001751 00000006323 10266566752 026514 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------- * GraphEdgeChangeEvent.java * ------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: GraphEdgeChangeEvent.java,v 1.2 2004/11/18 21:55:00 barak_naveh Exp $ * * Changes * ------- * 10-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.event; import org._3pq.jgrapht.Edge; /** * An event which indicates that a graph edge has changed, or is about to * change. The event can be used either as an indication after the edge * has been added or removed, or before it is added. The type of the * event can be tested using the {@link * org._3pq.jgrapht.event.GraphChangeEvent#getType()} method. * * @author Barak Naveh * * @since Aug 10, 2003 */ public class GraphEdgeChangeEvent extends GraphChangeEvent { private static final long serialVersionUID = 3618134563335844662L; /** * Before edge added event. This event is fired before an edge is added to * a graph. */ public static final int BEFORE_EDGE_ADDED = 21; /** * Before edge removed event. This event is fired before an edge is removed * from a graph. */ public static final int BEFORE_EDGE_REMOVED = 22; /** * Edge added event. This event is fired after an edge is added to a graph. */ public static final int EDGE_ADDED = 23; /** * Edge removed event. This event is fired after an edge is removed from a * graph. */ public static final int EDGE_REMOVED = 24; /** The edge that this event is related to. */ protected Edge m_edge; /** * Constructor for GraphEdgeChangeEvent. * * @param eventSource the source of this event. * @param type the event type of this event. * @param e the edge that this event is related to. */ public GraphEdgeChangeEvent( Object eventSource, int type, Edge e ) { super( eventSource, type ); m_edge = e; } /** * Returns the edge that this event is related to. * * @return the edge that this event is related to. */ public Edge getEdge( ) { return m_edge; } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/event/GraphListener.java 0000644 0001751 0001751 00000004205 10266566752 025322 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------ * GraphListener.java * ------------------ * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: GraphListener.java,v 1.4 2004/11/18 21:54:30 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 10-Aug-2003 : Adaptation to new event model (BN); * */ package org._3pq.jgrapht.event; /** * A listener that is notified when the graph changes. * ** If only notifications on vertex set changes are required it is more * efficient to use the VertexSetListener. *
* * @author Barak Naveh * * @see org._3pq.jgrapht.event.VertexSetListener * @since Jul 18, 2003 */ public interface GraphListener extends VertexSetListener { /** * Notifies that an edge has been added to the graph. * * @param e the edge event. */ public void edgeAdded( GraphEdgeChangeEvent e ); /** * Notifies that an edge has been removed from the graph. * * @param e the edge event. */ public void edgeRemoved( GraphEdgeChangeEvent e ); } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/event/GraphVertexChangeEvent.java 0000644 0001751 0001751 00000006403 10266566752 027124 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------------------- * GraphVertexChangeEvent.java * --------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: GraphVertexChangeEvent.java,v 1.2 2004/11/18 21:56:21 barak_naveh Exp $ * * Changes * ------- * 10-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.event; /** * An event which indicates that a graph vertex has changed, or is about to * change. The event can be used either as an indication after the * vertex has been added or removed, or before it is added. The type * of the event can be tested using the {@link * org._3pq.jgrapht.event.GraphChangeEvent#getType()} method. * * @author Barak Naveh * * @since Aug 10, 2003 */ public class GraphVertexChangeEvent extends GraphChangeEvent { private static final long serialVersionUID = 3690189962679104053L; /** * Before vertex added event. This event is fired before a vertex is added * to a graph. */ public static final int BEFORE_VERTEX_ADDED = 11; /** * Before vertex removed event. This event is fired before a vertex is * removed from a graph. */ public static final int BEFORE_VERTEX_REMOVED = 12; /** * Vertex added event. This event is fired after a vertex is added to a * graph. */ public static final int VERTEX_ADDED = 13; /** * Vertex removed event. This event is fired after a vertex is removed from * a graph. */ public static final int VERTEX_REMOVED = 14; /** The vertex that this event is related to. */ protected Object m_vertex; /** * Creates a new GraphVertexChangeEvent object. * * @param eventSource the source of the event. * @param type the type of the event. * @param vertex the vertex that the event is related to. */ public GraphVertexChangeEvent( Object eventSource, int type, Object vertex ) { super( eventSource, type ); m_vertex = vertex; } /** * Returns the vertex that this event is related to. * * @return the vertex that this event is related to. */ public Object getVertex( ) { return m_vertex; } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/event/TraversalListener.java 0000644 0001751 0001751 00000005355 10266566752 026233 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ---------------------- * TraversalListener.java * ---------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: TraversalListener.java,v 1.2 2004/11/18 21:56:10 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 11-Aug-2003 : Adaptation to new event model (BN); * */ package org._3pq.jgrapht.event; /** * A listener on graph iterator or on a graph traverser. * * @author Barak Naveh * * @since Jul 19, 2003 */ public interface TraversalListener { /** * Called to inform listeners that the traversal of the current connected * component has finished. * * @param e the traversal event. */ public void connectedComponentFinished( ConnectedComponentTraversalEvent e ); /** * Called to inform listeners that a traversal of a new connected component * has started. * * @param e the traversal event. */ public void connectedComponentStarted( ConnectedComponentTraversalEvent e ); /** * Called to inform the listener that the specified edge have been visited * during the graph traversal. Depending on the traversal algorithm, edge * might be visited more than once. * * @param e the edge traversal event. */ public void edgeTraversed( EdgeTraversalEvent e ); /** * Called to inform the listener that the specified vertex have been * visited during the graph traversal. Depending on the traversal * algorithm, vertex might be visited more than once. * * @param e the vertex traversal event. */ public void vertexTraversed( VertexTraversalEvent e ); } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/event/TraversalListenerAdapter.java 0000644 0001751 0001751 00000004576 10266566752 027540 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ----------------------------- * TraversalListenerAdapter.java * ----------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: TraversalListenerAdapter.java,v 1.2 2004/11/18 21:56:10 barak_naveh Exp $ * * Changes * ------- * 06-Aug-2003 : Initial revision (BN); * 11-Aug-2003 : Adaptation to new event model (BN); * */ package org._3pq.jgrapht.event; /** * An empty do-nothing implementation of the {@link TraversalListener} * interface used for subclasses. * * @author Barak Naveh * * @since Aug 6, 2003 */ public class TraversalListenerAdapter implements TraversalListener { /** * @see TraversalListener#connectedComponentFinished(ConnectedComponentTraversalEvent) */ public void connectedComponentFinished( ConnectedComponentTraversalEvent e ) {} /** * @see TraversalListener#connectedComponentStarted(ConnectedComponentTraversalEvent) */ public void connectedComponentStarted( ConnectedComponentTraversalEvent e ) {} /** * @see TraversalListener#edgeTraversed(EdgeTraversalEvent) */ public void edgeTraversed( EdgeTraversalEvent e ) {} /** * @see TraversalListener#vertexTraversed(VertexTraversalEvent) */ public void vertexTraversed( VertexTraversalEvent e ) {} } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/event/VertexSetListener.java 0000644 0001751 0001751 00000004374 10266566752 026221 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ---------------------- * VertexSetListener.java * ---------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: VertexSetListener.java,v 1.5 2005/04/23 08:09:29 perfecthash Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 10-Aug-2003 : Adaptation to new event model (BN); * */ package org._3pq.jgrapht.event; import java.util.EventListener; /** * A listener that is notified when the graph's vertex set changes. It should * be used when only notifications on vertex-set changes are of * interest. If all graph notifications are of interest better use *GraphListener
.
*
* @author Barak Naveh
*
* @see org._3pq.jgrapht.event.GraphListener
* @since Jul 18, 2003
*/
public interface VertexSetListener extends EventListener {
/**
* Notifies that a vertex has been added to the graph.
*
* @param e the vertex event.
*/
public void vertexAdded( GraphVertexChangeEvent e );
/**
* Notifies that a vertex has been removed from the graph.
*
* @param e the vertex event.
*/
public void vertexRemoved( GraphVertexChangeEvent e );
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/event/VertexTraversalEvent.java 0000644 0001751 0001751 00000004302 10266566752 026714 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -------------------------
* VertexTraversalEvent.java
* -------------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: VertexTraversalEvent.java,v 1.2 2004/11/18 21:34:45 barak_naveh Exp $
*
* Changes
* -------
* 11-Aug-2003 : Initial revision (BN);
*
*/
package org._3pq.jgrapht.event;
import java.util.EventObject;
/**
* A traversal event for a graph vertex.
*
* @author Barak Naveh
*
* @since Aug 11, 2003
*/
public class VertexTraversalEvent extends EventObject {
private static final long serialVersionUID = 3688790267213918768L;
/** The traversed vertex. */
protected Object m_vertex;
/**
* Creates a new VertexTraversalEvent.
*
* @param eventSource the source of the event.
* @param vertex the traversed vertex.
*/
public VertexTraversalEvent( Object eventSource, Object vertex ) {
super( eventSource );
m_vertex = vertex;
}
/**
* Returns the traversed vertex.
*
* @return the traversed vertex.
*/
public Object getVertex( ) {
return m_vertex;
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/event/package.html 0000644 0001751 0001751 00000000313 10266566752 024165 0 ustar moeller moeller
Event classes and listener interfaces, used to provide a change
notification mechanism on graph modification events.
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/ext/ 0000755 0001751 0001751 00000000000 11251311101 021331 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/ext/JGraphModelAdapter.java 0000644 0001751 0001751 00000120003 10266566752 025662 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -----------------------
* JGraphModelAdapter.java
* -----------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): Erik Postma
*
* $Id: JGraphModelAdapter.java,v 1.22 2005/07/17 05:40:49 perfecthash Exp $
*
* Changes
* -------
* 02-Aug-2003 : Initial revision (BN);
* 10-Aug-2003 : Adaptation to new event model (BN);
* 06-Nov-2003 : Allowed non-listenable underlying JGraphT graph (BN);
* 12-Dec-2003 : Added CellFactory support (BN);
* 27-Jan-2004 : Added support for JGraph->JGraphT change propagation (EP);
* 29-Jan-2005 : Added support for JGraph dangling edges (BN);
*
*/
package org._3pq.jgrapht.ext;
import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.BorderFactory;
import org._3pq.jgrapht.DirectedGraph;
import org._3pq.jgrapht.EdgeFactory;
import org._3pq.jgrapht.Graph;
import org._3pq.jgrapht.ListenableGraph;
import org._3pq.jgrapht.event.GraphEdgeChangeEvent;
import org._3pq.jgrapht.event.GraphListener;
import org._3pq.jgrapht.event.GraphVertexChangeEvent;
import org.jgraph.event.GraphModelEvent;
import org.jgraph.event.GraphModelEvent.GraphModelChange;
import org.jgraph.event.GraphModelListener;
import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.ConnectionSet;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.GraphCell;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.Port;
/**
* An adapter that reflects a JGraphT graph as a JGraph graph. This adapter is
* useful when using JGraph in order to visualize JGraphT graphs. For more
* about JGraph see
* http://jgraph.sourceforge.net
*
* * Modifications made to the underlying JGraphT graph are reflected to this * JGraph model if and only if the underlying JGraphT graph is a {@link * org._3pq.jgrapht.ListenableGraph}. If the underlying JGraphT graph is * not ListenableGraph, then this JGraph model represent a snapshot if * the graph at the time of its creation. *
* ** Changes made to this JGraph model are also reflected back to the underlying * JGraphT graph. To avoid confusion, variables are prefixed according to the * JGraph/JGraphT object(s) they are referring to. *
* ** KNOWN BUGS: There is a small issue to be aware of. JGraph allows * 'dangling edges' incident with just one vertex; JGraphT doesn't. Such a * configuration can arise when adding an edge or removing a vertex. The code * handles this by removing the newly-added dangling edge or removing all * edges incident with the vertex before actually removing the vertex, * respectively. This works very well, only it doesn't play all that nicely * with the undo-manager in the JGraph: for the second situation where you * remove a vertex incident with some edges, if you undo the removal, the * vertex is 'unremoved' but the edges aren't. *
* * @author Barak Naveh * * @since Aug 2, 2003 */ /* * FUTURE WORK: Now that the adapter supports JGraph dangling edges, it is * possible, with a little effort, to eliminate the "known bugs" above. Some * todo and fixme marks in the code indicate where the possible improvements * could be made to realize that. */ public class JGraphModelAdapter extends DefaultGraphModel { private static final long serialVersionUID = 3256722883706302515L; /** * The following m_(jCells|jtElement)Being(Added|Removed) sets are used to * prevent bouncing of events between the JGraph and JGraphT listeners. * They ensure that their respective add/remove operations are done * exactly once. Here is an example of how m_jCellsBeingAdded is used when * an edge is added to a JGraph graph: ** 1. First, we add the desired edge to m_jCellsBeingAdded to indicate * that the edge is being inserted internally. * 2. Then we invoke the JGraph 'insert' operation. * 3. The JGraph listener will detect the newly inserted edge. * 4. It checks if the edge is contained in m_jCellsBeingAdded. * 5. If yes, * it just removes it and does nothing else. * if no, * it knows that the edge was inserted externally and performs * the insertion. * 6. Lastly, we remove the edge from the m_jCellsBeingAdded. ** *
* Step 6 is not always required but we do it anyway as a safeguard against * the rare case where the edge to be added is already contained in the * graph and thus NO event will be fired. If 6 is not done, a junk edge * will remain in the m_jCellsBeingAdded set. *
* ** The other sets are used in a similar manner to the above. Apparently, * All that complication could be eliminated if JGraph and JGraphT had * both allowed operations that do not inform listeners... *
*/ final Set m_jCellsBeingAdded = new HashSet( ); final Set m_jCellsBeingRemoved = new HashSet( ); final Set m_jtElementsBeingAdded = new HashSet( ); final Set m_jtElementsBeingRemoved = new HashSet( ); private final CellFactory m_cellFactory; /** Maps JGraph edges to JGraphT edges */ private final Map m_cellToEdge = new HashMap( ); /** Maps JGraph vertices to JGraphT vertices */ private final Map m_cellToVertex = new HashMap( ); private AttributeMap m_defaultEdgeAttributes; private AttributeMap m_defaultVertexAttributes; /** Maps JGraphT edges to JGraph edges */ private final Map m_edgeToCell = new HashMap( ); /** Maps JGraphT vertices to JGraph vertices */ private final Map m_vertexToCell = new HashMap( ); private final ShieldedGraph m_jtGraph; /** * Constructs a new JGraph model adapter for the specified JGraphT graph. * * @param jGraphTGraph the JGraphT graph for which JGraph model adapter to * be created.null
is NOT permitted.
*/
public JGraphModelAdapter( Graph jGraphTGraph ) {
this( jGraphTGraph, createDefaultVertexAttributes( ),
createDefaultEdgeAttributes( jGraphTGraph ) );
}
/**
* Constructs a new JGraph model adapter for the specified JGraphT graph.
*
* @param jGraphTGraph the JGraphT graph for which JGraph model adapter to
* be created. null
is NOT permitted.
* @param defaultVertexAttributes a default map of JGraph attributes to
* format vertices. null
is NOT permitted.
* @param defaultEdgeAttributes a default map of JGraph attributes to
* format edges. null
is NOT permitted.
*/
public JGraphModelAdapter( Graph jGraphTGraph,
AttributeMap defaultVertexAttributes, AttributeMap defaultEdgeAttributes ) {
this( jGraphTGraph, defaultVertexAttributes, defaultEdgeAttributes,
new DefaultCellFactory( ) );
}
/**
* Constructs a new JGraph model adapter for the specified JGraphT graph.
*
* @param jGraphTGraph the JGraphT graph for which JGraph model adapter to
* be created. null
is NOT permitted.
* @param defaultVertexAttributes a default map of JGraph attributes to
* format vertices. null
is NOT permitted.
* @param defaultEdgeAttributes a default map of JGraph attributes to
* format edges. null
is NOT permitted.
* @param cellFactory a {@link CellFactory} to be used to create the JGraph
* cells. null
is NOT permitted.
*
* @throws IllegalArgumentException
*/
public JGraphModelAdapter( Graph jGraphTGraph,
AttributeMap defaultVertexAttributes,
AttributeMap defaultEdgeAttributes, CellFactory cellFactory ) {
super( );
if( jGraphTGraph == null || defaultVertexAttributes == null
|| defaultEdgeAttributes == null || cellFactory == null ) {
throw new IllegalArgumentException( "null is NOT permitted" );
}
m_jtGraph = new ShieldedGraph( jGraphTGraph );
setDefaultVertexAttributes( defaultVertexAttributes );
setDefaultEdgeAttributes( defaultEdgeAttributes );
m_cellFactory = cellFactory;
if( jGraphTGraph instanceof ListenableGraph ) {
ListenableGraph g = (ListenableGraph) jGraphTGraph;
g.addGraphListener( new JGraphTListener( ) );
}
for( Iterator i = jGraphTGraph.vertexSet( ).iterator( );
i.hasNext( ); ) {
handleJGraphTAddedVertex( i.next( ) );
}
for( Iterator i = jGraphTGraph.edgeSet( ).iterator( ); i.hasNext( ); ) {
handleJGraphTAddedEdge( (org._3pq.jgrapht.Edge) i.next( ) );
}
this.addGraphModelListener( new JGraphListener( ) );
}
/**
* Creates and returns a map of attributes to be used as defaults for edge
* attributes, depending on the specified graph.
*
* @param jGraphTGraph the graph for which default edge attributes to be
* created.
*
* @return a map of attributes to be used as default for edge attributes.
*/
public static AttributeMap createDefaultEdgeAttributes( Graph jGraphTGraph ) {
AttributeMap map = new AttributeMap( );
if( jGraphTGraph instanceof DirectedGraph ) {
GraphConstants.setLineEnd( map, GraphConstants.ARROW_TECHNICAL );
GraphConstants.setEndFill( map, true );
GraphConstants.setEndSize( map, 10 );
}
GraphConstants.setForeground( map, Color.decode( "#25507C" ) );
GraphConstants.setFont( map,
GraphConstants.DEFAULTFONT.deriveFont( Font.BOLD, 12 ) );
GraphConstants.setLineColor( map, Color.decode( "#7AA1E6" ) );
return map;
}
/**
* Creates and returns a map of attributes to be used as defaults for
* vertex attributes.
*
* @return a map of attributes to be used as defaults for vertex
* attributes.
*/
public static AttributeMap createDefaultVertexAttributes( ) {
AttributeMap map = new AttributeMap( );
Color c = Color.decode( "#FF9900" );
GraphConstants.setBounds( map, new Rectangle2D.Double( 50, 50, 90, 30 ) );
GraphConstants.setBorder( map, BorderFactory.createRaisedBevelBorder( ) );
GraphConstants.setBackground( map, c );
GraphConstants.setForeground( map, Color.white );
GraphConstants.setFont( map,
GraphConstants.DEFAULTFONT.deriveFont( Font.BOLD, 12 ) );
GraphConstants.setOpaque( map, true );
return map;
}
/**
* Returns the cell factory used to create the JGraph cells.
*
* @return the cell factory used to create the JGraph cells.
*/
public CellFactory getCellFactory( ) {
return m_cellFactory;
}
/**
* Sets the default edge attributes used for creating new JGraph edges.
*
* @param defaultEdgeAttributes the default edge attributes to set.
*/
public void setDefaultEdgeAttributes( AttributeMap defaultEdgeAttributes ) {
m_defaultEdgeAttributes = defaultEdgeAttributes;
}
/**
* Returns the default edge attributes used for creating new JGraph edges.
*
* @return the default edge attributes used for creating new JGraph edges.
*/
public AttributeMap getDefaultEdgeAttributes( ) {
return m_defaultEdgeAttributes;
}
/**
* Sets the default vertex attributes used for creating new JGraph
* vertices.
*
* @param defaultVertexAttributes the default vertex attributes to set.
*/
public void setDefaultVertexAttributes(
AttributeMap defaultVertexAttributes ) {
m_defaultVertexAttributes = defaultVertexAttributes;
}
/**
* Returns the default vertex attributes used for creating new JGraph
* vertices.
*
* @return the default vertex attributes used for creating new JGraph
* vertices.
*/
public AttributeMap getDefaultVertexAttributes( ) {
return m_defaultVertexAttributes;
}
/**
* Returns the JGraph edge cell that corresponds to the specified JGraphT
* edge. If no corresponding cell found, returns null
.
*
* @param jGraphTEdge a JGraphT edge of the JGraphT graph.
*
* @return the JGraph edge cell that corresponds to the specified JGraphT
* edge, or null
if no corresponding cell found.
*/
public DefaultEdge getEdgeCell( org._3pq.jgrapht.Edge jGraphTEdge ) {
return (DefaultEdge) m_edgeToCell.get( jGraphTEdge );
}
/**
* Returns the JGraph vertex cell that corresponds to the specified JGraphT
* vertex. If no corresponding cell found, returns null
.
*
* @param jGraphTVertex a JGraphT vertex of the JGraphT graph.
*
* @return the JGraph vertex cell that corresponds to the specified JGraphT
* vertex, or null
if no corresponding cell found.
*/
public DefaultGraphCell getVertexCell( Object jGraphTVertex ) {
return (DefaultGraphCell) m_vertexToCell.get( jGraphTVertex );
}
/**
* Returns the JGraph port cell that corresponds to the specified JGraphT
* vertex. If no corresponding port found, returns null
.
*
* @param jGraphTVertex a JGraphT vertex of the JGraphT graph.
*
* @return the JGraph port cell that corresponds to the specified JGraphT
* vertex, or null
if no corresponding cell found.
*/
public DefaultPort getVertexPort( Object jGraphTVertex ) {
DefaultGraphCell vertexCell = getVertexCell( jGraphTVertex );
if( vertexCell == null ) {
return null;
}
else {
return (DefaultPort) vertexCell.getChildAt( 0 );
}
}
/**
* Applies the specified attributes to the model, as in the {@link
* DefaultGraphModel#edit(java.util.Map, org.jgraph.graph.ConnectionSet,
* org.jgraph.graph.ParentMap, javax.swing.undo.UndoableEdit[])} method.
*
* @param attrs the attributes to be applied to the model.
*
* @deprecated this method will be deleted in the future. Use
* DefaultGraphModel#edit instead.
*/
public void edit( Map attrs ) {
edit( attrs, null, null, null );
}
/**
* Adds/removes an edge to/from the underlying JGraphT graph according to
* the change in the specified JGraph edge. If both vertices are
* connected, we ensure to have a corresponding JGraphT edge. Otherwise,
* we ensure NOT to have a corresponding JGraphT edge.
*
* * This method is to be called only for edges that have already been * changed in the JGraph graph. *
* * @param jEdge the JGraph edge that has changed. */ void handleJGraphChangedEdge( org.jgraph.graph.Edge jEdge ) { if( isDangling( jEdge ) ) { if( m_cellToEdge.containsKey( jEdge ) ) { // a non-dangling edge became dangling -- remove the JGraphT // edge by faking as if the edge is removed from the JGraph. // TODO: Consider keeping the JGraphT edges outside the graph // to avoid loosing user data, such as weights. handleJGraphRemovedEdge( jEdge ); } else { // a dangling edge is still dangling -- just ignore. } } else { // edge is not dangling if( m_cellToEdge.containsKey( jEdge ) ) { // edge already has a corresponding JGraphT edge. // check if any change to its endpoints. org._3pq.jgrapht.Edge jtEdge = (org._3pq.jgrapht.Edge) m_cellToEdge.get( jEdge ); Object jSource = getSourceVertex( this, jEdge ); Object jTarget = getTargetVertex( this, jEdge ); Object jtSource = m_cellToVertex.get( jSource ); Object jtTarget = m_cellToVertex.get( jTarget ); if( jtEdge.getSource( ) == jtSource && jtEdge.getTarget( ) == jtTarget ) { // no change in edge's endpoints -- nothing to do. } else { // edge's end-points have changed -- need to refresh the // JGraphT edge. Refresh by faking as if the edge has been // removed from JGraph and then added again. // ALSO HERE: consider an alternative that maintains user data handleJGraphRemovedEdge( jEdge ); handleJGraphInsertedEdge( jEdge ); } } else { // a new edge handleJGraphInsertedEdge( jEdge ); } } } /** * Adds to the underlying JGraphT graph an edge that corresponds to the * specified JGraph edge. If the specified JGraph edge is a dangling edge, * it is NOT added to the underlying JGraphT graph. * ** This method is to be called only for edges that have already been added * to the JGraph graph. *
* * @param jEdge the JGraph edge that has been added. */ void handleJGraphInsertedEdge( org.jgraph.graph.Edge jEdge ) { if( isDangling( jEdge ) ) { // JGraphT forbid dangling edges so we cannot add the edge yet. // If later the edge becomes connected, we will add it. } else { Object jSource = getSourceVertex( this, jEdge ); Object jTarget = getTargetVertex( this, jEdge ); Object jtSource = m_cellToVertex.get( jSource ); Object jtTarget = m_cellToVertex.get( jTarget ); org._3pq.jgrapht.Edge jtEdge = m_jtGraph.getEdgeFactory( ).createEdge( jtSource, jtTarget ); boolean added = m_jtGraph.addEdge( jtEdge ); if( added ) { m_cellToEdge.put( jEdge, jtEdge ); m_edgeToCell.put( jtEdge, jEdge ); } else { // Adding failed because user is using a JGraphT graph the // forbids parallel edges. // For consistency, we remove the edge from the JGraph too. internalRemoveCell( jEdge ); System.err.println( "Warning: an edge was deleted because the underlying " + "JGraphT graph refused to create it. " + "This situation can happen when a constraint of the " + "underlying graph is violated, e.g., an attempt to add " + "a parallel edge or a self-loop to a graph that forbids " + "them. To avoid this message, make sure to use a " + "suitable underlying JGraphT graph." ); } } } /** * Adds to the underlying JGraphT graph a vertex corresponding to the * specified JGraph vertex. In JGraph, two vertices with the same user * object are in principle allowed; in JGraphT, this would lead to * duplicate vertices, which is not allowed. So if such vertex already * exists, the specified vertex is REMOVED from the JGraph graph and a a * warning is printed. * ** This method is to be called only for vertices that have already been * added to the JGraph graph. *
* * @param jVertex the JGraph vertex that has been added. */ void handleJGraphInsertedVertex( GraphCell jVertex ) { Object jtVertex; if( jVertex instanceof DefaultGraphCell ) { jtVertex = ( (DefaultGraphCell) jVertex ).getUserObject( ); } else { // FIXME: Why toString? Explain if for a good reason otherwise fix. jtVertex = jVertex.toString( ); } if( m_vertexToCell.containsKey( jtVertex ) ) { // We have to remove the new vertex, because it would lead to // duplicate vertices. We can't use ShieldedGraph.removeVertex for // that, because it would remove the wrong (existing) vertex. System.err.println( "Warning: detected two JGraph vertices with " + "the same JGraphT vertex as user object. It is an " + "indication for a faulty situation that should NOT happen." + "Removing vertex: " + jVertex ); internalRemoveCell( jVertex ); } else { m_jtGraph.addVertex( jtVertex ); m_cellToVertex.put( jVertex, jtVertex ); m_vertexToCell.put( jtVertex, jVertex ); } } /** * Removes the edge corresponding to the specified JGraph edge from the * JGraphT graph. If the specified edge is not contained in {@link * #m_cellToEdge}, it is silently ignored. * ** This method is to be called only for edges that have already been * removed from the JGraph graph. *
* * @param jEdge the JGraph edge that has been removed. */ void handleJGraphRemovedEdge( org.jgraph.graph.Edge jEdge ) { if( m_cellToEdge.containsKey( jEdge ) ) { org._3pq.jgrapht.Edge jtEdge = (org._3pq.jgrapht.Edge) m_cellToEdge.get( jEdge ); m_jtGraph.removeEdge( jtEdge ); m_cellToEdge.remove( jEdge ); m_edgeToCell.remove( jtEdge ); } } /** * Removes the vertex corresponding to the specified JGraph vertex from the * JGraphT graph. If the specified vertex is not contained in {@link * #m_cellToVertex}, it is silently ignored. * ** If any edges are incident with this vertex, we first remove them from * the both graphs, because otherwise the JGraph graph would leave them * intact and the JGraphT graph would throw them out. TODO: Revise this * behavior now that we gracefully tolerate dangling edges. It might be * possible to remove just the JGraphT edges. The JGraph edges will be * left dangling, as a result. *
* ** This method is to be called only for vertices that have already been * removed from the JGraph graph. *
* * @param jVertex the JGraph vertex that has been removed. */ void handleJGraphRemovedVertex( GraphCell jVertex ) { if( m_cellToVertex.containsKey( jVertex ) ) { Object jtVertex = m_cellToVertex.get( jVertex ); List jtIncidentEdges = m_jtGraph.edgesOf( jtVertex ); if( !jtIncidentEdges.isEmpty( ) ) { // We can't just call removeAllEdges with this list: that // would throw a ConcurrentModificationException. So we create // a shallow copy. // This also triggers removal of the corresponding JGraph edges. m_jtGraph.removeAllEdges( new ArrayList( jtIncidentEdges ) ); } m_jtGraph.removeVertex( jtVertex ); m_cellToVertex.remove( jVertex ); m_vertexToCell.remove( jtVertex ); } } /** * Adds the specified JGraphT edge to be reflected by this graph model. To * be called only for edges that already exist in the JGraphT graph. * * @param jtEdge a JGraphT edge to be reflected by this graph model. */ void handleJGraphTAddedEdge( org._3pq.jgrapht.Edge jtEdge ) { DefaultEdge edgeCell = m_cellFactory.createEdgeCell( jtEdge ); m_edgeToCell.put( jtEdge, edgeCell ); m_cellToEdge.put( edgeCell, jtEdge ); ConnectionSet cs = new ConnectionSet( ); cs.connect( edgeCell, getVertexPort( jtEdge.getSource( ) ), getVertexPort( jtEdge.getTarget( ) ) ); internalInsertCell( edgeCell, createEdgeAttributeMap( edgeCell ), cs ); } /** * Adds the specified JGraphT vertex to be reflected by this graph model. * To be called only for edges that already exist in the JGraphT graph. * * @param jtVertex a JGraphT vertex to be reflected by this graph model. */ void handleJGraphTAddedVertex( Object jtVertex ) { DefaultGraphCell vertexCell = m_cellFactory.createVertexCell( jtVertex ); vertexCell.add( new DefaultPort( ) ); m_vertexToCell.put( jtVertex, vertexCell ); m_cellToVertex.put( vertexCell, jtVertex ); internalInsertCell( vertexCell, createVertexAttributeMap( vertexCell ), null ); } /** * Removes the specified JGraphT vertex from being reflected by this graph * model. To be called only for vertices that have already been removed * from the JGraphT graph. * * @param jtVertex a JGraphT vertex to be removed from being reflected by * this graph model. */ void handleJGraphTRemoveVertex( Object jtVertex ) { DefaultGraphCell vertexCell = (DefaultGraphCell) m_vertexToCell.remove( jtVertex ); m_cellToVertex.remove( vertexCell ); internalRemoveCell( vertexCell ); // FIXME: Why remove childAt(0)? Explain if correct, otherwise fix. if( vertexCell.getChildCount( ) > 0 ) { remove( new Object[] { vertexCell.getChildAt( 0 ) } ); } } /** * Removes the specified JGraphT edge from being reflected by this graph * model. To be called only for edges that have already been removed from * the JGraphT graph. * * @param jtEdge a JGraphT edge to be removed from being reflected by this * graph model. */ void handleJGraphTRemovedEdge( org._3pq.jgrapht.Edge jtEdge ) { DefaultEdge edgeCell = (DefaultEdge) m_edgeToCell.remove( jtEdge ); m_cellToEdge.remove( edgeCell ); internalRemoveCell( edgeCell ); } /** * Tests if the specified JGraph edge is 'dangling', that is having at * least one endpoint which is not connected to a vertex. * * @param jEdge the JGraph edge to be tested for being dangling. * * @returntrue
if the specified edge is dangling, otherwise
* false
.
*/
private boolean isDangling( org.jgraph.graph.Edge jEdge ) {
Object jSource = getSourceVertex( this, jEdge );
Object jTarget = getTargetVertex( this, jEdge );
return !m_cellToVertex.containsKey( jSource )
|| !m_cellToVertex.containsKey( jTarget );
}
private AttributeMap createEdgeAttributeMap( DefaultEdge edgeCell ) {
AttributeMap attrs = new AttributeMap( );
attrs.put( edgeCell, getDefaultEdgeAttributes( ).clone( ) );
return attrs;
}
private AttributeMap createVertexAttributeMap( GraphCell vertexCell ) {
AttributeMap attrs = new AttributeMap( );
attrs.put( vertexCell, getDefaultVertexAttributes( ).clone( ) );
return attrs;
}
/**
* Inserts the specified cell into the JGraph graph model.
*
* @param cell
* @param attrs
* @param cs
*/
private void internalInsertCell( GraphCell cell, AttributeMap attrs,
ConnectionSet cs ) {
m_jCellsBeingAdded.add( cell );
insert( new Object[] { cell }, attrs, cs, null, null );
m_jCellsBeingAdded.remove( cell );
}
/**
* Removed the specified cell from the JGraph graph model.
*
* @param cell
*/
private void internalRemoveCell( GraphCell cell ) {
m_jCellsBeingRemoved.add( cell );
remove( new Object[] { cell } );
m_jCellsBeingRemoved.remove( cell );
}
/**
* Creates the JGraph cells that reflect the respective JGraphT elements.
*
* @author Barak Naveh
*
* @since Dec 12, 2003
*/
public static interface CellFactory {
/**
* Creates an edge cell that contains its respective JGraphT edge.
*
* @param jGraphTEdge a JGraphT edge to be contained.
*
* @return an edge cell that contains its respective JGraphT edge.
*/
public DefaultEdge createEdgeCell( org._3pq.jgrapht.Edge jGraphTEdge );
/**
* Creates a vertex cell that contains its respective JGraphT vertex.
*
* @param jGraphTVertex a JGraphT vertex to be contained.
*
* @return a vertex cell that contains its respective JGraphT vertex.
*/
public DefaultGraphCell createVertexCell( Object jGraphTVertex );
}
/**
* A simple default cell factory.
*
* @author Barak Naveh
*
* @since Dec 12, 2003
*/
public static class DefaultCellFactory implements CellFactory, Serializable {
private static final long serialVersionUID = 3690194343461861173L;
/**
* @see org._3pq.jgrapht.ext.JGraphModelAdapter.CellFactory#createEdgeCell(org._3pq.jgrapht.Edge)
*/
public DefaultEdge createEdgeCell( org._3pq.jgrapht.Edge jGraphTEdge ) {
return new DefaultEdge( jGraphTEdge );
}
/**
* @see org._3pq.jgrapht.ext.JGraphModelAdapter.CellFactory#createVertexCell(Object)
*/
public DefaultGraphCell createVertexCell( Object jGraphTVertex ) {
return new DefaultGraphCell( jGraphTVertex );
}
}
/**
* * Inner class listening to the GraphModel. If something is changed in the * GraphModel, this Listener gets notified and propagates the change back * to the JGraphT graph, if it didn't originate there. *
* ** If this change contains changes that would make this an illegal JGraphT * graph, like adding an edge that is incident with only one vertex, the * illegal parts of the change are undone. *
*/ private class JGraphListener implements GraphModelListener, Serializable { private static final long serialVersionUID = 3544673988098865209L; /** * This method is called for all JGraph changes. * * @param e */ public void graphChanged( GraphModelEvent e ) { // We first remove edges that have to be removed, then we // remove vertices, then we add vertices and finally we add // edges. Otherwise, things might go wrong: for example, if we // would first remove vertices and then edges, removal of the // vertices might induce 'automatic' removal of edges. If we // later attempt to re-remove these edges, we get confused. GraphModelChange change = e.getChange( ); Object[] removedCells = change.getRemoved( ); if( removedCells != null ) { handleRemovedEdges( filterEdges( removedCells ) ); handleRemovedVertices( filterVertices( removedCells ) ); } Object[] insertedCells = change.getInserted( ); if( insertedCells != null ) { handleInsertedVertices( filterVertices( insertedCells ) ); handleInsertedEdges( filterEdges( insertedCells ) ); } // Now handle edges that became 'dangling' or became connected. Object[] changedCells = change.getChanged( ); if( changedCells != null ) { handleChangedEdges( filterEdges( changedCells ) ); } } /** * Filters a list of edges out of an array of JGraph GraphCell objects. * Other objects are thrown away. * * @param cells Array of cells to be filtered. * * @return a list of edges. */ private List filterEdges( Object[] cells ) { List jEdges = new ArrayList( ); for( int i = 0; i < cells.length; i++ ) { if( cells[ i ] instanceof org.jgraph.graph.Edge ) { jEdges.add( cells[ i ] ); } } return jEdges; } /** * Filters a list of vertices out of an array of JGraph GraphCell * objects. Other objects are thrown away. * * @param cells Array of cells to be filtered. * * @return a list of vertices. */ private List filterVertices( Object[] cells ) { List jVertices = new ArrayList( ); for( int i = 0; i < cells.length; i++ ) { Object cell = cells[ i ]; if( cell instanceof org.jgraph.graph.Edge ) { // ignore -- we don't care about edges. } else if( cell instanceof Port ) { // ignore -- we don't care about ports. } else if( cell instanceof DefaultGraphCell ) { DefaultGraphCell graphCell = (DefaultGraphCell) cell; // If a DefaultGraphCell has a Port as a child, it is a vertex. // Note: do not change the order of following conditions; // the code uses the short-circuit evaluation of ||. if( graphCell.isLeaf( ) || graphCell.getFirstChild( ) instanceof Port ) { jVertices.add( cell ); } } else if( cell instanceof GraphCell ) { // If it is not a DefaultGraphCell, it doesn't have // children. jVertices.add( cell ); } } return jVertices; } private void handleChangedEdges( List jEdges ) { for( Iterator i = jEdges.iterator( ); i.hasNext( ); ) { org.jgraph.graph.Edge jEdge = (org.jgraph.graph.Edge) i.next( ); handleJGraphChangedEdge( jEdge ); } } private void handleInsertedEdges( List jEdges ) { for( Iterator i = jEdges.iterator( ); i.hasNext( ); ) { org.jgraph.graph.Edge jEdge = (org.jgraph.graph.Edge) i.next( ); if( !m_jCellsBeingAdded.remove( jEdge ) ) { handleJGraphInsertedEdge( jEdge ); } } } private void handleInsertedVertices( List jVertices ) { for( Iterator i = jVertices.iterator( ); i.hasNext( ); ) { GraphCell jVertex = (GraphCell) i.next( ); if( !m_jCellsBeingAdded.remove( jVertex ) ) { handleJGraphInsertedVertex( jVertex ); } } } private void handleRemovedEdges( List jEdges ) { for( Iterator i = jEdges.iterator( ); i.hasNext( ); ) { org.jgraph.graph.Edge jEdge = (org.jgraph.graph.Edge) i.next( ); if( !m_jCellsBeingRemoved.remove( jEdge ) ) { handleJGraphRemovedEdge( jEdge ); } } } private void handleRemovedVertices( List jVertices ) { for( Iterator i = jVertices.iterator( ); i.hasNext( ); ) { GraphCell jVertex = (GraphCell) i.next( ); if( !m_jCellsBeingRemoved.remove( jVertex ) ) { handleJGraphRemovedVertex( jVertex ); } } } } /** * A listener on the underlying JGraphT graph. This listener is used to * keep the JGraph model in sync. Whenever one of the event handlers is * called, it first checks whether the change is due to a previous change * in the JGraph model. If it is, then no action is taken. * * @author Barak Naveh * * @since Aug 2, 2003 */ private class JGraphTListener implements GraphListener, Serializable { private static final long serialVersionUID = 3616724963609360440L; /** * @see GraphListener#edgeAdded(GraphEdgeChangeEvent) */ public void edgeAdded( GraphEdgeChangeEvent e ) { org._3pq.jgrapht.Edge jtEdge = e.getEdge( ); if( !m_jtElementsBeingAdded.remove( jtEdge ) ) { handleJGraphTAddedEdge( jtEdge ); } } /** * @see GraphListener#edgeRemoved(GraphEdgeChangeEvent) */ public void edgeRemoved( GraphEdgeChangeEvent e ) { org._3pq.jgrapht.Edge jtEdge = e.getEdge( ); if( !m_jtElementsBeingRemoved.remove( jtEdge ) ) { handleJGraphTRemovedEdge( jtEdge ); } } /** * @see org._3pq.jgrapht.event.VertexSetListener#vertexAdded(GraphVertexChangeEvent) */ public void vertexAdded( GraphVertexChangeEvent e ) { Object jtVertex = e.getVertex( ); if( !m_jtElementsBeingAdded.remove( jtVertex ) ) { handleJGraphTAddedVertex( jtVertex ); } } /** * @see org._3pq.jgrapht.event.VertexSetListener#vertexRemoved(GraphVertexChangeEvent) */ public void vertexRemoved( GraphVertexChangeEvent e ) { Object jtVertex = e.getVertex( ); if( !m_jtElementsBeingRemoved.remove( jtVertex ) ) { handleJGraphTRemoveVertex( jtVertex ); } } } /** * A wrapper around a JGraphT graph that ensures a few atomic operations. */ private class ShieldedGraph { private final Graph m_graph; ShieldedGraph( Graph graph ) { m_graph = graph; } EdgeFactory getEdgeFactory( ) { return m_graph.getEdgeFactory( ); } boolean addEdge( org._3pq.jgrapht.Edge jtEdge ) { m_jtElementsBeingAdded.add( jtEdge ); boolean added = m_graph.addEdge( jtEdge ); m_jtElementsBeingAdded.remove( jtEdge ); return added; } void addVertex( Object jtVertex ) { m_jtElementsBeingAdded.add( jtVertex ); m_graph.addVertex( jtVertex ); m_jtElementsBeingAdded.remove( jtVertex ); } List edgesOf( Object vertex ) { return m_graph.edgesOf( vertex ); } boolean removeAllEdges( Collection edges ) { return m_graph.removeAllEdges( edges ); } void removeEdge( org._3pq.jgrapht.Edge jtEdge ) { m_jtElementsBeingRemoved.add( jtEdge ); m_graph.removeEdge( jtEdge ); m_jtElementsBeingRemoved.remove( jtEdge ); } void removeVertex( Object jtVertex ) { m_jtElementsBeingRemoved.add( jtVertex ); m_graph.removeVertex( jtVertex ); m_jtElementsBeingRemoved.remove( jtVertex ); } } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/ext/VisioExporter.java 0000644 0001751 0001751 00000011275 10266566752 025061 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------ * VisioExporter.java * ------------------ * (C) Copyright 2003, by Avner Linder and Contributors. * * Original Author: Avner Linder * Contributor(s): Barak Naveh * * $Id: VisioExporter.java,v 1.2 2004/05/31 19:47:36 barak_naveh Exp $ * * Changes * ------- * 27-May-2004 : Initial Version (AL); * */ package org._3pq.jgrapht.ext; import java.io.OutputStream; import java.io.PrintStream; import java.util.Iterator; import org._3pq.jgrapht.Edge; import org._3pq.jgrapht.Graph; /** * Exports a graph to a csv format that can be imported into MS Visio. * *
* Tip: By default, the exported graph doesn't show link directions. To
* show link directions:
*
*
Extensions and integration means to other products.
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/generate/ 0000755 0001751 0001751 00000000000 11251311101 022323 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/generate/EmptyGraphGenerator.java 0000644 0001751 0001751 00000004670 10266566752 027161 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------- * GraphGenerator.java * ------------------- * (C) Copyright 2003, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: EmptyGraphGenerator.java,v 1.3 2004/11/18 22:07:52 barak_naveh Exp $ * * Changes * ------- * 16-Sep-2003 : Initial revision (JVS); * */ package org._3pq.jgrapht.generate; import java.util.Map; import org._3pq.jgrapht.Graph; import org._3pq.jgrapht.VertexFactory; /** * Generates an empty * graph of any size. An empty graph is a graph that has no edges. * * @author John V. Sichi * * @since Sep 16, 2003 */ public class EmptyGraphGenerator implements GraphGenerator { private int m_size; /** * Construct a new EmptyGraphGenerator. * * @param size number of vertices to be generated * * @throws IllegalArgumentException if the specified size is negative. */ public EmptyGraphGenerator( int size ) { if( size < 0 ) { throw new IllegalArgumentException( "must be non-negative" ); } m_size = size; } /** * {@inheritDoc} */ public void generateGraph( Graph target, VertexFactory vertexFactory, Map resultMap ) { for( int i = 0; i < m_size; ++i ) { target.addVertex( vertexFactory.createVertex( ) ); } } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/generate/GraphGenerator.java 0000644 0001751 0001751 00000005500 10266566752 026133 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------- * GraphGenerator.java * ------------------- * (C) Copyright 2003, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: GraphGenerator.java,v 1.2 2004/11/18 22:02:57 barak_naveh Exp $ * * Changes * ------- * 16-Sep-2003 : Initial revision (JVS); * */ package org._3pq.jgrapht.generate; import java.util.Map; import org._3pq.jgrapht.Graph; import org._3pq.jgrapht.VertexFactory; /** * GraphGenerator defines an interface for generating new graph structures. * * @author John V. Sichi * * @since Sep 16, 2003 */ public interface GraphGenerator { /** * Generate a graph structure. The topology of the generated graph is * dependent on the implementation. For graphs in which not all vertices * share the same automorphism equivalence class, the generator may * produce a labeling indicating the roles played by generated elements. * This is the purpose of the resultMap parameter. For example, a * generator for a wheel graph would designate a hub vertex. Role names * used as keys in resultMap should be declared as public static final * Strings by implementation classes. * * @param target receives the generated edges and vertices; if this is * non-empty on entry, the result will be a disconnected graph * since generated elements will not be connected to existing * elements * @param vertexFactory called to produce new vertices * @param resultMap if non-null, receives implementation-specific mappings * from String roles to graph elements (or collections of graph * elements) */ public void generateGraph( Graph target, VertexFactory vertexFactory, Map resultMap ); } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/generate/LinearGraphGenerator.java 0000644 0001751 0001751 00000006206 10266566752 027272 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------- * GraphGenerator.java * ------------------- * (C) Copyright 2003, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: LinearGraphGenerator.java,v 1.4 2005/04/23 08:09:29 perfecthash Exp $ * * Changes * ------- * 16-Sep-2003 : Initial revision (JVS); * */ package org._3pq.jgrapht.generate; import java.util.Map; import org._3pq.jgrapht.Graph; import org._3pq.jgrapht.VertexFactory; /** * Generates a linear graph of any size. For a directed graph, the edges are * oriented from START_VERTEX to END_VERTEX. * * @author John V. Sichi * * @since Sep 16, 2003 */ public class LinearGraphGenerator implements GraphGenerator { /** Role for the first vertex generated. */ public static final String START_VERTEX = "Start Vertex"; /** Role for the last vertex generated. */ public static final String END_VERTEX = "End Vertex"; private int m_size; /** * Construct a new LinearGraphGenerator. * * @param size number of vertices to be generated * * @throws IllegalArgumentException if the specified size is negative. */ public LinearGraphGenerator( int size ) { if( size < 0 ) { throw new IllegalArgumentException( "must be non-negative" ); } m_size = size; } /** * {@inheritDoc} */ public void generateGraph( Graph target, VertexFactory vertexFactory, Map resultMap ) { Object lastVertex = null; for( int i = 0; i < m_size; ++i ) { Object newVertex = vertexFactory.createVertex( ); target.addVertex( newVertex ); if( lastVertex == null ) { if( resultMap != null ) { resultMap.put( START_VERTEX, newVertex ); } } else { target.addEdge( lastVertex, newVertex ); } lastVertex = newVertex; } if( ( resultMap != null ) && ( lastVertex != null ) ) { resultMap.put( END_VERTEX, lastVertex ); } } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/generate/RingGraphGenerator.java 0000644 0001751 0001751 00000005660 10266566752 026762 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------- * GraphGenerator.java * ------------------- * (C) Copyright 2003, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: RingGraphGenerator.java,v 1.2 2004/11/18 22:09:37 barak_naveh Exp $ * * Changes * ------- * 16-Sep-2003 : Initial revision (JVS); * */ package org._3pq.jgrapht.generate; import java.util.HashMap; import java.util.Map; import org._3pq.jgrapht.Graph; import org._3pq.jgrapht.VertexFactory; /** * Generates a ring graph of any size. A ring graph is a graph that contains a * single cycle that passes through all its vertices exactly once. For a * directed graph, the generated edges are oriented consistently around the * ring. * * @author John V. Sichi * * @since Sep 16, 2003 */ public class RingGraphGenerator implements GraphGenerator { private int m_size; /** * Construct a new RingGraphGenerator. * * @param size number of vertices to be generated * * @throws IllegalArgumentException if the specified size is negative. */ public RingGraphGenerator( int size ) { if( size < 0 ) { throw new IllegalArgumentException( "must be non-negative" ); } m_size = size; } /** * {@inheritDoc} */ public void generateGraph( Graph target, VertexFactory vertexFactory, Map resultMap ) { if( m_size < 1 ) { return; } LinearGraphGenerator linearGenerator = new LinearGraphGenerator( m_size ); Map privateMap = new HashMap( ); linearGenerator.generateGraph( target, vertexFactory, privateMap ); Object startVertex = privateMap.get( LinearGraphGenerator.START_VERTEX ); Object endVertex = privateMap.get( LinearGraphGenerator.END_VERTEX ); target.addEdge( endVertex, startVertex ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/generate/WheelGraphGenerator.java 0000644 0001751 0001751 00000011163 10266566752 027122 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------- * GraphGenerator.java * ------------------- * (C) Copyright 2003, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: WheelGraphGenerator.java,v 1.3 2004/11/18 22:10:06 barak_naveh Exp $ * * Changes * ------- * 16-Sep-2003 : Initial revision (JVS); * */ package org._3pq.jgrapht.generate; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Map; import org._3pq.jgrapht.Graph; import org._3pq.jgrapht.VertexFactory; /** * Generates a wheel * graph of any size. Reminding a bicycle wheel, a wheel graph has a hub * vertex in the center and a rim of vertices around it that are connected to * each other (as a ring). The rim vertices are also connected to the hub with * edges that are called "spokes". * * @author John V. Sichi * * @since Sep 16, 2003 */ public class WheelGraphGenerator implements GraphGenerator { /** Role for the hub vertex. */ public static final String HUB_VERTEX = "Hub Vertex"; private boolean m_inwardSpokes; private int m_size; /** * Creates a new WheelGraphGenerator object. This constructor is more * suitable for undirected graphs, where spokes' direction is meaningless. * In the directed case, spokes will be oriented from rim to hub. * * @param size number of vertices to be generated. */ public WheelGraphGenerator( int size ) { this( size, true ); } /** * Construct a new WheelGraphGenerator. * * @param size number of vertices to be generated. * @param inwardSpokes iftrue
and graph is directed, spokes
* are oriented from rim to hub; else from hub to rim.
*
* @throws IllegalArgumentException
*/
public WheelGraphGenerator( int size, boolean inwardSpokes ) {
if( size < 0 ) {
throw new IllegalArgumentException( "must be non-negative" );
}
m_size = size;
m_inwardSpokes = inwardSpokes;
}
/**
* {@inheritDoc}
*/
public void generateGraph( Graph target, final VertexFactory vertexFactory,
Map resultMap ) {
if( m_size < 1 ) {
return;
}
// A little trickery to intercept the rim generation. This is
// necessary since target may be initially non-empty, meaning we can't
// rely on its vertex set after the rim is generated.
final Collection rim = new ArrayList( );
VertexFactory rimVertexFactory =
new VertexFactory( ) {
public Object createVertex( ) {
Object vertex = vertexFactory.createVertex( );
rim.add( vertex );
return vertex;
}
};
RingGraphGenerator ringGenerator = new RingGraphGenerator( m_size - 1 );
ringGenerator.generateGraph( target, rimVertexFactory, resultMap );
Object hubVertex = vertexFactory.createVertex( );
target.addVertex( hubVertex );
if( resultMap != null ) {
resultMap.put( HUB_VERTEX, hubVertex );
}
Iterator rimIter = rim.iterator( );
while( rimIter.hasNext( ) ) {
Object rimVertex = rimIter.next( );
if( m_inwardSpokes ) {
target.addEdge( rimVertex, hubVertex );
}
else {
target.addEdge( hubVertex, rimVertex );
}
}
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/generate/package.html 0000644 0001751 0001751 00000000203 10266566752 024634 0 ustar moeller moeller
Generators for graphs of various topologies.
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/ 0000755 0001751 0001751 00000000000 11251311101 021632 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/AbstractBaseGraph.java 0000644 0001751 0001751 00000074474 10266566752 026072 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ----------------------
* AbstractBaseGraph.java
* ----------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): John V. Sichi
*
* $Id: AbstractBaseGraph.java,v 1.17 2005/06/02 03:25:17 perfecthash Exp $
*
* Changes
* -------
* 24-Jul-2003 : Initial revision (BN);
* 10-Aug-2003 : General edge refactoring (BN);
* 06-Nov-2003 : Change edge sharing semantics (JVS);
* 07-Feb-2004 : Enabled serialization (BN);
* 01-Jun-2005 : Added EdgeListFactory (JVS);
*
*/
package org._3pq.jgrapht.graph;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org._3pq.jgrapht.DirectedGraph;
import org._3pq.jgrapht.Edge;
import org._3pq.jgrapht.EdgeFactory;
import org._3pq.jgrapht.Graph;
import org._3pq.jgrapht.GraphHelper;
import org._3pq.jgrapht.UndirectedGraph;
/**
* The most general implementation of the {@link org._3pq.jgrapht.Graph}
* interface. Its subclasses add various restrictions to get more specific
* graphs. The decision whether it is directed or undirected is decided at
* construction time and cannot be later modified (see constructor for
* details).
*
* * This graph implementation guarantees deterministic vertex and edge set * ordering (via {@link LinkedHashMap} and {@link LinkedHashSet}). *
* * @author Barak Naveh * * @since Jul 24, 2003 */ public abstract class AbstractBaseGraph extends AbstractGraph implements Graph, Cloneable, Serializable { private static final String LOOPS_NOT_ALLOWED = "loops not allowed"; // friendly (to improve performance) Map m_vertexMap; boolean m_allowingLoops; // private private Class m_factoryEdgeClass; private EdgeFactory m_edgeFactory; private EdgeListFactory m_edgeListFactory; private Set m_edgeSet; private transient Set m_unmodifiableEdgeSet = null; private transient Set m_unmodifiableVertexSet = null; private Specifics m_specifics; private boolean m_allowingMultipleEdges; /** * Construct a new pseudograph. The pseudograph can either be directed or * undirected, depending on the specified edge factory. A sample edge is * created using the edge factory to see if the factory is compatible with * this class of graph. For example, if this graph is a *DirectedGraph
the edge factory must produce
* DirectedEdge
s. If this is not the case, an
* IllegalArgumentException
is thrown.
*
* @param ef the edge factory of the new graph.
* @param allowMultipleEdges whether to allow multiple edges or not.
* @param allowLoops whether to allow edges that are self-loops or not.
*
* @throws NullPointerException if the specified edge factory is
* null
.
*/
public AbstractBaseGraph( EdgeFactory ef, boolean allowMultipleEdges,
boolean allowLoops ) {
if( ef == null ) {
throw new NullPointerException( );
}
m_vertexMap = new LinkedHashMap( );
m_edgeSet = new LinkedHashSet( );
m_edgeFactory = ef;
m_allowingLoops = allowLoops;
m_allowingMultipleEdges = allowMultipleEdges;
m_specifics = createSpecifics( );
Edge e = ef.createEdge( new Object( ), new Object( ) );
m_factoryEdgeClass = e.getClass( );
m_edgeListFactory = new ArrayListFactory( );
}
/**
* @see Graph#getAllEdges(Object, Object)
*/
public List getAllEdges( Object sourceVertex, Object targetVertex ) {
return m_specifics.getAllEdges( sourceVertex, targetVertex );
}
/**
* Returns true
if and only if self-loops are allowed in this
* graph. A self loop is an edge that its source and target vertices are
* the same.
*
* @return true
if and only if graph loops are allowed.
*/
public boolean isAllowingLoops( ) {
return m_allowingLoops;
}
/**
* Returns true
if and only if multiple edges are allowed in
* this graph. The meaning of multiple edges is that there can be many
* edges going from vertex v1 to vertex v2.
*
* @return true
if and only if multiple edges are allowed.
*/
public boolean isAllowingMultipleEdges( ) {
return m_allowingMultipleEdges;
}
/**
* @see Graph#getEdge(Object, Object)
*/
public Edge getEdge( Object sourceVertex, Object targetVertex ) {
return m_specifics.getEdge( sourceVertex, targetVertex );
}
/**
* @see Graph#getEdgeFactory()
*/
public EdgeFactory getEdgeFactory( ) {
return m_edgeFactory;
}
/**
* Set the {@link EdgeListFactory} to use for this graph. Initially, a
* graph is created with a default implementation which always supplies an
* {@link java.util.ArrayList} with capacity 1.
*
* @param edgeListFactory factory to use for subsequently created edge
* lists (this call has no effect on existing edge lists)
*/
public void setEdgeListFactory( EdgeListFactory edgeListFactory ) {
m_edgeListFactory = edgeListFactory;
}
/**
* @see Graph#addEdge(Object, Object)
*/
public Edge addEdge( Object sourceVertex, Object targetVertex ) {
assertVertexExist( sourceVertex );
assertVertexExist( targetVertex );
if( !m_allowingMultipleEdges
&& containsEdge( sourceVertex, targetVertex ) ) {
return null;
}
if( !m_allowingLoops && sourceVertex.equals( targetVertex ) ) {
throw new IllegalArgumentException( LOOPS_NOT_ALLOWED );
}
Edge e = m_edgeFactory.createEdge( sourceVertex, targetVertex );
if( containsEdge( e ) ) { // this restriction should stay!
return null;
}
else {
m_edgeSet.add( e );
m_specifics.addEdgeToTouchingVertices( e );
return e;
}
}
/**
* @see Graph#addEdge(Edge)
*/
public boolean addEdge( Edge e ) {
if( e == null ) {
throw new NullPointerException( );
}
else if( containsEdge( e ) ) {
return false;
}
Object sourceVertex = e.getSource( );
Object targetVertex = e.getTarget( );
assertVertexExist( sourceVertex );
assertVertexExist( targetVertex );
assertCompatibleWithEdgeFactory( e );
if( !m_allowingMultipleEdges
&& containsEdge( sourceVertex, targetVertex ) ) {
return false;
}
if( !m_allowingLoops && sourceVertex.equals( targetVertex ) ) {
throw new IllegalArgumentException( LOOPS_NOT_ALLOWED );
}
m_edgeSet.add( e );
m_specifics.addEdgeToTouchingVertices( e );
return true;
}
/**
* @see Graph#addVertex(Object)
*/
public boolean addVertex( Object v ) {
if( v == null ) {
throw new NullPointerException( );
}
else if( containsVertex( v ) ) {
return false;
}
else {
m_vertexMap.put( v, null ); // add with a lazy edge container entry
return true;
}
}
/**
* Returns a shallow copy of this graph instance. Neither edges nor
* vertices are cloned.
*
* @return a shallow copy of this set.
*
* @throws RuntimeException
*
* @see java.lang.Object#clone()
*/
public Object clone( ) {
try {
AbstractBaseGraph newGraph = (AbstractBaseGraph) super.clone( );
newGraph.m_vertexMap = new LinkedHashMap( );
newGraph.m_edgeSet = new LinkedHashSet( );
newGraph.m_factoryEdgeClass = this.m_factoryEdgeClass;
newGraph.m_edgeFactory = this.m_edgeFactory;
newGraph.m_unmodifiableEdgeSet = null;
newGraph.m_unmodifiableVertexSet = null;
// NOTE: it's important for this to happen in an object
// method so that the new inner class instance gets associated with
// the right outer class instance
newGraph.m_specifics = newGraph.createSpecifics( );
GraphHelper.addGraph( newGraph, this );
return newGraph;
}
catch( CloneNotSupportedException e ) {
e.printStackTrace( );
throw new RuntimeException( );
}
}
/**
* @see Graph#containsEdge(Edge)
*/
public boolean containsEdge( Edge e ) {
return m_edgeSet.contains( e );
}
/**
* @see Graph#containsVertex(Object)
*/
public boolean containsVertex( Object v ) {
return m_vertexMap.containsKey( v );
}
/**
* @see org._3pq.jgrapht.UndirectedGraph#degreeOf(java.lang.Object)
*/
public int degreeOf( Object vertex ) {
return m_specifics.degreeOf( vertex );
}
/**
* @see Graph#edgeSet()
*/
public Set edgeSet( ) {
if( m_unmodifiableEdgeSet == null ) {
m_unmodifiableEdgeSet = Collections.unmodifiableSet( m_edgeSet );
}
return m_unmodifiableEdgeSet;
}
/**
* @see Graph#edgesOf(Object)
*/
public List edgesOf( Object vertex ) {
return m_specifics.edgesOf( vertex );
}
/**
* @see org._3pq.jgrapht.DirectedGraph#inDegreeOf(java.lang.Object)
*/
public int inDegreeOf( Object vertex ) {
return m_specifics.inDegreeOf( vertex );
}
/**
* @see org._3pq.jgrapht.DirectedGraph#incomingEdgesOf(java.lang.Object)
*/
public List incomingEdgesOf( Object vertex ) {
return m_specifics.incomingEdgesOf( vertex );
}
/**
* @see org._3pq.jgrapht.DirectedGraph#outDegreeOf(java.lang.Object)
*/
public int outDegreeOf( Object vertex ) {
return m_specifics.outDegreeOf( vertex );
}
/**
* @see org._3pq.jgrapht.DirectedGraph#outgoingEdgesOf(java.lang.Object)
*/
public List outgoingEdgesOf( Object vertex ) {
return m_specifics.outgoingEdgesOf( vertex );
}
/**
* @see Graph#removeEdge(Object, Object)
*/
public Edge removeEdge( Object sourceVertex, Object targetVertex ) {
Edge e = getEdge( sourceVertex, targetVertex );
if( e != null ) {
m_specifics.removeEdgeFromTouchingVertices( e );
m_edgeSet.remove( e );
}
return e;
}
/**
* @see Graph#removeEdge(Edge)
*/
public boolean removeEdge( Edge e ) {
if( containsEdge( e ) ) {
m_specifics.removeEdgeFromTouchingVertices( e );
m_edgeSet.remove( e );
return true;
}
else {
return false;
}
}
/**
* @see Graph#removeVertex(Object)
*/
public boolean removeVertex( Object v ) {
if( containsVertex( v ) ) {
List touchingEdgesList = edgesOf( v );
// cannot iterate over list - will cause ConcurrentModificationException
Edge[] touchingEdges = new Edge[ touchingEdgesList.size( ) ];
touchingEdgesList.toArray( touchingEdges );
removeAllEdges( touchingEdges );
m_vertexMap.remove( v ); // remove the vertex itself
return true;
}
else {
return false;
}
}
/**
* @see Graph#vertexSet()
*/
public Set vertexSet( ) {
if( m_unmodifiableVertexSet == null ) {
m_unmodifiableVertexSet =
Collections.unmodifiableSet( m_vertexMap.keySet( ) );
}
return m_unmodifiableVertexSet;
}
private boolean assertCompatibleWithEdgeFactory( Edge e ) {
if( e == null ) {
throw new NullPointerException( );
}
else if( !m_factoryEdgeClass.isInstance( e ) ) {
throw new ClassCastException( "incompatible edge class" );
}
return true;
}
private Specifics createSpecifics( ) {
if( this instanceof DirectedGraph ) {
return new DirectedSpecifics( );
}
else if( this instanceof UndirectedGraph ) {
return new UndirectedSpecifics( );
}
else {
throw new IllegalArgumentException(
"must be instance of either DirectedGraph or UndirectedGraph" );
}
}
/**
* .
*
* @author Barak Naveh
*/
private abstract class Specifics implements Serializable {
/**
* .
*
* @param sourceVertex
* @param targetVertex
*
* @return
*/
public abstract List getAllEdges( Object sourceVertex,
Object targetVertex );
/**
* .
*
* @param sourceVertex
* @param targetVertex
*
* @return
*/
public abstract Edge getEdge( Object sourceVertex, Object targetVertex );
/**
* Adds the specified edge to the edge containers of its source and
* target vertices.
*
* @param e
*/
public abstract void addEdgeToTouchingVertices( Edge e );
/**
* .
*
* @param vertex
*
* @return
*/
public abstract int degreeOf( Object vertex );
/**
* .
*
* @param vertex
*
* @return
*/
public abstract List edgesOf( Object vertex );
/**
* .
*
* @param vertex
*
* @return
*/
public abstract int inDegreeOf( Object vertex );
/**
* .
*
* @param vertex
*
* @return
*/
public abstract List incomingEdgesOf( Object vertex );
/**
* .
*
* @param vertex
*
* @return
*/
public abstract int outDegreeOf( Object vertex );
/**
* .
*
* @param vertex
*
* @return
*/
public abstract List outgoingEdgesOf( Object vertex );
/**
* Removes the specified edge from the edge containers of its source
* and target vertices.
*
* @param e
*/
public abstract void removeEdgeFromTouchingVertices( Edge e );
}
private static class ArrayListFactory implements EdgeListFactory {
/**
* @see EdgeListFactory.createEdgeList
*/
public List createEdgeList( Object vertex ) {
// NOTE: use size 1 to keep memory usage under control
// for the common case of vertices with low degree
return new ArrayList( 1 );
}
}
/**
* A container of for vertex edges.
*
* * In this edge container we use array lists to minimize memory toll. * However, for high-degree vertices we replace the entire edge container * with a direct access subclass (to be implemented). *
* * @author Barak Naveh */ private static class DirectedEdgeContainer implements Serializable { List m_incoming; List m_outgoing; private transient List m_unmodifiableIncoming = null; private transient List m_unmodifiableOutgoing = null; DirectedEdgeContainer( EdgeListFactory edgeListFactory, Object vertex ) { m_incoming = edgeListFactory.createEdgeList( vertex ); m_outgoing = edgeListFactory.createEdgeList( vertex ); } /** * A lazy build of unmodifiable incoming edge list. * * @return */ public List getUnmodifiableIncomingEdges( ) { if( m_unmodifiableIncoming == null ) { m_unmodifiableIncoming = Collections.unmodifiableList( m_incoming ); } return m_unmodifiableIncoming; } /** * A lazy build of unmodifiable outgoing edge list. * * @return */ public List getUnmodifiableOutgoingEdges( ) { if( m_unmodifiableOutgoing == null ) { m_unmodifiableOutgoing = Collections.unmodifiableList( m_outgoing ); } return m_unmodifiableOutgoing; } /** * . * * @param e */ public void addIncomingEdge( Edge e ) { m_incoming.add( e ); } /** * . * * @param e */ public void addOutgoingEdge( Edge e ) { m_outgoing.add( e ); } /** * . * * @param e */ public void removeIncomingEdge( Edge e ) { m_incoming.remove( e ); } /** * . * * @param e */ public void removeOutgoingEdge( Edge e ) { m_outgoing.remove( e ); } } /** * . * * @author Barak Naveh */ private class DirectedSpecifics extends Specifics { private static final String NOT_IN_DIRECTED_GRAPH = "no such operation in a directed graph"; /** * @see Graph#getAllEdges(Object, Object) */ public List getAllEdges( Object sourceVertex, Object targetVertex ) { List edges = null; if( containsVertex( sourceVertex ) && containsVertex( targetVertex ) ) { edges = new ArrayList( ); DirectedEdgeContainer ec = getEdgeContainer( sourceVertex ); Iterator iter = ec.m_outgoing.iterator( ); while( iter.hasNext( ) ) { Edge e = (Edge) iter.next( ); if( e.getTarget( ).equals( targetVertex ) ) { edges.add( e ); } } } return edges; } /** * @see Graph#getEdge(Object, Object) */ public Edge getEdge( Object sourceVertex, Object targetVertex ) { if( containsVertex( sourceVertex ) && containsVertex( targetVertex ) ) { DirectedEdgeContainer ec = getEdgeContainer( sourceVertex ); Iterator iter = ec.m_outgoing.iterator( ); while( iter.hasNext( ) ) { Edge e = (Edge) iter.next( ); if( e.getTarget( ).equals( targetVertex ) ) { return e; } } } return null; } /** * @see AbstractBaseGraph#addEdgeToTouchingVertices(Edge) */ public void addEdgeToTouchingVertices( Edge e ) { Object source = e.getSource( ); Object target = e.getTarget( ); getEdgeContainer( source ).addOutgoingEdge( e ); getEdgeContainer( target ).addIncomingEdge( e ); } /** * @see UndirectedGraph#degreeOf(Object) */ public int degreeOf( Object vertex ) { throw new UnsupportedOperationException( NOT_IN_DIRECTED_GRAPH ); } /** * @see Graph#edgesOf(Object) */ public List edgesOf( Object vertex ) { ArrayList inAndOut = new ArrayList( getEdgeContainer( vertex ).m_incoming ); inAndOut.addAll( getEdgeContainer( vertex ).m_outgoing ); // we have two copies for each self-loop - remove one of them. if( m_allowingLoops ) { List loops = getAllEdges( vertex, vertex ); for( int i = 0; i < inAndOut.size( ); ) { Object e = inAndOut.get( i ); if( loops.contains( e ) ) { inAndOut.remove( i ); loops.remove( e ); // so we remove it only once } else { i++; } } } return inAndOut; } /** * @see DirectedGraph#inDegree(Object) */ public int inDegreeOf( Object vertex ) { return getEdgeContainer( vertex ).m_incoming.size( ); } /** * @see DirectedGraph#incomingEdges(Object) */ public List incomingEdgesOf( Object vertex ) { return getEdgeContainer( vertex ).getUnmodifiableIncomingEdges( ); } /** * @see DirectedGraph#outDegree(Object) */ public int outDegreeOf( Object vertex ) { return getEdgeContainer( vertex ).m_outgoing.size( ); } /** * @see DirectedGraph#outgoingEdges(Object) */ public List outgoingEdgesOf( Object vertex ) { return getEdgeContainer( vertex ).getUnmodifiableOutgoingEdges( ); } /** * @see AbstractBaseGraph#removeEdgeFromTouchingVertices(Edge) */ public void removeEdgeFromTouchingVertices( Edge e ) { Object source = e.getSource( ); Object target = e.getTarget( ); getEdgeContainer( source ).removeOutgoingEdge( e ); getEdgeContainer( target ).removeIncomingEdge( e ); } /** * A lazy build of edge container for specified vertex. * * @param vertex a vertex in this graph. * * @return EdgeContainer */ private DirectedEdgeContainer getEdgeContainer( Object vertex ) { assertVertexExist( vertex ); DirectedEdgeContainer ec = (DirectedEdgeContainer) m_vertexMap.get( vertex ); if( ec == null ) { ec = new DirectedEdgeContainer( m_edgeListFactory, vertex ); m_vertexMap.put( vertex, ec ); } return ec; } } /** * A container of for vertex edges. * ** In this edge container we use array lists to minimize memory toll. * However, for high-degree vertices we replace the entire edge container * with a direct access subclass (to be implemented). *
* * @author Barak Naveh */ private static class UndirectedEdgeContainer implements Serializable { List m_vertexEdges; private transient List m_unmodifiableVertexEdges = null; UndirectedEdgeContainer( EdgeListFactory edgeListFactory, Object vertex ) { m_vertexEdges = edgeListFactory.createEdgeList( vertex ); } /** * A lazy build of unmodifiable list of vertex edges * * @return */ public List getUnmodifiableVertexEdges( ) { if( m_unmodifiableVertexEdges == null ) { m_unmodifiableVertexEdges = Collections.unmodifiableList( m_vertexEdges ); } return m_unmodifiableVertexEdges; } /** * . * * @param e */ public void addEdge( Edge e ) { m_vertexEdges.add( e ); } /** * . * * @return */ public int edgeCount( ) { return m_vertexEdges.size( ); } /** * . * * @param e */ public void removeEdge( Edge e ) { m_vertexEdges.remove( e ); } } /** * . * * @author Barak Naveh */ private class UndirectedSpecifics extends Specifics { private static final String NOT_IN_UNDIRECTED_GRAPH = "no such operation in an undirected graph"; /** * @see Graph#getAllEdges(Object, Object) */ public List getAllEdges( Object sourceVertex, Object targetVertex ) { List edges = null; if( containsVertex( sourceVertex ) && containsVertex( targetVertex ) ) { edges = new ArrayList( ); Iterator iter = getEdgeContainer( sourceVertex ).m_vertexEdges.iterator( ); while( iter.hasNext( ) ) { Edge e = (Edge) iter.next( ); boolean equalStraight = sourceVertex.equals( e.getSource( ) ) && targetVertex.equals( e.getTarget( ) ); boolean equalInverted = sourceVertex.equals( e.getTarget( ) ) && targetVertex.equals( e.getSource( ) ); if( equalStraight || equalInverted ) { edges.add( e ); } } } return edges; } /** * @see Graph#getEdge(Object, Object) */ public Edge getEdge( Object sourceVertex, Object targetVertex ) { if( containsVertex( sourceVertex ) && containsVertex( targetVertex ) ) { Iterator iter = getEdgeContainer( sourceVertex ).m_vertexEdges.iterator( ); while( iter.hasNext( ) ) { Edge e = (Edge) iter.next( ); boolean equalStraight = sourceVertex.equals( e.getSource( ) ) && targetVertex.equals( e.getTarget( ) ); boolean equalInverted = sourceVertex.equals( e.getTarget( ) ) && targetVertex.equals( e.getSource( ) ); if( equalStraight || equalInverted ) { return e; } } } return null; } /** * @see AbstractBaseGraph#addEdgeToTouchingVertices(Edge) */ public void addEdgeToTouchingVertices( Edge e ) { Object source = e.getSource( ); Object target = e.getTarget( ); getEdgeContainer( source ).addEdge( e ); if( source != target ) { getEdgeContainer( target ).addEdge( e ); } } /** * @see UndirectedGraph#degree(Object) */ public int degreeOf( Object vertex ) { if( m_allowingLoops ) { // then we must count, and add loops twice int degree = 0; List edges = getEdgeContainer( vertex ).m_vertexEdges; for( Iterator iter = edges.iterator( ); iter.hasNext( ); ) { Edge e = (Edge) iter.next( ); if( e.getSource( ).equals( e.getTarget( ) ) ) { degree += 2; } else { degree += 1; } } return degree; } else { return getEdgeContainer( vertex ).edgeCount( ); } } /** * @see Graph#edges(Object) */ public List edgesOf( Object vertex ) { return getEdgeContainer( vertex ).getUnmodifiableVertexEdges( ); } /** * @see DirectedGraph#inDegreeOf(Object) */ public int inDegreeOf( Object vertex ) { throw new UnsupportedOperationException( NOT_IN_UNDIRECTED_GRAPH ); } /** * @see DirectedGraph#incomingEdgesOf(Object) */ public List incomingEdgesOf( Object vertex ) { throw new UnsupportedOperationException( NOT_IN_UNDIRECTED_GRAPH ); } /** * @see DirectedGraph#outDegreeOf(Object) */ public int outDegreeOf( Object vertex ) { throw new UnsupportedOperationException( NOT_IN_UNDIRECTED_GRAPH ); } /** * @see DirectedGraph#outgoingEdgesOf(Object) */ public List outgoingEdgesOf( Object vertex ) { throw new UnsupportedOperationException( NOT_IN_UNDIRECTED_GRAPH ); } /** * @see AbstractBaseGraph#removeEdgeFromTouchingVertices(Edge) */ public void removeEdgeFromTouchingVertices( Edge e ) { Object source = e.getSource( ); Object target = e.getTarget( ); getEdgeContainer( source ).removeEdge( e ); if( source != target ) { getEdgeContainer( target ).removeEdge( e ); } } /** * A lazy build of edge container for specified vertex. * * @param vertex a vertex in this graph. * * @return EdgeContainer */ private UndirectedEdgeContainer getEdgeContainer( Object vertex ) { assertVertexExist( vertex ); UndirectedEdgeContainer ec = (UndirectedEdgeContainer) m_vertexMap.get( vertex ); if( ec == null ) { ec = new UndirectedEdgeContainer( m_edgeListFactory, vertex ); m_vertexMap.put( vertex, ec ); } return ec; } } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/AbstractGraph.java 0000644 0001751 0001751 00000014014 10266566752 025257 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------ * AbstractGraph.java * ------------------ * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: AbstractGraph.java,v 1.11 2005/04/23 08:09:29 perfecthash Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import java.util.Collection; import java.util.Iterator; import java.util.List; import org._3pq.jgrapht.Edge; import org._3pq.jgrapht.Graph; /** * A skeletal implementation of the Graph interface, to minimize the * effort required to implement graph interfaces. This implementation is * applicable to both: directed graphs and undirected graphs. * * @author Barak Naveh * * @see org._3pq.jgrapht.Graph * @see org._3pq.jgrapht.DirectedGraph * @see org._3pq.jgrapht.UndirectedGraph */ public abstract class AbstractGraph implements Graph { /** * Construct a new empty graph object. */ public AbstractGraph( ) {} /** * @see Graph#addAllEdges(Collection) */ public boolean addAllEdges( Collection edges ) { boolean modified = false; for( Iterator iter = edges.iterator( ); iter.hasNext( ); ) { modified |= addEdge( (Edge) iter.next( ) ); } return modified; } /** * @see Graph#addAllVertices(Collection) */ public boolean addAllVertices( Collection vertices ) { boolean modified = false; for( Iterator iter = vertices.iterator( ); iter.hasNext( ); ) { modified |= addVertex( iter.next( ) ); } return modified; } /** * @see Graph#containsEdge(Object, Object) */ public boolean containsEdge( Object sourceVertex, Object targetVertex ) { return getEdge( sourceVertex, targetVertex ) != null; } /** * @see Graph#removeAllEdges(Collection) */ public boolean removeAllEdges( Collection edges ) { boolean modified = false; for( Iterator iter = edges.iterator( ); iter.hasNext( ); ) { modified |= removeEdge( (Edge) iter.next( ) ); } return modified; } /** * @see Graph#removeAllEdges(Object, Object) */ public List removeAllEdges( Object sourceVertex, Object targetVertex ) { List removed = getAllEdges( sourceVertex, targetVertex ); removeAllEdges( removed ); return removed; } /** * @see Graph#removeAllVertices(Collection) */ public boolean removeAllVertices( Collection vertices ) { boolean modified = false; for( Iterator iter = vertices.iterator( ); iter.hasNext( ); ) { modified |= removeVertex( iter.next( ) ); } return modified; } /** * Returns a string of the parenthesized pair (V, E) representing this * G=(V,E) graph. 'V' is the string representation of the vertex set, and * 'E' is the string representation of the edge set. * * @return a string representation of this graph. */ public String toString( ) { return toStringFromSets( vertexSet( ), edgeSet( ) ); } /** * Ensures that the specified vertex exists in this graph, or else throws * exception. * * @param v vertex * * @returntrue
if this assertion holds.
*
* @throws NullPointerException if specified vertex is null
.
* @throws IllegalArgumentException if specified vertex does not exist in
* this graph.
*/
protected boolean assertVertexExist( Object v ) {
if( containsVertex( v ) ) {
return true;
}
else if( v == null ) {
throw new NullPointerException( );
}
else {
throw new IllegalArgumentException( "no such vertex in graph" );
}
}
/**
* Removes all the edges in this graph that are also contained in the
* specified edge array. After this call returns, this graph will contain
* no edges in common with the specified edges. This method will invoke
* the {@link Graph#removeEdge(Edge)} method.
*
* @param edges edges to be removed from this graph.
*
* @return true if this graph changed as a result of the call.
*
* @see Graph#removeEdge(Edge)
* @see Graph#containsEdge(Edge)
*/
protected boolean removeAllEdges( Edge[] edges ) {
boolean modified = false;
for( int i = 0; i < edges.length; i++ ) {
modified |= removeEdge( edges[ i ] );
}
return modified;
}
/**
* Helper for subclass implementations of toString( ).
*
* @param vertexSet the vertex set V to be printed
* @param edgeSet the edge set E to be printed
*
* @return a string representation of (V,E)
*/
protected String toStringFromSets( Collection vertexSet, Collection edgeSet ) {
return "(" + vertexSet.toString( ) + ", " + edgeSet.toString( ) + ")";
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/AsUndirectedGraph.java 0000644 0001751 0001751 00000015241 10266566752 026071 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ----------------------
* AsUndirectedGraph.java
* ----------------------
* (C) Copyright 2003, by John V. Sichi and Contributors.
*
* Original Author: John V. Sichi
* Contributor(s): -
*
* $Id: AsUndirectedGraph.java,v 1.6 2004/11/19 09:59:26 barak_naveh Exp $
*
* Changes
* -------
* 14-Aug-2003 : Initial revision (JVS);
*
*/
package org._3pq.jgrapht.graph;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org._3pq.jgrapht.DirectedGraph;
import org._3pq.jgrapht.Edge;
import org._3pq.jgrapht.UndirectedGraph;
import org._3pq.jgrapht.edge.UndirectedEdge;
/**
* An undirected view of the backing directed graph specified in the
* constructor. This graph allows modules to apply algorithms designed for
* undirected graphs to a directed graph by simply ignoring edge direction. If
* the backing directed graph is an oriented graph,
* then the view will be a simple graph; otherwise, it will be a multigraph.
* Query operations on this graph "read through" to the backing graph.
* Attempts to add edges will result in an
* UnsupportedOperationException
, but vertex addition/removal and
* edge removal are all supported (and immediately reflected in the backing
* graph).
*
* * Note that edges returned by this graph's accessors are really just the edges * of the underlying directed graph. Since there is no interface distinction * between directed and undirected edges, this detail should be irrelevant to * algorithms. *
* ** This graph does not pass the hashCode and equals operations through * to the backing graph, but relies on Object's equals and * hashCode methods. This graph will be serializable if the backing * graph is serializable. *
* * @author John V. Sichi * * @since Aug 14, 2003 */ public class AsUndirectedGraph extends GraphDelegator implements Serializable, UndirectedGraph { private static final long serialVersionUID = 3257845485078065462L; private static final String NO_EDGE_ADD = "this graph does not support edge addition"; private static final String UNDIRECTED = "this graph only supports undirected operations"; /** * Constructor for AsUndirectedGraph. * * @param g the backing directed graph over which an undirected view is to * be created. */ public AsUndirectedGraph( DirectedGraph g ) { super( g ); } /** * @see org._3pq.jgrapht.Graph#getAllEdges(Object, Object) */ public List getAllEdges( Object sourceVertex, Object targetVertex ) { List forwardList = super.getAllEdges( sourceVertex, targetVertex ); if( sourceVertex.equals( targetVertex ) ) { // avoid duplicating loops return forwardList; } List reverseList = super.getAllEdges( targetVertex, sourceVertex ); List list = new ArrayList( forwardList.size( ) + reverseList.size( ) ); list.addAll( forwardList ); list.addAll( reverseList ); return list; } /** * @see org._3pq.jgrapht.Graph#getEdge(Object, Object) */ public Edge getEdge( Object sourceVertex, Object targetVertex ) { Edge edge = super.getEdge( sourceVertex, targetVertex ); if( edge != null ) { return edge; } // try the other direction return super.getEdge( targetVertex, sourceVertex ); } /** * @see org._3pq.jgrapht.Graph#addAllEdges(Collection) */ public boolean addAllEdges( Collection edges ) { throw new UnsupportedOperationException( NO_EDGE_ADD ); } /** * @see org._3pq.jgrapht.Graph#addEdge(Edge) */ public boolean addEdge( Edge e ) { throw new UnsupportedOperationException( NO_EDGE_ADD ); } /** * @see org._3pq.jgrapht.Graph#addEdge(Object, Object) */ public Edge addEdge( Object sourceVertex, Object targetVertex ) { throw new UnsupportedOperationException( NO_EDGE_ADD ); } /** * @see UndirectedGraph#degreeOf(Object) */ public int degreeOf( Object vertex ) { // this counts loops twice, which is consistent with AbstractBaseGraph return super.inDegreeOf( vertex ) + super.outDegreeOf( vertex ); } /** * @see DirectedGraph#inDegreeOf(Object) */ public int inDegreeOf( Object vertex ) { throw new UnsupportedOperationException( UNDIRECTED ); } /** * @see DirectedGraph#incomingEdgesOf(Object) */ public List incomingEdgesOf( Object vertex ) { throw new UnsupportedOperationException( UNDIRECTED ); } /** * @see DirectedGraph#outDegreeOf(Object) */ public int outDegreeOf( Object vertex ) { throw new UnsupportedOperationException( UNDIRECTED ); } /** * @see DirectedGraph#outgoingEdgesOf(Object) */ public List outgoingEdgesOf( Object vertex ) { throw new UnsupportedOperationException( UNDIRECTED ); } /** * @see AbstractBaseGraph#toString() */ public String toString( ) { // take care to print edges using the undirected convention Collection edgeSet = new ArrayList( ); Iterator iter = edgeSet( ).iterator( ); while( iter.hasNext( ) ) { Edge edge = (Edge) iter.next( ); edgeSet.add( new UndirectedEdge( edge.getSource( ), edge.getTarget( ) ) ); } return super.toStringFromSets( vertexSet( ), edgeSet ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/DefaultDirectedGraph.java 0000644 0001751 0001751 00000004552 10266566752 026552 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------- * DefaultDirectedGraph.java * ------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: DefaultDirectedGraph.java,v 1.3 2004/11/19 10:11:33 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.EdgeFactory; import org._3pq.jgrapht.edge.EdgeFactories; /** * A directed graph. A directed graph is a non-simple directed graph in which * multiple edges between any two vertices are not permitted, but loops * are. * ** prefixed 'Default' to avoid name collision with the DirectedGraph interface. *
*/ public class DefaultDirectedGraph extends AbstractBaseGraph implements DirectedGraph { private static final long serialVersionUID = 3544953246956466230L; /** * Creates a new directed graph. */ public DefaultDirectedGraph( ) { this( new EdgeFactories.DirectedEdgeFactory( ) ); } /** * Creates a new directed graph with the specified edge factory. * * @param ef the edge factory of the new graph. */ public DefaultDirectedGraph( EdgeFactory ef ) { super( ef, false, true ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/DefaultDirectedWeightedGraph.java 0000644 0001751 0001751 00000005052 10266566752 030227 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------------------------- * DefaultDirectedWeightedGraph.java * --------------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: DefaultDirectedWeightedGraph.java,v 1.2 2004/11/19 10:13:34 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.EdgeFactory; import org._3pq.jgrapht.WeightedGraph; import org._3pq.jgrapht.edge.EdgeFactories; /** * A directed weighted graph. A directed weighted graph is a non-simple * directed graph in which multiple edges between any two vertices are * not permitted, but loops are. The graph has weights on its edges. * ** prefixed 'Default' to avoid name collision with the DirectedWeightedGraph * interface. *
* * @see org._3pq.jgrapht.graph.DefaultDirectedGraph */ public class DefaultDirectedWeightedGraph extends DefaultDirectedGraph implements WeightedGraph { private static final long serialVersionUID = 3761405317841171513L; /** * Creates a new directed weighted graph. */ public DefaultDirectedWeightedGraph( ) { this( new EdgeFactories.DirectedWeightedEdgeFactory( ) ); } /** * Creates a new directed weighted graph with the specified edge factory. * * @param ef the edge factory of the new graph. */ public DefaultDirectedWeightedGraph( EdgeFactory ef ) { super( ef ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/DefaultListenableGraph.java 0000644 0001751 0001751 00000033452 10266566752 027112 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------------------- * DefaultListenableGraph.java * --------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: DefaultListenableGraph.java,v 1.13 2005/04/23 08:09:29 perfecthash Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 04-Aug-2003 : Strong refs to listeners instead of weak refs (BN); * 10-Aug-2003 : Adaptation to new event model (BN); * 07-Mar-2004 : Fixed unnecessary clone bug #819075 (BN); * */ package org._3pq.jgrapht.graph; import java.util.ArrayList; import java.util.EventListener; import java.util.List; import org._3pq.jgrapht.Edge; import org._3pq.jgrapht.Graph; import org._3pq.jgrapht.ListenableGraph; import org._3pq.jgrapht.event.GraphEdgeChangeEvent; import org._3pq.jgrapht.event.GraphListener; import org._3pq.jgrapht.event.GraphVertexChangeEvent; import org._3pq.jgrapht.event.VertexSetListener; /** * A graph backed by the the graph specified at the constructor, which can be * listened byGraphListener
s and by
* VertexSetListener
s. Operations on this graph "pass through" to
* the to the backing graph. Any modification made to this graph or the
* backing graph is reflected by the other.
*
* * This graph does not pass the hashCode and equals operations through * to the backing graph, but relies on Object's equals and * hashCode methods. *
* * @author Barak Naveh * * @see GraphListener * @see VertexSetListener * @since Jul 20, 2003 */ public class DefaultListenableGraph extends GraphDelegator implements ListenableGraph, Cloneable { private static final long serialVersionUID = 3977575900898471984L; private ArrayList m_graphListeners = new ArrayList( ); private ArrayList m_vertexSetListeners = new ArrayList( ); private FlyweightEdgeEvent m_reuseableEdgeEvent; private FlyweightVertexEvent m_reuseableVertexEvent; private boolean m_reuseEvents; /** * Creates a new listenable graph. * * @param g the backing graph. */ public DefaultListenableGraph( Graph g ) { this( g, false ); } /** * Creates a new listenable graph. If thereuseEvents
flag is
* set to true
this class will reuse previously fired events
* and will not create a new object for each event. This option increases
* performance but should be used with care, especially in multithreaded
* environment.
*
* @param g the backing graph.
* @param reuseEvents whether to reuse previously fired event objects
* instead of creating a new event object for each event.
*
* @throws IllegalArgumentException if the backing graph is already a
* listenable graph.
*/
public DefaultListenableGraph( Graph g, boolean reuseEvents ) {
super( g );
m_reuseEvents = reuseEvents;
m_reuseableEdgeEvent = new FlyweightEdgeEvent( this, -1, null );
m_reuseableVertexEvent = new FlyweightVertexEvent( this, -1, null );
// the following restriction could be probably relaxed in the future.
if( g instanceof ListenableGraph ) {
throw new IllegalArgumentException(
"base graph cannot be listenable" );
}
}
/**
* If the reuseEvents
flag is set to true
this
* class will reuse previously fired events and will not create a new
* object for each event. This option increases performance but should be
* used with care, especially in multithreaded environment.
*
* @param reuseEvents whether to reuse previously fired event objects
* instead of creating a new event object for each event.
*/
public void setReuseEvents( boolean reuseEvents ) {
m_reuseEvents = reuseEvents;
}
/**
* Tests whether the reuseEvents
flag is set. If the flag is
* set to true
this class will reuse previously fired events
* and will not create a new object for each event. This option increases
* performance but should be used with care, especially in multithreaded
* environment.
*
* @return the value of the reuseEvents
flag.
*/
public boolean isReuseEvents( ) {
return m_reuseEvents;
}
/**
* @see Graph#addEdge(Object, Object)
*/
public Edge addEdge( Object sourceVertex, Object targetVertex ) {
Edge e = super.addEdge( sourceVertex, targetVertex );
if( e != null ) {
fireEdgeAdded( e );
}
return e;
}
/**
* @see Graph#addEdge(Edge)
*/
public boolean addEdge( Edge e ) {
boolean modified = super.addEdge( e );
if( modified ) {
fireEdgeAdded( e );
}
return modified;
}
/**
* @see ListenableGraph#addGraphListener(GraphListener)
*/
public void addGraphListener( GraphListener l ) {
addToListenerList( m_graphListeners, l );
}
/**
* @see Graph#addVertex(Object)
*/
public boolean addVertex( Object v ) {
boolean modified = super.addVertex( v );
if( modified ) {
fireVertexAdded( v );
}
return modified;
}
/**
* @see ListenableGraph#addVertexSetListener(VertexSetListener)
*/
public void addVertexSetListener( VertexSetListener l ) {
addToListenerList( m_vertexSetListeners, l );
}
/**
* @see java.lang.Object#clone()
*/
public Object clone( ) {
try {
DefaultListenableGraph g = (DefaultListenableGraph) super.clone( );
g.m_graphListeners = new ArrayList( );
g.m_vertexSetListeners = new ArrayList( );
return g;
}
catch( CloneNotSupportedException e ) {
// should never get here since we're Cloneable
e.printStackTrace( );
throw new RuntimeException( "internal error" );
}
}
/**
* @see Graph#removeEdge(Object, Object)
*/
public Edge removeEdge( Object sourceVertex, Object targetVertex ) {
Edge e = super.removeEdge( sourceVertex, targetVertex );
if( e != null ) {
fireEdgeRemoved( e );
}
return e;
}
/**
* @see Graph#removeEdge(Edge)
*/
public boolean removeEdge( Edge e ) {
boolean modified = super.removeEdge( e );
if( modified ) {
fireEdgeRemoved( e );
}
return modified;
}
/**
* @see ListenableGraph#removeGraphListener(GraphListener)
*/
public void removeGraphListener( GraphListener l ) {
m_graphListeners.remove( l );
}
/**
* @see Graph#removeVertex(Object)
*/
public boolean removeVertex( Object v ) {
if( containsVertex( v ) ) {
List touchingEdgesList = edgesOf( v );
// cannot iterate over list - will cause ConcurrentModificationException
Edge[] touchingEdges = new Edge[ touchingEdgesList.size( ) ];
touchingEdgesList.toArray( touchingEdges );
removeAllEdges( touchingEdges );
super.removeVertex( v ); // remove the vertex itself
fireVertexRemoved( v );
return true;
}
else {
return false;
}
}
/**
* @see ListenableGraph#removeVertexSetListener(VertexSetListener)
*/
public void removeVertexSetListener( VertexSetListener l ) {
m_vertexSetListeners.remove( l );
}
/**
* Notify listeners that the specified edge was added.
*
* @param edge the edge that was added.
*/
protected void fireEdgeAdded( Edge edge ) {
GraphEdgeChangeEvent e =
createGraphEdgeChangeEvent( GraphEdgeChangeEvent.EDGE_ADDED, edge );
for( int i = 0; i < m_graphListeners.size( ); i++ ) {
GraphListener l = (GraphListener) m_graphListeners.get( i );
l.edgeAdded( e );
}
}
/**
* Notify listeners that the specified edge was removed.
*
* @param edge the edge that was removed.
*/
protected void fireEdgeRemoved( Edge edge ) {
GraphEdgeChangeEvent e =
createGraphEdgeChangeEvent( GraphEdgeChangeEvent.EDGE_REMOVED, edge );
for( int i = 0; i < m_graphListeners.size( ); i++ ) {
GraphListener l = (GraphListener) m_graphListeners.get( i );
l.edgeRemoved( e );
}
}
/**
* Notify listeners that the specified vertex was added.
*
* @param vertex the vertex that was added.
*/
protected void fireVertexAdded( Object vertex ) {
GraphVertexChangeEvent e =
createGraphVertexChangeEvent( GraphVertexChangeEvent.VERTEX_ADDED,
vertex );
for( int i = 0; i < m_vertexSetListeners.size( ); i++ ) {
VertexSetListener l =
(VertexSetListener) m_vertexSetListeners.get( i );
l.vertexAdded( e );
}
for( int i = 0; i < m_graphListeners.size( ); i++ ) {
GraphListener l = (GraphListener) m_graphListeners.get( i );
l.vertexAdded( e );
}
}
/**
* Notify listeners that the specified vertex was removed.
*
* @param vertex the vertex that was removed.
*/
protected void fireVertexRemoved( Object vertex ) {
GraphVertexChangeEvent e =
createGraphVertexChangeEvent( GraphVertexChangeEvent.VERTEX_REMOVED,
vertex );
for( int i = 0; i < m_vertexSetListeners.size( ); i++ ) {
VertexSetListener l =
(VertexSetListener) m_vertexSetListeners.get( i );
l.vertexRemoved( e );
}
for( int i = 0; i < m_graphListeners.size( ); i++ ) {
GraphListener l = (GraphListener) m_graphListeners.get( i );
l.vertexRemoved( e );
}
}
private void addToListenerList( List list, EventListener l ) {
if( !list.contains( l ) ) {
list.add( l );
}
}
private GraphEdgeChangeEvent createGraphEdgeChangeEvent( int eventType,
Edge edge ) {
if( m_reuseEvents ) {
m_reuseableEdgeEvent.setType( eventType );
m_reuseableEdgeEvent.setEdge( edge );
return m_reuseableEdgeEvent;
}
else {
return new GraphEdgeChangeEvent( this, eventType, edge );
}
}
private GraphVertexChangeEvent createGraphVertexChangeEvent(
int eventType, Object vertex ) {
if( m_reuseEvents ) {
m_reuseableVertexEvent.setType( eventType );
m_reuseableVertexEvent.setVertex( vertex );
return m_reuseableVertexEvent;
}
else {
return new GraphVertexChangeEvent( this, eventType, vertex );
}
}
/**
* A reuseable edge event.
*
* @author Barak Naveh
*
* @since Aug 10, 2003
*/
private static class FlyweightEdgeEvent extends GraphEdgeChangeEvent {
private static final long serialVersionUID = 3907207152526636089L;
/**
* @see GraphEdgeChangeEvent#GraphEdgeChangeEvent(Object, int, Edge)
*/
public FlyweightEdgeEvent( Object eventSource, int type, Edge e ) {
super( eventSource, type, e );
}
/**
* Sets the edge of this event.
*
* @param e the edge to be set.
*/
protected void setEdge( Edge e ) {
m_edge = e;
}
/**
* Set the event type of this event.
*
* @param type the type to be set.
*/
protected void setType( int type ) {
m_type = type;
}
}
/**
* A reuseable vertex event.
*
* @author Barak Naveh
*
* @since Aug 10, 2003
*/
private static class FlyweightVertexEvent extends GraphVertexChangeEvent {
private static final long serialVersionUID = 3257848787857585716L;
/**
* @see GraphVertexChangeEvent#GraphVertexChangeEvent(Object, int,
* Object)
*/
public FlyweightVertexEvent( Object eventSource, int type, Object vertex ) {
super( eventSource, type, vertex );
}
/**
* Set the event type of this event.
*
* @param type type to be set.
*/
protected void setType( int type ) {
m_type = type;
}
/**
* Sets the vertex of this event.
*
* @param vertex the vertex to be set.
*/
protected void setVertex( Object vertex ) {
m_vertex = vertex;
}
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/DirectedMultigraph.java 0000644 0001751 0001751 00000004371 10266566752 026317 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -----------------------
* DirectedMultigraph.java
* -----------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: DirectedMultigraph.java,v 1.2 2004/11/19 10:15:03 barak_naveh Exp $
*
* Changes
* -------
* 05-Aug-2003 : Initial revision (BN);
*
*/
package org._3pq.jgrapht.graph;
import org._3pq.jgrapht.DirectedGraph;
import org._3pq.jgrapht.EdgeFactory;
import org._3pq.jgrapht.edge.EdgeFactories;
/**
* A directed multigraph. A directed multigraph is a non-simple directed graph
* in which loops and multiple edges between any two vertices are permitted.
*/
public class DirectedMultigraph extends AbstractBaseGraph
implements DirectedGraph {
private static final long serialVersionUID = 3258408413590599219L;
/**
* Creates a new directed multigraph.
*/
public DirectedMultigraph( ) {
this( new EdgeFactories.DirectedEdgeFactory( ) );
}
/**
* Creates a new directed multigraph with the specified edge factory.
*
* @param ef the edge factory of the new graph.
*/
public DirectedMultigraph( EdgeFactory ef ) {
super( ef, true, true );
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/DirectedSubgraph.java 0000644 0001751 0001751 00000004507 10266566752 025757 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ---------------------
* DirectedSubgraph.java
* ---------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: DirectedSubgraph.java,v 1.7 2004/11/19 10:47:38 barak_naveh Exp $
*
* Changes
* -------
* 05-Aug-2003 : Initial revision (BN);
*
*/
package org._3pq.jgrapht.graph;
import java.util.Set;
import org._3pq.jgrapht.DirectedGraph;
/**
* A directed graph that is a subgraph on other graph.
*
* @see org._3pq.jgrapht.graph.Subgraph
*/
public class DirectedSubgraph extends Subgraph implements DirectedGraph {
private static final long serialVersionUID = 3616445700507054133L;
/**
* Creates a new directed subgraph.
*
* @param base the base (backing) graph on which the subgraph will be
* based.
* @param vertexSubset vertices to include in the subgraph. If
* null
then all vertices are included.
* @param edgeSubset edges to in include in the subgraph. If
* null
then all the edges whose vertices found in the
* graph are included.
*/
public DirectedSubgraph( DirectedGraph base, Set vertexSubset,
Set edgeSubset ) {
super( base, vertexSubset, edgeSubset );
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/DirectedWeightedMultigraph.java 0000644 0001751 0001751 00000004564 10266566752 030004 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -------------------------------
* DirectedWeightedMultigraph.java
* -------------------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: DirectedWeightedMultigraph.java,v 1.2 2004/11/19 10:16:04 barak_naveh Exp $
*
* Changes
* -------
* 05-Aug-2003 : Initial revision (BN);
*
*/
package org._3pq.jgrapht.graph;
import org._3pq.jgrapht.EdgeFactory;
import org._3pq.jgrapht.WeightedGraph;
import org._3pq.jgrapht.edge.EdgeFactories;
/**
* A directed weighted multigraph. A directed weighted multigraph is a
* non-simple directed graph in which loops and multiple edges between any two
* vertices are permitted, and edges have weights.
*/
public class DirectedWeightedMultigraph extends DirectedMultigraph
implements WeightedGraph {
private static final long serialVersionUID = 4049071636005206066L;
/**
* Creates a new directed weighted multigraph.
*/
public DirectedWeightedMultigraph( ) {
this( new EdgeFactories.DirectedWeightedEdgeFactory( ) );
}
/**
* Creates a new directed weighted multigraph with the specified edge
* factory.
*
* @param ef the edge factory of the new graph.
*/
public DirectedWeightedMultigraph( EdgeFactory ef ) {
super( ef );
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/DirectedWeightedSubgraph.java 0000644 0001751 0001751 00000004665 10266566752 027445 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -----------------------------
* DirectedWeightedSubgraph.java
* -----------------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: DirectedWeightedSubgraph.java,v 1.5 2004/11/19 10:42:38 barak_naveh Exp $
*
* Changes
* -------
* 05-Aug-2003 : Initial revision (BN);
*
*/
package org._3pq.jgrapht.graph;
import java.util.Set;
import org._3pq.jgrapht.DirectedGraph;
import org._3pq.jgrapht.WeightedGraph;
/**
* A directed weighted graph that is a subgraph on other graph.
*
* @see Subgraph
*/
public class DirectedWeightedSubgraph extends DirectedSubgraph
implements WeightedGraph {
private static final long serialVersionUID = 3905799799168250680L;
/**
* Creates a new weighted directed subgraph.
*
* @param base the base (backing) graph on which the subgraph will be
* based.
* @param vertexSubset vertices to include in the subgraph. If
* null
then all vertices are included.
* @param edgeSubset edges to in include in the subgraph. If
* null
then all the edges whose vertices found in the
* graph are included.
*/
public DirectedWeightedSubgraph( WeightedGraph base, Set vertexSubset,
Set edgeSubset ) {
super( (DirectedGraph) base, vertexSubset, edgeSubset );
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/EdgeListFactory.java 0000644 0001751 0001751 00000004564 10266566752 025573 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ----------------
* EdgeListFactory.java
* ----------------
* (C) Copyright 2005, by John V. Sichi and Contributors.
*
* Original Author: John V. Sichi
* Contributor(s): -
*
* $Id: EdgeListFactory.java,v 1.1 2005/06/02 03:25:17 perfecthash Exp $
*
* Changes
* -------
* 01-Jun-2005 : Initial revision (JVS);
*
*/
package org._3pq.jgrapht.graph;
import java.util.List;
/**
* A factory for edge lists. This interface allows the creator of a graph to
* choose the {@link java.util.List} implementation used internally by the
* graph to maintain lists of edges. This provides control over performance
* tradeoffs between memory and CPU usage.
*
* @author John V. Sichi
* @version $Id: EdgeListFactory.java,v 1.1 2005/06/02 03:25:17 perfecthash Exp $
*/
public interface EdgeListFactory {
/**
* Create a new edge list for a particular vertex.
*
* @param vertex the vertex for which the edge list is being created;
* sophisticated factories may be able to use this information to
* choose an optimal list representation (e.g. ArrayList for a
* vertex expected to have low degree, and TreeList for a vertex
* expected to have high degree)
*
* @return new list
*/
public List createEdgeList( Object vertex );
}
// End EdgeListFactory.java
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/GraphDelegator.java 0000644 0001751 0001751 00000014124 10266566752 025424 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -------------------
* GraphDelegator.java
* -------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: GraphDelegator.java,v 1.5 2005/01/29 23:26:14 barak_naveh Exp $
*
* Changes
* -------
* 24-Jul-2003 : Initial revision (BN);
*
*/
package org._3pq.jgrapht.graph;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import org._3pq.jgrapht.DirectedGraph;
import org._3pq.jgrapht.Edge;
import org._3pq.jgrapht.EdgeFactory;
import org._3pq.jgrapht.Graph;
import org._3pq.jgrapht.UndirectedGraph;
/**
* A graph backed by the the graph specified at the constructor, which
* delegates all its methods to the backing graph. Operations on this graph
* "pass through" to the to the backing graph. Any modification made to this
* graph or the backing graph is reflected by the other.
*
* * This graph does not pass the hashCode and equals operations through * to the backing graph, but relies on Object's equals and * hashCode methods. *
* ** This class is mostly used as a base for extending subclasses. *
* * @author Barak Naveh * * @since Jul 20, 2003 */ public class GraphDelegator extends AbstractGraph implements Graph, Serializable { private static final long serialVersionUID = 3257005445226181425L; /** The graph to which operations are delegated. */ private Graph m_delegate; /** * Constructor for GraphDelegator. * * @param g the backing graph (the delegate). * * @throws NullPointerException */ public GraphDelegator( Graph g ) { super( ); if( g == null ) { throw new NullPointerException( ); } m_delegate = g; } /** * @see Graph#getAllEdges(Object, Object) */ public List getAllEdges( Object sourceVertex, Object targetVertex ) { return m_delegate.getAllEdges( sourceVertex, targetVertex ); } /** * @see Graph#getEdge(Object, Object) */ public Edge getEdge( Object sourceVertex, Object targetVertex ) { return m_delegate.getEdge( sourceVertex, targetVertex ); } /** * @see Graph#getEdgeFactory() */ public EdgeFactory getEdgeFactory( ) { return m_delegate.getEdgeFactory( ); } /** * @see Graph#addEdge(Edge) */ public boolean addEdge( Edge e ) { return m_delegate.addEdge( e ); } /** * @see Graph#addEdge(Object, Object) */ public Edge addEdge( Object sourceVertex, Object targetVertex ) { return m_delegate.addEdge( sourceVertex, targetVertex ); } /** * @see Graph#addVertex(Object) */ public boolean addVertex( Object v ) { return m_delegate.addVertex( v ); } /** * @see Graph#containsEdge(Edge) */ public boolean containsEdge( Edge e ) { return m_delegate.containsEdge( e ); } /** * @see Graph#containsVertex(Object) */ public boolean containsVertex( Object v ) { return m_delegate.containsVertex( v ); } /** * @see UndirectedGraph#degreeOf(Object) */ public int degreeOf( Object vertex ) { return ( (UndirectedGraph) m_delegate ).degreeOf( vertex ); } /** * @see Graph#edgeSet() */ public Set edgeSet( ) { return m_delegate.edgeSet( ); } /** * @see Graph#edgesOf(Object) */ public List edgesOf( Object vertex ) { return m_delegate.edgesOf( vertex ); } /** * @see DirectedGraph#inDegreeOf(Object) */ public int inDegreeOf( Object vertex ) { return ( (DirectedGraph) m_delegate ).inDegreeOf( vertex ); } /** * @see DirectedGraph#incomingEdgesOf(Object) */ public List incomingEdgesOf( Object vertex ) { return ( (DirectedGraph) m_delegate ).incomingEdgesOf( vertex ); } /** * @see DirectedGraph#outDegreeOf(Object) */ public int outDegreeOf( Object vertex ) { return ( (DirectedGraph) m_delegate ).outDegreeOf( vertex ); } /** * @see DirectedGraph#outgoingEdgesOf(Object) */ public List outgoingEdgesOf( Object vertex ) { return ( (DirectedGraph) m_delegate ).outgoingEdgesOf( vertex ); } /** * @see Graph#removeEdge(Edge) */ public boolean removeEdge( Edge e ) { return m_delegate.removeEdge( e ); } /** * @see Graph#removeEdge(Object, Object) */ public Edge removeEdge( Object sourceVertex, Object targetVertex ) { return m_delegate.removeEdge( sourceVertex, targetVertex ); } /** * @see Graph#removeVertex(Object) */ public boolean removeVertex( Object v ) { return m_delegate.removeVertex( v ); } /** * @see java.lang.Object#toString() */ public String toString( ) { return m_delegate.toString( ); } /** * @see Graph#vertexSet() */ public Set vertexSet( ) { return m_delegate.vertexSet( ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/ListenableDirectedGraph.java 0000644 0001751 0001751 00000004204 10266566752 027242 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ---------------------------- * ListenableDirectedGraph.java * ---------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: ListenableDirectedGraph.java,v 1.2 2004/11/19 10:34:22 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.DirectedGraph; /** * A directed graph which is also {@link org._3pq.jgrapht.ListenableGraph}. * * @see org._3pq.jgrapht.graph.DefaultListenableGraph */ public class ListenableDirectedGraph extends DefaultListenableGraph implements DirectedGraph { private static final long serialVersionUID = 3257571698126368824L; /** * Creates a new listenable directed graph. */ public ListenableDirectedGraph( ) { this( new DefaultDirectedGraph( ) ); } /** * Creates a new listenable directed graph. * * @param base the backing graph. */ public ListenableDirectedGraph( DirectedGraph base ) { super( base ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/ListenableDirectedWeightedGraph.java 0000644 0001751 0001751 00000004432 10266566752 030726 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------------------ * ListenableDirectedWeightedGraph.java * ------------------------------------ * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: ListenableDirectedWeightedGraph.java,v 1.2 2004/11/19 10:36:18 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.WeightedGraph; /** * A directed weighted graph which is also {@link * org._3pq.jgrapht.ListenableGraph}. * * @see org._3pq.jgrapht.graph.DefaultListenableGraph */ public class ListenableDirectedWeightedGraph extends ListenableDirectedGraph implements WeightedGraph { private static final long serialVersionUID = 3977582476627621938L; /** * Creates a new listenable directed weighted graph. */ public ListenableDirectedWeightedGraph( ) { this( new DefaultDirectedWeightedGraph( ) ); } /** * Creates a new listenable directed weighted graph. * * @param base the backing graph. */ public ListenableDirectedWeightedGraph( WeightedGraph base ) { super( (DirectedGraph) base ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/ListenableUndirectedGraph.java 0000644 0001751 0001751 00000004235 10266566752 027611 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------------ * ListenableUndirectedGraph.java * ------------------------------ * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: ListenableUndirectedGraph.java,v 1.2 2004/11/19 10:40:50 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.UndirectedGraph; /** * An undirected graph which is also {@link org._3pq.jgrapht.ListenableGraph}. * * @see org._3pq.jgrapht.graph.DefaultListenableGraph */ public class ListenableUndirectedGraph extends DefaultListenableGraph implements UndirectedGraph { private static final long serialVersionUID = 3256999969193145905L; /** * Creates a new listenable undirected simple graph. */ public ListenableUndirectedGraph( ) { this( new SimpleGraph( ) ); } /** * Creates a new listenable undirected graph. * * @param base the backing graph. */ public ListenableUndirectedGraph( UndirectedGraph base ) { super( base ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/ListenableUndirectedWeightedGraph.java 0000644 0001751 0001751 00000004454 10266566752 031275 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* -------------------------------------- * ListenableUndirectedWeightedGraph.java * -------------------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: ListenableUndirectedWeightedGraph.java,v 1.2 2004/11/19 10:37:34 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.UndirectedGraph; import org._3pq.jgrapht.WeightedGraph; /** * An undirected weighted graph which is also {@link * org._3pq.jgrapht.ListenableGraph}. * * @see org._3pq.jgrapht.graph.DefaultListenableGraph */ public class ListenableUndirectedWeightedGraph extends ListenableUndirectedGraph implements WeightedGraph { private static final long serialVersionUID = 3690762799613949747L; /** * Creates a new listenable undirected weighted graph. */ public ListenableUndirectedWeightedGraph( ) { this( new SimpleWeightedGraph( ) ); } /** * Creates a new listenable undirected weighted graph. * * @param base the backing graph. */ public ListenableUndirectedWeightedGraph( WeightedGraph base ) { super( (UndirectedGraph) base ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/Multigraph.java 0000644 0001751 0001751 00000004506 10266566752 024653 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------- * Multigraph.java * --------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: Multigraph.java,v 1.2 2004/11/19 10:04:07 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.EdgeFactory; import org._3pq.jgrapht.UndirectedGraph; import org._3pq.jgrapht.edge.EdgeFactories; /** * A multigraph. A multigraph is a non-simple undirected graph in which no * loops are permitted, but multiple edges between any two vertices are. If * you're unsure about multigraphs, see: * http://mathworld.wolfram.com/Multigraph.html. */ public class Multigraph extends AbstractBaseGraph implements UndirectedGraph { private static final long serialVersionUID = 3257001055819871795L; /** * Creates a new multigraph. */ public Multigraph( ) { this( new EdgeFactories.UndirectedEdgeFactory( ) ); } /** * Creates a new multigraph with the specified edge factory. * * @param ef the edge factory of the new graph. */ public Multigraph( EdgeFactory ef ) { super( ef, true, false ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/Pseudograph.java 0000644 0001751 0001751 00000004472 10266566752 025022 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ---------------- * Pseudograph.java * ---------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: Pseudograph.java,v 1.2 2004/11/19 10:05:19 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.EdgeFactory; import org._3pq.jgrapht.UndirectedGraph; import org._3pq.jgrapht.edge.EdgeFactories; /** * A pseudograph. A pseudograph is a non-simple undirected graph in which both * graph loops and multiple edges are permitted. If you're unsure about * pseudographs, see: * http://mathworld.wolfram.com/Pseudograph.html. */ public class Pseudograph extends AbstractBaseGraph implements UndirectedGraph { private static final long serialVersionUID = 3833183614484755253L; /** * Creates a new pseudograph. */ public Pseudograph( ) { this( new EdgeFactories.UndirectedEdgeFactory( ) ); } /** * Creates a new pseudograph with the specified edge factory. * * @param ef the edge factory of the new graph. */ public Pseudograph( EdgeFactory ef ) { super( ef, true, true ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/SimpleDirectedGraph.java 0000644 0001751 0001751 00000004412 10266566752 026412 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------ * SimpleDirectedGraph.java * ------------------------ * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: SimpleDirectedGraph.java,v 1.2 2004/11/19 10:09:31 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.EdgeFactory; import org._3pq.jgrapht.edge.EdgeFactories; /** * A simple directed graph. A simple directed graph is a directed graph in * which neither multiple edges between any two vertices nor loops are * permitted. */ public class SimpleDirectedGraph extends AbstractBaseGraph implements DirectedGraph { private static final long serialVersionUID = 4049358608472879671L; /** * Creates a new simple directed graph. */ public SimpleDirectedGraph( ) { this( new EdgeFactories.DirectedEdgeFactory( ) ); } /** * Creates a new simple directed graph with the specified edge factory. * * @param ef the edge factory of the new graph. */ public SimpleDirectedGraph( EdgeFactory ef ) { super( ef, false, false ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/SimpleDirectedWeightedGraph.java 0000644 0001751 0001751 00000004501 10266566752 030072 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* -------------------------------- * SimpleDirectedWeightedGraph.java * -------------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: SimpleDirectedWeightedGraph.java,v 1.3 2004/11/19 10:09:44 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.EdgeFactory; import org._3pq.jgrapht.WeightedGraph; import org._3pq.jgrapht.edge.EdgeFactories; /** * A simple directed weighted graph. A simple directed weighted graph is a * simple directed graph for which edges are assigned weights. */ public class SimpleDirectedWeightedGraph extends SimpleDirectedGraph implements WeightedGraph { private static final long serialVersionUID = 3904960841681220919L; /** * Creates a new simple directed weighted graph with the specified edge * factory. * * @param ef the edge factory of the new graph. */ public SimpleDirectedWeightedGraph( EdgeFactory ef ) { super( ef ); } /** * Creates a new simple directed weighted graph. */ public SimpleDirectedWeightedGraph( ) { this( new EdgeFactories.DirectedWeightedEdgeFactory( ) ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/SimpleGraph.java 0000644 0001751 0001751 00000004522 10266566752 024750 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ---------------- * SimpleGraph.java * ---------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: SimpleGraph.java,v 1.2 2004/11/19 10:17:12 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.EdgeFactory; import org._3pq.jgrapht.UndirectedGraph; import org._3pq.jgrapht.edge.EdgeFactories; /** * A simple graph. A simple graph is an undirected graph for which at most one * edge connects any two vertices, and loops are not permitted. If you're * unsure about simple graphs, see: * http://mathworld.wolfram.com/SimpleGraph.html. */ public class SimpleGraph extends AbstractBaseGraph implements UndirectedGraph { private static final long serialVersionUID = 3545796589454112304L; /** * Creates a new simple graph with the specified edge factory. * * @param ef the edge factory of the new graph. */ public SimpleGraph( EdgeFactory ef ) { super( ef, false, false ); } /** * Creates a new simple graph. */ public SimpleGraph( ) { this( new EdgeFactories.UndirectedEdgeFactory( ) ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/SimpleWeightedGraph.java 0000644 0001751 0001751 00000004314 10266566752 026430 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------ * SimpleWeightedGraph.java * ------------------------ * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: SimpleWeightedGraph.java,v 1.2 2004/11/19 10:17:55 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.EdgeFactory; import org._3pq.jgrapht.WeightedGraph; import org._3pq.jgrapht.edge.EdgeFactories; /** * A simple weighted graph. A simple weighted graph is a simple graph for * which edges are assigned weights. */ public class SimpleWeightedGraph extends SimpleGraph implements WeightedGraph { private static final long serialVersionUID = 3906088949100655922L; /** * Creates a new simple weighted graph with the specified edge factory. * * @param ef the edge factory of the new graph. */ public SimpleWeightedGraph( EdgeFactory ef ) { super( ef ); } /** * Creates a new simple weighted graph. */ public SimpleWeightedGraph( ) { this( new EdgeFactories.UndirectedWeightedEdgeFactory( ) ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/Subgraph.java 0000644 0001751 0001751 00000044436 10266566752 024320 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------- * Subgraph.java * ------------- * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: Subgraph.java,v 1.19 2005/05/25 00:38:37 perfecthash Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 26-Jul-2003 : Accurate constructors to avoid casting problems (BN); * 10-Aug-2003 : Adaptation to new event model (BN); * 23-Oct-2003 : Allowed non-listenable graph as base (BN); * 07-Feb-2004 : Enabled serialization (BN); * 20-Mar-2004 : Cancelled verification of element identity to base graph (BN); * 21-Sep-2004 : Added induced subgraph * */ package org._3pq.jgrapht.graph; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.Edge; import org._3pq.jgrapht.EdgeFactory; import org._3pq.jgrapht.Graph; import org._3pq.jgrapht.ListenableGraph; import org._3pq.jgrapht.UndirectedGraph; import org._3pq.jgrapht.event.GraphEdgeChangeEvent; import org._3pq.jgrapht.event.GraphListener; import org._3pq.jgrapht.event.GraphVertexChangeEvent; /** * A subgraph is a graph that has a subset of vertices and a subset of edges * with respect to some base graph. More formally, a subgraph G(V,E) that is * based on a base graph Gb(Vb,Eb) satisfies the following subgraph * property: V is a subset of Vb and E is a subset of Eb. Other than * this property, a subgraph is a graph with any respect and fully complies * with theGraph
interface.
*
* * If the base graph is a {@link org._3pq.jgrapht.ListenableGraph}, the * subgraph listens on the base graph and guarantees the subgraph property. If * an edge or a vertex is removed from the base graph, it is automatically * removed from the subgraph. Subgraph listeners are informed on such removal * only if it results in a cascaded removal from the subgraph. If the subgraph * has been created as an induced subgraph it also keeps track of edges being * added to its vertices. If vertices are added to the base graph, the * subgraph remains unaffected. *
* ** If the base graph is not a ListenableGraph, then the subgraph * property cannot be guaranteed. If edges or vertices are removed from the * base graph, they are not removed from the subgraph. *
* ** Modifications to Subgraph are allowed as long as the subgraph property is * maintained. Addition of vertices or edges are allowed as long as they also * exist in the base graph. Removal of vertices or edges is always allowed. * The base graph is never affected by any modification made to the * subgraph. *
* *
* A subgraph may provide a "live-window" on a base graph, so that changes made
* to its vertices or edges are immediately reflected in the base graph, and
* vice versa. For that to happen, vertices and edges added to the subgraph
* must be identical (that is, reference-equal and not only
* value-equal) to their respective ones in the base graph. Previous versions
* of this class enforced such identity, at a severe performance cost.
* Currently it is no longer enforced. If you want to achieve a "live-window"
* functionality, your safest tactics would be to NOT override the
* equals()
methods of your vertices and edges. If you use a class
* that has already overridden the equals()
method, such as
* String
, than you can use a wrapper around it, or else use it
* directly but exercise a great care to avoid having different-but-equal
* instances in the subgraph and the base graph.
*
* This graph implementation guarantees deterministic vertex and edge set * ordering (via {@link LinkedHashSet}). *
* * @author Barak Naveh * * @see org._3pq.jgrapht.Graph * @see java.util.Set * @since Jul 18, 2003 */ public class Subgraph extends AbstractGraph implements Serializable { private static final String NO_SUCH_EDGE_IN_BASE = "no such edge in base graph"; private static final String NO_SUCH_VERTEX_IN_BASE = "no such vertex in base graph"; // Set m_edgeSet = new LinkedHashSet( ); // friendly to improve performance Set m_vertexSet = new LinkedHashSet( ); // friendly to improve performance // private transient Set m_unmodifiableEdgeSet = null; private transient Set m_unmodifiableVertexSet = null; private Graph m_base; private boolean m_isInduced = false; private boolean m_verifyIntegrity = true; /** * Creates a new Subgraph. * * @param base the base (backing) graph on which the subgraph will be * based. * @param vertexSubset vertices to include in the subgraph. If *null
then all vertices are included.
* @param edgeSubset edges to in include in the subgraph. If
* null
then all the edges whose vertices found in the
* graph are included.
*/
public Subgraph( Graph base, Set vertexSubset, Set edgeSubset ) {
super( );
m_base = base;
if( m_base instanceof ListenableGraph ) {
( (ListenableGraph) m_base ).addGraphListener( new BaseGraphListener( ) );
}
addVerticesUsingFilter( base.vertexSet( ), vertexSubset );
addEdgesUsingFilter( base.edgeSet( ), edgeSubset );
}
/**
* Creates a new induced Subgraph. The subgraph will keep track of edges
* being added to its vertex subset as well as deletion of edges and
* vertices. If base it not listenable, this is identical to the call
* Subgraph(base, vertexSubset, null) .
*
* @param base the base (backing) graph on which the subgraph will be
* based.
* @param vertexSubset vertices to include in the subgraph. If
* null
then all vertices are included.
*/
public Subgraph( Graph base, Set vertexSubset ) {
this( base, vertexSubset, null );
m_isInduced = true;
}
/**
* @see org._3pq.jgrapht.Graph#getAllEdges(Object, Object)
*/
public List getAllEdges( Object sourceVertex, Object targetVertex ) {
List edges = null;
if( containsVertex( sourceVertex ) && containsVertex( targetVertex ) ) {
edges = new ArrayList( );
List baseEdges = m_base.getAllEdges( sourceVertex, targetVertex );
for( Iterator i = baseEdges.iterator( ); i.hasNext( ); ) {
Edge e = (Edge) i.next( );
if( m_edgeSet.contains( e ) ) { // add if subgraph also contains it
edges.add( e );
}
}
}
return edges;
}
/**
* @see org._3pq.jgrapht.Graph#getEdge(Object, Object)
*/
public Edge getEdge( Object sourceVertex, Object targetVertex ) {
List edges = getAllEdges( sourceVertex, targetVertex );
if( edges == null || edges.isEmpty( ) ) {
return null;
}
else {
return (Edge) edges.get( 0 );
}
}
/**
* @see org._3pq.jgrapht.Graph#getEdgeFactory()
*/
public EdgeFactory getEdgeFactory( ) {
return m_base.getEdgeFactory( );
}
/**
* Sets the the check integrity flag.
*
* @param verifyIntegrity
*
* @see Subgraph
* @deprecated method will be deleted in future versions. verifyIntegrity
* flag has no effect now.
*/
public void setVerifyIntegrity( boolean verifyIntegrity ) {
m_verifyIntegrity = verifyIntegrity;
}
/**
* Returns the value of the verifyIntegrity flag.
*
* @return the value of the verifyIntegrity flag.
*
* @deprecated method will be deleted in future versions.
*/
public boolean isVerifyIntegrity( ) {
return m_verifyIntegrity;
}
/**
* @see org._3pq.jgrapht.Graph#addEdge(Object, Object)
*/
public Edge addEdge( Object sourceVertex, Object targetVertex ) {
assertVertexExist( sourceVertex );
assertVertexExist( targetVertex );
if( !m_base.containsEdge( sourceVertex, targetVertex ) ) {
throw new IllegalArgumentException( NO_SUCH_EDGE_IN_BASE );
}
List edges = m_base.getAllEdges( sourceVertex, targetVertex );
for( Iterator i = edges.iterator( ); i.hasNext( ); ) {
Edge e = (Edge) i.next( );
if( !containsEdge( e ) ) {
m_edgeSet.add( e );
return e;
}
}
return null;
}
/**
* Adds the specified edge to this subgraph.
*
* @param e the edge to be added.
*
* @return true
if the edge was added, otherwise
* false
.
*
* @throws NullPointerException
* @throws IllegalArgumentException
*
* @see Subgraph
* @see org._3pq.jgrapht.Graph#addEdge(Edge)
*/
public boolean addEdge( Edge e ) {
if( e == null ) {
throw new NullPointerException( );
}
if( !m_base.containsEdge( e ) ) {
throw new IllegalArgumentException( NO_SUCH_EDGE_IN_BASE );
}
assertVertexExist( e.getSource( ) );
assertVertexExist( e.getTarget( ) );
if( containsEdge( e ) ) {
return false;
}
else {
m_edgeSet.add( e );
return true;
}
}
/**
* Adds the specified vertex to this subgraph.
*
* @param v the vertex to be added.
*
* @return true
if the vertex was added, otherwise
* false
.
*
* @throws NullPointerException
* @throws IllegalArgumentException
*
* @see Subgraph
* @see org._3pq.jgrapht.Graph#addVertex(Object)
*/
public boolean addVertex( Object v ) {
if( v == null ) {
throw new NullPointerException( );
}
if( !m_base.containsVertex( v ) ) {
throw new IllegalArgumentException( NO_SUCH_VERTEX_IN_BASE );
}
if( containsVertex( v ) ) {
return false;
}
else {
m_vertexSet.add( v );
return true;
}
}
/**
* @see org._3pq.jgrapht.Graph#containsEdge(Edge)
*/
public boolean containsEdge( Edge e ) {
return m_edgeSet.contains( e );
}
/**
* @see org._3pq.jgrapht.Graph#containsVertex(Object)
*/
public boolean containsVertex( Object v ) {
return m_vertexSet.contains( v );
}
/**
* @see UndirectedGraph#degreeOf(Object)
*/
public int degreeOf( Object vertex ) {
assertVertexExist( vertex );
// sophisticated way to check runtime class of base ;-)
( (UndirectedGraph) m_base ).degreeOf( vertex );
int degree = 0;
for( Iterator i = m_base.edgesOf( vertex ).iterator( ); i.hasNext( ); ) {
Edge e = (Edge) i.next( );
if( containsEdge( e ) ) {
degree++;
if( e.getSource( ).equals( e.getTarget( ) ) ) {
degree++;
}
}
}
return degree;
}
/**
* @see org._3pq.jgrapht.Graph#edgeSet()
*/
public Set edgeSet( ) {
if( m_unmodifiableEdgeSet == null ) {
m_unmodifiableEdgeSet = Collections.unmodifiableSet( m_edgeSet );
}
return m_unmodifiableEdgeSet;
}
/**
* @see org._3pq.jgrapht.Graph#edgesOf(Object)
*/
public List edgesOf( Object vertex ) {
assertVertexExist( vertex );
ArrayList edges = new ArrayList( );
List baseEdges = m_base.edgesOf( vertex );
for( Iterator i = baseEdges.iterator( ); i.hasNext( ); ) {
Edge e = (Edge) i.next( );
if( containsEdge( e ) ) {
edges.add( e );
}
}
return edges;
}
/**
* @see DirectedGraph#inDegreeOf(Object)
*/
public int inDegreeOf( Object vertex ) {
assertVertexExist( vertex );
int degree = 0;
for( Iterator i =
( (DirectedGraph) m_base ).incomingEdgesOf( vertex ).iterator( );
i.hasNext( ); ) {
if( containsEdge( (Edge) i.next( ) ) ) {
degree++;
}
}
return degree;
}
/**
* @see DirectedGraph#incomingEdgesOf(Object)
*/
public List incomingEdgesOf( Object vertex ) {
assertVertexExist( vertex );
ArrayList edges = new ArrayList( );
List baseEdges =
( (DirectedGraph) m_base ).incomingEdgesOf( vertex );
for( Iterator i = baseEdges.iterator( ); i.hasNext( ); ) {
Edge e = (Edge) i.next( );
if( containsEdge( e ) ) {
edges.add( e );
}
}
return edges;
}
/**
* @see DirectedGraph#outDegreeOf(Object)
*/
public int outDegreeOf( Object vertex ) {
assertVertexExist( vertex );
int degree = 0;
for( Iterator i =
( (DirectedGraph) m_base ).outgoingEdgesOf( vertex ).iterator( );
i.hasNext( ); ) {
if( containsEdge( (Edge) i.next( ) ) ) {
degree++;
}
}
return degree;
}
/**
* @see DirectedGraph#outgoingEdgesOf(Object)
*/
public List outgoingEdgesOf( Object vertex ) {
assertVertexExist( vertex );
ArrayList edges = new ArrayList( );
List baseEdges =
( (DirectedGraph) m_base ).outgoingEdgesOf( vertex );
for( Iterator i = baseEdges.iterator( ); i.hasNext( ); ) {
Edge e = (Edge) i.next( );
if( containsEdge( e ) ) {
edges.add( e );
}
}
return edges;
}
/**
* @see org._3pq.jgrapht.Graph#removeEdge(Edge)
*/
public boolean removeEdge( Edge e ) {
return m_edgeSet.remove( e );
}
/**
* @see org._3pq.jgrapht.Graph#removeEdge(Object, Object)
*/
public Edge removeEdge( Object sourceVertex, Object targetVertex ) {
Edge e = getEdge( sourceVertex, targetVertex );
return m_edgeSet.remove( e ) ? e : null;
}
/**
* @see org._3pq.jgrapht.Graph#removeVertex(Object)
*/
public boolean removeVertex( Object v ) {
// If the base graph does NOT contain v it means we are here in
// response to removal of v from the base. In such case we don't need
// to remove all the edges of v as they were already removed.
if( containsVertex( v ) && m_base.containsVertex( v ) ) {
removeAllEdges( edgesOf( v ) );
}
return m_vertexSet.remove( v );
}
/**
* @see org._3pq.jgrapht.Graph#vertexSet()
*/
public Set vertexSet( ) {
if( m_unmodifiableVertexSet == null ) {
m_unmodifiableVertexSet =
Collections.unmodifiableSet( m_vertexSet );
}
return m_unmodifiableVertexSet;
}
private void addEdgesUsingFilter( Set edgeSet, Set filter ) {
Edge e;
boolean containsVertices;
boolean edgeIncluded;
for( Iterator i = edgeSet.iterator( ); i.hasNext( ); ) {
e = (Edge) i.next( );
containsVertices =
containsVertex( e.getSource( ) )
&& containsVertex( e.getTarget( ) );
// note the use of short circuit evaluation
edgeIncluded = ( filter == null ) || filter.contains( e );
if( containsVertices && edgeIncluded ) {
addEdge( e );
}
}
}
private void addVerticesUsingFilter( Set vertexSet, Set filter ) {
Object v;
for( Iterator i = vertexSet.iterator( ); i.hasNext( ); ) {
v = i.next( );
// note the use of short circuit evaluation
if( filter == null || filter.contains( v ) ) {
addVertex( v );
}
}
}
/**
* An internal listener on the base graph.
*
* @author Barak Naveh
*
* @since Jul 20, 2003
*/
private class BaseGraphListener implements GraphListener, Serializable {
/**
* @see GraphListener#edgeAdded(GraphEdgeChangeEvent)
*/
public void edgeAdded( GraphEdgeChangeEvent e ) {
if( m_isInduced ) {
addEdge( e.getEdge( ) );
}
}
/**
* @see GraphListener#edgeRemoved(GraphEdgeChangeEvent)
*/
public void edgeRemoved( GraphEdgeChangeEvent e ) {
Edge edge = e.getEdge( );
removeEdge( edge );
}
/**
* @see VertexSetListener#vertexAdded(GraphVertexChangeEvent)
*/
public void vertexAdded( GraphVertexChangeEvent e ) {
// we don't care
}
/**
* @see VertexSetListener#vertexRemoved(GraphVertexChangeEvent)
*/
public void vertexRemoved( GraphVertexChangeEvent e ) {
Object vertex = e.getVertex( );
removeVertex( vertex );
}
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/UndirectedSubgraph.java 0000644 0001751 0001751 00000004536 10266566752 026324 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -----------------------
* UndirectedSubgraph.java
* -----------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: UndirectedSubgraph.java,v 1.6 2004/11/19 10:44:14 barak_naveh Exp $
*
* Changes
* -------
* 05-Aug-2003 : Initial revision (BN);
*
*/
package org._3pq.jgrapht.graph;
import java.util.Set;
import org._3pq.jgrapht.UndirectedGraph;
/**
* An undirected graph that is a subgraph on other graph.
*
* @see org._3pq.jgrapht.graph.Subgraph
*/
public class UndirectedSubgraph extends Subgraph implements UndirectedGraph {
private static final long serialVersionUID = 3256728359772631350L;
/**
* Creates a new undirected subgraph.
*
* @param base the base (backing) graph on which the subgraph will be
* based.
* @param vertexSubset vertices to include in the subgraph. If
* null
then all vertices are included.
* @param edgeSubset edges to in include in the subgraph. If
* null
then all the edges whose vertices found in the
* graph are included.
*/
public UndirectedSubgraph( UndirectedGraph base, Set vertexSubset,
Set edgeSubset ) {
super( base, vertexSubset, edgeSubset );
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/UndirectedWeightedSubgraph.java 0000644 0001751 0001751 00000004714 10266566752 030003 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -------------------------------
* UndirectedWeightedSubgraph.java
* -------------------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: UndirectedWeightedSubgraph.java,v 1.5 2004/11/19 10:44:14 barak_naveh Exp $
*
* Changes
* -------
* 05-Aug-2003 : Initial revision (BN);
*
*/
package org._3pq.jgrapht.graph;
import java.util.Set;
import org._3pq.jgrapht.UndirectedGraph;
import org._3pq.jgrapht.WeightedGraph;
/**
* An undirected weighted graph that is a subgraph on other graph.
*
* @see Subgraph
*/
public class UndirectedWeightedSubgraph extends UndirectedSubgraph
implements WeightedGraph {
private static final long serialVersionUID = 3689346615735236409L;
/**
* Creates a new undirected weighted subgraph.
*
* @param base the base (backing) graph on which the subgraph will be
* based.
* @param vertexSubset vertices to include in the subgraph. If
* null
then all vertices are included.
* @param edgeSubset edges to in include in the subgraph. If
* null
then all the edges whose vertices found in the
* graph are included.
*/
public UndirectedWeightedSubgraph( WeightedGraph base, Set vertexSubset,
Set edgeSubset ) {
super( (UndirectedGraph) base, vertexSubset, edgeSubset );
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/UnmodifiableDirectedGraph.java 0000644 0001751 0001751 00000004053 10266566752 027560 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ------------------------------
* UnmodifiableDirectedGraph.java
* ------------------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: UnmodifiableDirectedGraph.java,v 1.2 2004/11/19 10:21:13 barak_naveh Exp $
*
* Changes
* -------
* 05-Aug-2003 : Initial revision (BN);
*
*/
package org._3pq.jgrapht.graph;
import org._3pq.jgrapht.DirectedGraph;
/**
* A directed graph that cannot be modified.
*
* @see org._3pq.jgrapht.graph.UnmodifiableGraph
*/
public class UnmodifiableDirectedGraph extends UnmodifiableGraph
implements DirectedGraph {
private static final long serialVersionUID = 3978701783725913906L;
/**
* Creates a new unmodifiable directed graph based on the specified backing
* graph.
*
* @param g the backing graph on which an unmodifiable graph is to be
* created.
*/
public UnmodifiableDirectedGraph( DirectedGraph g ) {
super( g );
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/UnmodifiableGraph.java 0000644 0001751 0001751 00000011321 10266566752 026110 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ----------------------
* UnmodifiableGraph.java
* ----------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: UnmodifiableGraph.java,v 1.4 2004/11/19 10:21:36 barak_naveh Exp $
*
* Changes
* -------
* 24-Jul-2003 : Initial revision (BN);
*
*/
package org._3pq.jgrapht.graph;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import org._3pq.jgrapht.Edge;
import org._3pq.jgrapht.Graph;
/**
* An unmodifiable view of the backing graph specified in the constructor. This
* graph allows modules to provide users with "read-only" access to internal
* graphs. Query operations on this graph "read through" to the backing graph,
* and attempts to modify this graph result in an
* UnsupportedOperationException
.
*
* * This graph does not pass the hashCode and equals operations through * to the backing graph, but relies on Object's equals and * hashCode methods. This graph will be serializable if the backing * graph is serializable. *
* * @author Barak Naveh * * @since Jul 24, 2003 */ public class UnmodifiableGraph extends GraphDelegator implements Serializable { private static final long serialVersionUID = 3544957670722713913L; private static final String UNMODIFIABLE = "this graph is unmodifiable"; /** * Creates a new unmodifiable graph based on the specified backing graph. * * @param g the backing graph on which an unmodifiable graph is to be * created. */ public UnmodifiableGraph( Graph g ) { super( g ); } /** * @see Graph#addAllEdges(Collection) */ public boolean addAllEdges( Collection edges ) { throw new UnsupportedOperationException( UNMODIFIABLE ); } /** * @see Graph#addAllVertices(Collection) */ public boolean addAllVertices( Collection vertices ) { throw new UnsupportedOperationException( UNMODIFIABLE ); } /** * @see Graph#addEdge(Edge) */ public boolean addEdge( Edge e ) { throw new UnsupportedOperationException( UNMODIFIABLE ); } /** * @see Graph#addEdge(Object, Object) */ public Edge addEdge( Object sourceVertex, Object targetVertex ) { throw new UnsupportedOperationException( UNMODIFIABLE ); } /** * @see Graph#addVertex(Object) */ public boolean addVertex( Object v ) { throw new UnsupportedOperationException( UNMODIFIABLE ); } /** * @see Graph#removeAllEdges(Collection) */ public boolean removeAllEdges( Collection edges ) { throw new UnsupportedOperationException( UNMODIFIABLE ); } /** * @see Graph#removeAllEdges(Object, Object) */ public List removeAllEdges( Object sourceVertex, Object targetVertex ) { throw new UnsupportedOperationException( UNMODIFIABLE ); } /** * @see Graph#removeAllVertices(Collection) */ public boolean removeAllVertices( Collection vertices ) { throw new UnsupportedOperationException( UNMODIFIABLE ); } /** * @see Graph#removeEdge(Edge) */ public boolean removeEdge( Edge e ) { throw new UnsupportedOperationException( UNMODIFIABLE ); } /** * @see Graph#removeEdge(Object, Object) */ public Edge removeEdge( Object sourceVertex, Object targetVertex ) { throw new UnsupportedOperationException( UNMODIFIABLE ); } /** * @see Graph#removeVertex(Object) */ public boolean removeVertex( Object v ) { throw new UnsupportedOperationException( UNMODIFIABLE ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/UnmodifiableUndirectedGraph.java 0000644 0001751 0001751 00000004053 10266566752 030123 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* -------------------------------- * UnmodifiableUndirectedGraph.java * -------------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: UnmodifiableUndirectedGraph.java,v 1.2 2004/11/19 10:23:14 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.UndirectedGraph; /** * An undirected graph that cannot be modified. * * @see UnmodifiableGraph */ public class UnmodifiableUndirectedGraph extends UnmodifiableGraph implements UndirectedGraph { private static final long serialVersionUID = 3258134639355704624L; /** * Creates a new unmodifiable undirected graph based on the specified * backing graph. * * @param g the backing graph on which an unmodifiable graph is to be * created. */ public UnmodifiableUndirectedGraph( UndirectedGraph g ) { super( g ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/WeightedMultigraph.java 0000644 0001751 0001751 00000004706 10266566752 026336 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ----------------------- * WeightedMultigraph.java * ----------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: WeightedMultigraph.java,v 1.2 2004/11/19 10:24:49 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.EdgeFactory; import org._3pq.jgrapht.WeightedGraph; import org._3pq.jgrapht.edge.EdgeFactories; /** * A weighted multigraph. A weighted multigraph is a non-simple undirected * graph in which no loops are permitted, but multiple edges between any two * vertices are. The edges of a weighted multigraph have weights. If you're * unsure about multigraphs, see: * http://mathworld.wolfram.com/Multigraph.html. */ public class WeightedMultigraph extends Multigraph implements WeightedGraph { private static final long serialVersionUID = 3544671793370640696L; /** * Creates a new weighted multigraph with the specified edge factory. * * @param ef the edge factory of the new graph. */ public WeightedMultigraph( EdgeFactory ef ) { super( ef ); } /** * Creates a new weighted multigraph. */ public WeightedMultigraph( ) { this( new EdgeFactories.UndirectedWeightedEdgeFactory( ) ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/WeightedPseudograph.java 0000644 0001751 0001751 00000004675 10266566752 026510 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------ * WeightedPseudograph.java * ------------------------ * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: WeightedPseudograph.java,v 1.2 2004/11/19 10:25:32 barak_naveh Exp $ * * Changes * ------- * 05-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.EdgeFactory; import org._3pq.jgrapht.WeightedGraph; import org._3pq.jgrapht.edge.EdgeFactories; /** * A weighted pseudograph. A weighted pseudograph is a non-simple undirected * graph in which both graph loops and multiple edges are permitted. The edges * of a weighted pseudograph have weights. If you're unsure about * pseudographs, see: * http://mathworld.wolfram.com/Pseudograph.html. */ public class WeightedPseudograph extends Pseudograph implements WeightedGraph { private static final long serialVersionUID = 3257290244524356152L; /** * Creates a new weighted pseudograph with the specified edge factory. * * @param ef the edge factory of the new graph. */ public WeightedPseudograph( EdgeFactory ef ) { super( ef ); } /** * Creates a new weighted pseudograph. */ public WeightedPseudograph( ) { this( new EdgeFactories.UndirectedWeightedEdgeFactory( ) ); } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/graph/package.html 0000644 0001751 0001751 00000000172 10266566752 024150 0 ustar moeller moeller Implementations of various graphs. libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/traverse/ 0000755 0001751 0001751 00000000000 11251311101 022364 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/traverse/AbstractGraphIterator.java 0000644 0001751 0001751 00000013477 10266566752 027537 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (barak_naveh@users.sourceforge.net) * * (C) Copyright 2003, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* -------------------------- * AbstractGraphIterator.java * -------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: AbstractGraphIterator.java,v 1.2 2003/08/11 10:37:44 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 11-Aug-2003 : Adaptation to new event model (BN); * */ package org._3pq.jgrapht.traverse; import java.util.ArrayList; import java.util.List; import org._3pq.jgrapht.event.ConnectedComponentTraversalEvent; import org._3pq.jgrapht.event.EdgeTraversalEvent; import org._3pq.jgrapht.event.TraversalListener; import org._3pq.jgrapht.event.VertexTraversalEvent; /** * An empty implementation of a graph iterator to minimize the effort required * to implement graph iterators. * * @author Barak Naveh * * @since Jul 19, 2003 */ public abstract class AbstractGraphIterator implements GraphIterator { private List m_traversalListeners = new ArrayList( ); private boolean m_crossComponentTraversal = true; private boolean m_reuseEvents = false; /** * Sets the cross component traversal flag - indicates whether to traverse * the graph across connected components. * * @param crossComponentTraversal iftrue
traverses across
* connected components.
*/
public void setCrossComponentTraversal( boolean crossComponentTraversal ) {
m_crossComponentTraversal = crossComponentTraversal;
}
/**
* Test whether this iterator is set to traverse the graph across connected
* components.
*
* @return true
if traverses across connected components,
* otherwise false
.
*/
public boolean isCrossComponentTraversal( ) {
return m_crossComponentTraversal;
}
/**
* @see GraphIterator#setReuseEvents(boolean)
*/
public void setReuseEvents( boolean reuseEvents ) {
m_reuseEvents = reuseEvents;
}
/**
* @see GraphIterator#isReuseEvents()
*/
public boolean isReuseEvents( ) {
return m_reuseEvents;
}
/**
* Adds the specified traversal listener to this iterator.
*
* @param l the traversal listener to be added.
*/
public void addTraversalListener( TraversalListener l ) {
if( !m_traversalListeners.contains( l ) ) {
m_traversalListeners.add( l );
}
}
/**
* Unsupported.
*
* @throws UnsupportedOperationException
*/
public void remove( ) {
throw new UnsupportedOperationException( );
}
/**
* Removes the specified traversal listener from this iterator.
*
* @param l the traversal listener to be removed.
*/
public void removeTraversalListener( TraversalListener l ) {
m_traversalListeners.remove( l );
}
/**
* Informs all listeners that the traversal of the current connected
* component finished.
*
* @param e the connected component finished event.
*/
protected void fireConnectedComponentFinished(
ConnectedComponentTraversalEvent e ) {
int len = m_traversalListeners.size( );
for( int i = 0; i < len; i++ ) {
TraversalListener l =
(TraversalListener) m_traversalListeners.get( i );
l.connectedComponentFinished( e );
}
}
/**
* Informs all listeners that a traversal of a new connected component has
* started.
*
* @param e the connected component started event.
*/
protected void fireConnectedComponentStarted(
ConnectedComponentTraversalEvent e ) {
int len = m_traversalListeners.size( );
for( int i = 0; i < len; i++ ) {
TraversalListener l =
(TraversalListener) m_traversalListeners.get( i );
l.connectedComponentStarted( e );
}
}
/**
* Informs all listeners that a the specified edge was visited.
*
* @param e the edge traversal event.
*/
protected void fireEdgeTraversed( EdgeTraversalEvent e ) {
int len = m_traversalListeners.size( );
for( int i = 0; i < len; i++ ) {
TraversalListener l =
(TraversalListener) m_traversalListeners.get( i );
l.edgeTraversed( e );
}
}
/**
* Informs all listeners that a the specified vertex was visited.
*
* @param e the vertex traversal event.
*/
protected void fireVertexTraversed( VertexTraversalEvent e ) {
int len = m_traversalListeners.size( );
for( int i = 0; i < len; i++ ) {
TraversalListener l =
(TraversalListener) m_traversalListeners.get( i );
l.vertexTraversed( e );
}
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/traverse/BreadthFirstIterator.java 0000644 0001751 0001751 00000010032 10266566752 027353 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -------------------------
* BreadthFirstIterator.java
* -------------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): Liviu Rau
*
* $Id: BreadthFirstIterator.java,v 1.9 2004/09/17 07:24:12 perfecthash Exp $
*
* Changes
* -------
* 24-Jul-2003 : Initial revision (BN);
* 06-Aug-2003 : Extracted common logic to TraverseUtils.XXFirstIterator (BN);
* 31-Jan-2004 : Reparented and changed interface to parent class (BN);
*
*/
package org._3pq.jgrapht.traverse;
import java.util.LinkedList;
import org._3pq.jgrapht.Edge;
import org._3pq.jgrapht.Graph;
/**
* A breadth-first iterator for a directed and an undirected graph. For this
* iterator to work correctly the graph must not be modified during iteration.
* Currently there are no means to ensure that, nor to fail-fast. The results
* of such modifications are undefined.
*
* @author Barak Naveh
*
* @since Jul 19, 2003
*/
public class BreadthFirstIterator extends CrossComponentIterator {
/**
* Note to users: this queue implementation is a bit lame in terms
* of GC efficiency. If you need it to be improved either let us know or
* use the source...
*/
private LinkedList m_queue = new LinkedList( );
/**
* Creates a new breadth-first iterator for the specified graph.
*
* @param g the graph to be iterated.
*/
public BreadthFirstIterator( Graph g ) {
this( g, null );
}
/**
* Creates a new breadth-first iterator for the specified graph. Iteration
* will start at the specified start vertex and will be limited to the
* connected component that includes that vertex. If the specified start
* vertex is null
, iteration will start at an arbitrary
* vertex and will not be limited, that is, will be able to traverse all
* the graph.
*
* @param g the graph to be iterated.
* @param startVertex the vertex iteration to be started.
*/
public BreadthFirstIterator( Graph g, Object startVertex ) {
super( g, startVertex );
}
/**
* @see org._3pq.jgrapht.traverse.CrossComponentIterator#isConnectedComponentExhausted()
*/
protected boolean isConnectedComponentExhausted( ) {
return m_queue.isEmpty( );
}
/**
* @see org._3pq.jgrapht.traverse.CrossComponentIterator#encounterVertex(java.lang.Object,
* org._3pq.jgrapht.Edge)
*/
protected void encounterVertex( Object vertex, Edge edge ) {
putSeenData( vertex, null );
m_queue.addLast( vertex );
}
/**
* @see org._3pq.jgrapht.traverse.CrossComponentIterator#encounterVertexAgain(java.lang.Object,
* org._3pq.jgrapht.Edge)
*/
protected void encounterVertexAgain( Object vertex, Edge edge ) {}
/**
* @see org._3pq.jgrapht.traverse.CrossComponentIterator#provideNextVertex()
*/
protected Object provideNextVertex( ) {
return m_queue.removeFirst( );
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/traverse/ClosestFirstIterator.java 0000644 0001751 0001751 00000024274 10266566752 027433 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -------------------------
* ClosestFirstIterator.java
* -------------------------
* (C) Copyright 2003, by John V. Sichi and Contributors.
*
* Original Author: John V. Sichi
* Contributor(s): Barak Naveh
*
* $Id: ClosestFirstIterator.java,v 1.6 2005/05/30 05:37:29 perfecthash Exp $
*
* Changes
* -------
* 02-Sep-2003 : Initial revision (JVS);
* 31-Jan-2004 : Reparented and changed interface to parent class (BN);
* 29-May-2005 : Added radius support (JVS);
*
*/
package org._3pq.jgrapht.traverse;
import org._3pq.jgrapht.Edge;
import org._3pq.jgrapht.Graph;
import org._3pq.jgrapht.util.FibonacciHeap;
/**
* A closest-first iterator for a directed or undirected graph. For this
* iterator to work correctly the graph must not be modified during iteration.
* Currently there are no means to ensure that, nor to fail-fast. The results
* of such modifications are undefined.
*
* * The metric for closest here is the path length from a start vertex. * Edge.getWeight() is summed to calculate path length. Negative edge weights * will result in an IllegalArgumentException. Optionally, path length may be * bounded by a finite radius. *
* * @author John V. Sichi * * @since Sep 2, 2003 */ public class ClosestFirstIterator extends CrossComponentIterator { /** Priority queue of fringe vertices. */ private FibonacciHeap m_heap = new FibonacciHeap( ); /** Maximum distance to search. */ private double m_radius = Double.POSITIVE_INFINITY; /** * Creates a new closest-first iterator for the specified graph. * * @param g the graph to be iterated. */ public ClosestFirstIterator( Graph g ) { this( g, null ); } /** * Creates a new closest-first iterator for the specified graph. Iteration * will start at the specified start vertex and will be limited to the * connected component that includes that vertex. If the specified start * vertex isnull
, iteration will start at an arbitrary
* vertex and will not be limited, that is, will be able to traverse all
* the graph.
*
* @param g the graph to be iterated.
* @param startVertex the vertex iteration to be started.
*/
public ClosestFirstIterator( Graph g, Object startVertex ) {
this( g, startVertex, Double.POSITIVE_INFINITY );
}
/**
* Creates a new radius-bounded closest-first iterator for the specified
* graph. Iteration will start at the specified start vertex and will be
* limited to the subset of the connected component which includes that
* vertex and is reachable via paths of length less than or equal to the
* specified radius. The specified start vertex may not be
* null
.
*
* @param g the graph to be iterated.
* @param startVertex the vertex iteration to be started.
* @param radius limit on path length, or Double.POSITIVE_INFINITY for
* unbounded search.
*/
public ClosestFirstIterator( Graph g, Object startVertex, double radius ) {
super( g, startVertex );
m_radius = radius;
checkRadiusTraversal( isCrossComponentTraversal( ) );
}
// override AbstractGraphIterator
public void setCrossComponentTraversal( boolean crossComponentTraversal ) {
checkRadiusTraversal( crossComponentTraversal );
super.setCrossComponentTraversal( crossComponentTraversal );
}
/**
* Get the length of the shortest path known to the given vertex. If the
* vertex has already been visited, then it is truly the shortest path
* length; otherwise, it is the best known upper bound.
*
* @param vertex vertex being sought from start vertex
*
* @return length of shortest path known, or Double.POSITIVE_INFINITY if no
* path found yet
*/
public double getShortestPathLength( Object vertex ) {
QueueEntry entry = (QueueEntry) getSeenData( vertex );
if( entry == null ) {
return Double.POSITIVE_INFINITY;
}
return entry.getShortestPathLength( );
}
/**
* Get the spanning tree edge reaching a vertex which has been seen already
* in this traversal. This edge is the last link in the shortest known
* path between the start vertex and the requested vertex. If the vertex
* has already been visited, then it is truly the minimum spanning tree
* edge; otherwise, it is the best candidate seen so far.
*
* @param vertex the spanned vertex.
*
* @return the spanning tree edge, or null if the vertex either has not
* been seen yet or is the start vertex.
*/
public Edge getSpanningTreeEdge( Object vertex ) {
QueueEntry entry = (QueueEntry) getSeenData( vertex );
if( entry == null ) {
return null;
}
return entry.m_spanningTreeEdge;
}
/**
* @see org._3pq.jgrapht.traverse.CrossComponentIterator#isConnectedComponentExhausted()
*/
protected boolean isConnectedComponentExhausted( ) {
if( m_heap.size( ) == 0 ) {
return true;
}
else {
if( m_heap.min( ).getKey( ) > m_radius ) {
m_heap.clear( );
return true;
}
else {
return false;
}
}
}
/**
* @see org._3pq.jgrapht.traverse.CrossComponentIterator#encounterVertex(java.lang.Object,
* org._3pq.jgrapht.Edge)
*/
protected void encounterVertex( Object vertex, Edge edge ) {
QueueEntry entry = createSeenData( vertex, edge );
putSeenData( vertex, entry );
m_heap.insert( entry, entry.getShortestPathLength( ) );
}
/**
* Override superclass. When we see a vertex again, we need to see if the
* new edge provides a shorter path than the old edge.
*
* @param vertex the vertex re-encountered
* @param edge the edge via which the vertex was re-encountered
*/
protected void encounterVertexAgain( Object vertex, Edge edge ) {
QueueEntry entry = (QueueEntry) getSeenData( vertex );
if( entry.m_frozen ) {
// no improvement for this vertex possible
return;
}
double candidatePathLength = calculatePathLength( vertex, edge );
if( candidatePathLength < entry.getShortestPathLength( ) ) {
entry.m_spanningTreeEdge = edge;
m_heap.decreaseKey( entry, candidatePathLength );
}
}
/**
* @see org._3pq.jgrapht.traverse.CrossComponentIterator#provideNextVertex()
*/
protected Object provideNextVertex( ) {
QueueEntry entry = (QueueEntry) m_heap.removeMin( );
entry.m_frozen = true;
return entry.m_vertex;
}
private void assertNonNegativeEdge( Edge edge ) {
if( edge.getWeight( ) < 0 ) {
throw new IllegalArgumentException(
"negative edge weights not allowed" );
}
}
/**
* Determine path length to a vertex via an edge, using the path length for
* the opposite vertex.
*
* @param vertex the vertex for which to calculate the path length.
* @param edge the edge via which the path is being extended.
*
* @return calculated path length.
*/
private double calculatePathLength( Object vertex, Edge edge ) {
assertNonNegativeEdge( edge );
Object otherVertex = edge.oppositeVertex( vertex );
QueueEntry otherEntry = (QueueEntry) getSeenData( otherVertex );
return otherEntry.getShortestPathLength( ) + edge.getWeight( );
}
private void checkRadiusTraversal( boolean crossComponentTraversal ) {
if( crossComponentTraversal && ( m_radius != Double.POSITIVE_INFINITY ) ) {
throw new IllegalArgumentException(
"radius may not be specified for cross-component traversal" );
}
}
/**
* The first time we see a vertex, make up a new queue entry for it.
*
* @param vertex a vertex which has just been encountered.
* @param edge the edge via which the vertex was encountered.
*
* @return the new queue entry.
*/
private QueueEntry createSeenData( Object vertex, Edge edge ) {
double shortestPathLength;
if( edge == null ) {
shortestPathLength = 0;
}
else {
shortestPathLength = calculatePathLength( vertex, edge );
}
QueueEntry entry = new QueueEntry( shortestPathLength );
entry.m_vertex = vertex;
entry.m_spanningTreeEdge = edge;
return entry;
}
/**
* Private data to associate with each entry in the priority queue.
*/
private static class QueueEntry extends FibonacciHeap.Node {
/** Best spanning tree edge to vertex seen so far. */
Edge m_spanningTreeEdge;
/** The vertex reached. */
Object m_vertex;
/** True once m_spanningTreeEdge is guaranteed to be the true minimum. */
boolean m_frozen;
QueueEntry( double key ) {
super( key );
}
double getShortestPathLength( ) {
return getKey( );
}
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/traverse/CrossComponentIterator.java 0000644 0001751 0001751 00000035160 10266566752 027757 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ---------------------------
* CrossComponentIterator.java
* ---------------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): John V. Sichi
*
* $Id: CrossComponentIterator.java,v 1.6 2005/04/23 08:09:29 perfecthash Exp $
*
* Changes
* -------
* 31-Jul-2003 : Initial revision (BN);
* 11-Aug-2003 : Adaptation to new event model (BN);
* 31-Jan-2004 : Extracted cross-component traversal functionality (BN);
*
*/
package org._3pq.jgrapht.traverse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import org._3pq.jgrapht.DirectedGraph;
import org._3pq.jgrapht.Edge;
import org._3pq.jgrapht.Graph;
import org._3pq.jgrapht.event.ConnectedComponentTraversalEvent;
import org._3pq.jgrapht.event.EdgeTraversalEvent;
import org._3pq.jgrapht.event.VertexTraversalEvent;
/**
* Provides a cross-connected-component traversal functionality for iterator
* subclasses.
*
* @author Barak Naveh
*
* @since Jan 31, 2004
*/
public abstract class CrossComponentIterator extends AbstractGraphIterator {
private static final int CCS_BEFORE_COMPONENT = 1;
private static final int CCS_WITHIN_COMPONENT = 2;
private static final int CCS_AFTER_COMPONENT = 3;
//
private final ConnectedComponentTraversalEvent m_ccFinishedEvent =
new ConnectedComponentTraversalEvent( this,
ConnectedComponentTraversalEvent.CONNECTED_COMPONENT_FINISHED );
private final ConnectedComponentTraversalEvent m_ccStartedEvent =
new ConnectedComponentTraversalEvent( this,
ConnectedComponentTraversalEvent.CONNECTED_COMPONENT_STARTED );
// TODO: support ConcurrentModificationException if graph modified
// during iteration.
private FlyweightEdgeEvent m_reusableEdgeEvent;
private FlyweightVertexEvent m_reusableVertexEvent;
private Iterator m_vertexIterator = null;
/**
* Stores the vertices that have been seen during iteration and
* (optionally) some additional traversal info regarding each vertex.
*/
private Map m_seen = new HashMap( );
private Object m_startVertex;
private Specifics m_specifics;
/** The connected component state */
private int m_state = CCS_BEFORE_COMPONENT;
/**
* Creates a new iterator for the specified graph. Iteration will start at
* the specified start vertex. If the specified start vertex is
* null
, Iteration will start at an arbitrary graph vertex.
*
* @param g the graph to be iterated.
* @param startVertex the vertex iteration to be started.
*
* @throws NullPointerException
* @throws IllegalArgumentException
*/
public CrossComponentIterator( Graph g, Object startVertex ) {
super( );
if( g == null ) {
throw new NullPointerException( "graph must not be null" );
}
m_specifics = createGraphSpecifics( g );
m_vertexIterator = g.vertexSet( ).iterator( );
setCrossComponentTraversal( startVertex == null );
m_reusableEdgeEvent = new FlyweightEdgeEvent( this, null );
m_reusableVertexEvent = new FlyweightVertexEvent( this, null );
if( startVertex == null ) {
// pick a start vertex if graph not empty
if( m_vertexIterator.hasNext( ) ) {
m_startVertex = m_vertexIterator.next( );
}
else {
m_startVertex = null;
}
}
else if( g.containsVertex( startVertex ) ) {
m_startVertex = startVertex;
}
else {
throw new IllegalArgumentException(
"graph must contain the start vertex" );
}
}
/**
* @see java.util.Iterator#hasNext()
*/
public boolean hasNext( ) {
if( m_startVertex != null ) {
encounterStartVertex( );
}
if( isConnectedComponentExhausted( ) ) {
if( m_state == CCS_WITHIN_COMPONENT ) {
m_state = CCS_AFTER_COMPONENT;
fireConnectedComponentFinished( m_ccFinishedEvent );
}
if( isCrossComponentTraversal( ) ) {
while( m_vertexIterator.hasNext( ) ) {
Object v = m_vertexIterator.next( );
if( !isSeenVertex( v ) ) {
encounterVertex( v, null );
m_state = CCS_BEFORE_COMPONENT;
return true;
}
}
return false;
}
else {
return false;
}
}
else {
return true;
}
}
/**
* @see java.util.Iterator#next()
*/
public Object next( ) {
if( m_startVertex != null ) {
encounterStartVertex( );
}
if( hasNext( ) ) {
if( m_state == CCS_BEFORE_COMPONENT ) {
m_state = CCS_WITHIN_COMPONENT;
fireConnectedComponentStarted( m_ccStartedEvent );
}
Object nextVertex = provideNextVertex( );
fireVertexTraversed( createVertexTraversalEvent( nextVertex ) );
addUnseenChildrenOf( nextVertex );
return nextVertex;
}
else {
throw new NoSuchElementException( );
}
}
/**
* Returns true if there are no more uniterated vertices in the
* currently iterated connected component; false otherwise.
*
* @return true if there are no more uniterated vertices in the
* currently iterated connected component; false
* otherwise.
*/
protected abstract boolean isConnectedComponentExhausted( );
/**
* Update data structures the first time we see a vertex.
*
* @param vertex the vertex encountered
* @param edge the edge via which the vertex was encountered, or null if
* the vertex is a starting point
*/
protected abstract void encounterVertex( Object vertex, Edge edge );
/**
* Returns the vertex to be returned in the following call to the iterator
* next
method.
*
* @return the next vertex to be returned by this iterator.
*/
protected abstract Object provideNextVertex( );
/**
* Access the data stored for a seen vertex.
*
* @param vertex a vertex which has already been seen.
*
* @return data associated with the seen vertex or null
if no
* data was associated with the vertex. A null
return
* can also indicate that the vertex was explicitly associated
* with null
.
*/
protected Object getSeenData( Object vertex ) {
return m_seen.get( vertex );
}
/**
* Determines whether a vertex has been seen yet by this traversal.
*
* @param vertex vertex in question
*
* @return true if vertex has already been seen
*/
protected boolean isSeenVertex( Object vertex ) {
return m_seen.containsKey( vertex );
}
/**
* Called whenever we re-encounter a vertex. The default implementation
* does nothing.
*
* @param vertex the vertex re-encountered
* @param edge the edge via which the vertex was re-encountered
*/
protected abstract void encounterVertexAgain( Object vertex, Edge edge );
/**
* Stores iterator-dependent data for a vertex that has been seen.
*
* @param vertex a vertex which has been seen.
* @param data data to be associated with the seen vertex.
*
* @return previous value associated with specified vertex or
* null
if no data was associated with the vertex. A
* null
return can also indicate that the vertex was
* explicitly associated with null
.
*/
protected Object putSeenData( Object vertex, Object data ) {
return m_seen.put( vertex, data );
}
static Specifics createGraphSpecifics( Graph g ) {
if( g instanceof DirectedGraph ) {
return new DirectedSpecifics( (DirectedGraph) g );
}
else {
return new UndirectedSpecifics( g );
}
}
private void addUnseenChildrenOf( Object vertex ) {
List edges = m_specifics.edgesOf( vertex );
for( Iterator i = edges.iterator( ); i.hasNext( ); ) {
Edge e = (Edge) i.next( );
fireEdgeTraversed( createEdgeTraversalEvent( e ) );
Object v = e.oppositeVertex( vertex );
if( isSeenVertex( v ) ) {
encounterVertexAgain( v, e );
}
else {
encounterVertex( v, e );
}
}
}
private EdgeTraversalEvent createEdgeTraversalEvent( Edge edge ) {
if( isReuseEvents( ) ) {
m_reusableEdgeEvent.setEdge( edge );
return m_reusableEdgeEvent;
}
else {
return new EdgeTraversalEvent( this, edge );
}
}
private VertexTraversalEvent createVertexTraversalEvent( Object vertex ) {
if( isReuseEvents( ) ) {
m_reusableVertexEvent.setVertex( vertex );
return m_reusableVertexEvent;
}
else {
return new VertexTraversalEvent( this, vertex );
}
}
private void encounterStartVertex( ) {
encounterVertex( m_startVertex, null );
m_startVertex = null;
}
static interface SimpleContainer {
/**
* Tests if this container is empty.
*
* @return true
if empty, otherwise false
.
*/
public boolean isEmpty( );
/**
* Adds the specified object to this container.
*
* @param o the object to be added.
*/
public void add( Object o );
/**
* Remove an object from this container and return it.
*
* @return the object removed from this container.
*/
public Object remove( );
}
/**
* Provides unified interface for operations that are different in directed
* graphs and in undirected graphs.
*/
abstract static class Specifics {
/**
* Returns the edges outgoing from the specified vertex in case of
* directed graph, and the edge touching the specified vertex in case
* of undirected graph.
*
* @param vertex the vertex whose outgoing edges are to be returned.
*
* @return the edges outgoing from the specified vertex in case of
* directed graph, and the edge touching the specified vertex
* in case of undirected graph.
*/
public abstract List edgesOf( Object vertex );
}
/**
* A reusable edge event.
*
* @author Barak Naveh
*
* @since Aug 11, 2003
*/
static class FlyweightEdgeEvent extends EdgeTraversalEvent {
private static final long serialVersionUID = 4051327833765000755L;
/**
* @see EdgeTraversalEvent#EdgeTraversalEvent(Object, Edge)
*/
public FlyweightEdgeEvent( Object eventSource, Edge edge ) {
super( eventSource, edge );
}
/**
* Sets the edge of this event.
*
* @param edge the edge to be set.
*/
protected void setEdge( Edge edge ) {
m_edge = edge;
}
}
/**
* A reusable vertex event.
*
* @author Barak Naveh
*
* @since Aug 11, 2003
*/
static class FlyweightVertexEvent extends VertexTraversalEvent {
private static final long serialVersionUID = 3834024753848399924L;
/**
* @see VertexTraversalEvent#VertexTraversalEvent(Object, Object)
*/
public FlyweightVertexEvent( Object eventSource, Object vertex ) {
super( eventSource, vertex );
}
/**
* Sets the vertex of this event.
*
* @param vertex the vertex to be set.
*/
protected void setVertex( Object vertex ) {
m_vertex = vertex;
}
}
/**
* An implementation of {@link TraverseUtils.Specifics} for a directed
* graph.
*/
private static class DirectedSpecifics extends Specifics {
private DirectedGraph m_graph;
/**
* Creates a new DirectedSpecifics object.
*
* @param g the graph for which this specifics object to be created.
*/
public DirectedSpecifics( DirectedGraph g ) {
m_graph = g;
}
/**
* @see CrossComponentIterator.Specifics#edgesOf(Object)
*/
public List edgesOf( Object vertex ) {
return m_graph.outgoingEdgesOf( vertex );
}
}
/**
* An implementation of {@link TraverseUtils.Specifics} in which edge
* direction (if any) is ignored.
*/
private static class UndirectedSpecifics extends Specifics {
private Graph m_graph;
/**
* Creates a new UndirectedSpecifics object.
*
* @param g the graph for which this specifics object to be created.
*/
public UndirectedSpecifics( Graph g ) {
m_graph = g;
}
/**
* @see CrossComponentIterator.Specifics#edgesOf(Object)
*/
public List edgesOf( Object vertex ) {
return m_graph.edgesOf( vertex );
}
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/traverse/DepthFirstIterator.java 0000644 0001751 0001751 00000007663 10266566752 027066 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -----------------------
* DepthFirstIterator.java
* -----------------------
* (C) Copyright 2003, by Liviu Rau and Contributors.
*
* Original Author: Liviu Rau
* Contributor(s): Barak Naveh
*
* $Id: DepthFirstIterator.java,v 1.10 2004/10/29 21:30:48 barak_naveh Exp $
*
* Changes
* -------
* 29-Jul-2003 : Initial revision (LR);
* 31-Jul-2003 : Fixed traversal across connected components (BN);
* 06-Aug-2003 : Extracted common logic to TraverseUtils.XXFirstIterator (BN);
* 31-Jan-2004 : Reparented and changed interface to parent class (BN);
*
*/
package org._3pq.jgrapht.traverse;
import java.util.ArrayList;
import java.util.List;
import org._3pq.jgrapht.Edge;
import org._3pq.jgrapht.Graph;
/**
* A depth-first iterator for a directed and an undirected graph. For this
* iterator to work correctly the graph must not be modified during iteration.
* Currently there are no means to ensure that, nor to fail-fast. The results
* of such modifications are undefined.
*
* @author Liviu Rau
* @author Barak Naveh
*
* @since Jul 29, 2003
*/
public class DepthFirstIterator extends CrossComponentIterator {
private List m_stack = new ArrayList( );
/**
* Creates a new depth-first iterator for the specified graph.
*
* @param g the graph to be iterated.
*/
public DepthFirstIterator( Graph g ) {
this( g, null );
}
/**
* Creates a new depth-first iterator for the specified graph. Iteration
* will start at the specified start vertex and will be limited to the
* connected component that includes that vertex. If the specified start
* vertex is null
, iteration will start at an arbitrary
* vertex and will not be limited, that is, will be able to traverse all
* the graph.
*
* @param g the graph to be iterated.
* @param startVertex the vertex iteration to be started.
*/
public DepthFirstIterator( Graph g, Object startVertex ) {
super( g, startVertex );
}
/**
* @see org._3pq.jgrapht.traverse.CrossComponentIterator#isConnectedComponentExhausted()
*/
protected boolean isConnectedComponentExhausted( ) {
return m_stack.isEmpty( );
}
/**
* @see org._3pq.jgrapht.traverse.CrossComponentIterator#encounterVertex(java.lang.Object,
* org._3pq.jgrapht.Edge)
*/
protected void encounterVertex( Object vertex, Edge edge ) {
putSeenData( vertex, null );
m_stack.add( vertex );
}
/**
* @see org._3pq.jgrapht.traverse.CrossComponentIterator#encounterVertexAgain(java.lang.Object,
* org._3pq.jgrapht.Edge)
*/
protected void encounterVertexAgain( Object vertex, Edge edge ) {}
/**
* @see org._3pq.jgrapht.traverse.CrossComponentIterator#provideNextVertex()
*/
protected Object provideNextVertex( ) {
return m_stack.remove( m_stack.size( ) - 1 );
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/traverse/GraphIterator.java 0000644 0001751 0001751 00000006717 10266566752 026052 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (barak_naveh@users.sourceforge.net)
*
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ------------------
* GraphIterator.java
* ------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: GraphIterator.java,v 1.2 2003/08/11 10:37:44 barak_naveh Exp $
*
* Changes
* -------
* 31-Jul-2003 : Initial revision (BN);
* 11-Aug-2003 : Adaptation to new event model (BN);
*
*/
package org._3pq.jgrapht.traverse;
import java.util.Iterator;
import org._3pq.jgrapht.event.TraversalListener;
/**
* A graph iterator.
*
* @author Barak Naveh
*
* @since Jul 31, 2003
*/
public interface GraphIterator extends Iterator {
/**
* Test whether this iterator is set to traverse the grpah across connected
* components.
*
* @return true
if traverses across connected components,
* otherwise false
.
*/
public boolean isCrossComponentTraversal( );
/**
* Sets a value the reuseEvents
flag. If the
* reuseEvents
flag is set to true
this class
* will reuse previously fired events and will not create a new object for
* each event. This option increases performance but should be used with
* care, especially in multithreaded environment.
*
* @param reuseEvents whether to reuse previously fired event objects
* instead of creating a new event object for each event.
*/
public void setReuseEvents( boolean reuseEvents );
/**
* Tests whether the reuseEvents
flag is set. If the flag is
* set to true
this class will reuse previously fired events
* and will not create a new object for each event. This option increases
* performance but should be used with care, especially in multithreaded
* environment.
*
* @return the value of the reuseEvents
flag.
*/
public boolean isReuseEvents( );
/**
* Adds the specified traversal listener to this iterator.
*
* @param l the traversal listener to be added.
*/
public void addTraversalListener( TraversalListener l );
/**
* Unsupported.
*
* @throws UnsupportedOperationException
*/
public void remove( );
/**
* Removes the specified traversal listener from this iterator.
*
* @param l the traversal listener to be removed.
*/
public void removeTraversalListener( TraversalListener l );
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/traverse/TopologicalOrderIterator.java 0000644 0001751 0001751 00000014671 10266566752 030257 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -----------------------------
* TopologicalOrderIterator.java
* -----------------------------
* (C) Copyright 2004, by Marden Neubert and Contributors.
*
* Original Author: Marden Neubert
* Contributor(s): Barak Naveh, John V. Sichi
*
* $Id: TopologicalOrderIterator.java,v 1.2 2005/04/26 06:46:38 perfecthash Exp $
*
* Changes
* -------
* 17-Dec-2004 : Initial revision (MN);
* 25-Apr-2005 : Fixes for start vertex order (JVS);
*
*/
package org._3pq.jgrapht.traverse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import org._3pq.jgrapht.DirectedGraph;
import org._3pq.jgrapht.Edge;
import org._3pq.jgrapht.util.ModifiableInteger;
/**
* Implements topological order traversal for a directed graph. A topological
* sort is a permutation p of the vertices of a graph such that an
* edge (i,j) implies that i appears before j in
* p (Skiena 1990, p. 208). See also
* http://mathworld.wolfram.com/TopologicalSort.html.
*
* * See "Algorithms in Java, Third Edition, Part 5: Graph Algorithms" by Robert * Sedgewick and "Data Structures and Algorithms with Object-Oriented Design * Patterns in Java" by Bruno R. Preiss for implementation alternatives. The * latter can be found online at http://www.brpreiss.com/books/opus5/ *
* ** For this iterator to work correctly the graph must not be modified during * iteration. Currently there are no means to ensure that, nor to fail-fast. * The results of such modifications are undefined. *
* * @author Marden Neubert * * @since Dec 18, 2004 */ public class TopologicalOrderIterator extends CrossComponentIterator { private LinkedList m_queue; private Map m_inDegreeMap; /** * Creates a new topological order iterator over the directed graph * specified. Traversal will start at one of the graphs sources. * See the definition of source at * http://mathworld.wolfram.com/Source.html. * * @param dg the directed graph to be iterated. */ public TopologicalOrderIterator( DirectedGraph dg ) { this( dg, new LinkedList( ), new HashMap( ) ); } // NOTE: This is a hack to deal with the fact that CrossComponentIterator // needs to know the start vertex in its constructor private TopologicalOrderIterator( DirectedGraph dg, LinkedList queue, Map inDegreeMap ) { this( dg, initialize( dg, queue, inDegreeMap ) ); m_queue = queue; m_inDegreeMap = inDegreeMap; } // NOTE: This is intentionally private, because starting the sort "in the // middle" doesn't make sense. private TopologicalOrderIterator( DirectedGraph dg, Object start ) { super( dg, start ); } /** * @see CrossComponentIterator#isConnectedComponentExhausted() */ protected boolean isConnectedComponentExhausted( ) { // FIXME jvs 25-Apr-2005: This isn't correct for a graph with more than // one component. We will actually exhaust a connected component // before the queue is empty, because initialize adds roots from all // components to the queue. return m_queue.isEmpty( ); } /** * @see CrossComponentIterator#encounterVertex(Object, Edge) */ protected void encounterVertex( Object vertex, Edge edge ) { putSeenData( vertex, null ); decrementInDegree( vertex ); } /** * @see CrossComponentIterator#encounterVertexAgain(Object, Edge) */ protected void encounterVertexAgain( Object vertex, Edge edge ) { decrementInDegree( vertex ); } /** * @see CrossComponentIterator#provideNextVertex() */ protected Object provideNextVertex( ) { return m_queue.removeFirst( ); } /** * Decrements the in-degree of a vertex. * * @param vertex the vertex whose in-degree will be decremented. */ private void decrementInDegree( Object vertex ) { ModifiableInteger inDegree = (ModifiableInteger) m_inDegreeMap.get( vertex ); if( inDegree.value > 0 ) { inDegree.value--; if( inDegree.value == 0 ) { m_queue.addLast( vertex ); } } } /** * Initializes the internal traversal object structure. Sets up the * internal queue with the directed graph vertices and creates the control * structure for the in-degrees. * * @param dg the directed graph to be iterated. * @param queue initializer for m_queue * @param inDegreeMap initializer for m_inDegreeMap * * @return start vertex */ private static Object initialize( DirectedGraph dg, LinkedList queue, Map inDegreeMap ) { for( Iterator i = dg.vertexSet( ).iterator( ); i.hasNext( ); ) { Object vertex = i.next( ); int inDegree = dg.inDegreeOf( vertex ); inDegreeMap.put( vertex, new ModifiableInteger( inDegree ) ); if( inDegree == 0 ) { queue.add( vertex ); } } if( queue.isEmpty( ) ) { return null; } else { return queue.getFirst( ); } } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/traverse/package.html 0000644 0001751 0001751 00000000155 10266566752 024703 0 ustar moeller moeller Graph traversal means. libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/util/ 0000755 0001751 0001751 00000000000 11251311101 021506 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/util/FibonacciHeap.java 0000644 0001751 0001751 00000046766 10266566752 025104 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (barak_naveh@users.sourceforge.net) * * (C) Copyright 2003, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* -------------------------- * FibonnaciHeap.java * -------------------------- * (C) Copyright 1999-2003, by Nathan Fiedler and Contributors. * * Original Author: Nathan Fiedler * Contributor(s): John V. Sichi * * $Id: FibonacciHeap.java,v 1.1 2003/09/04 18:15:14 perfecthash Exp $ * * Changes * ------- * 03-Sept-2003 : Adapted from Nathan Fiedler (JVS); * * Name Date Description * ---- ---- ----------- * nf 08/31/97 Initial version * nf 09/07/97 Removed FibHeapData interface * nf 01/20/01 Added synchronization * nf 01/21/01 Made Node an inner class * nf 01/05/02 Added clear(), renamed empty() to * isEmpty(), and renamed printHeap() * to toString() * nf 01/06/02 Removed all synchronization * */ package org._3pq.jgrapht.util; import java.util.Stack; /** * This class implements a Fibonacci heap data structure. Much of the code in * this class is based on the algorithms in the "Introduction to Algorithms" * by Cormen, Leiserson, and Rivest in Chapter 21. The amortized running time * of most of these methods is O(1), making it a very fast data structure. * Several have an actual running time of O(1). removeMin() and delete() have * O(log n) amortized running times because they do the heap consolidation. If * you attempt to store nodes in this heap with key values of -Infinity * (Double.NEGATIVE_INFINITY) thedelete()
operation may fail to
* remove the correct element.
*
* * Note that this implementation is not synchronized. If multiple * threads access a set concurrently, and at least one of the threads modifies * the set, it must be synchronized externally. This is typically * accomplished by synchronizing on some object that naturally encapsulates * the set. *
* ** This class was originally developed by Nathan Fiedler for the GraphMaker * project. It was imported to JGraphT with permission, courtesy of Nathan * Fiedler. *
* * @author Nathan Fiedler */ public class FibonacciHeap { /** Points to the minimum node in the heap. */ private Node m_min; /** Number of nodes in the heap. */ private int m_n; /** * Constructs a FibonacciHeap object that contains no elements. */ public FibonacciHeap( ) {} // FibonacciHeap /** * Tests if the Fibonacci heap is empty or not. Returns true if the heap is * empty, false otherwise. * ** Running time: O(1) actual *
* * @return true if the heap is empty, false otherwise */ public boolean isEmpty( ) { return m_min == null; } // isEmpty /** * Removes all elements from this heap. */ public void clear( ) { m_min = null; m_n = 0; } // clear /** * Decreases the key value for a heap node, given the new value to take on. * The structure of the heap may be changed and will not be consolidated. * ** Running time: O(1) amortized *
* * @param x node to decrease the key of * @param k new key value for node x * * @exception IllegalArgumentException Thrown if k is larger than x.key * value. */ public void decreaseKey( Node x, double k ) { if( k > x.m_key ) { throw new IllegalArgumentException( "decreaseKey() got larger key value" ); } x.m_key = k; Node y = x.m_parent; if( ( y != null ) && ( x.m_key < y.m_key ) ) { cut( x, y ); cascadingCut( y ); } if( x.m_key < m_min.m_key ) { m_min = x; } } // decreaseKey /** * Deletes a node from the heap given the reference to the node. The trees * in the heap will be consolidated, if necessary. This operation may fail * to remove the correct element if there are nodes with key value * -Infinity. * ** Running time: O(log n) amortized *
* * @param x node to remove from heap */ public void delete( Node x ) { // make x as small as possible decreaseKey( x, Double.NEGATIVE_INFINITY ); // remove the smallest, which decreases n also removeMin( ); } // delete /** * Inserts a new data element into the heap. No heap consolidation is * performed at this time, the new node is simply inserted into the root * list of this heap. * ** Running time: O(1) actual *
* * @param node new node to insert into heap * @param key key value associated with data object */ public void insert( Node node, double key ) { node.m_key = key; // concatenate node into min list if( m_min != null ) { node.m_left = m_min; node.m_right = m_min.m_right; m_min.m_right = node; node.m_right.m_left = node; if( key < m_min.m_key ) { m_min = node; } } else { m_min = node; } m_n++; } // insert /** * Returns the smallest element in the heap. This smallest element is the * one with the minimum key value. * ** Running time: O(1) actual *
* * @return heap node with the smallest key */ public Node min( ) { return m_min; } // min /** * Removes the smallest element from the heap. This will cause the trees in * the heap to be consolidated, if necessary. * ** Running time: O(log n) amortized *
* * @return node with the smallest key */ public Node removeMin( ) { Node z = m_min; if( z != null ) { int numKids = z.m_degree; Node x = z.m_child; Node tempRight; // for each child of z do... while( numKids > 0 ) { tempRight = x.m_right; // remove x from child list x.m_left.m_right = x.m_right; x.m_right.m_left = x.m_left; // add x to root list of heap x.m_left = m_min; x.m_right = m_min.m_right; m_min.m_right = x; x.m_right.m_left = x; // set parent[x] to null x.m_parent = null; x = tempRight; numKids--; } // remove z from root list of heap z.m_left.m_right = z.m_right; z.m_right.m_left = z.m_left; if( z == z.m_right ) { m_min = null; } else { m_min = z.m_right; consolidate( ); } // decrement size of heap m_n--; } return z; } // removeMin /** * Returns the size of the heap which is measured in the number of elements * contained in the heap. * ** Running time: O(1) actual *
* * @return number of elements in the heap */ public int size( ) { return m_n; } // size /** * Joins two Fibonacci heaps into a new one. No heap consolidation is * performed at this time. The two root lists are simply joined together. * ** Running time: O(1) actual *
* * @param h1 first heap * @param h2 second heap * * @return new heap containing h1 and h2 */ public static FibonacciHeap union( FibonacciHeap h1, FibonacciHeap h2 ) { FibonacciHeap h = new FibonacciHeap( ); if( ( h1 != null ) && ( h2 != null ) ) { h.m_min = h1.m_min; if( h.m_min != null ) { if( h2.m_min != null ) { h.m_min.m_right.m_left = h2.m_min.m_left; h2.m_min.m_left.m_right = h.m_min.m_right; h.m_min.m_right = h2.m_min; h2.m_min.m_left = h.m_min; if( h2.m_min.m_key < h1.m_min.m_key ) { h.m_min = h2.m_min; } } } else { h.m_min = h2.m_min; } h.m_n = h1.m_n + h2.m_n; } return h; } // union /** * Creates a String representation of this Fibonacci heap. * * @return String of this. */ public String toString( ) { if( m_min == null ) { return "FibonacciHeap=[]"; } // create a new stack and put root on it Stack stack = new Stack( ); stack.push( m_min ); StringBuffer buf = new StringBuffer( 512 ); buf.append( "FibonacciHeap=[" ); // do a simple breadth-first traversal on the tree while( !stack.empty( ) ) { Node curr = (Node) stack.pop( ); buf.append( curr ); buf.append( ", " ); if( curr.m_child != null ) { stack.push( curr.m_child ); } Node start = curr; curr = curr.m_right; while( curr != start ) { buf.append( curr ); buf.append( ", " ); if( curr.m_child != null ) { stack.push( curr.m_child ); } curr = curr.m_right; } } buf.append( ']' ); return buf.toString( ); } // toString /** * Performs a cascading cut operation. This cuts y from its parent and then * does the same for its parent, and so on up the tree. * ** Running time: O(log n); O(1) excluding the recursion *
* * @param y node to perform cascading cut on */ protected void cascadingCut( Node y ) { Node z = y.m_parent; // if there's a parent... if( z != null ) { // if y is unmarked, set it marked if( !y.m_mark ) { y.m_mark = true; } else { // it's marked, cut it from parent cut( y, z ); // cut its parent as well cascadingCut( z ); } } } // cascadingCut /** * Consolidates the trees in the heap by joining trees of equal degree * until there are no more trees of equal degree in the root list. * ** Running time: O(log n) amortized *
*/ protected void consolidate( ) { int arraySize = m_n + 1; Node[] array = new Node[ arraySize ]; // Initialize degree array for( int i = 0; i < arraySize; i++ ) { array[ i ] = null; } // Find the number of root nodes. int numRoots = 0; Node x = m_min; if( x != null ) { numRoots++; x = x.m_right; while( x != m_min ) { numRoots++; x = x.m_right; } } // For each node in root list do... while( numRoots > 0 ) { // Access this node's degree.. int d = x.m_degree; Node next = x.m_right; // ..and see if there's another of the same degree. while( array[ d ] != null ) { // There is, make one of the nodes a child of the other. Node y = array[ d ]; // Do this based on the key value. if( x.m_key > y.m_key ) { Node temp = y; y = x; x = temp; } // Node y disappears from root list. link( y, x ); // We've handled this degree, go to next one. array[ d ] = null; d++; } // Save this node for later when we might encounter another // of the same degree. array[ d ] = x; // Move forward through list. x = next; numRoots--; } // Set min to null (effectively losing the root list) and // reconstruct the root list from the array entries in array[]. m_min = null; for( int i = 0; i < arraySize; i++ ) { if( array[ i ] != null ) { // We've got a live one, add it to root list. if( m_min != null ) { // First remove node from root list. array[ i ].m_left.m_right = array[ i ].m_right; array[ i ].m_right.m_left = array[ i ].m_left; // Now add to root list, again. array[ i ].m_left = m_min; array[ i ].m_right = m_min.m_right; m_min.m_right = array[ i ]; array[ i ].m_right.m_left = array[ i ]; // Check if this is a new min. if( array[ i ].m_key < m_min.m_key ) { m_min = array[ i ]; } } else { m_min = array[ i ]; } } } } // consolidate /** * The reverse of the link operation: removes x from the child list of y. * This method assumes that min is non-null. * ** Running time: O(1) *
* * @param x child of y to be removed from y's child list * @param y parent of x about to lose a child */ protected void cut( Node x, Node y ) { // remove x from childlist of y and decrement degree[y] x.m_left.m_right = x.m_right; x.m_right.m_left = x.m_left; y.m_degree--; // reset y.child if necessary if( y.m_child == x ) { y.m_child = x.m_right; } if( y.m_degree == 0 ) { y.m_child = null; } // add x to root list of heap x.m_left = m_min; x.m_right = m_min.m_right; m_min.m_right = x; x.m_right.m_left = x; // set parent[x] to nil x.m_parent = null; // set mark[x] to false x.m_mark = false; } // cut /** * Make node y a child of node x. * ** Running time: O(1) actual *
* * @param y node to become child * @param x node to become parent */ protected void link( Node y, Node x ) { // remove y from root list of heap y.m_left.m_right = y.m_right; y.m_right.m_left = y.m_left; // make y a child of x y.m_parent = x; if( x.m_child == null ) { x.m_child = y; y.m_right = y; y.m_left = y; } else { y.m_left = x.m_child; y.m_right = x.m_child.m_right; x.m_child.m_right = y; y.m_right.m_left = y; } // increase degree[x] x.m_degree++; // set mark[y] false y.m_mark = false; } // link /** * Implements a node of the Fibonacci heap. It holds the information * necessary for maintaining the structure of the heap. It also holds the * reference to the key value (which is used to determine the heap * structure). Additional Node data should be stored in a subclass. * * @author Nathan Fiedler */ public static class Node { /** first child node */ Node m_child; /** left sibling node */ Node m_left; /** parent node */ Node m_parent; /** right sibling node */ Node m_right; /** * true if this node has had a child removed since this node was added * to its parent */ boolean m_mark; /** key value for this node */ double m_key; /** number of children of this node (does not count grandchildren) */ int m_degree; /** * Default constructor. Initializes the right and left pointers, * making this a circular doubly-linked list. * * @param key initial key for node */ public Node( double key ) { m_right = this; m_left = this; m_key = key; } /** * Obtain the key for this node. * * @return the key */ public final double getKey( ) { return m_key; } /** * Return the string representation of this object. * * @return string representing this object */ public String toString( ) { if( true ) { return Double.toString( m_key ); } else { StringBuffer buf = new StringBuffer( ); buf.append( "Node=[parent = " ); if( m_parent != null ) { buf.append( Double.toString( m_parent.m_key ) ); } else { buf.append( "---" ); } buf.append( ", key = " ); buf.append( Double.toString( m_key ) ); buf.append( ", degree = " ); buf.append( Integer.toString( m_degree ) ); buf.append( ", right = " ); if( m_right != null ) { buf.append( Double.toString( m_right.m_key ) ); } else { buf.append( "---" ); } buf.append( ", left = " ); if( m_left != null ) { buf.append( Double.toString( m_left.m_key ) ); } else { buf.append( "---" ); } buf.append( ", child = " ); if( m_child != null ) { buf.append( Double.toString( m_child.m_key ) ); } else { buf.append( "---" ); } buf.append( ']' ); return buf.toString( ); } } // toString } // Node } // FibonacciHeap libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/util/ModifiableInteger.java 0000644 0001751 0001751 00000020624 10266566752 025763 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ---------------------- * ModifiableInteger.java * ---------------------- * * (C) Copyright 2002-2004, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: ModifiableInteger.java,v 1.3 2005/07/17 05:40:49 perfecthash Exp $ * * Changes * ------- * 2004-05-27 : Initial version (BN); * */ package org._3pq.jgrapht.util; /** * TheModifiableInteger
class wraps a value of the primitive type
* int
in an object, similarly to {@link java.lang.Integer}. An
* object of type ModifiableInteger
contains a single field whose
* type is int
.
*
*
* Unlike java.lang.Integer
, the int value which the
* ModifiableInteger represents can be modified. It becomes useful when used
* together with the collection framework. For example, if you want to have a
* {@link java.util.List} of counters. You could use Integer
but
* that would have became wasteful and inefficient if you frequently had to
* update the counters.
*
* WARNING: Because instances of this class are mutable, great care must be
* exercised if used as keys of a {@link java.util.Map} or as values in a
* {@link java.util.Set} in a manner that affects equals comparisons while the
* instances are keys in the map (or values in the set). For more see
* documentation of Map
and Set
.
*
ModifiableInteger
. */
public int value;
/**
* !!! DON'T USE - Use the {@link #ModifiableInteger(int)} constructor
* instead !!!
*
* * This constructor is for the use of java.beans.XMLDecoder * deserialization. The constructor is marked as 'deprecated' to indicate * to the programmer against using it by mistake. *
* * @deprecated not really deprecated, just marked so to avoid mistaken use. */ public ModifiableInteger( ) {} /** * Constructs a newly allocatedModifiableInteger
object that
* represents the specified int
value.
*
* @param value the value to be represented by the
* ModifiableInteger
object.
*/
public ModifiableInteger( int value ) {
this.value = value;
}
/**
* Sets a new value for this modifiable integer.
*
* @param value the new value to set.
*/
public void setValue( int value ) {
this.value = value;
}
/**
* Returns the value of this object, similarly to {@link #intValue()}. This
* getter is NOT redundant. It is used for serialization by
* java.beans.XMLEncoder.
*
* @return the value.
*/
public int getValue( ) {
return this.value;
}
/**
* Compares two ModifiableInteger
objects numerically.
*
* @param anotherInteger the ModifiableInteger
to be compared.
*
* @return the value 0
if this ModifiableInteger
* is equal to the argument ModifiableInteger
; a
* value less than 0
if this
* ModifiableInteger
is numerically less than the
* argument ModifiableInteger
; and a value greater
* than 0
if this ModifiableInteger
is
* numerically greater than the argument
* ModifiableInteger
(signed comparison).
*/
public int compareTo( ModifiableInteger anotherInteger ) {
int thisVal = this.value;
int anotherVal = anotherInteger.value;
return thisVal < anotherVal ? -1 : ( thisVal == anotherVal ? 0 : 1 );
}
/**
* Compares this ModifiableInteger
object to another object.
* If the object is an ModifiableInteger
, this function
* behaves like compareTo(Integer)
. Otherwise, it throws a
* ClassCastException
(as ModifiableInteger
* objects are only comparable to other ModifiableInteger
* objects).
*
* @param o the Object
to be compared.
*
* @return the value 0
if the argument is a
* ModifiableInteger
numerically equal to this
* ModifiableInteger
; a value less than
* 0
if the argument is a
* ModifiableInteger
numerically greater than this
* ModifiableInteger
; and a value greater than
* 0
if the argument is a
* ModifiableInteger
numerically less than this
* ModifiableInteger
.
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo( Object o ) {
return compareTo( (ModifiableInteger) o );
}
/**
* @see Number#doubleValue()
*/
public double doubleValue( ) {
return this.value;
}
/**
* Compares this object to the specified object. The result is
* true
if and only if the argument is not null
* and is an ModifiableInteger
object that contains the same
* int
value as this object.
*
* @param o the object to compare with.
*
* @return true
if the objects are the same;
* false
otherwise.
*/
public boolean equals( Object o ) {
if( o instanceof ModifiableInteger ) {
return this.value == ( (ModifiableInteger) o ).value;
}
return false;
}
/**
* @see Number#floatValue()
*/
public float floatValue( ) {
return this.value;
}
/**
* Returns a hash code for this ModifiableInteger
.
*
* @return a hash code value for this object, equal to the primitive
* int
value represented by this
* ModifiableInteger
object.
*/
public int hashCode( ) {
return this.value;
}
/**
* @see Number#intValue()
*/
public int intValue( ) {
return this.value;
}
/**
* @see Number#longValue()
*/
public long longValue( ) {
return this.value;
}
/**
* Returns an Integer
object representing this
* ModifiableInteger
's value.
*
* @return an Integer
representation of the value of this
* object.
*/
public Integer toInteger( ) {
return new Integer( this.value );
}
/**
* Returns a String
object representing this
* ModifiableInteger
's value. The value is converted to
* signed decimal representation and returned as a string, exactly as if
* the integer value were given as an argument to the {@link
* java.lang.Integer#toString(int)} method.
*
* @return a string representation of the value of this object in
* base 10.
*/
public String toString( ) {
return String.valueOf( this.value );
}
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/util/package.html 0000644 0001751 0001751 00000000256 10266566752 024027 0 ustar moeller moeller
Non-graph-specific data structures, algorithms, and utilities used by
JGraphT.
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/DirectedGraph.java 0000644 0001751 0001751 00000006536 10266566752 024150 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ------------------
* DirectedGraph.java
* ------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: DirectedGraph.java,v 1.3 2004/05/01 23:15:46 barak_naveh Exp $
*
* Changes
* -------
* 24-Jul-2003 : Initial revision (BN);
*
*/
package org._3pq.jgrapht;
import java.util.List;
/**
* A graph whose all edges are directed. This is the root interface of all
* directed graphs.
*
* * See * http://mathworld.wolfram.com/DirectedGraph.html for more on directed * graphs. *
* * @author Barak Naveh * * @since Jul 14, 2003 */ public interface DirectedGraph extends Graph { /** * Returns the "in degree" of the specified vertex. An in degree of a * vertex in a directed graph is the number of inward directed edges from * that vertex. See * http://mathworld.wolfram.com/Indegree.html. * * @param vertex vertex whose degree is to be calculated. * * @return the degree of the specified vertex. */ public int inDegreeOf( Object vertex ); /** * Returns a list of all edges incoming into the specified vertex. * * @param vertex the vertex for which the list of incoming edges to be * returned. * * @return a list of all edges incoming into the specified vertex. */ public List incomingEdgesOf( Object vertex ); /** * Returns the "out degree" of the specified vertex. An out degree of a * vertex in a directed graph is the number of outward directed edges from * that vertex. See * http://mathworld.wolfram.com/Outdegree.html. * * @param vertex vertex whose degree is to be calculated. * * @return the degree of the specified vertex. */ public int outDegreeOf( Object vertex ); /** * Returns a list of all edges outgoing from the specified vertex. * * @param vertex the vertex for which the list of outgoing edges to be * returned. * * @return a list of all edges outgoing from the specified vertex. */ public List outgoingEdgesOf( Object vertex ); } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/Edge.java 0000644 0001751 0001751 00000010432 10266566752 022275 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------- * Edge.java * --------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: Edge.java,v 1.6 2004/05/01 23:15:46 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 06-Nov-2003 : Change edge sharing semantics (JVS); * */ package org._3pq.jgrapht; /** * An edge used with graph objects. This is the root interface in the edge * hierarchy. * ** NOTE: the source and target associations of an Edge must be immutable after * construction for all implementations. The reason is that once an Edge is * added to a Graph, the Graph representation may be optimized via internal * indexing data structures; if the Edge associations were to change, these * structures would be corrupted. However, other properties of an edge (such * as weight or label) may be mutable, although this still requires caution: * changes to Edges shared by multiple Graphs may not always be desired, and * indexing mechanisms for these properties may require a change notification * mechanism. *
* * @author Barak Naveh * * @since Jul 14, 2003 */ public interface Edge extends Cloneable { /** The default weight for an edge. */ public static double DEFAULT_EDGE_WEIGHT = 1.0; /** * Returns the source vertex of this edge. * * @return the source vertex of this edge. */ public Object getSource( ); /** * Returns the target vertex of this edge. * * @return the target vertex of this edge. */ public Object getTarget( ); /** * Sets the weight of this edge. If this edge is unweighted an *UnsupportedOperationException
is thrown.
*
* @param weight new weight.
*
* @throws UnsupportedOperationException if this edge is unweighted.
*/
public void setWeight( double weight );
/**
* Returns the weight of this edge. If this edge is unweighted the value
* 1.0
is returned.
*
* @return the weight of this element.
*/
public double getWeight( );
/**
* Creates and returns a shallow copy of this edge. The vertices of this
* edge are not cloned.
*
* @return a shallow copy of this edge.
*
* @see Cloneable
*/
public Object clone( );
/**
* Returns true if this edge contains the specified vertex. More
* formally, returns true if and only if the following condition
* holds:
* * this.getSource().equals(v) || this.getTarget().equals(v) ** * @param v vertex whose presence in this edge is to be tested. * * @return true if this edge contains the specified vertex. */ public boolean containsVertex( Object v ); /** * Returns the vertex opposite to the specified vertex. * * @param v the vertex whose opposite is required. * * @return the vertex opposite to the specified vertex. * * @throws IllegalArgumentException if v is neither the source nor the * target vertices of this edge. * @throws NullPointerException if v is
null
.
*/
public Object oppositeVertex( Object v );
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/EdgeFactory.java 0000644 0001751 0001751 00000003647 10266566752 023637 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ----------------
* EdgeFactory.java
* ----------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: EdgeFactory.java,v 1.2 2004/05/01 23:15:46 barak_naveh Exp $
*
* Changes
* -------
* 24-Jul-2003 : Initial revision (BN);
*
*/
package org._3pq.jgrapht;
/**
* An edge factory used by graphs for creating new edges.
*
* @author Barak Naveh
*
* @since Jul 14, 2003
*/
public interface EdgeFactory {
/**
* Creates a new edge whose endpoints are the specified source and target
* vertices.
*
* @param sourceVertex the source vertex.
* @param targetVertex the target vertex.
*
* @return a new edge whose endpoints are the specified source and target
* vertices.
*/
public Edge createEdge( Object sourceVertex, Object targetVertex );
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/Graph.java 0000644 0001751 0001751 00000043225 10266566752 022500 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ----------
* Graph.java
* ----------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): John V. Sichi
*
* $Id: Graph.java,v 1.13 2005/05/25 00:38:37 perfecthash Exp $
*
* Changes
* -------
* 24-Jul-2003 : Initial revision (BN);
* 06-Nov-2003 : Change edge sharing semantics (JVS);
*
*/
package org._3pq.jgrapht;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* The root interface in the graph hierarchy. A mathematical graph-theory
* graph object G(V,E) contains a set V of vertices and a
* set E of edges. Each edge e=(v1,v2) in E connects vertex v1 to
* vertex v2. for more information about graphs and their related definitions
* see
* http://mathworld.wolfram.com/Graph.html.
*
*
* This library generally follows the terminology found at:
* http://mathworld.wolfram.com/topics/GraphTheory.html. Implementation of
* this interface can provide simple-graphs, multigraphs, pseudographs etc.
* The package org._3pq.jgrapht.graph
provides a gallery of
* abstract and concrete graph implementations.
*
* This library works best when vertices represent arbitrary objects and edges * represent the relationships between them. Vertex and edge instances may be * shared by more than one graph. *
* * @author Barak Naveh * * @since Jul 14, 2003 */ public interface Graph { /** * Returns a list of all edges connecting source vertex to target vertex if * such vertices exist in this graph. If any of the vertices does not * exist or isnull
, returns null
. If both
* vertices exist but no edges found, returns an empty list.
*
* * In undirected graphs, some of the returned edges may have their source * and target vertices in the opposite order. In simple graphs the * returned list is either singleton list or empty list. *
* * @param sourceVertex source vertex of the edge. * @param targetVertex target vertex of the edge. * * @return a list of all edges connecting source vertex to target vertex. */ public List getAllEdges( Object sourceVertex, Object targetVertex ); /** * Returns an edge connecting source vertex to target vertex if such * vertices and such edge exist in this graph. Otherwise returns *null
. If any of the specified vertices is
* null
returns null
*
* * In undirected graphs, the returned edge may have its source and target * vertices in the opposite order. *
* * @param sourceVertex source vertex of the edge. * @param targetVertex target vertex of the edge. * * @return an edge connecting source vertex to target vertex. */ public Edge getEdge( Object sourceVertex, Object targetVertex ); /** * Returns the edge factory using which this graph creates new edges. The * edge factory is defined when the graph is constructed and must not be * modified. * * @return the edge factory using which this graph creates new edges. */ public EdgeFactory getEdgeFactory( ); /** * Adds all of the specified edges to this graph. The behavior of this * operation is undefined if the specified vertex collection is modified * while the operation is in progress. This method will invoke the {@link * #addEdge(Edge)} method. * * @param edges the edges to be added to this graph. * * @return true if this graph changed as a result of the call * * @throws NullPointerException if the specified edges contains one or more * null edges, or if the specified vertex collection is * null. * * @see #addVertex(Object) */ public boolean addAllEdges( Collection edges ); /** * Adds all of the specified vertices to this graph. The behavior of this * operation is undefined if the specified vertex collection is modified * while the operation is in progress. This method will invoke the {@link * #addVertex(Object)} method. * * @param vertices the vertices to be added to this graph. * * @return true if this graph changed as a result of the call * * @throws NullPointerException if the specified vertices contains one or * more null vertices, or if the specified vertex collection is * null. * * @see #addVertex(Object) */ public boolean addAllVertices( Collection vertices ); /** * Creates a new edge in this graph, going from the source vertex to the * target vertex, and returns the created edge. Some graphs do not allow * edge-multiplicity. In such cases, if the graph already contains an edge * from the specified source to the specified target, than this method * does not change the graph and returnsnull
.
*
* * The source and target vertices must already be contained in this graph. * If they are not found in graph IllegalArgumentException is thrown. *
* *
* This method creates the new edge e
using this graph's
* EdgeFactory
. For the new edge to be added e
* must not be equal to any other edge the graph (even if the graph
* allows edge-multiplicity). More formally, the graph must not contain
* any edge e2
such that e2.equals(e)
. If such
* e2
is found then the newly created edge e
is
* abandoned, the method leaves this graph unchanged returns
* null
.
*
null
.
*
* @throws IllegalArgumentException if source or target vertices are not
* found in the graph.
* @throws NullPointerException if any of the specified vertices is
* null
.
*
* @see #getEdgeFactory()
*/
public Edge addEdge( Object sourceVertex, Object targetVertex );
/**
* Adds the specified edge to this graph. More formally, adds the specified
* edge, e
, to this graph if this graph contains no edge
* e2
such that e2.equals(e)
. If this graph
* already contains such edge, the call leaves this graph unchanged and
* returns false. If the edge was added to the graph, returns
* true
.
*
*
* Some graphs do not allow edge-multiplicity. In such cases, if the graph
* already contains an edge going from e.getSource()
vertex
* to e.getTarget()
vertex, than this method does not change
* the graph and returns false
.
*
* The source and target vertices of the specified edge must already be in * this graph. If this is not the case, IllegalArgumentException is * thrown. The edge must also be assignment compatible with the class of * the edges produced by the edge factory of this graph. If this is not * the case ClassCastException is thrown. *
* * @param e edge to be added to this graph. * * @return true if this graph did not already contain the * specified edge. * * @throws IllegalArgumentException if source or target vertices of * specified edge are not found in this graph. * @throws ClassCastException if the specified edge is not assignment * compatible with the class of edges produced by the edge factory * of this graph. * @throws NullPointerException if the specified edge isnull
.
*
* @see #addEdge(Object, Object)
* @see #getEdgeFactory()
* @see EdgeFactory
*/
public boolean addEdge( Edge e );
/**
* Adds the specified vertex to this graph if not already present. More
* formally, adds the specified vertex, v
, to this graph if
* this graph contains no vertex u
such that
* u.equals(v)
. If this graph already contains such vertex,
* the call leaves this graph unchanged and returns false. In
* combination with the restriction on constructors, this ensures that
* graphs never contain duplicate vertices.
*
* @param v vertex to be added to this graph.
*
* @return true if this graph did not already contain the
* specified vertex.
*
* @throws NullPointerException if the specified vertex is
* null
.
*/
public boolean addVertex( Object v );
/**
* Returns true if and only if this graph contains an edge going
* from the source vertex to the target vertex. In undirected graphs the
* same result is obtained when source and target are inverted. If any of
* the specified vertices does not exist in the graph, or if is
* null
, returns false
.
*
* @param sourceVertex source vertex of the edge.
* @param targetVertex target vertex of the edge.
*
* @return true if this graph contains the specified edge.
*/
public boolean containsEdge( Object sourceVertex, Object targetVertex );
/**
* Returns true if this graph contains the specified edge. More
* formally, returns true if and only if this graph contains an
* edge e2
such that e.equals(e2)
. If the
* specified edge is null
returns false
.
*
* @param e edge whose presence in this graph is to be tested.
*
* @return true if this graph contains the specified edge.
*/
public boolean containsEdge( Edge e );
/**
* Returns true if this graph contains the specified vertex. More
* formally, returns true if and only if this graph contains a
* vertex u
such that u.equals(v)
. If the
* specified vertex is null
returns false
.
*
* @param v vertex whose presence in this graph is to be tested.
*
* @return true if this graph contains the specified vertex.
*/
public boolean containsVertex( Object v );
/**
* Returns a set of the edges contained in this graph. The set is backed by
* the graph, so changes to the graph are reflected in the set. If the
* graph is modified while an iteration over the set is in progress, the
* results of the iteration are undefined.
*
* * The graph implementation may maintain a particular set ordering (e.g. * via {@link java.util.LinkedHashSet}) for deterministic iteration, but * this is not required. It is the responsibility of callers who rely on * this behavior to only use graph implementations which support it. *
* * @return a set of the edges contained in this graph. */ public Set edgeSet( ); /** * Returns a list of all edges touching the specified vertex. If no edges * are touching the specified vertex returns an empty list. * * @param vertex the vertex for which a list of touching edges to be * returned. * * @return a list of all edges touching the specified vertex. */ public List edgesOf( Object vertex ); /** * Removes all the edges in this graph that are also contained in the * specified edge collection. After this call returns, this graph will * contain no edges in common with the specified edges. This method will * invoke the {@link #removeEdge(Edge)} method. * * @param edges edges to be removed from this graph. * * @return true if this graph changed as a result of the call * * @throws NullPointerException if the specified edge collection is * null. * * @see #removeEdge(Edge) * @see #containsEdge(Edge) */ public boolean removeAllEdges( Collection edges ); /** * Removes all the edges going from the specified source vertex to the * specified target vertex, and returns a list of all removed edges. * Returnsnull
if any of the specified vertices does exist
* in the graph. If both vertices exist but no edge found, returns an
* empty list. This method will either invoke the {@link
* #removeEdge(Edge)} method, or the {@link #removeEdge(Object, Object)}
* method.
*
* @param sourceVertex source vertex of the edge.
* @param targetVertex target vertex of the edge.
*
* @return The removed edge, or null
if no edge removed.
*/
public List removeAllEdges( Object sourceVertex, Object targetVertex );
/**
* Removes all the vertices in this graph that are also contained in the
* specified vertex collection. After this call returns, this graph will
* contain no vertices in common with the specified vertices. This method
* will invoke the {@link #removeVertex(Object)} method.
*
* @param vertices vertices to be removed from this graph.
*
* @return true if this graph changed as a result of the call
*
* @throws NullPointerException if the specified vertex collection is
* null.
*
* @see #removeVertex(Object)
* @see #containsVertex(Object)
*/
public boolean removeAllVertices( Collection vertices );
/**
* Removes an edge going from source vertex to target vertex, if such
* vertices and such edge exist in this graph. Returns the edge if removed
* or null
otherwise.
*
* @param sourceVertex source vertex of the edge.
* @param targetVertex target vertex of the edge.
*
* @return The removed edge, or null
if no edge removed.
*/
public Edge removeEdge( Object sourceVertex, Object targetVertex );
/**
* Removes the specified edge from the graph. Removes the specified edge
* from this graph if it is present. More formally, removes an edge
* e2
such that e2.equals(e)
, if the graph
* contains such edge. Returns true if the graph contained the
* specified edge. (The graph will not contain the specified edge once the
* call returns).
*
*
* If the specified edge is null
returns false
.
*
true
if and only if the graph contained the
* specified edge.
*/
public boolean removeEdge( Edge e );
/**
* Removes the specified vertex from this graph including all its touching
* edges if present. More formally, if the graph contains a vertex
* u
such that u.equals(v)
, the call removes all
* edges that touch u
and then removes u
itself.
* If no such u
is found, the call leaves the graph
* unchanged. Returns true if the graph contained the specified
* vertex. (The graph will not contain the specified vertex once the call
* returns).
*
*
* If the specified vertex is null
returns false
.
*
true
if the graph contained the specified vertex;
* false
otherwise.
*/
public boolean removeVertex( Object v );
/**
* Returns a set of the vertices contained in this graph. The set is backed
* by the graph, so changes to the graph are reflected in the set. If the
* graph is modified while an iteration over the set is in progress, the
* results of the iteration are undefined.
*
* * The graph implementation may maintain a particular set ordering (e.g. * via {@link java.util.LinkedHashSet}) for deterministic iteration, but * this is not required. It is the responsibility of callers who rely on * this behavior to only use graph implementations which support it. *
* * @return a set view of the vertices contained in this graph. */ public Set vertexSet( ); } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/GraphHelper.java 0000644 0001751 0001751 00000025052 10266566752 023636 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ---------------- * GraphHelper.java * ---------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): Mikael Hansen * * $Id: GraphHelper.java,v 1.11 2005/04/23 08:25:17 perfecthash Exp $ * * Changes * ------- * 10-Jul-2003 : Initial revision (BN); * 06-Nov-2003 : Change edge sharing semantics (JVS); * */ package org._3pq.jgrapht; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org._3pq.jgrapht.edge.DirectedEdge; import org._3pq.jgrapht.graph.AsUndirectedGraph; /** * A collection of utilities to assist the working with graphs. * * @author Barak Naveh * * @since Jul 31, 2003 */ public final class GraphHelper { private GraphHelper( ) {} // ensure non-instantiability. /** * Creates a new edge and adds it to the specified graph similarly to the * {@link Graph#addEdge(Object, Object)} method. * * @param g the graph for which the edge to be added. * @param sourceVertex source vertex of the edge. * @param targetVertex target vertex of the edge. * @param weight weight of the edge. * * @return The newly created edge if added to the graph, otherwise *null
.
*
* @see Graph#addEdge(Object, Object)
*/
public static Edge addEdge( Graph g, Object sourceVertex,
Object targetVertex, double weight ) {
EdgeFactory ef = g.getEdgeFactory( );
Edge e = ef.createEdge( sourceVertex, targetVertex );
// we first create the edge and set the weight to make sure that
// listeners will see the correct weight upon addEdge.
e.setWeight( weight );
return g.addEdge( e ) ? e : null;
}
/**
* Adds the specified edge to the specified graph including its vertices.
* If any of the vertices of the specified edge are not already in the
* graph they are also added (before the edge is added).
*
* @param g the graph for which the specified edge to be added.
* @param e the edge to be added to the graph (including its vertices).
*
* @return true
if and only if the specified edge was not
* already contained in the graph.
*/
public static boolean addEdgeWithVertices( Graph g, Edge e ) {
g.addVertex( e.getSource( ) );
g.addVertex( e.getTarget( ) );
return g.addEdge( e );
}
/**
* Adds the specified source and target vertices to the graph, if not
* already included, and creates a new edge and adds it to the specified
* graph similarly to the {@link Graph#addEdge(Object, Object)} method.
*
* @param g the graph for which the specified edge to be added.
* @param sourceVertex source vertex of the edge.
* @param targetVertex target vertex of the edge.
*
* @return The newly created edge if added to the graph, otherwise
* null
.
*/
public static Edge addEdgeWithVertices( Graph g, Object sourceVertex,
Object targetVertex ) {
g.addVertex( sourceVertex );
g.addVertex( targetVertex );
return g.addEdge( sourceVertex, targetVertex );
}
/**
* Adds the specified source and target vertices to the graph, if not
* already included, and creates a new weighted edge and adds it to the
* specified graph similarly to the {@link Graph#addEdge(Object, Object)}
* method.
*
* @param g the graph for which the specified edge to be added.
* @param sourceVertex source vertex of the edge.
* @param targetVertex target vertex of the edge.
* @param weight weight of the edge.
*
* @return The newly created edge if added to the graph, otherwise
* null
.
*/
public static Edge addEdgeWithVertices( Graph g, Object sourceVertex,
Object targetVertex, double weight ) {
g.addVertex( sourceVertex );
g.addVertex( targetVertex );
return addEdge( g, sourceVertex, targetVertex, weight );
}
/**
* Adds all the vertices and all the edges of the specified source graph to
* the specified destination graph. First all vertices of the source graph
* are added to the destination graph. Then every edge of the source graph
* is added to the destination graph. This method returns
* true
if the destination graph has been modified as a
* result of this operation, otherwise it returns false
.
*
* * The behavior of this operation is undefined if any of the specified * graphs is modified while operation is in progress. *
* * @param destination the graph to which vertices and edges are added. * @param source the graph used as source for vertices and edges to add. * * @returntrue
if and only if the destination graph has been
* changed as a result of this operation.
*/
public static boolean addGraph( Graph destination, Graph source ) {
boolean modified = destination.addAllVertices( source.vertexSet( ) );
modified |= destination.addAllEdges( source.edgeSet( ) );
return modified;
}
/**
* Adds all the vertices and all the edges of the specified source digraph
* to the specified destination digraph, reversing all of the edges.
*
* * The behavior of this operation is undefined if any of the specified * graphs is modified while operation is in progress. *
* * @param destination the graph to which vertices and edges are added. * @param source the graph used as source for vertices and edges to add. */ public static void addGraphReversed( DirectedGraph destination, DirectedGraph source ) { destination.addAllVertices( source.vertexSet( ) ); Iterator edgesIter = source.edgeSet( ).iterator( ); while( edgesIter.hasNext( ) ) { DirectedEdge edge = (DirectedEdge) edgesIter.next( ); DirectedEdge reversedEdge = new DirectedEdge( edge.getTarget( ), edge.getSource( ) ); destination.addEdge( reversedEdge ); } } /** * Returns a list of vertices that are the neighbors of a specified vertex. * If the graph is a multigraph vertices may appear more than once in the * returned list. * * @param g the graph to look for neighbors in. * @param vertex the vertex to get the neighbors of. * * @return a list of the vertices that are the neighbors of the specified * vertex. */ public static List neighborListOf( Graph g, Object vertex ) { List neighbors = new ArrayList( ); for( Iterator i = g.edgesOf( vertex ).iterator( ); i.hasNext( ); ) { Edge e = (Edge) i.next( ); neighbors.add( e.oppositeVertex( vertex ) ); } return neighbors; } /** * Returns a list of vertices that are the predecessors of a specified * vertex. If the graph is a multigraph, vertices may appear more than * once in the returned list. * * @param g the graph to look for predecessors in. * @param vertex the vertex to get the predecessors of. * * @return a list of the vertices that are the predecessors of the * specified vertex. */ public static List predecessorListOf( DirectedGraph g, Object vertex ) { List predecessors = new ArrayList( ); List edges = g.incomingEdgesOf( vertex ); for( Iterator i = edges.iterator( ); i.hasNext( ); ) { Edge e = (Edge) i.next( ); predecessors.add( e.oppositeVertex( vertex ) ); } return predecessors; } /** * Returns a list of vertices that are the successors of a specified * vertex. If the graph is a multigraph vertices may appear more than once * in the returned list. * * @param g the graph to look for successors in. * @param vertex the vertex to get the successors of. * * @return a list of the vertices that are the successors of the specified * vertex. */ public static List successorListOf( DirectedGraph g, Object vertex ) { List successors = new ArrayList( ); List edges = g.outgoingEdgesOf( vertex ); for( Iterator i = edges.iterator( ); i.hasNext( ); ) { Edge e = (Edge) i.next( ); successors.add( e.oppositeVertex( vertex ) ); } return successors; } /** * Returns an undirected view of the specified graph. If the specified * graph is directed, returns an undirected view of it. If the specified * graph is undirected, just returns it. * * @param g the graph for which an undirected view to be returned. * * @return an undirected view of the specified graph, if it is directed, or * or the specified graph itself if it is undirected. * * @throws IllegalArgumentException if the graph is neither DirectedGraph * nor UndirectedGraph. * * @see AsUndirectedGraph */ public static UndirectedGraph undirectedGraph( Graph g ) { if( g instanceof DirectedGraph ) { return new AsUndirectedGraph( (DirectedGraph) g ); } else if( g instanceof UndirectedGraph ) { return (UndirectedGraph) g; } else { throw new IllegalArgumentException( "Graph must be either DirectedGraph or UndirectedGraph" ); } } } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/LabeledElement.java 0000644 0001751 0001751 00000004236 10266566752 024300 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------- * LabeledElement.java * ------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: LabeledElement.java,v 1.2 2004/05/01 23:15:46 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * */ package org._3pq.jgrapht; /** * An graph element (vertex or edge) that can have a label. * * @author Barak Naveh * * @since Jul 14, 2003 */ public interface LabeledElement { /** * Sets the specified label object to this element. * * @param label a label to set to this element. */ public void setLabel( Object label ); /** * Returns the element's label, ornull
if element has no
* label.
*
* @return the element's label, or null
if element has no
* label.
*/
public Object getLabel( );
/**
* Tests if this element has a label.
*
* @return true
if the element has a label, otherwise
* false
.
*/
public boolean hasLabel( );
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/ListenableGraph.java 0000644 0001751 0001751 00000005163 10266566752 024502 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* --------------------
* ListenableGraph.java
* --------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: ListenableGraph.java,v 1.3 2004/05/01 23:15:46 barak_naveh Exp $
*
* Changes
* -------
* 24-Jul-2003 : Initial revision (BN);
* 10-Aug-2003 : Adaptation to new event model (BN);
*
*/
package org._3pq.jgrapht;
import org._3pq.jgrapht.event.GraphListener;
import org._3pq.jgrapht.event.VertexSetListener;
/**
* A graph that supports listeners on structural change events.
*
* @author Barak Naveh
*
* @see org._3pq.jgrapht.event.GraphListener
* @see org._3pq.jgrapht.event.VertexSetListener
* @since Jul 20, 2003
*/
public interface ListenableGraph extends Graph {
/**
* Adds the specified graph listener to this graph, if not already present.
*
* @param l the listener to be added.
*/
public void addGraphListener( GraphListener l );
/**
* Adds the specified vertex set listener to this graph, if not already
* present.
*
* @param l the listener to be added.
*/
public void addVertexSetListener( VertexSetListener l );
/**
* Removes the specified graph listener from this graph, if present.
*
* @param l he listener to be removed.
*/
public void removeGraphListener( GraphListener l );
/**
* Removes the specified vertex set listener from this graph, if present.
*
* @param l the listener to be removed.
*/
public void removeVertexSetListener( VertexSetListener l );
}
libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/UndirectedGraph.java 0000644 0001751 0001751 00000004144 10266566752 024504 0 ustar moeller moeller /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2004, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* --------------------
* UndirectedGraph.java
* --------------------
* (C) Copyright 2003, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: UndirectedGraph.java,v 1.3 2004/05/01 23:15:46 barak_naveh Exp $
*
* Changes
* -------
* 24-Jul-2003 : Initial revision (BN);
*
*/
package org._3pq.jgrapht;
/**
* A graph whose all edges are undirected. This is the root interface of all
* undirected graphs.
*
* * See * http://mathworld.wolfram.com/Graph.html for more on undirected and on * directed graphs. *
* * @author Barak Naveh * * @since Jul 14, 2003 */ public interface UndirectedGraph extends Graph { /** * Returns the degree of the specified vertex. A degree of a vertex in an * undirected graph is the number of edges touching that vertex. * * @param vertex vertex whose degree is to be calculated. * * @return the degree of the specified vertex. */ public int degreeOf( Object vertex ); } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/VertexFactory.java 0000644 0001751 0001751 00000003511 10266566752 024236 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------ * VertexFactory.java * ------------------ * (C) Copyright 2003, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: VertexFactory.java,v 1.3 2004/05/01 23:15:46 barak_naveh Exp $ * * Changes * ------- * 16-Sep-2003 : Initial revision (JVS); * */ package org._3pq.jgrapht; /** * A vertex factory used by graph algorithms for creating new vertices. * Normally, vertices are constructed by user code and added to a graph * explicitly, but algorithms which generate new vertices require a factory. * * @author John V. Sichi * * @since Sep 16, 2003 */ public interface VertexFactory { /** * Creates a new vertex. * * @return the new vertex */ public Object createVertex( ); } libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/WeightedGraph.java 0000644 0001751 0001751 00000003176 10266566752 024162 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------ * WeightedGraph.java * ------------------ * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: WeightedGraph.java,v 1.3 2004/05/01 23:15:46 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 13-Aug-2003 : Included weight methods in Edge interface (BN); * */ package org._3pq.jgrapht; /** * A tagging interface for a graph whose all edges have weights. * * @author Barak Naveh * * @since Jul 23, 2003 */ public interface WeightedGraph extends Graph {} libjgrapht0.6-java-0.6.0/src/org/_3pq/jgrapht/package.html 0000644 0001751 0001751 00000000402 10266566752 023043 0 ustar moeller moeller The front-end API's interfaces and classes, including {@link org._3pq.jgrapht.Graph}, {@link org._3pq.jgrapht.DirectedGraph} and {@link org._3pq.jgrapht.UndirectedGraph}. libjgrapht0.6-java-0.6.0/src/overview.html 0000644 0001751 0001751 00000000770 10266566752 020236 0 ustar moeller moeller JGraphT is a free Java class library that provides mathematical graph-theory objects and algorithms. This is an open-source java graph library that supports a rich gallery of graphs and is designed to be powerful, extensible and easy to use.Visit http://jgrapht.sourceforge.net to download and to get the latest info on JGraphT. libjgrapht0.6-java-0.6.0/testsrc/ 0000755 0001751 0001751 00000000000 10266566747 016402 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/testsrc/org/ 0000755 0001751 0001751 00000000000 10266566747 017171 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/ 0000755 0001751 0001751 00000000000 10266566747 020033 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/ 0000755 0001751 0001751 00000000000 11251311101 021431 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/alg/ 0000755 0001751 0001751 00000000000 11251311101 022174 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/alg/AllAlgTests.java 0000644 0001751 0001751 00000004302 10266566752 025252 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ---------------- * AllAlgTests.java * ---------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: AllAlgTests.java,v 1.12 2004/09/17 07:24:12 perfecthash Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.alg; import junit.framework.Test; import junit.framework.TestSuite; /** * A TestSuite for all tests in this package. * * @author Barak Naveh */ public final class AllAlgTests { private AllAlgTests( ) {} // ensure non-instantiability. /** * Creates a test suite for all tests in this package. * * @return a test suite for all tests in this package. */ public static Test suite( ) { TestSuite suite = new TestSuite( ); //$JUnit-BEGIN$ suite.addTest( new TestSuite( ConnectivityInspectorTest.class ) ); suite.addTest( new TestSuite( DijkstraShortestPathTest.class ) ); suite.addTest( new TestSuite( VertexCoversTest.class ) ); suite.addTest( new TestSuite( CycleDetectorTest.class ) ); //$JUnit-END$ return suite; } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/alg/ConnectivityInspectorTest.java 0000644 0001751 0001751 00000022634 10266566752 030310 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------------ * ConnectivityInspectorTest.java * ------------------------------ * (C) Copyright 2003-2005, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): John V. Sichi * * $Id: ConnectivityInspectorTest.java,v 1.11 2005/05/25 00:38:37 perfecthash Exp $ * * Changes * ------- * 07-Aug-2003 : Initial revision (BN); * 20-Apr-2005 : Added StrongConnectivityInspector test (JVS); * */ package org._3pq.jgrapht.alg; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import junit.framework.TestCase; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.Edge; import org._3pq.jgrapht.graph.DefaultDirectedGraph; import org._3pq.jgrapht.graph.DirectedSubgraph; import org._3pq.jgrapht.graph.ListenableDirectedGraph; import org._3pq.jgrapht.graph.Pseudograph; /** * . * * @author Barak Naveh */ public class ConnectivityInspectorTest extends TestCase { private static final String V1 = new String( "v1" ); private static final String V2 = new String( "v2" ); private static final String V3 = new String( "v3" ); private static final String V4 = new String( "v4" ); // Edge m_e1; Edge m_e2; Edge m_e3; Edge m_e3_b; Edge m_u; /** * . * * @return a graph */ public Pseudograph create( ) { Pseudograph g = new Pseudograph( ); assertEquals( 0, g.vertexSet( ).size( ) ); g.addVertex( V1 ); assertEquals( 1, g.vertexSet( ).size( ) ); g.addVertex( V2 ); assertEquals( 2, g.vertexSet( ).size( ) ); g.addVertex( V3 ); assertEquals( 3, g.vertexSet( ).size( ) ); g.addVertex( V4 ); assertEquals( 4, g.vertexSet( ).size( ) ); assertEquals( 0, g.edgeSet( ).size( ) ); m_e1 = g.addEdge( V1, V2 ); assertEquals( 1, g.edgeSet( ).size( ) ); m_e2 = g.addEdge( V2, V3 ); assertEquals( 2, g.edgeSet( ).size( ) ); m_e3 = g.addEdge( V3, V1 ); assertEquals( 3, g.edgeSet( ).size( ) ); m_e3_b = g.addEdge( V3, V1 ); assertEquals( 4, g.edgeSet( ).size( ) ); assertNotNull( m_e3_b ); m_u = g.addEdge( V1, V1 ); assertEquals( 5, g.edgeSet( ).size( ) ); m_u = g.addEdge( V1, V1 ); assertEquals( 6, g.edgeSet( ).size( ) ); return g; } /** * . */ public void testDirectedGraph( ) { ListenableDirectedGraph g = new ListenableDirectedGraph( ); g.addVertex( V1 ); g.addVertex( V2 ); g.addVertex( V3 ); g.addEdge( V1, V2 ); ConnectivityInspector inspector = new ConnectivityInspector( g ); g.addGraphListener( inspector ); assertEquals( false, inspector.isGraphConnected( ) ); g.addEdge( V1, V3 ); assertEquals( true, inspector.isGraphConnected( ) ); } /** * . */ public void testIsGraphConnected( ) { Pseudograph g = create( ); ConnectivityInspector inspector = new ConnectivityInspector( g ); assertEquals( false, inspector.isGraphConnected( ) ); g.removeVertex( V4 ); inspector = new ConnectivityInspector( g ); assertEquals( true, inspector.isGraphConnected( ) ); g.removeVertex( V1 ); assertEquals( 1, g.edgeSet( ).size( ) ); g.removeEdge( m_e2 ); g.addEdge( V2, V2 ); assertEquals( 1, g.edgeSet( ).size( ) ); inspector = new ConnectivityInspector( g ); assertEquals( false, inspector.isGraphConnected( ) ); } /** * . */ public void testStronglyConnected1( ) { DirectedGraph g = new DefaultDirectedGraph( ); g.addVertex( V1 ); g.addVertex( V2 ); g.addVertex( V3 ); g.addVertex( V4 ); g.addEdge( V1, V2 ); g.addEdge( V2, V1 ); // strongly connected g.addEdge( V3, V4 ); // only weakly connected StrongConnectivityInspector inspector = new StrongConnectivityInspector( g ); // convert from List to Set because we need to ignore order // during comparison Set actualSets = new HashSet( inspector.stronglyConnectedSets( ) ); // construct the expected answer Set expectedSets = new HashSet( ); Set set = new HashSet( ); set.add( V1 ); set.add( V2 ); expectedSets.add( set ); set = new HashSet( ); set.add( V3 ); expectedSets.add( set ); set = new HashSet( ); set.add( V4 ); expectedSets.add( set ); assertEquals( expectedSets, actualSets ); actualSets.clear( ); List subgraphs = inspector.stronglyConnectedSubgraphs( ); Iterator iter = subgraphs.iterator( ); while( iter.hasNext( ) ) { DirectedSubgraph sg = (DirectedSubgraph) iter.next( ); actualSets.add( sg.vertexSet( ) ); StrongConnectivityInspector ci = new StrongConnectivityInspector( sg ); assertTrue( ci.isStronglyConnected( ) ); } assertEquals( expectedSets, actualSets ); } /** * . */ public void testStronglyConnected2( ) { DirectedGraph g = new DefaultDirectedGraph( ); g.addVertex( V1 ); g.addVertex( V2 ); g.addVertex( V3 ); g.addVertex( V4 ); g.addEdge( V1, V2 ); g.addEdge( V2, V1 ); // strongly connected g.addEdge( V4, V3 ); // only weakly connected g.addEdge( V3, V2 ); // only weakly connected StrongConnectivityInspector inspector = new StrongConnectivityInspector( g ); // convert from List to Set because we need to ignore order // during comparison Set actualSets = new HashSet( inspector.stronglyConnectedSets( ) ); // construct the expected answer Set expectedSets = new HashSet( ); Set set = new HashSet( ); set.add( V1 ); set.add( V2 ); expectedSets.add( set ); set = new HashSet( ); set.add( V3 ); expectedSets.add( set ); set = new HashSet( ); set.add( V4 ); expectedSets.add( set ); assertEquals( expectedSets, actualSets ); actualSets.clear( ); List subgraphs = inspector.stronglyConnectedSubgraphs( ); Iterator iter = subgraphs.iterator( ); while( iter.hasNext( ) ) { DirectedSubgraph sg = (DirectedSubgraph) iter.next( ); actualSets.add( sg.vertexSet( ) ); StrongConnectivityInspector ci = new StrongConnectivityInspector( sg ); assertTrue( ci.isStronglyConnected( ) ); } assertEquals( expectedSets, actualSets ); } /** * . */ public void testStronglyConnected3( ) { DirectedGraph g = new DefaultDirectedGraph( ); g.addVertex( V1 ); g.addVertex( V2 ); g.addVertex( V3 ); g.addVertex( V4 ); g.addEdge( V1, V2 ); g.addEdge( V2, V3 ); g.addEdge( V3, V1 ); // strongly connected g.addEdge( V1, V4 ); g.addEdge( V2, V4 ); g.addEdge( V3, V4 ); // weakly connected StrongConnectivityInspector inspector = new StrongConnectivityInspector( g ); // convert from List to Set because we need to ignore order // during comparison Set actualSets = new HashSet( inspector.stronglyConnectedSets( ) ); // construct the expected answer Set expectedSets = new HashSet( ); Set set = new HashSet( ); set.add( V1 ); set.add( V2 ); set.add( V3 ); expectedSets.add( set ); set = new HashSet( ); set.add( V4 ); expectedSets.add( set ); assertEquals( expectedSets, actualSets ); actualSets.clear( ); List subgraphs = inspector.stronglyConnectedSubgraphs( ); Iterator iter = subgraphs.iterator( ); while( iter.hasNext( ) ) { DirectedSubgraph sg = (DirectedSubgraph) iter.next( ); actualSets.add( sg.vertexSet( ) ); StrongConnectivityInspector ci = new StrongConnectivityInspector( sg ); assertTrue( ci.isStronglyConnected( ) ); } assertEquals( expectedSets, actualSets ); } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/alg/CycleDetectorTest.java 0000644 0001751 0001751 00000010233 10266566752 026464 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------------ * CycleDetectorTest.java * ------------------------------ * (C) Copyright 2003, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: CycleDetectorTest.java,v 1.3 2005/04/23 08:25:18 perfecthash Exp $ * * Changes * ------- * 16-Sept-2004 : Initial revision (JVS); * */ package org._3pq.jgrapht.alg; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import junit.framework.TestCase; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.Graph; import org._3pq.jgrapht.graph.DefaultDirectedGraph; /** * . * * @author John V. Sichi */ public class CycleDetectorTest extends TestCase { private static final String V1 = "v1"; private static final String V2 = "v2"; private static final String V3 = "v3"; private static final String V4 = "v4"; private static final String V5 = "v5"; private static final String V6 = "v6"; /** * . * * @param g */ public void createGraph( Graph g ) { g.addVertex( V1 ); g.addVertex( V2 ); g.addVertex( V3 ); g.addVertex( V4 ); g.addVertex( V5 ); g.addVertex( V6 ); g.addEdge( V1, V2 ); g.addEdge( V2, V3 ); g.addEdge( V3, V4 ); g.addEdge( V4, V1 ); g.addEdge( V4, V5 ); g.addEdge( V5, V6 ); g.addEdge( V1, V6 ); } /** * . */ public void testDirectedWithCycle( ) { DirectedGraph g = new DefaultDirectedGraph( ); createGraph( g ); Set cyclicSet = new HashSet( ); cyclicSet.add( V1 ); cyclicSet.add( V2 ); cyclicSet.add( V3 ); cyclicSet.add( V4 ); Set acyclicSet = new HashSet( ); acyclicSet.add( V5 ); acyclicSet.add( V6 ); runTest( g, cyclicSet, acyclicSet ); } /** * . */ public void testDirectedWithoutCycle( ) { DirectedGraph g = new DefaultDirectedGraph( ); createGraph( g ); g.removeVertex( V2 ); Set cyclicSet = Collections.EMPTY_SET; Set acyclicSet = g.vertexSet( ); runTest( g, cyclicSet, acyclicSet ); } private void runTest( DirectedGraph g, Set cyclicSet, Set acyclicSet ) { CycleDetector detector = new CycleDetector( g ); Set emptySet = Collections.EMPTY_SET; assertEquals( !cyclicSet.isEmpty( ), detector.detectCycles( ) ); assertEquals( cyclicSet, detector.findCycles( ) ); Iterator iter = cyclicSet.iterator( ); while( iter.hasNext( ) ) { Object v = iter.next( ); assertEquals( true, detector.detectCyclesContainingVertex( v ) ); assertEquals( cyclicSet, detector.findCyclesContainingVertex( v ) ); } iter = acyclicSet.iterator( ); while( iter.hasNext( ) ) { Object v = iter.next( ); assertEquals( false, detector.detectCyclesContainingVertex( v ) ); assertEquals( emptySet, detector.findCyclesContainingVertex( v ) ); } } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/alg/DijkstraShortestPathTest.java 0000644 0001751 0001751 00000007772 10266566752 030075 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------------ * DijkstraShortestPathTest.java * ------------------------------ * (C) Copyright 2003, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: DijkstraShortestPathTest.java,v 1.4 2005/05/30 05:37:29 perfecthash Exp $ * * Changes * ------- * 03-Sept-2003 : Initial revision (JVS); * */ package org._3pq.jgrapht.alg; import java.util.Arrays; import java.util.List; import junit.framework.TestCase; import org._3pq.jgrapht.Edge; import org._3pq.jgrapht.Graph; import org._3pq.jgrapht.graph.SimpleWeightedGraph; /** * . * * @author John V. Sichi */ public class DijkstraShortestPathTest extends TestCase { static final String V1 = "v1"; static final String V2 = "v2"; static final String V3 = "v3"; static final String V4 = "v4"; static final String V5 = "v5"; Edge m_e12; Edge m_e13; Edge m_e15; Edge m_e24; Edge m_e34; Edge m_e45; /** * . */ public void testConstructor( ) { DijkstraShortestPath path; Graph g = create( ); path = new DijkstraShortestPath( g, V3, V4, Double.POSITIVE_INFINITY ); assertEquals( Arrays.asList( new Edge[] { m_e13, m_e12, m_e24 } ), path.getPathEdgeList( ) ); assertEquals( 10.0, path.getPathLength( ), 0 ); path = new DijkstraShortestPath( g, V3, V4, 7 ); assertNull( path.getPathEdgeList( ) ); assertEquals( Double.POSITIVE_INFINITY, path.getPathLength( ), 0 ); } /** * . */ public void testPathBetween( ) { List path; Graph g = create( ); path = DijkstraShortestPath.findPathBetween( g, V1, V2 ); assertEquals( Arrays.asList( new Edge[] { m_e12 } ), path ); path = DijkstraShortestPath.findPathBetween( g, V1, V4 ); assertEquals( Arrays.asList( new Edge[] { m_e12, m_e24 } ), path ); path = DijkstraShortestPath.findPathBetween( g, V1, V5 ); assertEquals( Arrays.asList( new Edge[] { m_e12, m_e24, m_e45 } ), path ); path = DijkstraShortestPath.findPathBetween( g, V3, V4 ); assertEquals( Arrays.asList( new Edge[] { m_e13, m_e12, m_e24 } ), path ); } private Graph create( ) { Graph g = new SimpleWeightedGraph( ); g.addVertex( V1 ); g.addVertex( V2 ); g.addVertex( V3 ); g.addVertex( V4 ); g.addVertex( V5 ); m_e12 = g.addEdge( V1, V2 ); m_e12.setWeight( 2 ); m_e13 = g.addEdge( V1, V3 ); m_e13.setWeight( 3 ); m_e24 = g.addEdge( V2, V4 ); m_e24.setWeight( 5 ); m_e34 = g.addEdge( V3, V4 ); m_e34.setWeight( 20 ); m_e45 = g.addEdge( V4, V5 ); m_e45.setWeight( 5 ); m_e15 = g.addEdge( V1, V5 ); m_e15.setWeight( 100 ); return g; } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/alg/VertexCoversTest.java 0000644 0001751 0001751 00000010473 10266566752 026400 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------------- * VertexCoversTest.java * --------------------- * (C) Copyright 2003, by Linda Buisman and Contributors. * * Original Author: Linda Buisman * Contributor(s): Barak Naveh * * $Id: VertexCoversTest.java,v 1.2 2004/05/01 12:17:52 barak_naveh Exp $ * * Changes * ------- * 06-Nov-2003 : Initial revision (LB); * 10-Nov-2003 : Adapted to VertexCovers (BN); * */ package org._3pq.jgrapht.alg; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.Vector; import junit.framework.TestCase; import org._3pq.jgrapht.Graph; import org._3pq.jgrapht.GraphHelper; import org._3pq.jgrapht.graph.Pseudograph; /** * Tests the vertex cover algorithms. * * @author Linda Buisman * * @since Nov 6, 2003 */ public class VertexCoversTest extends TestCase { private static final int TEST_GRAPH_SIZE = 200; private static final int TEST_REPEATS = 20; /** * . */ public void testFind2ApproximationCover( ) { VertexCovers vc = new VertexCovers( ); for( int i = 0; i < TEST_REPEATS; i++ ) { Graph g = createRandomGraph( ); assertTrue( isCover( vc.find2ApproximationCover( g ), g ) ); } } /** * . */ public void testFindGreedyCover( ) { VertexCovers vc = new VertexCovers( ); for( int i = 0; i < TEST_REPEATS; i++ ) { Graph g = createRandomGraph( ); Set c = vc.findGreedyCover( GraphHelper.undirectedGraph( g ) ); assertTrue( isCover( c, g ) ); } } /** * Checks if the specified vertex set covers every edge of the graph. Uses * the definition of Vertex Cover - removes every edge that is incident on * a vertex in vertexSet. If no edges are left, vertexSet is a vertex * cover for the specified graph. * * @param vertexSet the vertices to be tested for covering the graph. * @param g the graph to be covered. * * @return */ private boolean isCover( Set vertexSet, Graph g ) { Set uncoveredEdges = new HashSet( g.edgeSet( ) ); for( Iterator i = vertexSet.iterator( ); i.hasNext( ); ) { uncoveredEdges.removeAll( g.edgesOf( i.next( ) ) ); } return uncoveredEdges.size( ) == 0; } /** * Create a random graph of TEST_GRAPH_SIZE nodes. * * @return */ private Graph createRandomGraph( ) { // TODO: move random graph generator to be under GraphGenerator framework. Pseudograph g = new Pseudograph( ); for( int i = 0; i < TEST_GRAPH_SIZE; i++ ) { g.addVertex( new Integer( i ) ); } Vector vertices = new Vector( g.vertexSet( ) ); // join every vertex with a random number of other vertices for( int source = 0; source < TEST_GRAPH_SIZE; source++ ) { int numEdgesToCreate = (int) Math.random( ) * TEST_GRAPH_SIZE / 2 + 1; for( int j = 0; j < numEdgesToCreate; j++ ) { // find a random vertex to join to int target = (int) Math.floor( Math.random( ) * TEST_GRAPH_SIZE ); g.addEdge( vertices.get( source ), vertices.get( target ) ); } } return g; } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/edge/ 0000755 0001751 0001751 00000000000 11251311101 022335 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/edge/AllEdgeTests.java 0000644 0001751 0001751 00000004122 10266566752 025554 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ----------------- * AllEdgeTests.java * ----------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: AllEdgeTests.java,v 1.5 2004/05/01 12:19:25 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.edge; import junit.framework.Test; import junit.framework.TestSuite; /** * A TestSuite for all tests in this package. * * @author Barak Naveh */ public final class AllEdgeTests { /* * We don't want instances of this class. */ private AllEdgeTests( ) {} /** * Creates a test suite for all tests in this package. * * @return a test suite for all tests in this package. */ public static Test suite( ) { TestSuite suite = new TestSuite( ); //$JUnit-BEGIN$ suite.addTest( new TestSuite( DirectedEdgeTest.class ) ); suite.addTest( new TestSuite( UndirectedEdgeTest.class ) ); //$JUnit-END$ return suite; } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/edge/DefaultEdgeTest.java 0000644 0001751 0001751 00000006110 10266566752 026244 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------------- * DefaultEdgeTest.java * --------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: DefaultEdgeTest.java,v 1.3 2004/05/01 12:19:25 barak_naveh Exp $ * * Changes * ------- * 09-Aug-2003 : Initial revision (BN); * 10-Aug-2003 : General edge refactoring (BN); * */ package org._3pq.jgrapht.edge; import junit.framework.TestCase; import org._3pq.jgrapht.Edge; /** * Tests for the {@link org._3pq.jgrapht.edge.DefaultEdge} class. * * @author Barak Naveh * * @since Jul 24, 2003 */ public abstract class DefaultEdgeTest extends TestCase { Edge m_e1; Edge m_e1Clone; Edge m_e3; String m_source1 = "s1"; String m_target1 = "t1"; String m_target2 = "t2"; /** * . */ public void testClone( ) { assertEquals( m_e1Clone.getSource( ), m_e1.getSource( ) ); assertEquals( m_e1Clone.getTarget( ), m_e1.getTarget( ) ); } /** * . */ public void testContainsVertex( ) { assertTrue( m_e1.containsVertex( m_source1 ) ); assertTrue( m_e1.containsVertex( m_target1 ) ); assertFalse( m_e1.containsVertex( m_target2 ) ); } /** * . */ public void testGetSource( ) { assertTrue( m_e1.getSource( ) == m_source1 ); assertTrue( m_e1.getSource( ).equals( "s1" ) ); } /** * . */ public void testGetTarget( ) { assertTrue( m_e1.getTarget( ) == m_target1 ); assertTrue( m_e1.getTarget( ).equals( "t1" ) ); } /** * . */ public void testOppositeVertex( ) { assertEquals( m_source1, m_e1.oppositeVertex( m_target1 ) ); assertEquals( m_target1, m_e1.oppositeVertex( m_source1 ) ); assertFalse( m_source1 == m_e1.oppositeVertex( m_source1 ) ); assertFalse( m_target1 == m_e1.oppositeVertex( m_target1 ) ); } /** * @see TestCase#setUp() */ protected abstract void setUp( ) throws Exception; } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/edge/DirectedEdgeTest.java 0000644 0001751 0001751 00000004131 10266566752 026404 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------------- * DirectedEdgeTest.java * --------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: DirectedEdgeTest.java,v 1.3 2004/05/01 12:19:25 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 10-Aug-2003 : General edge refactoring (BN); * */ package org._3pq.jgrapht.edge; /** * Tests for the {@link DirectedEdge} class. * * @author Barak Naveh * * @since Jul 24, 2003 */ public class DirectedEdgeTest extends DefaultEdgeTest { /** * . */ public void testToString( ) { assertTrue( "(s1,t1)".equals( m_e1.toString( ) ) ); assertTrue( "(s1,t1)".equals( m_e1Clone.toString( ) ) ); } /** * @see DefaultEdgeTest#setUp() */ protected void setUp( ) throws Exception { m_e1 = new DirectedEdge( m_source1, m_target1 ); m_e3 = new DirectedEdge( new Object( ), new Object( ) ); m_e1Clone = (DirectedEdge) m_e1.clone( ); } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/edge/UndirectedEdgeTest.java 0000644 0001751 0001751 00000004216 10266566752 026753 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ----------------------- * UndirectedEdgeTest.java * ----------------------- * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: UndirectedEdgeTest.java,v 1.3 2004/05/01 12:19:53 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * 10-Aug-2003 : General edge refactoring (BN); * */ package org._3pq.jgrapht.edge; import org._3pq.jgrapht.Edge; /** * Tests for the {@link UndirectedEdge} class. * * @author Barak Naveh * * @since Jul 24, 2003 */ public class UndirectedEdgeTest extends DefaultEdgeTest { /** * . */ public void testToString( ) { assertTrue( "{s1,t1}".equals( m_e1.toString( ) ) ); assertTrue( "{s1,t1}".equals( m_e1Clone.toString( ) ) ); } /** * @see junit.framework.TestCase#setUp() */ protected void setUp( ) throws Exception { m_e1 = new UndirectedEdge( m_source1, m_target1 ); m_e3 = new UndirectedEdge( new Object( ), new Object( ) ); m_e1Clone = (Edge) m_e1.clone( ); } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/generate/ 0000755 0001751 0001751 00000000000 11251311101 023223 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/generate/AllGenerateTests.java 0000644 0001751 0001751 00000004016 10266566752 027332 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------------- * AllGenerateTests.java * --------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: AllGenerateTests.java,v 1.4 2004/05/01 12:21:54 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.generate; import junit.framework.Test; import junit.framework.TestSuite; /** * A TestSuite for all tests in this package. * * @author Barak Naveh */ public final class AllGenerateTests { private AllGenerateTests( ) {} // ensure non-instantiability. /** * Creates a test suite for all tests in this package. * * @return a test suite for all tests in this package. */ public static Test suite( ) { TestSuite suite = new TestSuite( ); //$JUnit-BEGIN$ suite.addTest( new TestSuite( GraphGeneratorTest.class ) ); //$JUnit-END$ return suite; } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/generate/GraphGeneratorTest.java 0000644 0001751 0001751 00000012065 10266566752 027677 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ----------------------- * GraphGeneratorTest.java * ----------------------- * (C) Copyright 2003-2004, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: GraphGeneratorTest.java,v 1.3 2004/05/01 12:22:22 barak_naveh Exp $ * * Changes * ------- * 17-Sep-2003 : Initial revision (JVS); * */ package org._3pq.jgrapht.generate; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; import junit.framework.TestCase; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.Edge; import org._3pq.jgrapht.VertexFactory; import org._3pq.jgrapht.graph.DefaultDirectedGraph; /** * . * * @author John V. Sichi * * @since Sep 17, 2003 */ public class GraphGeneratorTest extends TestCase { private static final int SIZE = 10; private VertexFactory m_vertexFactory = new VertexFactory( ) { private int m_i; public Object createVertex( ) { return new Integer( ++m_i ); } }; /** * . */ public void testEmptyGraphGenerator( ) { GraphGenerator gen = new EmptyGraphGenerator( SIZE ); DirectedGraph g = new DefaultDirectedGraph( ); Map resultMap = new HashMap( ); gen.generateGraph( g, m_vertexFactory, resultMap ); assertEquals( SIZE, g.vertexSet( ).size( ) ); assertEquals( 0, g.edgeSet( ).size( ) ); assertTrue( resultMap.isEmpty( ) ); } /** * . */ public void testLinearGraphGenerator( ) { GraphGenerator gen = new LinearGraphGenerator( SIZE ); DirectedGraph g = new DefaultDirectedGraph( ); Map resultMap = new HashMap( ); gen.generateGraph( g, m_vertexFactory, resultMap ); assertEquals( SIZE, g.vertexSet( ).size( ) ); assertEquals( SIZE - 1, g.edgeSet( ).size( ) ); Object startVertex = resultMap.get( LinearGraphGenerator.START_VERTEX ); Object endVertex = resultMap.get( LinearGraphGenerator.END_VERTEX ); Iterator vertexIter = g.vertexSet( ).iterator( ); while( vertexIter.hasNext( ) ) { Object vertex = vertexIter.next( ); if( vertex == startVertex ) { assertEquals( 0, g.inDegreeOf( vertex ) ); assertEquals( 1, g.outDegreeOf( vertex ) ); continue; } if( vertex == endVertex ) { assertEquals( 1, g.inDegreeOf( vertex ) ); assertEquals( 0, g.outDegreeOf( vertex ) ); continue; } assertEquals( 1, g.inDegreeOf( vertex ) ); assertEquals( 1, g.outDegreeOf( vertex ) ); } } /** * . */ public void testRingGraphGenerator( ) { GraphGenerator gen = new RingGraphGenerator( SIZE ); DirectedGraph g = new DefaultDirectedGraph( ); Map resultMap = new HashMap( ); gen.generateGraph( g, m_vertexFactory, resultMap ); assertEquals( SIZE, g.vertexSet( ).size( ) ); assertEquals( SIZE, g.edgeSet( ).size( ) ); Object startVertex = g.vertexSet( ).iterator( ).next( ); assertEquals( 1, g.outDegreeOf( startVertex ) ); Object nextVertex = startVertex; Set seen = new HashSet( ); for( int i = 0; i < SIZE; ++i ) { Edge nextEdge = (Edge) g.outgoingEdgesOf( nextVertex ).get( 0 ); nextVertex = nextEdge.getTarget( ); assertEquals( 1, g.inDegreeOf( nextVertex ) ); assertEquals( 1, g.outDegreeOf( nextVertex ) ); assertTrue( !seen.contains( nextVertex ) ); seen.add( nextVertex ); } // do you ever get the feeling you're going in circles? assertTrue( nextVertex == startVertex ); assertTrue( resultMap.isEmpty( ) ); } // TODO: testWheelGraphGenerator } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/graph/ 0000755 0001751 0001751 00000000000 11251311101 022532 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/graph/AllGraphTests.java 0000644 0001751 0001751 00000004446 10266566752 026157 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ----------------- * AllGraphTests.java * ----------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: AllGraphTests.java,v 1.6 2004/05/01 12:24:01 barak_naveh Exp $ * * Changes * ------- * 03-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import junit.framework.Test; import junit.framework.TestSuite; /** * A TestSuite for all tests in this package. * * @author Barak Naveh * * @since Aug 3, 2003 */ public final class AllGraphTests { private AllGraphTests( ) {} // ensure non-instantiability. /** * Creates a test suite for all tests in this package. * * @return a test suite for all tests in this package. */ public static Test suite( ) { TestSuite suite = new TestSuite( ); //$JUnit-BEGIN$ suite.addTest( new TestSuite( DefaultDirectedGraphTest.class ) ); suite.addTest( new TestSuite( ListenableGraphTest.class ) ); suite.addTest( new TestSuite( SimpleDirectedGraphTest.class ) ); suite.addTest( new TestSuite( AsUndirectedGraphTest.class ) ); suite.addTest( new TestSuite( CloneTest.class ) ); //$JUnit-END$ return suite; } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/graph/AsUndirectedGraphTest.java 0000644 0001751 0001751 00000011462 10266566752 027632 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* -------------------------- * AsUndirectedGraphTest.java * -------------------------- * (C) Copyright 2003, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: AsUndirectedGraphTest.java,v 1.4 2004/05/01 12:24:01 barak_naveh Exp $ * * Changes * ------- * 14-Aug-2003 : Initial revision (JVS); * */ package org._3pq.jgrapht.graph; import java.util.List; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.Edge; import org._3pq.jgrapht.EnhancedTestCase; import org._3pq.jgrapht.UndirectedGraph; /** * A unit test for the AsDirectedGraph view. * * @author John V. Sichi */ public class AsUndirectedGraphTest extends EnhancedTestCase { private DirectedGraph m_directed; private Edge m_loop; private String m_v1 = "v1"; private String m_v2 = "v2"; private String m_v3 = "v3"; private String m_v4 = "v4"; private UndirectedGraph m_undirected; /** * @see junit.framework.TestCase#TestCase(java.lang.String) */ public AsUndirectedGraphTest( String name ) { super( name ); } /** * . */ public void testAddEdge( ) { try { m_undirected.addEdge( m_v3, m_v4 ); assertFalse( ); } catch( UnsupportedOperationException e ) { assertTrue( ); } } /** * . */ public void testAddVertex( ) { String v5 = "v5"; m_undirected.addVertex( v5 ); assertEquals( true, m_undirected.containsVertex( v5 ) ); assertEquals( true, m_directed.containsVertex( v5 ) ); } /** * . */ public void testDegreeOf( ) { assertEquals( 1, m_undirected.degreeOf( m_v1 ) ); assertEquals( 3, m_undirected.degreeOf( m_v2 ) ); assertEquals( 1, m_undirected.degreeOf( m_v3 ) ); assertEquals( 3, m_undirected.degreeOf( m_v4 ) ); } /** * . */ public void testGetAllEdges( ) { List edges = m_undirected.getAllEdges( m_v3, m_v2 ); assertEquals( 1, edges.size( ) ); assertEquals( m_directed.getEdge( m_v2, m_v3 ), edges.iterator( ).next( ) ); edges = m_undirected.getAllEdges( m_v4, m_v4 ); assertEquals( 1, edges.size( ) ); assertEquals( m_loop, edges.iterator( ).next( ) ); } /** * . */ public void testGetEdge( ) { assertEquals( m_directed.getEdge( m_v1, m_v2 ), m_undirected.getEdge( m_v1, m_v2 ) ); assertEquals( m_directed.getEdge( m_v1, m_v2 ), m_undirected.getEdge( m_v2, m_v1 ) ); assertEquals( m_directed.getEdge( m_v4, m_v4 ), m_undirected.getEdge( m_v4, m_v4 ) ); } /** * . */ public void testRemoveEdge( ) { m_undirected.removeEdge( m_loop ); assertEquals( false, m_undirected.containsEdge( m_loop ) ); assertEquals( false, m_directed.containsEdge( m_loop ) ); } /** * . */ public void testRemoveVertex( ) { m_undirected.removeVertex( m_v4 ); assertEquals( false, m_undirected.containsVertex( m_v4 ) ); assertEquals( false, m_directed.containsVertex( m_v4 ) ); } /** * . */ protected void setUp( ) { m_directed = new DefaultDirectedGraph( ); m_undirected = new AsUndirectedGraph( m_directed ); m_directed.addVertex( m_v1 ); m_directed.addVertex( m_v2 ); m_directed.addVertex( m_v3 ); m_directed.addVertex( m_v4 ); m_directed.addEdge( m_v1, m_v2 ); m_directed.addEdge( m_v2, m_v3 ); m_directed.addEdge( m_v2, m_v4 ); m_loop = m_directed.addEdge( m_v4, m_v4 ); } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/graph/CloneTest.java 0000644 0001751 0001751 00000004733 10266566752 025341 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* -------------- * CloneTest.java * -------------- * (C) Copyright 2003, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: CloneTest.java,v 1.4 2004/05/01 12:24:01 barak_naveh Exp $ * * Changes * ------- * 06-Oct-2003 : Initial revision (JVS); * */ package org._3pq.jgrapht.graph; import org._3pq.jgrapht.EnhancedTestCase; /** * A unit test for a cloning bug, adapted from a forum entry from Linda * Buisman. * * @author John V. Sichi * * @since Oct 6, 2003 */ public class CloneTest extends EnhancedTestCase { /** * @see junit.framework.TestCase#TestCase(java.lang.String) */ public CloneTest( String name ) { super( name ); } /** * Test graph cloning. */ public void testCloneSpecificsBug( ) { SimpleGraph g1 = new SimpleGraph( ); String one = "1"; String two = "2"; String three = "3"; g1.addVertex( one ); g1.addVertex( two ); g1.addVertex( three ); g1.addEdge( one, two ); g1.addEdge( two, three ); SimpleGraph g2 = (SimpleGraph) g1.clone( ); assertEquals( 2, g2.edgeSet( ).size( ) ); assertNotNull( g2.getEdge( one, two ) ); assertTrue( g2.removeEdge( g2.getEdge( one, two ) ) ); assertNotNull( g2.removeEdge( "2", "3" ) ); assertTrue( g2.edgeSet( ).isEmpty( ) ); } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/graph/DefaultDirectedGraphTest.java 0000644 0001751 0001751 00000011247 10266566752 030311 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ----------------------------- * DefaultDirectedGraphTest.java * ----------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: DefaultDirectedGraphTest.java,v 1.5 2005/06/08 22:24:57 perfecthash Exp $ * * Changes * ------- * 09-Aug-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.Edge; import org._3pq.jgrapht.EnhancedTestCase; /** * A unit test for directed multigraph. * * @author Barak Naveh * * @since Aug 9, 2003 */ public class DefaultDirectedGraphTest extends EnhancedTestCase { private String m_v1 = "v1"; private String m_v2 = "v2"; private String m_v3 = "v3"; /** * . */ public void testEdgeListFactory( ) { DirectedMultigraph g = new DirectedMultigraph( ); g.setEdgeListFactory( new LinkedListFactory( ) ); initMultiTriangleWithMultiLoop( g ); } /** * . */ public void testEdgeOrderDeterminism( ) { DirectedGraph g = new DirectedMultigraph( ); g.addVertex( m_v1 ); g.addVertex( m_v2 ); g.addVertex( m_v3 ); Edge e1 = g.addEdge( m_v1, m_v2 ); Edge e2 = g.addEdge( m_v2, m_v3 ); Edge e3 = g.addEdge( m_v3, m_v1 ); Iterator iter = g.edgeSet( ).iterator( ); assertEquals( e1, iter.next( ) ); assertEquals( e2, iter.next( ) ); assertEquals( e3, iter.next( ) ); } /** * . */ public void testEdgesOf( ) { DirectedGraph g = createMultiTriangleWithMultiLoop( ); assertEquals( 3, g.edgesOf( m_v1 ).size( ) ); assertEquals( 2, g.edgesOf( m_v2 ).size( ) ); } /** * . */ public void testGetAllEdges( ) { DirectedGraph g = createMultiTriangleWithMultiLoop( ); List loops = g.getAllEdges( m_v1, m_v1 ); assertEquals( 1, loops.size( ) ); } /** * . */ public void testInDegreeOf( ) { DirectedGraph g = createMultiTriangleWithMultiLoop( ); assertEquals( 2, g.inDegreeOf( m_v1 ) ); assertEquals( 1, g.inDegreeOf( m_v2 ) ); } /** * . */ public void testOutDegreeOf( ) { DirectedGraph g = createMultiTriangleWithMultiLoop( ); assertEquals( 2, g.outDegreeOf( m_v1 ) ); assertEquals( 1, g.outDegreeOf( m_v2 ) ); } /** * . */ public void testVertexOrderDeterminism( ) { DirectedGraph g = createMultiTriangleWithMultiLoop( ); Iterator iter = g.vertexSet( ).iterator( ); assertEquals( m_v1, iter.next( ) ); assertEquals( m_v2, iter.next( ) ); assertEquals( m_v3, iter.next( ) ); } private DirectedGraph createMultiTriangleWithMultiLoop( ) { DirectedGraph g = new DirectedMultigraph( ); initMultiTriangleWithMultiLoop( g ); return g; } private void initMultiTriangleWithMultiLoop( DirectedGraph g ) { g.addVertex( m_v1 ); g.addVertex( m_v2 ); g.addVertex( m_v3 ); g.addEdge( m_v1, m_v1 ); g.addEdge( m_v1, m_v2 ); g.addEdge( m_v2, m_v3 ); g.addEdge( m_v3, m_v1 ); } private static class LinkedListFactory implements EdgeListFactory { /** * . * * @param vertex * * @return */ public List createEdgeList( Object vertex ) { return new LinkedList( ); } } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/graph/ListenableGraphTest.java 0000644 0001751 0001751 00000015060 10266566752 027340 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------ * ListenableGraphTest.java * ------------------------ * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: ListenableGraphTest.java,v 1.5 2004/05/01 12:24:01 barak_naveh Exp $ * * Changes * ------- * 03-Aug-2003 : Initial revision (BN); * 10-Aug-2003 : Adaptation to new event model (BN); * */ package org._3pq.jgrapht.graph; import junit.framework.TestCase; import org._3pq.jgrapht.Edge; import org._3pq.jgrapht.ListenableGraph; import org._3pq.jgrapht.event.GraphEdgeChangeEvent; import org._3pq.jgrapht.event.GraphListener; import org._3pq.jgrapht.event.GraphVertexChangeEvent; import org._3pq.jgrapht.event.VertexSetListener; /** * Unit test for {@link ListenableGraph} class. * * @author Barak Naveh * * @since Aug 3, 2003 */ public class ListenableGraphTest extends TestCase { Edge m_lastAddedEdge; Edge m_lastRemovedEdge; Object m_lastAddedVertex; Object m_lastRemovedVertex; /** * @see junit.framework.TestCase#TestCase(java.lang.String) */ public ListenableGraphTest( String name ) { super( name ); } /** * Tests GraphListener listener. */ public void testGraphListener( ) { init( ); ListenableGraph g = new ListenableUndirectedGraph( ); GraphListener listener = new MyGraphListner( ); g.addGraphListener( listener ); String v1 = new String( "v1" ); String v2 = new String( "v2" ); // test vertex notification g.addVertex( v1 ); assertEquals( v1, m_lastAddedVertex ); assertEquals( null, m_lastRemovedVertex ); init( ); g.removeVertex( v1 ); assertEquals( v1, m_lastRemovedVertex ); assertEquals( null, m_lastAddedVertex ); // test edge notification g.addVertex( v1 ); g.addVertex( v2 ); init( ); Edge e = g.addEdge( v1, v2 ); assertEquals( e, m_lastAddedEdge ); assertEquals( null, m_lastRemovedEdge ); init( ); assertTrue( g.removeEdge( e ) ); assertEquals( e, m_lastRemovedEdge ); assertEquals( null, m_lastAddedEdge ); g.removeVertex( v1 ); g.removeVertex( v2 ); // // test notification stops when removing listener // g.removeGraphListener( listener ); init( ); g.addVertex( v1 ); g.addVertex( v2 ); e = g.addEdge( v1, v2 ); g.removeEdge( e ); assertEquals( null, m_lastAddedEdge ); assertEquals( null, m_lastAddedVertex ); assertEquals( null, m_lastRemovedEdge ); assertEquals( null, m_lastRemovedVertex ); } /** * Tests VertexSetListener listener. */ public void testVertexSetListener( ) { init( ); ListenableGraph g = new ListenableUndirectedGraph( ); VertexSetListener listener = new MyGraphListner( ); g.addVertexSetListener( listener ); String v1 = new String( "v1" ); String v2 = new String( "v2" ); // test vertex notification g.addVertex( v1 ); assertEquals( v1, m_lastAddedVertex ); assertEquals( null, m_lastRemovedVertex ); init( ); g.removeVertex( v1 ); assertEquals( v1, m_lastRemovedVertex ); assertEquals( null, m_lastAddedVertex ); // test edge notification g.addVertex( v1 ); g.addVertex( v2 ); init( ); Edge e = g.addEdge( v1, v2 ); assertEquals( null, m_lastAddedEdge ); assertEquals( null, m_lastRemovedEdge ); init( ); assertTrue( g.removeEdge( e ) ); assertEquals( null, m_lastRemovedEdge ); assertEquals( null, m_lastAddedEdge ); g.removeVertex( v1 ); g.removeVertex( v2 ); // // test notification stops when removing listener // g.removeVertexSetListener( listener ); init( ); g.addVertex( v1 ); g.addVertex( v2 ); e = g.addEdge( v1, v2 ); g.removeEdge( e ); assertEquals( null, m_lastAddedEdge ); assertEquals( null, m_lastAddedVertex ); assertEquals( null, m_lastRemovedEdge ); assertEquals( null, m_lastRemovedVertex ); } private void init( ) { m_lastAddedEdge = null; m_lastAddedVertex = null; m_lastRemovedEdge = null; m_lastRemovedVertex = null; } /** * A listener on the tested graph. * * @author Barak Naveh * * @since Aug 3, 2003 */ private class MyGraphListner implements GraphListener { /** * @see GraphListener#edgeAdded(GraphEdgeChangeEvent) */ public void edgeAdded( GraphEdgeChangeEvent e ) { m_lastAddedEdge = e.getEdge( ); } /** * @see GraphListener#edgeRemoved(GraphEdgeChangeEvent) */ public void edgeRemoved( GraphEdgeChangeEvent e ) { m_lastRemovedEdge = e.getEdge( ); } /** * @see VertexSetListener#vertexAdded(GraphVertexChangeEvent) */ public void vertexAdded( GraphVertexChangeEvent e ) { m_lastAddedVertex = e.getVertex( ); } /** * @see VertexSetListener#vertexRemoved(GraphVertexChangeEvent) */ public void vertexRemoved( GraphVertexChangeEvent e ) { m_lastRemovedVertex = e.getVertex( ); } } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/graph/SimpleDirectedGraphTest.java 0000644 0001751 0001751 00000025057 10266566752 030162 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ---------------------------- * SimpleDirectedGraphTest.java * ---------------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: SimpleDirectedGraphTest.java,v 1.7 2004/05/01 12:24:01 barak_naveh Exp $ * * Changes * ------- * 25-Jul-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.graph; import java.util.Iterator; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.Edge; import org._3pq.jgrapht.EdgeFactory; import org._3pq.jgrapht.EnhancedTestCase; /** * A unit test for simple directed graph. * * @author Barak Naveh * * @since Jul 25, 2003 */ public class SimpleDirectedGraphTest extends EnhancedTestCase { DirectedGraph m_gEmpty; private DirectedGraph m_g1; private DirectedGraph m_g2; private DirectedGraph m_g3; private DirectedGraph m_g4; private Edge m_eLoop; private EdgeFactory m_eFactory; private String m_v1 = "v1"; private String m_v2 = "v2"; private String m_v3 = "v3"; private String m_v4 = "v4"; /** * @see junit.framework.TestCase#TestCase(java.lang.String) */ public SimpleDirectedGraphTest( String name ) { super( name ); } /** * Class to test for boolean addEdge(Edge) */ public void testAddEdgeEdge( ) { init( ); try { m_g1.addEdge( m_eLoop ); // loops not allowed assertFalse( ); } catch( IllegalArgumentException e ) { assertTrue( ); } try { m_g3.addEdge( null ); assertFalse( ); // NPE } catch( NullPointerException e ) { assertTrue( ); } Edge e = m_eFactory.createEdge( m_v2, m_v1 ); try { m_g1.addEdge( e ); // no such vertex in graph assertFalse( ); } catch( IllegalArgumentException ile ) { assertTrue( ); } assertEquals( false, m_g2.addEdge( e ) ); assertEquals( false, m_g3.addEdge( e ) ); assertEquals( true, m_g4.addEdge( e ) ); } /** * Class to test for Edge addEdge(Object, Object) */ public void testAddEdgeObjectObject( ) { init( ); try { m_g1.addEdge( m_v1, m_v1 ); // loops not allowed assertFalse( ); } catch( IllegalArgumentException e ) { assertTrue( ); } try { m_g3.addEdge( null, null ); assertFalse( ); // NPE } catch( NullPointerException e ) { assertTrue( ); } try { m_g1.addEdge( m_v2, m_v1 ); // no such vertex in graph assertFalse( ); } catch( IllegalArgumentException ile ) { assertTrue( ); } assertNull( m_g2.addEdge( m_v2, m_v1 ) ); assertNull( m_g3.addEdge( m_v2, m_v1 ) ); assertNotNull( m_g4.addEdge( m_v2, m_v1 ) ); } /** * . */ public void testAddVertex( ) { init( ); assertEquals( 1, m_g1.vertexSet( ).size( ) ); assertEquals( 2, m_g2.vertexSet( ).size( ) ); assertEquals( 3, m_g3.vertexSet( ).size( ) ); assertEquals( 4, m_g4.vertexSet( ).size( ) ); assertFalse( m_g1.addVertex( m_v1 ) ); assertTrue( m_g1.addVertex( m_v2 ) ); assertEquals( 2, m_g1.vertexSet( ).size( ) ); } /** * Class to test for boolean containsEdge(Edge) */ public void testContainsEdgeEdge( ) { init( ); //TODO Implement containsEdge(). } /** * Class to test for boolean containsEdge(Object, Object) */ public void testContainsEdgeObjectObject( ) { init( ); assertFalse( m_g1.containsEdge( m_v1, m_v2 ) ); assertFalse( m_g1.containsEdge( m_v1, m_v1 ) ); assertTrue( m_g2.containsEdge( m_v1, m_v2 ) ); assertTrue( m_g2.containsEdge( m_v2, m_v1 ) ); assertTrue( m_g3.containsEdge( m_v1, m_v2 ) ); assertTrue( m_g3.containsEdge( m_v2, m_v1 ) ); assertTrue( m_g3.containsEdge( m_v3, m_v2 ) ); assertTrue( m_g3.containsEdge( m_v2, m_v3 ) ); assertTrue( m_g3.containsEdge( m_v1, m_v3 ) ); assertTrue( m_g3.containsEdge( m_v3, m_v1 ) ); assertFalse( m_g4.containsEdge( m_v1, m_v4 ) ); m_g4.addEdge( m_v1, m_v4 ); assertTrue( m_g4.containsEdge( m_v1, m_v4 ) ); assertFalse( m_g3.containsEdge( m_v4, m_v2 ) ); assertFalse( m_g3.containsEdge( null, null ) ); } /** * . */ public void testContainsVertex( ) { init( ); //TODO Implement containsVertex(). } /** * . */ public void testEdgeSet( ) { init( ); //TODO Implement edgeSet(). } /** * . */ public void testEdgesOf( ) { init( ); assertEquals( m_g4.edgesOf( m_v1 ).size( ), 2 ); assertEquals( m_g3.edgesOf( m_v1 ).size( ), 4 ); Iterator iter = m_g3.edgesOf( m_v1 ).iterator( ); int count = 0; while( iter.hasNext( ) ) { iter.next( ); count++; } assertEquals( count, 4 ); } /** * . */ public void testGetAllEdges( ) { init( ); //TODO Implement getAllEdges(). } /** * . */ public void testGetEdge( ) { init( ); //TODO Implement getEdge(). } /** * . */ public void testGetEdgeFactory( ) { init( ); //TODO Implement getEdgeFactory(). } /** * . */ public void testInDegreeOf( ) { init( ); assertEquals( 0, m_g1.inDegreeOf( m_v1 ) ); assertEquals( 1, m_g2.inDegreeOf( m_v1 ) ); assertEquals( 1, m_g2.inDegreeOf( m_v2 ) ); assertEquals( 2, m_g3.inDegreeOf( m_v1 ) ); assertEquals( 2, m_g3.inDegreeOf( m_v2 ) ); assertEquals( 2, m_g3.inDegreeOf( m_v3 ) ); assertEquals( 1, m_g4.inDegreeOf( m_v1 ) ); assertEquals( 1, m_g4.inDegreeOf( m_v2 ) ); assertEquals( 1, m_g4.inDegreeOf( m_v3 ) ); assertEquals( 1, m_g4.inDegreeOf( m_v4 ) ); try { m_g3.inDegreeOf( new Object( ) ); assertFalse( ); } catch( IllegalArgumentException e ) { assertTrue( ); } try { m_g3.inDegreeOf( null ); assertFalse( ); } catch( NullPointerException e ) { assertTrue( ); } } /** * . */ public void testIncomingEdgesOf( ) { init( ); //TODO Implement incomingEdgesOf(). } /** * . */ public void testOutDegreeOf( ) { init( ); //TODO Implement outDegreeOf(). } /** * . */ public void testOutgoingEdgesOf( ) { init( ); //TODO Implement outgoingEdgesOf(). } /** * Class to test for boolean removeEdge(Edge) */ public void testRemoveEdgeEdge( ) { init( ); assertEquals( m_g4.edgeSet( ).size( ), 4 ); m_g4.removeEdge( m_v1, m_v2 ); assertEquals( m_g4.edgeSet( ).size( ), 3 ); assertFalse( m_g4.removeEdge( m_eLoop ) ); assertTrue( m_g4.removeEdge( m_g4.getEdge( m_v2, m_v3 ) ) ); assertEquals( m_g4.edgeSet( ).size( ), 2 ); } /** * Class to test for Edge removeEdge(Object, Object) */ public void testRemoveEdgeObjectObject( ) { init( ); //TODO Implement removeEdge(). } /** * . */ public void testRemoveVertex( ) { init( ); assertEquals( 4, m_g4.vertexSet( ).size( ) ); assertTrue( m_g4.removeVertex( m_v1 ) ); assertEquals( 3, m_g4.vertexSet( ).size( ) ); assertEquals( 2, m_g4.edgeSet( ).size( ) ); assertFalse( m_g4.removeVertex( m_v1 ) ); assertTrue( m_g4.removeVertex( m_v2 ) ); assertEquals( 1, m_g4.edgeSet( ).size( ) ); assertTrue( m_g4.removeVertex( m_v3 ) ); assertEquals( 0, m_g4.edgeSet( ).size( ) ); assertEquals( 1, m_g4.vertexSet( ).size( ) ); assertTrue( m_g4.removeVertex( m_v4 ) ); assertEquals( 0, m_g4.vertexSet( ).size( ) ); } /** * . */ public void testVertexSet( ) { init( ); //TODO Implement vertexSet(). } private void init( ) { m_gEmpty = new SimpleDirectedGraph( ); m_g1 = new SimpleDirectedGraph( ); m_g2 = new SimpleDirectedGraph( ); m_g3 = new SimpleDirectedGraph( ); m_g4 = new SimpleDirectedGraph( ); m_eFactory = m_g1.getEdgeFactory( ); m_eLoop = m_eFactory.createEdge( m_v1, m_v1 ); m_g1.addVertex( m_v1 ); m_g2.addVertex( m_v1 ); m_g2.addVertex( m_v2 ); m_g2.addEdge( m_v1, m_v2 ); m_g2.addEdge( m_v2, m_v1 ); m_g3.addVertex( m_v1 ); m_g3.addVertex( m_v2 ); m_g3.addVertex( m_v3 ); m_g3.addEdge( m_v1, m_v2 ); m_g3.addEdge( m_v2, m_v1 ); m_g3.addEdge( m_v2, m_v3 ); m_g3.addEdge( m_v3, m_v2 ); m_g3.addEdge( m_v3, m_v1 ); m_g3.addEdge( m_v1, m_v3 ); m_g4.addVertex( m_v1 ); m_g4.addVertex( m_v2 ); m_g4.addVertex( m_v3 ); m_g4.addVertex( m_v4 ); m_g4.addEdge( m_v1, m_v2 ); m_g4.addEdge( m_v2, m_v3 ); m_g4.addEdge( m_v3, m_v4 ); m_g4.addEdge( m_v4, m_v1 ); } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/graph/SubgraphTest.java 0000644 0001751 0001751 00000010512 10266566752 026044 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------ * SubgraphTest.java * ------------------------ * (C) Copyright 2003, by Michael Behrisch and Contributors. * * Original Author: Michael Behrisch * Contributor(s): - * * $Id: SubgraphTest.java,v 1.3 2005/04/23 08:09:29 perfecthash Exp $ * * Changes * ------- * 21-Sep-2004 : Initial revision (MB); * */ package org._3pq.jgrapht.graph; import java.util.*; import junit.framework.TestCase; import org._3pq.jgrapht.Graph; /** * Unit test for {@link Subgraph} class. * * @author Michael Behrisch * * @since Sep 21, 2004 */ public class SubgraphTest extends TestCase { private String _v1 = new String( "v1" ); private String _v2 = new String( "v2" ); private String _v3 = new String( "v3" ); private String _v4 = new String( "v4" ); /** * @see junit.framework.TestCase#TestCase(java.lang.String) */ public SubgraphTest( String name ) { super( name ); } /** * . */ public void testInducedSubgraphListener( ) { Graph g = init( true ); Subgraph sub = new Subgraph( g, null ); assertEquals( g.vertexSet( ), sub.vertexSet( ) ); assertEquals( g.edgeSet( ), sub.edgeSet( ) ); g.addEdge( _v3, _v4 ); assertEquals( g.vertexSet( ), sub.vertexSet( ) ); assertEquals( g.edgeSet( ), sub.edgeSet( ) ); } /** * Tests Subgraph. */ public void testSubgraph( ) { Graph g = init( false ); Subgraph sub = new Subgraph( g, null, null ); assertEquals( g.vertexSet( ), sub.vertexSet( ) ); assertEquals( g.edgeSet( ), sub.edgeSet( ) ); Set vset = new HashSet( g.vertexSet( ) ); g.removeVertex( _v1 ); assertEquals( vset, sub.vertexSet( ) ); //losing track g = init( false ); vset = new HashSet( ); vset.add( _v1 ); sub = new Subgraph( g, vset, null ); assertEquals( vset, sub.vertexSet( ) ); assertEquals( 0, sub.degreeOf( _v1 ) ); assertEquals( Collections.EMPTY_SET, sub.edgeSet( ) ); vset.add( _v2 ); vset.add( _v3 ); sub = new Subgraph( g, vset, new HashSet( g.getAllEdges( _v1, _v2 ) ) ); assertEquals( vset, sub.vertexSet( ) ); assertEquals( 1, sub.edgeSet( ).size( ) ); } /** * . */ public void testSubgraphListener( ) { Graph g = init( true ); Subgraph sub = new Subgraph( g, null, null ); assertEquals( g.vertexSet( ), sub.vertexSet( ) ); assertEquals( g.edgeSet( ), sub.edgeSet( ) ); Set vset = new HashSet( g.vertexSet( ) ); g.removeVertex( _v1 ); vset.remove( _v1 ); assertEquals( vset, sub.vertexSet( ) ); //not losing track assertEquals( g.edgeSet( ), sub.edgeSet( ) ); } private Graph init( boolean listenable ) { Graph g; if( listenable ) { g = new ListenableUndirectedGraph( ); } else { g = new SimpleGraph( ); } g.addVertex( _v1 ); g.addVertex( _v2 ); g.addVertex( _v3 ); g.addVertex( _v4 ); g.addEdge( _v1, _v2 ); g.addEdge( _v2, _v3 ); g.addEdge( _v3, _v1 ); g.addEdge( _v1, _v4 ); return g; } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/traverse/ 0000755 0001751 0001751 00000000000 11251311101 023264 5 ustar moeller moeller libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/traverse/AbstractGraphIteratorTest.java 0000644 0001751 0001751 00000014011 10266566752 031260 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------------------------ * AbstractGraphIteratorTest.java * ------------------------------ * (C) Copyright 2003, by Liviu Rau and Contributors. * * Original Author: Liviu Rau * Contributor(s): Barak Naveh * * $Id: AbstractGraphIteratorTest.java,v 1.8 2005/05/30 05:37:29 perfecthash Exp $ * * Changes * ------- * 30-Jul-2003 : Initial revision (LR); * 06-Aug-2003 : Test traversal listener & extract a shared superclass (BN); * */ package org._3pq.jgrapht.traverse; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.EnhancedTestCase; import org._3pq.jgrapht.event.ConnectedComponentTraversalEvent; import org._3pq.jgrapht.event.EdgeTraversalEvent; import org._3pq.jgrapht.event.TraversalListener; import org._3pq.jgrapht.event.VertexTraversalEvent; import org._3pq.jgrapht.graph.DefaultDirectedWeightedGraph; /** * A basis for testing {@link org._3pq.jgrapht.traverse.BreadthFirstIterator} * and {@link org._3pq.jgrapht.traverse.DepthFirstIterator} classes. * * @author Liviu Rau * * @since Jul 30, 2003 */ public abstract class AbstractGraphIteratorTest extends EnhancedTestCase { StringBuffer m_result; /** * . */ public void testDirectedGraph( ) { m_result = new StringBuffer( ); DirectedGraph graph = createDirectedGraph( ); AbstractGraphIterator iterator = createIterator( graph, "1" ); iterator.addTraversalListener( new MyTraversalListener( ) ); while( iterator.hasNext( ) ) { m_result.append( (String) iterator.next( ) ); if( iterator.hasNext( ) ) { m_result.append( ',' ); } } assertEquals( getExpectedStr2( ), m_result.toString( ) ); } abstract String getExpectedStr1( ); abstract String getExpectedStr2( ); DirectedGraph createDirectedGraph( ) { DirectedGraph graph = new DefaultDirectedWeightedGraph( ); // String v1 = "1"; String v2 = "2"; String v3 = "3"; String v4 = "4"; String v5 = "5"; String v6 = "6"; String v7 = "7"; String v8 = "8"; String v9 = "9"; graph.addVertex( v1 ); graph.addVertex( v2 ); graph.addVertex( "3" ); graph.addVertex( "4" ); graph.addVertex( "5" ); graph.addVertex( "6" ); graph.addVertex( "7" ); graph.addVertex( "8" ); graph.addVertex( "9" ); graph.addVertex( "orphan" ); // NOTE: set weights on some of the edges to test traversals like // ClosestFirstIterator where it matters. For other traversals, it // will be ignored. Rely on the default edge weight being 1. graph.addEdge( v1, v2 ); graph.addEdge( v1, v3 ).setWeight( 100 ); graph.addEdge( v2, v4 ).setWeight( 1000 ); graph.addEdge( v3, v5 ); graph.addEdge( v3, v6 ).setWeight( 100 ); graph.addEdge( v5, v6 ); graph.addEdge( v5, v7 ).setWeight( 200 ); graph.addEdge( v6, v1 ); graph.addEdge( v7, v8 ).setWeight( 100 ); graph.addEdge( v7, v9 ); graph.addEdge( v8, v2 ); graph.addEdge( v9, v4 ); return graph; } abstract AbstractGraphIterator createIterator( DirectedGraph g, Object startVertex ); /** * Internal traversal listener. * * @author Barak Naveh */ private class MyTraversalListener implements TraversalListener { private int m_componentNumber = 0; private int m_numComponentVertices = 0; /** * @see TraversalListener#connectedComponentFinished(ConnectedComponentTraversalEvent) */ public void connectedComponentFinished( ConnectedComponentTraversalEvent e ) { switch( m_componentNumber ) { case 1: assertEquals( getExpectedStr1( ), m_result.toString( ) ); assertEquals( 9, m_numComponentVertices ); break; case 2: assertEquals( getExpectedStr2( ), m_result.toString( ) ); assertEquals( 1, m_numComponentVertices ); break; default: assertFalse( ); break; } m_numComponentVertices = 0; } /** * @see TraversalListener#connectedComponentStarted(ConnectedComponentTraversalEvent) */ public void connectedComponentStarted( ConnectedComponentTraversalEvent e ) { m_componentNumber++; } /** * @see TraversalListener#edgeTraversed(EdgeTraversalEvent) */ public void edgeTraversed( EdgeTraversalEvent e ) { // to be tested... } /** * @see TraversalListener#vertexTraversed(VertexTraversalEvent) */ public void vertexTraversed( VertexTraversalEvent e ) { m_numComponentVertices++; } } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/traverse/AllTraverseTests.java 0000644 0001751 0001751 00000004457 10266566752 027445 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ---------------- * AllTraverseTests.java * ---------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: AllTraverseTests.java,v 1.7 2005/04/26 06:46:38 perfecthash Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * */ package org._3pq.jgrapht.traverse; import junit.framework.Test; import junit.framework.TestSuite; /** * A TestSuite for all tests in this package. * * @author Barak Naveh */ public final class AllTraverseTests { private AllTraverseTests( ) {} // ensure non-instantiability. /** * Creates a test suite for all tests in this package. * * @return a test suite for all tests in this package. */ public static Test suite( ) { TestSuite suite = new TestSuite( ); //$JUnit-BEGIN$ suite.addTest( new TestSuite( BreadthFirstIteratorTest.class ) ); suite.addTest( new TestSuite( DepthFirstIteratorTest.class ) ); suite.addTest( new TestSuite( ClosestFirstIteratorTest.class ) ); suite.addTest( new TestSuite( IgnoreDirectionTest.class ) ); suite.addTest( new TestSuite( TopologicalOrderIteratorTest.class ) ); //$JUnit-END$ return suite; } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/traverse/BreadthFirstIteratorTest.java 0000644 0001751 0001751 00000004517 10266566752 031126 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ----------------------------- * BreadthFirstIteratorTest.java * ----------------------------- * (C) Copyright 2003, by Liviu Rau and Contributors. * * Original Author: Liviu Rau * Contributor(s): Barak Naveh * * $Id: BreadthFirstIteratorTest.java,v 1.8 2004/05/01 12:27:51 barak_naveh Exp $ * * Changes * ------- * 30-Jul-2003 : Initial revision (LR); * 06-Aug-2003 : Test traversal listener & extract a shared superclass (BN); * */ package org._3pq.jgrapht.traverse; import org._3pq.jgrapht.DirectedGraph; /** * Tests for the {@link BreadthFirstIterator} class. * *
* NOTE: This test uses hard-coded expected ordering isn't really guaranteed by * the specification of the algorithm. This could cause false failures if the * traversal implementation changes. *
* * @author Liviu Rau * * @since Jul 30, 2003 */ public class BreadthFirstIteratorTest extends AbstractGraphIteratorTest { String getExpectedStr1( ) { return "1,2,3,4,5,6,7,8,9"; } String getExpectedStr2( ) { return "1,2,3,4,5,6,7,8,9,orphan"; } AbstractGraphIterator createIterator( DirectedGraph g, Object vertex ) { AbstractGraphIterator i = new BreadthFirstIterator( g, vertex ); i.setCrossComponentTraversal( true ); return i; } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/traverse/ClosestFirstIteratorTest.java 0000644 0001751 0001751 00000005423 10266566752 031166 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ----------------------------- * ClosestFirstIteratorTest.java * ----------------------------- * (C) Copyright 2003, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: ClosestFirstIteratorTest.java,v 1.4 2005/05/30 05:37:29 perfecthash Exp $ * * Changes * ------- * 03-Sep-2003 : Initial revision (JVS); * 29-May-2005 : Test radius support (JVS); * */ package org._3pq.jgrapht.traverse; import org._3pq.jgrapht.DirectedGraph; /** * Tests for ClosestFirstIterator. * * @author John V. Sichi * * @since Sep 3, 2003 */ public class ClosestFirstIteratorTest extends AbstractGraphIteratorTest { /** * . */ public void testRadius( ) { m_result = new StringBuffer( ); DirectedGraph graph = createDirectedGraph( ); // NOTE: pick 301 as the radius because it discriminates // the boundary case edge between v7 and v9 AbstractGraphIterator iterator = new ClosestFirstIterator( graph, "1", 301 ); while( iterator.hasNext( ) ) { m_result.append( (String) iterator.next( ) ); if( iterator.hasNext( ) ) { m_result.append( ',' ); } } assertEquals( "1,2,3,5,6,7", m_result.toString( ) ); } // NOTE: the edge weights make the result deterministic String getExpectedStr1( ) { return "1,2,3,5,6,7,9,4,8"; } String getExpectedStr2( ) { return getExpectedStr1( ) + ",orphan"; } AbstractGraphIterator createIterator( DirectedGraph g, Object vertex ) { AbstractGraphIterator i = new ClosestFirstIterator( g, vertex ); i.setCrossComponentTraversal( true ); return i; } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/traverse/DepthFirstIteratorTest.java 0000644 0001751 0001751 00000004505 10266566752 030616 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------------------- * DepthFirstIteratorTest.java * --------------------------- * (C) Copyright 2003, by Liviu Rau and Contributors. * * Original Author: Liviu Rau * Contributor(s): Barak Naveh * * $Id: DepthFirstIteratorTest.java,v 1.8 2004/05/01 12:27:51 barak_naveh Exp $ * * Changes * ------- * 30-Jul-2003 : Initial revision (LR); * 06-Aug-2003 : Test traversal listener & extract a shared superclass (BN); * */ package org._3pq.jgrapht.traverse; import org._3pq.jgrapht.DirectedGraph; /** * Tests for the {@link DepthFirstIteratorTest} class. * ** NOTE: This test uses hard-coded expected ordering isn't really guaranteed by * the specification of the algorithm. This could cause false failures if the * traversal implementation changes. *
* * @author Liviu Rau * * @since Jul 30, 2003 */ public class DepthFirstIteratorTest extends AbstractGraphIteratorTest { String getExpectedStr1( ) { return "1,3,6,5,7,9,4,8,2"; } String getExpectedStr2( ) { return "1,3,6,5,7,9,4,8,2,orphan"; } AbstractGraphIterator createIterator( DirectedGraph g, Object vertex ) { AbstractGraphIterator i = new DepthFirstIterator( g, vertex ); i.setCrossComponentTraversal( true ); return i; } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/traverse/IgnoreDirectionTest.java 0000644 0001751 0001751 00000005043 10266566752 030112 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------------------- * IgnoreDirectionTest.java * --------------------------- * (C) Copyright 2003, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: IgnoreDirectionTest.java,v 1.5 2004/05/01 12:27:51 barak_naveh Exp $ * * Changes * ------- * 08-Aug-2003 : Initial revision (JVS); * */ package org._3pq.jgrapht.traverse; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.UndirectedGraph; import org._3pq.jgrapht.graph.AsUndirectedGraph; /** * Tests for the ignoreDirection parameter to XXFirstIterator. * ** NOTE: This test uses hard-coded expected ordering isn't really guaranteed by * the specification of the algorithm. This could cause false failures if the * traversal implementation changes. *
* * @author John V. Sichi * * @since Aug 8, 2003 */ public class IgnoreDirectionTest extends AbstractGraphIteratorTest { String getExpectedStr1( ) { return "4,9,7,8,5,6,1,3,2"; } String getExpectedStr2( ) { return "4,9,7,8,5,6,1,3,2,orphan"; } AbstractGraphIterator createIterator( DirectedGraph g, Object vertex ) { // ignore the passed in vertex and always start from v4, since that's // the only vertex without out-edges UndirectedGraph undirectedView = new AsUndirectedGraph( g ); AbstractGraphIterator i = new DepthFirstIterator( undirectedView, "4" ); i.setCrossComponentTraversal( true ); return i; } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/traverse/TopologicalOrderIteratorTest.java 0000644 0001751 0001751 00000006550 10266566752 032014 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------------------- * IgnoreDirectionTest.java * --------------------------- * (C) Copyright 2005, by John V. Sichi and Contributors. * * Original Author: John V. Sichi * Contributor(s): - * * $Id: TopologicalOrderIteratorTest.java,v 1.1 2005/04/26 06:46:38 perfecthash Exp $ * * Changes * ------- * 25-Apr-2005 : Initial revision (JVS); * */ package org._3pq.jgrapht.traverse; import java.util.Iterator; import org._3pq.jgrapht.DirectedGraph; import org._3pq.jgrapht.EnhancedTestCase; import org._3pq.jgrapht.graph.DefaultDirectedGraph; /** * Tests for TopologicalOrderIterator. * * @author John V. Sichi * * @since Apr 25, 2005 */ public class TopologicalOrderIteratorTest extends EnhancedTestCase { /** * . */ public void testRecipe( ) { DirectedGraph graph = new DefaultDirectedGraph( ); String[] v = new String[ 9 ]; v[ 0 ] = "preheat oven"; v[ 1 ] = "sift dry ingredients"; v[ 2 ] = "stir wet ingredients"; v[ 3 ] = "mix wet and dry ingredients"; v[ 4 ] = "spoon onto pan"; v[ 5 ] = "bake"; v[ 6 ] = "cool"; v[ 7 ] = "frost"; v[ 8 ] = "eat"; // add in mixed up order graph.addVertex( v[ 4 ] ); graph.addVertex( v[ 8 ] ); graph.addVertex( v[ 1 ] ); graph.addVertex( v[ 3 ] ); graph.addVertex( v[ 7 ] ); graph.addVertex( v[ 6 ] ); graph.addVertex( v[ 0 ] ); graph.addVertex( v[ 2 ] ); graph.addVertex( v[ 5 ] ); // specify enough edges to guarantee deterministic total order graph.addEdge( v[ 0 ], v[ 1 ] ); graph.addEdge( v[ 1 ], v[ 2 ] ); graph.addEdge( v[ 0 ], v[ 2 ] ); graph.addEdge( v[ 1 ], v[ 3 ] ); graph.addEdge( v[ 2 ], v[ 3 ] ); graph.addEdge( v[ 3 ], v[ 4 ] ); graph.addEdge( v[ 4 ], v[ 5 ] ); graph.addEdge( v[ 5 ], v[ 6 ] ); graph.addEdge( v[ 6 ], v[ 7 ] ); graph.addEdge( v[ 7 ], v[ 8 ] ); graph.addEdge( v[ 6 ], v[ 8 ] ); Iterator iter = new TopologicalOrderIterator( graph ); int i = 0; while( iter.hasNext( ) ) { assertEquals( v[ i ], iter.next( ) ); ++i; } } } // End TopologicalOrderIteratorTest.java libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/AllTests.java 0000644 0001751 0001751 00000006336 10266566752 024074 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* ------------- * AllTests.java * ------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: AllTests.java,v 1.6 2005/04/14 22:04:05 perfecthash Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * */ package org._3pq.jgrapht; import java.util.Enumeration; import junit.framework.Test; import junit.framework.TestSuite; import org._3pq.jgrapht.alg.AllAlgTests; import org._3pq.jgrapht.edge.AllEdgeTests; import org._3pq.jgrapht.generate.AllGenerateTests; import org._3pq.jgrapht.graph.AllGraphTests; import org._3pq.jgrapht.traverse.AllTraverseTests; /** * Runs all unit tests of the JGraphT library. * * @author Barak Naveh */ public final class AllTests { private AllTests( ) {} // ensure non-instantiability. /** * Creates a test suite that includes all JGraphT tests. * * @return a test suite that includes all JGraphT tests. */ public static Test suite( ) { ExpandableTestSuite suite = new ExpandableTestSuite( "All tests of JGraphT" ); suite.addTestSuit( (TestSuite) AllAlgTests.suite( ) ); suite.addTestSuit( (TestSuite) AllEdgeTests.suite( ) ); suite.addTestSuit( (TestSuite) AllGenerateTests.suite( ) ); suite.addTestSuit( (TestSuite) AllGraphTests.suite( ) ); suite.addTestSuit( (TestSuite) AllTraverseTests.suite( ) ); return suite; } private static class ExpandableTestSuite extends TestSuite { /** * @see TestSuite#TestSuite() */ public ExpandableTestSuite( ) { super( ); } /** * @see TestSuite#TestSuite(java.lang.String) */ public ExpandableTestSuite( String name ) { super( name ); } /** * Adds all the test from the specified suite into this suite. * * @param suite */ public void addTestSuit( TestSuite suite ) { for( Enumeration e = suite.tests( ); e.hasMoreElements( ); ) { Test t = (Test) e.nextElement( ); this.addTest( t ); } } } } libjgrapht0.6-java-0.6.0/testsrc/org/_3pq/jgrapht/EnhancedTestCase.java 0000644 0001751 0001751 00000004126 10266566752 025475 0 ustar moeller moeller /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Lead: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2004, by Barak Naveh and Contributors. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* --------------------- * EnhancedTestCase.java * --------------------- * (C) Copyright 2003, by Barak Naveh and Contributors. * * Original Author: Barak Naveh * Contributor(s): - * * $Id: EnhancedTestCase.java,v 1.2 2004/05/01 12:24:42 barak_naveh Exp $ * * Changes * ------- * 24-Jul-2003 : Initial revision (BN); * */ package org._3pq.jgrapht; import junit.framework.TestCase; /** * A little extension to JUnit's TestCase. * * @author Barak Naveh * * @since Jul 25, 2003 */ public class EnhancedTestCase extends TestCase { /** * @see TestCase#TestCase() */ public EnhancedTestCase( ) { super( ); } /** * @see TestCase#TestCase(java.lang.String) */ public EnhancedTestCase( String name ) { super( name ); } /** * It means: it's wrong that we got here. */ public void assertFalse( ) { assertTrue( false ); } /** * It means: it's right that we got here. */ public void assertTrue( ) { assertTrue( true ); } } libjgrapht0.6-java-0.6.0/README.html 0000644 0001751 0001751 00000033126 10266566746 016542 0 ustar moeller moellerReleased: July, 2005
Written by Barak Naveh (barak_naveh@users.sourceforge.net) and Contributors.
(C) Copyright 2003-2005, by Barak Naveh and Contributors. All rights reserved.
Please address all contributions, suggestions, and inquiries to the current project administrator, John Sichi.
JGraphT is a free Java class library that provides mathematical graph-theory objects and algorithms. It runs on Java 2 Platform (JDK 1.4 or later).
JGraphT is licensed under the terms of the GNU Lesser General Public License (LGPL). A copy of the license is included in the download.
Please note that JGraphT is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Please refer to the license for details.
jgrapht.jar |
the compiled JGraphT library |
README.html |
this file |
licence-LGPL.txt |
GNU Lesser General Public License |
javadoc/ |
Javadoc documentation |
lib/ |
required libraries:
|
src/ |
source code |
testsrc/ |
source code of unit tests |
The package org._3pq.jgrapht.demo
includes small demo
applications to help you get started. If you spawn your own demo app and
think others can use it, please send it to us and we will add it to that
package.
To help upgrading, JGraphT maintains a one-version-backwards compatibility. While this compatibility is not a hard promise, it generally respected. You can upgrade via:
The safe way : compile your app with the JGraphT version that immediately follows your existing version and follow the deprecation notes, if exist, and modify your application accordingly. Then move to the next version, and on, until you're current.
The fast way : go to the latest JGraphT right away - if it works, you're done.
Reading the change history is always recommended.
A local copy of the Javadoc HTML files is included in this distribution. The latest version of these files is also available on-line at http://jgrapht.sourceforge.net/javadoc.
junit.jar
runtime file is included in
this distribution. JUnit is licensed under the terms of the IBM Common
Public License. You can find out more about JUnit and/or download the
latest version from http://www.junit.org. The JUnit tests included
with JGraphT have been created using JUnit 3.8.1.jgraph.jar
runtime
file of JGraph is included in this distribution (file
lib/lib-readme.txt has version information). JGraph is licensed
under the terms of the GNU Lesser General Public License
(LGPL). You can find more about JGraph and/or download the latest
version from http://www.sourceforge.net/projects/jgraph.The JGraphT website is at: http://jgrapht.sourceforge.net. You can use this site to:
If you add improvements to JGraphT please send them to us. We will add them to the next release so that everyone can enjoy them. You might also benefit from it: others may fix bugs in your source files or may continue to enhance them.
If you particularly like the elegance of a certain part, don't be shy to drop a compliment to its author - it's heartening (and it's free)! On the other hand, if you think you can do better, let's see your source...
Changes to JGraphT in each version:
JGraphModelAdapter
support for JGraph's "dangling"
edges, its constructors have slightly changed and now forbid
null
values. Improved interface to
DijskstraShortestPath
, and added radius support to
ClosestFirstIterator
. Added new
StrongConnectivityInspector
algorithm (contributed by
Chris Soltenborn) and TopologicalOrderIterator
(contributed by Marden Neubert). Deleted deprecated
TraverseUtils
. Upgraded to JGraph 5.6.1.1.JGraphT wouldn't be the library it is today without the source contributions made by the authors:
(if we have missed your name on this list, please email us to get it fixed).
Other people have also helped in different ways: reporting bugs, requesting features, commenting, and by merely asking very good questions. Many thanks to all of you.
Regards,
Barak Naveh
JGraphT Project Founder
© Copyright 2003-2004, by Barak Naveh and Contributors. All rights reserved. |