Package org.jgrapht
Class Graphs
java.lang.Object
org.jgrapht.Graphs
- Direct Known Subclasses:
GraphHelper
A collection of utilities to assist with graph manipulation.
- Since:
- Jul 31, 2003
- Author:
- Barak Naveh
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <V,
E> boolean addAllEdges
(Graph<? super V, ? super E> destination, Graph<V, E> source, Collection<? extends E> edges) Adds a subset of the edges of the specified source graph to the specified destination graph.static <V,
E> boolean addAllVertices
(Graph<? super V, ? super E> destination, Collection<? extends V> vertices) Adds all of the specified vertices to the destination graph.static <V,
E> E Creates a new edge and adds it to the specified graph similarly to theGraph.addEdge(Object, Object)
method.static <V,
E> boolean addEdgeWithVertices
(Graph<V, E> targetGraph, Graph<V, E> sourceGraph, E edge) Adds the specified edge to the graph, including its vertices if not already included.static <V,
E> E addEdgeWithVertices
(Graph<V, E> g, V sourceVertex, V 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 theGraph.addEdge(Object, Object)
method.static <V,
E> E addEdgeWithVertices
(Graph<V, E> g, V sourceVertex, V 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 theGraph.addEdge(Object, Object)
method.static <V,
E> boolean Adds all the vertices and all the edges of the specified source graph to the specified destination graph.static <V,
E> void addGraphReversed
(DirectedGraph<? super V, ? super E> destination, DirectedGraph<V, E> 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 <V,
E> V getOppositeVertex
(Graph<V, E> g, E e, V v) Gets the vertex opposite another vertex across an edge.static <V,
E> List <V> getPathVertexList
(GraphPath<V, E> path) Gets the list of vertices visited by a path.static <V,
E> List <V> neighborListOf
(Graph<V, E> g, V vertex) Returns a list of vertices that are the neighbors of a specified vertex.static <V,
E> List <V> predecessorListOf
(DirectedGraph<V, E> g, V vertex) Returns a list of vertices that are the direct predecessors of a specified vertex.static <V,
E> List <V> successorListOf
(DirectedGraph<V, E> g, V vertex) Returns a list of vertices that are the direct successors of a specified vertex.static <V,
E> boolean testIncidence
(Graph<V, E> g, E e, V v) Tests whether an edge is incident to a vertex.static <V,
E> UndirectedGraph <V, E> undirectedGraph
(Graph<V, E> g) Returns an undirected view of the specified graph.
-
Constructor Details
-
Graphs
public Graphs()
-
-
Method Details
-
addEdge
Creates a new edge and adds it to the specified graph similarly to theGraph.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:
-
addEdgeWithVertices
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 theGraph.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 <V,E> boolean addEdgeWithVertices(Graph<V, E> targetGraph, Graph<V, E> sourceGraph, E edge) Adds the specified edge to the graph, including its vertices if not already included.- Parameters:
targetGraph
- the graph for which the specified edge to be added.sourceGraph
- the graph in which the specified edge is already presentedge
- edge to add- Returns:
- true if the target graph did not already contain the specified edge.
-
addEdgeWithVertices
public static <V,E> E addEdgeWithVertices(Graph<V, E> g, V sourceVertex, V 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 theGraph.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
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 returnstrue
if the destination graph has been modified as a result of this operation, otherwise it returnsfalse
.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 <V,E> void addGraphReversed(DirectedGraph<? super V, ? super E> destination, DirectedGraph<V, E> source) Adds all the vertices and all the edges of the specified source digraph to the specified destination digraph, reversing all of the edges. If you want to do this as a linked view of the source graph (rather than by copying to a destination graph), useEdgeReversedGraph
instead.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.- See Also:
-
addAllEdges
public static <V,E> boolean addAllEdges(Graph<? super V, ? super E> destination, Graph<V, E> source, Collection<? extends E> edges) Adds a subset of the edges of the specified source graph to the specified destination graph. The behavior of this operation is undefined if either of the graphs is modified while the operation is in progress.addEdgeWithVertices(org.jgrapht.Graph<V, E>, V, V)
is used for the transfer, so source vertexes will be added automatically to the target graph.- Parameters:
destination
- the graph to which edges are to be addedsource
- the graph used as a source for edges to addedges
- the edges to be added- Returns:
- true if this graph changed as a result of the call
-
addAllVertices
public static <V,E> boolean addAllVertices(Graph<? super V, ? super E> destination, Collection<? extends V> vertices) Adds all of the specified vertices to the destination 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 theGraph.addVertex(Object)
method.- Parameters:
destination
- the graph to which edges are to be addedvertices
- the vertices to be added to the graph.- Returns:
- true if 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 Also:
-
neighborListOf
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
Returns a list of vertices that are the direct 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 direct predecessors of the specified vertex.
-
successorListOf
Returns a list of vertices that are the direct 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 direct successors of the specified vertex.
-
undirectedGraph
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 already undirected, just returns it.- Parameters:
g
- the graph for which an undirected view is to be returned.- Returns:
- an undirected view of the specified graph, if it is directed, or or the specified graph itself if it is already undirected.
- Throws:
IllegalArgumentException
- if the graph is neither DirectedGraph nor UndirectedGraph.- See Also:
-
testIncidence
Tests whether an edge is incident to a vertex.- Parameters:
g
- graph containing e and ve
- edge in gv
- vertex in g- Returns:
- true iff e is incident on v
-
getOppositeVertex
Gets the vertex opposite another vertex across an edge.- Parameters:
g
- graph containing e and ve
- edge in gv
- vertex in g- Returns:
- vertex opposite to v across e
-
getPathVertexList
Gets the list of vertices visited by a path.- Parameters:
path
- path of interest- Returns:
- corresponding vertex list
-