org.jscience.computing.graph
Class GraphHelper

java.lang.Object
  extended by org.jscience.computing.graph.GraphHelper

public final class GraphHelper
extends java.lang.Object

A collection of utilities to assist the working with graphs.

Since:
Jul 31, 2003

Method Summary
static Edge addEdge(Graph g, java.lang.Object sourceVertex, java.lang.Object targetVertex, double weight)
          Creates a new edge and adds it to the specified graph similarly to the Graph.addEdge(Object,Object) method.
static boolean addEdgeWithVertices(Graph g, Edge e)
          Adds the specified edge to the specified graph including its vertices.
static Edge addEdgeWithVertices(Graph g, java.lang.Object sourceVertex, java.lang.Object targetVertex)
          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 Graph.addEdge(Object,Object) method.
static Edge addEdgeWithVertices(Graph g, java.lang.Object sourceVertex, java.lang.Object targetVertex, double weight)
          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 Graph.addEdge(Object,Object) method.
static boolean addGraph(Graph destination, Graph source)
          Adds all the vertices and all the edges of the specified source graph to the specified destination graph.
static void addGraphReversed(DirectedGraph destination, DirectedGraph source)
          Adds all the vertices and all the edges of the specified source digraph to the specified destination digraph, reversing all of the edges.
static java.util.List neighborListOf(Graph g, java.lang.Object vertex)
          Returns a list of vertices that are the neighbors of a specified vertex.
static java.util.List predecessorListOf(DirectedGraph g, java.lang.Object vertex)
          Returns a list of vertices that are the predecessors of a specified vertex.
static java.util.List successorListOf(DirectedGraph g, java.lang.Object vertex)
          Returns a list of vertices that are the successors of a specified vertex.
static UndirectedGraph undirectedGraph(Graph g)
          Returns an undirected view of the specified graph.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

addEdge

public static Edge addEdge(Graph g,
                           java.lang.Object sourceVertex,
                           java.lang.Object targetVertex,
                           double weight)
Creates a new edge and adds it to the specified graph similarly to the Graph.addEdge(Object,Object) method.

Parameters:
g - the graph for which the edge to be added.
sourceVertex - source vertex of the edge.
targetVertex - target vertex of the edge.
weight - weight of the edge.
Returns:
The newly created edge if added to the graph, otherwise null.
See Also:
Graph.addEdge(Object,Object)

addEdgeWithVertices

public static boolean addEdgeWithVertices(Graph g,
                                          Edge e)
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).

Parameters:
g - the graph for which the specified edge to be added.
e - the edge to be added to the graph (including its vertices).
Returns:
true if and only if the specified edge was not already contained in the graph.

addEdgeWithVertices

public static Edge addEdgeWithVertices(Graph g,
                                       java.lang.Object sourceVertex,
                                       java.lang.Object targetVertex)
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 Graph.addEdge(Object,Object) method.

Parameters:
g - the graph for which the specified edge to be added.
sourceVertex - source vertex of the edge.
targetVertex - target vertex of the edge.
Returns:
The newly created edge if added to the graph, otherwise null.

addEdgeWithVertices

public static Edge addEdgeWithVertices(Graph g,
                                       java.lang.Object sourceVertex,
                                       java.lang.Object targetVertex,
                                       double weight)
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 Graph.addEdge(Object,Object) method.

Parameters:
g - the graph for which the specified edge to be added.
sourceVertex - source vertex of the edge.
targetVertex - target vertex of the edge.
weight - weight of the edge.
Returns:
The newly created edge if added to the graph, otherwise null.

addGraph

public static boolean addGraph(Graph destination,
                               Graph source)
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.

Parameters:
destination - the graph to which vertices and edges are added.
source - the graph used as source for vertices and edges to add.
Returns:
true if and only if the destination graph has been changed as a result of this operation.

addGraphReversed

public static void addGraphReversed(DirectedGraph destination,
                                    DirectedGraph source)
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.

Parameters:
destination - the graph to which vertices and edges are added.
source - the graph used as source for vertices and edges to add.

neighborListOf

public static java.util.List neighborListOf(Graph g,
                                            java.lang.Object vertex)
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.

Parameters:
g - the graph to look for neighbors in.
vertex - the vertex to get the neighbors of.
Returns:
a list of the vertices that are the neighbors of the specified vertex.

predecessorListOf

public static java.util.List predecessorListOf(DirectedGraph g,
                                               java.lang.Object vertex)
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.

Parameters:
g - the graph to look for predecessors in.
vertex - the vertex to get the predecessors of.
Returns:
a list of the vertices that are the predecessors of the specified vertex.

successorListOf

public static java.util.List successorListOf(DirectedGraph g,
                                             java.lang.Object vertex)
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.

Parameters:
g - the graph to look for successors in.
vertex - the vertex to get the successors of.
Returns:
a list of the vertices that are the successors of the specified vertex.

undirectedGraph

public static UndirectedGraph undirectedGraph(Graph g)
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.

Parameters:
g - the graph for which an undirected view to be returned.
Returns:
an undirected view of the specified graph, if it is directed, or or the specified graph itself if it is undirected.
Throws:
java.lang.IllegalArgumentException - if the graph is neither DirectedGraph nor UndirectedGraph.
See Also:
AsUndirectedGraph