Class 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 of Graph that is suitable for sparse graphs and permits both directed and undirected edges.
    See Also:
    Serialized Form
    • Field Detail

      • vertex_maps

        protected java.util.Map<V,​java.util.Map<V,​E>[]> vertex_maps
      • directed_edges

        protected java.util.Map<E,​Pair<V>> directed_edges
      • undirected_edges

        protected java.util.Map<E,​Pair<V>> undirected_edges
    • Constructor Detail

      • SparseGraph

        public SparseGraph()
        Creates an instance.
    • 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 Supplier
        E - the edge type for the graph Supplier
        Returns:
        a Supplier that creates an instance of this graph type.
      • 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:
        • v2 is not connected to v1
        • 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 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>
        Parameters:
        v1 - the first endpoint of the returned edge
        v2 - the second endpoint of the returned edge
        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 java.util.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>
        Parameters:
        v1 - the first endpoint of the returned edge set
        v2 - the second endpoint of the returned edge set
        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>
        Parameters:
        edge - the edge to be added
        endpoints - the endpoints to be connected to this edge
        edgeType - 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: 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 java.util.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 java.util.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 java.util.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 java.util.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>
        Parameters:
        edge - the edge whose type is to be returned
        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>
        Parameters:
        directed_edge - the edge whose source is to be returned
        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>
        Parameters:
        directed_edge - the edge whose destination is to be returned
        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 java.util.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 java.util.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 java.util.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 java.util.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:

        • vertex is not an element of this graph
        • vertex is null
        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