edu.uci.ics.jung.graph
Class SparseGraph<V,E>

java.lang.Object
  extended by edu.uci.ics.jung.graph.AbstractGraph<V,E>
      extended by edu.uci.ics.jung.graph.SparseGraph<V,E>
All Implemented Interfaces:
Graph<V,E>, Hypergraph<V,E>, Serializable

public class SparseGraph<V,E>
extends AbstractGraph<V,E>
implements Graph<V,E>, Serializable

An implementation of Graph that is suitable for sparse graphs and permits both directed and undirected edges.

See Also:
Serialized Form

Field Summary
protected  Map<E,Pair<V>> directed_edges
           
protected static int INCIDENT
           
protected static int INCOMING
           
protected static int OUTGOING
           
protected  Map<E,Pair<V>> undirected_edges
           
protected  Map<V,Map<V,E>[]> vertex_maps
           
 
Constructor Summary
SparseGraph()
          Creates an instance.
 
Method Summary
 boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType)
          Adds edge to this graph with the specified endpoints and EdgeType.
 boolean addVertex(V vertex)
          Adds vertex to this graph.
 boolean containsEdge(E edge)
          Returns true if this graph's edge collection contains edge.
 boolean containsVertex(V vertex)
          Returns true if this graph's vertex collection contains vertex.
 E findEdge(V v1, V v2)
          Returns an edge that connects this vertex to v.
 Collection<E> findEdgeSet(V v1, V v2)
          Returns all edges that connects this vertex to v.
 EdgeType getDefaultEdgeType()
          Returns the default edge type for this graph.
 V getDest(E directed_edge)
          If directed_edge is a directed edge in this graph, returns the destination; otherwise returns null.
 int getEdgeCount()
          Returns the number of edges in this graph.
 int getEdgeCount(EdgeType edge_type)
          Returns the number of edges of type edge_type in this graph.
 Collection<E> getEdges()
          Returns a view of all edges in this graph.
 Collection<E> getEdges(EdgeType edgeType)
          Returns the collection of edges in this graph which are of type edge_type.
 EdgeType getEdgeType(E edge)
          Returns the edge type of edge in this graph.
 Pair<V> getEndpoints(E edge)
          Returns the endpoints of edge as a Pair.
static
<V,E> org.apache.commons.collections15.Factory<Graph<V,E>>
getFactory()
          Returns a Factory that creates an instance of this graph type.
 Collection<E> getIncidentEdges(V vertex)
          Returns the collection of edges in this graph which are connected to vertex.
 Collection<E> getInEdges(V vertex)
          Returns a Collection view of the incoming edges incident to vertex in this graph.
 Collection<V> getNeighbors(V vertex)
          Returns the collection of vertices which are connected to vertex via any edges in this graph.
 Collection<E> getOutEdges(V vertex)
          Returns a Collection view of the outgoing edges incident to vertex in this graph.
 Collection<V> getPredecessors(V vertex)
          Returns a Collection view of the predecessors of vertex in this graph.
 V getSource(E directed_edge)
          If directed_edge is a directed edge in this graph, returns the source; otherwise returns null.
 Collection<V> getSuccessors(V vertex)
          Returns a Collection view of the successors of vertex in this graph.
 int getVertexCount()
          Returns the number of vertices in this graph.
 Collection<V> getVertices()
          Returns a view of all vertices in this graph.
 boolean isDest(V vertex, E edge)
          Returns true if vertex is the destination of edge.
 boolean isSource(V vertex, E edge)
          Returns true if vertex is the source of edge.
 boolean removeEdge(E edge)
          Removes edge from this graph.
 boolean removeVertex(V vertex)
          Removes vertex from 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

vertex_maps

protected Map<V,Map<V,E>[]> vertex_maps

directed_edges

protected Map<E,Pair<V>> directed_edges

undirected_edges

protected Map<E,Pair<V>> undirected_edges
Constructor Detail

SparseGraph

public SparseGraph()
Creates an instance.

Method Detail

getFactory

public static <V,E> org.apache.commons.collections15.Factory<Graph<V,E>> getFactory()
Returns a Factory that creates an instance of this graph type.

Type Parameters:
V - the vertex type for the graph factory
E - the edge type for the graph factory

findEdge

public E findEdge(V v1,
                  V v2)
Description copied from interface: Hypergraph
Returns an edge that connects this vertex to v. If this edge is not uniquely defined (that is, if the graph contains more than one edge connecting v1 to v2), 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:

Note: for purposes of this method, v1 is only considered to be connected to v2 via a given directed edge e if v1 == e.getSource() && v2 == e.getDest() evaluates to true. (v1 and v2 are connected by an undirected edge u if u is incident to both v1 and v2.)

Specified by:
findEdge in interface Hypergraph<V,E>
Overrides:
findEdge in class AbstractGraph<V,E>
Returns:
an edge that connects v1 to v2, or null if no such edge exists (or either vertex is not present)
See Also:
Hypergraph.findEdgeSet(Object, Object)

findEdgeSet

public Collection<E> findEdgeSet(V v1,
                                 V v2)
Description copied from interface: Hypergraph
Returns all edges that connects this vertex to v. If this edge is not uniquely defined (that is, if the graph contains more than one edge connecting v1 to v2), any of these edges may be returned. findEdgeSet(v1, v2) may be used to return all such edges. Returns null if v2 is not connected to v1.
Returns an empty collection if either v1 or v2 are not present in this graph.

Note: for purposes of this method, v1 is only considered to be connected to v2 via a given directed edge d if v1 == d.getSource() && v2 == d.getDest() evaluates to true. (v1 and v2 are connected by an undirected edge u if u is incident to both v1 and v2.)

Specified by:
findEdgeSet in interface Hypergraph<V,E>
Overrides:
findEdgeSet in class AbstractGraph<V,E>
Returns:
a collection containing all edges that connect v1 to v2, or null if 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: AbstractGraph
Adds edge to this graph with the specified endpoints and EdgeType.

Specified by:
addEdge in class AbstractGraph<V,E>
Returns:
true iff the graph was modified as a result of this call

getInEdges

public Collection<E> getInEdges(V vertex)
Description copied from interface: Graph
Returns a Collection view of the incoming edges incident to vertex in this graph.

Specified by:
getInEdges in interface Graph<V,E>
Specified by:
getInEdges in interface Hypergraph<V,E>
Parameters:
vertex - the vertex whose incoming edges are to be returned
Returns:
a Collection view of the incoming edges incident to vertex in this graph

getOutEdges

public Collection<E> getOutEdges(V vertex)
Description copied from interface: Graph
Returns a Collection view of the outgoing edges incident to vertex in this graph.

Specified by:
getOutEdges in interface Graph<V,E>
Specified by:
getOutEdges in interface Hypergraph<V,E>
Parameters:
vertex - the vertex whose outgoing edges are to be returned
Returns:
a Collection view of the outgoing edges incident to vertex in this graph

getPredecessors

public Collection<V> getPredecessors(V vertex)
Description copied from interface: Graph
Returns a Collection view of the predecessors of vertex in this graph. A predecessor of vertex is defined as a vertex v which is connected to vertex by an edge e, where e is an outgoing edge of v and an incoming edge of vertex.

Specified by:
getPredecessors in interface Graph<V,E>
Specified by:
getPredecessors in interface Hypergraph<V,E>
Parameters:
vertex - the vertex whose predecessors are to be returned
Returns:
a Collection view of the predecessors of vertex in this graph

getSuccessors

public Collection<V> getSuccessors(V vertex)
Description copied from interface: Graph
Returns a Collection view of the successors of vertex in this graph. A successor of vertex is defined as a vertex v which is connected to vertex by an edge e, where e is an incoming edge of v and an outgoing edge of vertex.

Specified by:
getSuccessors in interface Graph<V,E>
Specified by:
getSuccessors in interface Hypergraph<V,E>
Parameters:
vertex - the vertex whose predecessors are to be returned
Returns:
a Collection view of the successors of vertex in this graph

getEdges

public Collection<E> getEdges(EdgeType edgeType)
Description copied from interface: Hypergraph
Returns the collection of edges in this graph which are of type edge_type.

Specified by:
getEdges in interface Hypergraph<V,E>
Parameters:
edgeType - the type of edges to be returned
Returns:
the collection of edges which are of type edge_type, or null if the graph does not accept edges of this type
See Also:
EdgeType

getEndpoints

public Pair<V> getEndpoints(E edge)
Description copied from interface: Graph
Returns the endpoints of edge as a Pair.

Specified by:
getEndpoints in interface Graph<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: Hypergraph
Returns the edge type of edge in this graph.

Specified by:
getEdgeType in interface Hypergraph<V,E>
Returns:
the EdgeType of edge, or null if edge has no defined type

getSource

public V getSource(E directed_edge)
Description copied from interface: Graph
If directed_edge is a directed edge in this graph, returns the source; otherwise returns null. The source of a directed edge d is defined to be the vertex for which d is an outgoing edge. directed_edge is guaranteed to be a directed edge if its EdgeType is DIRECTED.

Specified by:
getSource in interface Graph<V,E>
Specified by:
getSource in interface Hypergraph<V,E>
Returns:
the source of directed_edge if it is a directed edge in this graph, or null otherwise

getDest

public V getDest(E directed_edge)
Description copied from interface: Graph
If directed_edge is a directed edge in this graph, returns the destination; otherwise returns null. The destination of a directed edge d is defined to be the vertex incident to d for which d is an incoming edge. directed_edge is guaranteed to be a directed edge if its EdgeType is DIRECTED.

Specified by:
getDest in interface Graph<V,E>
Specified by:
getDest in interface Hypergraph<V,E>
Returns:
the destination of directed_edge if it is a directed edge in this graph, or null otherwise

isSource

public boolean isSource(V vertex,
                        E edge)
Description copied from interface: Graph
Returns true if vertex is the source of edge. Equivalent to getSource(edge).equals(vertex).

Specified by:
isSource in interface Graph<V,E>
Parameters:
vertex - the vertex to be queried
edge - the edge to be queried
Returns:
true iff vertex is the source of edge

isDest

public boolean isDest(V vertex,
                      E edge)
Description copied from interface: Graph
Returns true if vertex is the destination of edge. Equivalent to getDest(edge).equals(vertex).

Specified by:
isDest in interface Graph<V,E>
Parameters:
vertex - the vertex to be queried
edge - the edge to be queried
Returns:
true iff vertex is the destination of edge

getEdges

public Collection<E> getEdges()
Description copied from interface: Hypergraph
Returns a view of all edges in this graph. In general, this obeys the Collection contract, and therefore makes no guarantees about the ordering of the vertices within the set.

Specified by:
getEdges in interface Hypergraph<V,E>
Returns:
a Collection view of all edges in this graph

getVertices

public Collection<V> getVertices()
Description copied from interface: Hypergraph
Returns a view of all vertices in this graph. In general, this obeys the Collection contract, and therefore makes no guarantees about the ordering of the vertices within the set.

Specified by:
getVertices in interface Hypergraph<V,E>
Returns:
a Collection view of all vertices in this graph

containsVertex

public boolean containsVertex(V vertex)
Description copied from interface: Hypergraph
Returns true if this graph's vertex collection contains vertex. Equivalent to getVertices().contains(vertex).

Specified by:
containsVertex in interface Hypergraph<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: Hypergraph
Returns true if this graph's edge collection contains edge. Equivalent to getEdges().contains(edge).

Specified by:
containsEdge in interface Hypergraph<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: Hypergraph
Returns the number of edges in this graph.

Specified by:
getEdgeCount in interface Hypergraph<V,E>
Returns:
the number of edges in this graph

getVertexCount

public int getVertexCount()
Description copied from interface: Hypergraph
Returns the number of vertices in this graph.

Specified by:
getVertexCount in interface Hypergraph<V,E>
Returns:
the number of vertices in this graph

getNeighbors

public Collection<V> getNeighbors(V vertex)
Description copied from interface: Hypergraph
Returns the collection of vertices which are connected to vertex via any edges in this graph. If vertex is connected to itself with a self-loop, then it will be included in the collection returned.

Specified by:
getNeighbors in interface Hypergraph<V,E>
Parameters:
vertex - the vertex whose neighbors are to be returned
Returns:
the collection of vertices which are connected to vertex, or null if vertex is not present

getIncidentEdges

public Collection<E> getIncidentEdges(V vertex)
Description copied from interface: Hypergraph
Returns the collection of edges in this graph which are connected to vertex.

Specified by:
getIncidentEdges in interface Hypergraph<V,E>
Parameters:
vertex - the vertex whose incident edges are to be returned
Returns:
the collection of edges which are connected to vertex, or null if vertex is not present

addVertex

public boolean addVertex(V vertex)
Description copied from interface: Hypergraph
Adds vertex to this graph. Fails if vertex is null or already in the graph.

Specified by:
addVertex in interface Hypergraph<V,E>
Parameters:
vertex - the vertex to add
Returns:
true if the add is successful, and false otherwise

removeVertex

public boolean removeVertex(V vertex)
Description copied from interface: Hypergraph
Removes vertex from this graph. As a side effect, removes any edges e incident to vertex if the removal of vertex would cause e to 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:

Specified by:
removeVertex in interface Hypergraph<V,E>
Parameters:
vertex - the vertex to remove
Returns:
true if the removal is successful, false otherwise

removeEdge

public boolean removeEdge(E edge)
Description copied from interface: Hypergraph
Removes edge from this graph. Fails if edge is null, or is otherwise not an element of this graph.

Specified by:
removeEdge in interface Hypergraph<V,E>
Parameters:
edge - the edge to remove
Returns:
true if the removal is successful, false otherwise

getEdgeCount

public int getEdgeCount(EdgeType edge_type)
Description copied from interface: Hypergraph
Returns the number of edges of type edge_type in this graph.

Specified by:
getEdgeCount in interface Hypergraph<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_type in this graph

getDefaultEdgeType

public EdgeType getDefaultEdgeType()
Description copied from interface: Hypergraph
Returns the default edge type for this graph.

Specified by:
getDefaultEdgeType in interface Hypergraph<V,E>
Returns:
the default edge type for this graph


Copyright © 2009. All Rights Reserved.