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

java.lang.Object
  extended by edu.uci.ics.jung.graph.AbstractGraph<V,E>
      extended by edu.uci.ics.jung.graph.SparseMultigraph<V,E>
All Implemented Interfaces:
Graph<V,E>, Hypergraph<V,E>, MultiGraph<V,E>, Serializable
Direct Known Subclasses:
OrderedSparseMultigraph

public class SparseMultigraph<V,E>
extends AbstractGraph<V,E>
implements MultiGraph<V,E>, Serializable

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

See Also:
Serialized Form

Field Summary
protected  Set<E> directedEdges
           
protected  Map<E,Pair<V>> edges
           
protected  Map<V,Pair<Set<E>>> vertices
           
 
Constructor Summary
SparseMultigraph()
          Creates a new 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.
 EdgeType getDefaultEdgeType()
          Returns the default edge type for this graph.
 V getDest(E 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.
protected  Collection<E> getIncoming_internal(V 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.
protected  Collection<E> getOutgoing_internal(V vertex)
           
 Collection<V> getPredecessors(V vertex)
          Returns a Collection view of the predecessors of vertex in this graph.
 V getSource(E 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, findEdgeSet, 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
 

Field Detail

vertices

protected Map<V,Pair<Set<E>>> vertices

edges

protected Map<E,Pair<V>> edges

directedEdges

protected Set<E> directedEdges
Constructor Detail

SparseMultigraph

public SparseMultigraph()
Creates a new 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

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

getIncoming_internal

protected Collection<E> getIncoming_internal(V vertex)

getOutgoing_internal

protected Collection<E> getOutgoing_internal(V vertex)

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

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

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

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

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

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)

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

getSource

public V getSource(E 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 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

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

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

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

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.