Package edu.uci.ics.jung.graph
Class SparseGraph<V,E>
- java.lang.Object
-
- edu.uci.ics.jung.graph.AbstractGraph<V,E>
-
- edu.uci.ics.jung.graph.SparseGraph<V,E>
-
- All Implemented Interfaces:
Graph<V,E>,Hypergraph<V,E>,java.io.Serializable
public class SparseGraph<V,E> extends AbstractGraph<V,E> implements Graph<V,E>
An implementation ofGraphthat is suitable for sparse graphs and permits both directed and undirected edges.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<E,Pair<V>>directed_edgesprotected static intINCIDENTprotected static intINCOMINGprotected static intOUTGOINGprotected java.util.Map<E,Pair<V>>undirected_edgesprotected java.util.Map<V,java.util.Map<V,E>[]>vertex_maps
-
Constructor Summary
Constructors Constructor Description SparseGraph()Creates an instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType)Addsedgeto this graph with the specifiedendpointsandEdgeType.booleanaddVertex(V vertex)Addsvertexto this graph.booleancontainsEdge(E edge)Returns true if this graph's edge collection containsedge.booleancontainsVertex(V vertex)Returns true if this graph's vertex collection containsvertex.EfindEdge(V v1, V v2)Returns an edge that connects this vertex tov.java.util.Collection<E>findEdgeSet(V v1, V v2)Returns all edges that connects this vertex tov.EdgeTypegetDefaultEdgeType()Returns the default edge type for this graph.VgetDest(E directed_edge)Ifdirected_edgeis a directed edge in this graph, returns the destination; otherwise returnsnull.intgetEdgeCount()Returns the number of edges in this graph.intgetEdgeCount(EdgeType edge_type)Returns the number of edges of typeedge_typein this graph.java.util.Collection<E>getEdges()Returns a view of all edges in this graph.java.util.Collection<E>getEdges(EdgeType edgeType)Returns the collection of edges in this graph which are of typeedge_type.EdgeTypegetEdgeType(E edge)Returns the edge type ofedgein this graph.Pair<V>getEndpoints(E edge)Returns the endpoints ofedgeas aPair.static <V,E>
com.google.common.base.Supplier<Graph<V,E>>getFactory()java.util.Collection<E>getIncidentEdges(V vertex)Returns the collection of edges in this graph which are connected tovertex.java.util.Collection<E>getInEdges(V vertex)Returns aCollectionview of the incoming edges incident tovertexin this graph.java.util.Collection<V>getNeighbors(V vertex)Returns the collection of vertices which are connected tovertexvia any edges in this graph.java.util.Collection<E>getOutEdges(V vertex)Returns aCollectionview of the outgoing edges incident tovertexin this graph.java.util.Collection<V>getPredecessors(V vertex)Returns aCollectionview of the predecessors ofvertexin this graph.VgetSource(E directed_edge)Ifdirected_edgeis a directed edge in this graph, returns the source; otherwise returnsnull.java.util.Collection<V>getSuccessors(V vertex)Returns aCollectionview of the successors ofvertexin this graph.intgetVertexCount()Returns the number of vertices in this graph.java.util.Collection<V>getVertices()Returns a view of all vertices in this graph.booleanisDest(V vertex, E edge)Returnstrueifvertexis the destination ofedge.booleanisSource(V vertex, E edge)Returnstrueifvertexis the source ofedge.booleanremoveEdge(E edge)Removesedgefrom this graph.booleanremoveVertex(V vertex)Removesvertexfrom this graph.-
Methods inherited from class edu.uci.ics.jung.graph.AbstractGraph
addEdge, addEdge, addEdge, addEdge, addEdge, degree, getIncidentCount, getIncidentVertices, getNeighborCount, getOpposite, getPredecessorCount, getSuccessorCount, getValidatedEndpoints, inDegree, isIncident, isNeighbor, isPredecessor, isSuccessor, outDegree, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface edu.uci.ics.jung.graph.Graph
addEdge, addEdge, getOpposite, getPredecessorCount, getSuccessorCount, inDegree, isPredecessor, isSuccessor, outDegree
-
Methods inherited from interface edu.uci.ics.jung.graph.Hypergraph
addEdge, addEdge, degree, getIncidentCount, getIncidentVertices, getNeighborCount, isIncident, isNeighbor
-
-
-
-
Field Detail
-
INCOMING
protected static final int INCOMING
- See Also:
- Constant Field Values
-
OUTGOING
protected static final int OUTGOING
- See Also:
- Constant Field Values
-
INCIDENT
protected static final int INCIDENT
- See Also:
- Constant Field Values
-
-
Method Detail
-
getFactory
public static <V,E> com.google.common.base.Supplier<Graph<V,E>> getFactory()
- Type Parameters:
V- the vertex type for the graph SupplierE- the edge type for the graph Supplier- Returns:
- a
Supplierthat creates an instance of this graph type.
-
findEdge
public E findEdge(V v1, V v2)
Description copied from interface:HypergraphReturns an edge that connects this vertex tov. If this edge is not uniquely defined (that is, if the graph contains more than one edge connectingv1tov2), any of these edges may be returned.findEdgeSet(v1, v2)may be used to return all such edges. Returns null if either of the following is true:v2is not connected tov1- either
v1orv2are not present in this graph
Note: for purposes of this method,
v1is only considered to be connected tov2via a given directed edgeeifv1 == e.getSource() && v2 == e.getDest()evaluates totrue. (v1andv2are connected by an undirected edgeuifuis incident to bothv1andv2.)- Specified by:
findEdgein interfaceHypergraph<V,E>- Overrides:
findEdgein classAbstractGraph<V,E>- Parameters:
v1- the first endpoint of the returned edgev2- the second endpoint of the returned edge- Returns:
- an edge that connects
v1tov2, ornullif no such edge exists (or either vertex is not present) - See Also:
Hypergraph.findEdgeSet(Object, Object)
-
findEdgeSet
public java.util.Collection<E> findEdgeSet(V v1, V v2)
Description copied from interface:HypergraphReturns all edges that connects this vertex tov. If this edge is not uniquely defined (that is, if the graph contains more than one edge connectingv1tov2), any of these edges may be returned.findEdgeSet(v1, v2)may be used to return all such edges. Returns null ifv2is not connected tov1.
Returns an empty collection if eitherv1orv2are not present in this graph.Note: for purposes of this method,
v1is only considered to be connected tov2via a given directed edgedifv1 == d.getSource() && v2 == d.getDest()evaluates totrue. (v1andv2are connected by an undirected edgeuifuis incident to bothv1andv2.)- Specified by:
findEdgeSetin interfaceHypergraph<V,E>- Overrides:
findEdgeSetin classAbstractGraph<V,E>- Parameters:
v1- the first endpoint of the returned edge setv2- the second endpoint of the returned edge set- Returns:
- a collection containing all edges that connect
v1tov2, ornullif either vertex is not present - See Also:
Hypergraph.findEdge(Object, Object)
-
addEdge
public boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType)
Description copied from class:AbstractGraphAddsedgeto this graph with the specifiedendpointsandEdgeType.- Specified by:
addEdgein classAbstractGraph<V,E>- Parameters:
edge- the edge to be addedendpoints- the endpoints to be connected to this edgeedgeType- the type of edge to add- Returns:
- true iff the graph was modified as a result of this call
-
getInEdges
public java.util.Collection<E> getInEdges(V vertex)
Description copied from interface:GraphReturns aCollectionview of the incoming edges incident tovertexin this graph.- Specified by:
getInEdgesin interfaceGraph<V,E>- Specified by:
getInEdgesin interfaceHypergraph<V,E>- Parameters:
vertex- the vertex whose incoming edges are to be returned- Returns:
- a
Collectionview of the incoming edges incident tovertexin this graph
-
getOutEdges
public java.util.Collection<E> getOutEdges(V vertex)
Description copied from interface:GraphReturns aCollectionview of the outgoing edges incident tovertexin this graph.- Specified by:
getOutEdgesin interfaceGraph<V,E>- Specified by:
getOutEdgesin interfaceHypergraph<V,E>- Parameters:
vertex- the vertex whose outgoing edges are to be returned- Returns:
- a
Collectionview of the outgoing edges incident tovertexin this graph
-
getPredecessors
public java.util.Collection<V> getPredecessors(V vertex)
Description copied from interface:GraphReturns aCollectionview of the predecessors ofvertexin this graph. A predecessor ofvertexis defined as a vertexvwhich is connected tovertexby an edgee, whereeis an outgoing edge ofvand an incoming edge ofvertex.- Specified by:
getPredecessorsin interfaceGraph<V,E>- Specified by:
getPredecessorsin interfaceHypergraph<V,E>- Parameters:
vertex- the vertex whose predecessors are to be returned- Returns:
- a
Collectionview of the predecessors ofvertexin this graph
-
getSuccessors
public java.util.Collection<V> getSuccessors(V vertex)
Description copied from interface:GraphReturns aCollectionview of the successors ofvertexin this graph. A successor ofvertexis defined as a vertexvwhich is connected tovertexby an edgee, whereeis an incoming edge ofvand an outgoing edge ofvertex.- Specified by:
getSuccessorsin interfaceGraph<V,E>- Specified by:
getSuccessorsin interfaceHypergraph<V,E>- Parameters:
vertex- the vertex whose predecessors are to be returned- Returns:
- a
Collectionview of the successors ofvertexin this graph
-
getEdges
public java.util.Collection<E> getEdges(EdgeType edgeType)
Description copied from interface:HypergraphReturns the collection of edges in this graph which are of typeedge_type.- Specified by:
getEdgesin interfaceHypergraph<V,E>- Parameters:
edgeType- the type of edges to be returned- Returns:
- the collection of edges which are of type
edge_type, ornullif the graph does not accept edges of this type - See Also:
EdgeType
-
getEndpoints
public Pair<V> getEndpoints(E edge)
Description copied from interface:GraphReturns the endpoints ofedgeas aPair.- Specified by:
getEndpointsin interfaceGraph<V,E>- Parameters:
edge- the edge whose endpoints are to be returned- Returns:
- the endpoints (incident vertices) of
edge
-
getEdgeType
public EdgeType getEdgeType(E edge)
Description copied from interface:HypergraphReturns the edge type ofedgein this graph.- Specified by:
getEdgeTypein interfaceHypergraph<V,E>- Parameters:
edge- the edge whose type is to be returned- Returns:
- the
EdgeTypeofedge, ornullifedgehas no defined type
-
getSource
public V getSource(E directed_edge)
Description copied from interface:GraphIfdirected_edgeis a directed edge in this graph, returns the source; otherwise returnsnull. The source of a directed edgedis defined to be the vertex for whichdis an outgoing edge.directed_edgeis guaranteed to be a directed edge if itsEdgeTypeisDIRECTED.
-
getDest
public V getDest(E directed_edge)
Description copied from interface:GraphIfdirected_edgeis a directed edge in this graph, returns the destination; otherwise returnsnull. The destination of a directed edgedis defined to be the vertex incident todfor whichdis an incoming edge.directed_edgeis guaranteed to be a directed edge if itsEdgeTypeisDIRECTED.
-
isSource
public boolean isSource(V vertex, E edge)
Description copied from interface:GraphReturnstrueifvertexis the source ofedge. Equivalent togetSource(edge).equals(vertex).
-
isDest
public boolean isDest(V vertex, E edge)
Description copied from interface:GraphReturnstrueifvertexis the destination ofedge. Equivalent togetDest(edge).equals(vertex).
-
getEdges
public java.util.Collection<E> getEdges()
Description copied from interface:HypergraphReturns a view of all edges in this graph. In general, this obeys theCollectioncontract, and therefore makes no guarantees about the ordering of the vertices within the set.- Specified by:
getEdgesin interfaceHypergraph<V,E>- Returns:
- a
Collectionview of all edges in this graph
-
getVertices
public java.util.Collection<V> getVertices()
Description copied from interface:HypergraphReturns a view of all vertices in this graph. In general, this obeys theCollectioncontract, and therefore makes no guarantees about the ordering of the vertices within the set.- Specified by:
getVerticesin interfaceHypergraph<V,E>- Returns:
- a
Collectionview of all vertices in this graph
-
containsVertex
public boolean containsVertex(V vertex)
Description copied from interface:HypergraphReturns true if this graph's vertex collection containsvertex. Equivalent togetVertices().contains(vertex).- Specified by:
containsVertexin interfaceHypergraph<V,E>- Parameters:
vertex- the vertex whose presence is being queried- Returns:
- true iff this graph contains a vertex
vertex
-
containsEdge
public boolean containsEdge(E edge)
Description copied from interface:HypergraphReturns true if this graph's edge collection containsedge. Equivalent togetEdges().contains(edge).- Specified by:
containsEdgein interfaceHypergraph<V,E>- Parameters:
edge- the edge whose presence is being queried- Returns:
- true iff this graph contains an edge
edge
-
getEdgeCount
public int getEdgeCount()
Description copied from interface:HypergraphReturns the number of edges in this graph.- Specified by:
getEdgeCountin interfaceHypergraph<V,E>- Returns:
- the number of edges in this graph
-
getVertexCount
public int getVertexCount()
Description copied from interface:HypergraphReturns the number of vertices in this graph.- Specified by:
getVertexCountin interfaceHypergraph<V,E>- Returns:
- the number of vertices in this graph
-
getNeighbors
public java.util.Collection<V> getNeighbors(V vertex)
Description copied from interface:HypergraphReturns the collection of vertices which are connected tovertexvia any edges in this graph. Ifvertexis connected to itself with a self-loop, then it will be included in the collection returned.- Specified by:
getNeighborsin interfaceHypergraph<V,E>- Parameters:
vertex- the vertex whose neighbors are to be returned- Returns:
- the collection of vertices which are connected to
vertex, ornullifvertexis not present
-
getIncidentEdges
public java.util.Collection<E> getIncidentEdges(V vertex)
Description copied from interface:HypergraphReturns the collection of edges in this graph which are connected tovertex.- Specified by:
getIncidentEdgesin interfaceHypergraph<V,E>- Parameters:
vertex- the vertex whose incident edges are to be returned- Returns:
- the collection of edges which are connected to
vertex, ornullifvertexis not present
-
addVertex
public boolean addVertex(V vertex)
Description copied from interface:HypergraphAddsvertexto this graph. Fails ifvertexis null or already in the graph.- Specified by:
addVertexin interfaceHypergraph<V,E>- Parameters:
vertex- the vertex to add- Returns:
trueif the add is successful, andfalseotherwise
-
removeVertex
public boolean removeVertex(V vertex)
Description copied from interface:HypergraphRemovesvertexfrom this graph. As a side effect, removes any edgeseincident tovertexif the removal ofvertexwould causeeto be incident to an illegal number of vertices. (Thus, for example, incident hyperedges are not removed, but incident edges--which must be connected to a vertex at both endpoints--are removed.)Fails under the following circumstances:
vertexis not an element of this graphvertexisnull
- Specified by:
removeVertexin interfaceHypergraph<V,E>- Parameters:
vertex- the vertex to remove- Returns:
trueif the removal is successful,falseotherwise
-
removeEdge
public boolean removeEdge(E edge)
Description copied from interface:HypergraphRemovesedgefrom this graph. Fails ifedgeis null, or is otherwise not an element of this graph.- Specified by:
removeEdgein interfaceHypergraph<V,E>- Parameters:
edge- the edge to remove- Returns:
trueif the removal is successful,falseotherwise
-
getEdgeCount
public int getEdgeCount(EdgeType edge_type)
Description copied from interface:HypergraphReturns the number of edges of typeedge_typein this graph.- Specified by:
getEdgeCountin interfaceHypergraph<V,E>- Parameters:
edge_type- the type of edge for which the count is to be returned- Returns:
- the number of edges of type
edge_typein this graph
-
getDefaultEdgeType
public EdgeType getDefaultEdgeType()
Description copied from interface:HypergraphReturns the default edge type for this graph.- Specified by:
getDefaultEdgeTypein interfaceHypergraph<V,E>- Returns:
- the default edge type for this graph
-
-