Class SetHypergraph<V,​H>

  • All Implemented Interfaces:
    Hypergraph<V,​H>, MultiGraph<V,​H>, java.io.Serializable

    public class SetHypergraph<V,​H>
    extends java.lang.Object
    implements Hypergraph<V,​H>, MultiGraph<V,​H>, java.io.Serializable
    An implementation of Hypergraph that is suitable for sparse graphs and permits parallel edges.
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Map<H,​java.util.Set<V>> edges  
      protected java.util.Map<V,​java.util.Set<H>> vertices  
    • Constructor Summary

      Constructors 
      Constructor Description
      SetHypergraph()
      Creates a SetHypergraph and initializes the internal data structures.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean addEdge​(H hyperedge, java.util.Collection<? extends V> to_attach)
      Adds hyperedge to this graph and connects them to the vertex collection to_attach.
      boolean addEdge​(H hyperedge, java.util.Collection<? extends V> to_attach, EdgeType edge_type)
      Adds edge to this graph with type edge_type.
      boolean addVertex​(V vertex)
      Adds vertex to this graph.
      boolean containsEdge​(H 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.
      int degree​(V vertex)
      Returns the number of edges incident to vertex.
      H findEdge​(V v1, V v2)
      Returns an edge that connects this vertex to v.
      java.util.Collection<H> 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​(H 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.
      java.util.Collection<H> getEdges()
      Returns a view of all edges in this graph.
      java.util.Collection<H> getEdges​(EdgeType edge_type)
      Returns the collection of edges in this graph which are of type edge_type.
      EdgeType getEdgeType​(H edge)
      Returns the edge type of edge in this graph.
      static <V,​H>
      com.google.common.base.Supplier<Hypergraph<V,​H>>
      getFactory()
      Returns a Factory which creates instances of this class.
      int getIncidentCount​(H edge)
      Returns the number of vertices that are incident to edge.
      java.util.Collection<H> getIncidentEdges​(V vertex)
      Returns the collection of edges in this graph which are connected to vertex.
      java.util.Collection<V> getIncidentVertices​(H edge)
      Returns the collection of vertices in this graph which are connected to edge.
      java.util.Collection<H> getInEdges​(V vertex)
      Returns a Collection view of the incoming edges incident to vertex in this graph.
      int getNeighborCount​(V vertex)
      Returns the number of vertices that are adjacent to vertex (that is, the number of vertices that are incident to edges in vertex's incident edge set).
      java.util.Collection<V> getNeighbors​(V vertex)
      Returns the collection of vertices which are connected to vertex via any edges in this graph.
      java.util.Collection<H> getOutEdges​(V vertex)
      Returns a Collection view of the outgoing edges incident to vertex in this graph.
      java.util.Collection<V> getPredecessors​(V vertex)
      Returns a Collection view of the predecessors of vertex in this graph.
      V getSource​(H directed_edge)
      If directed_edge is a directed edge in this graph, returns the source; otherwise returns null.
      java.util.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.
      java.util.Collection<V> getVertices()
      Returns a view of all vertices in this graph.
      int inDegree​(V vertex)
      Returns the number of incoming edges incident to vertex.
      boolean isIncident​(V vertex, H edge)
      Returns true if vertex and edge are incident to each other.
      boolean isNeighbor​(V v1, V v2)
      Returns true if v1 and v2 share an incident edge.
      int outDegree​(V vertex)
      Returns the number of outgoing edges incident to vertex.
      boolean removeEdge​(H hyperedge)
      Removes edge from this graph.
      boolean removeVertex​(V vertex)
      Removes vertex from this graph.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • vertices

        protected java.util.Map<V,​java.util.Set<H>> vertices
      • edges

        protected java.util.Map<H,​java.util.Set<V>> edges
    • Constructor Detail

      • SetHypergraph

        public SetHypergraph()
        Creates a SetHypergraph and initializes the internal data structures.
    • Method Detail

      • getFactory

        public static <V,​H> com.google.common.base.Supplier<Hypergraph<V,​H>> getFactory()
        Returns a Factory which creates instances of this class.
        Type Parameters:
        V - vertex type of the hypergraph to be created
        H - edge type of the hypergraph to be created
        Returns:
        a Factory which creates instances of this class
      • addEdge

        public boolean addEdge​(H hyperedge,
                               java.util.Collection<? extends V> to_attach)
        Adds hyperedge to this graph and connects them to the vertex collection to_attach. Any vertices in to_attach that appear more than once will only appear once in the incident vertex collection for hyperedge, that is, duplicates will be ignored.
        Specified by:
        addEdge in interface Hypergraph<V,​H>
        Parameters:
        hyperedge - the edge to add
        to_attach - the vertices to which the edge will be connected
        Returns:
        true if the add is successful, and false otherwise
        See Also:
        Hypergraph.addEdge(Object, Collection)
      • addEdge

        public boolean addEdge​(H hyperedge,
                               java.util.Collection<? extends V> to_attach,
                               EdgeType edge_type)
        Description copied from interface: Hypergraph
        Adds edge to this graph with type edge_type. Fails under the following circumstances:
        • edge is already an element of the graph
        • either edge or vertices is null
        • vertices has the wrong number of vertices for the graph type
        • vertices are already connected by another edge in this graph, and this graph does not accept parallel edges
        • edge_type is not legal for this graph
        Specified by:
        addEdge in interface Hypergraph<V,​H>
        Parameters:
        hyperedge - edge to add to this graph
        to_attach - vertices which are to be connected by this edge
        edge_type - type of edge to add
        Returns:
        true if the add is successful, and false otherwise
        See Also:
        Hypergraph.addEdge(Object, Collection, EdgeType)
      • getEdgeType

        public EdgeType getEdgeType​(H edge)
        Description copied from interface: Hypergraph
        Returns the edge type of edge in this graph.
        Specified by:
        getEdgeType in interface Hypergraph<V,​H>
        Parameters:
        edge - the edge whose type is to be returned
        Returns:
        the EdgeType of edge, or null if edge has no defined type
        See Also:
        Hypergraph.getEdgeType(Object)
      • 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,​H>
        Parameters:
        vertex - the vertex whose presence is being queried
        Returns:
        true iff this graph contains a vertex vertex
      • containsEdge

        public boolean containsEdge​(H 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,​H>
        Parameters:
        edge - the edge whose presence is being queried
        Returns:
        true iff this graph contains an edge edge
      • getEdges

        public java.util.Collection<H> 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,​H>
        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,​H>
        Returns:
        a Collection view of all vertices in this graph
      • getEdgeCount

        public int getEdgeCount()
        Description copied from interface: Hypergraph
        Returns the number of edges in this graph.
        Specified by:
        getEdgeCount in interface Hypergraph<V,​H>
        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,​H>
        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,​H>
        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<H> 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,​H>
        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
      • getIncidentVertices

        public java.util.Collection<V> getIncidentVertices​(H edge)
        Description copied from interface: Hypergraph
        Returns the collection of vertices in this graph which are connected to edge. Note that for some graph types there are guarantees about the size of this collection (i.e., some graphs contain edges that have exactly two endpoints, which may or may not be distinct). Implementations for those graph types may provide alternate methods that provide more convenient access to the vertices.
        Specified by:
        getIncidentVertices in interface Hypergraph<V,​H>
        Parameters:
        edge - the edge whose incident vertices are to be returned
        Returns:
        the collection of vertices which are connected to edge, or null if edge is not present
      • findEdge

        public H 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,​H>
        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<H> 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,​H>
        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)
      • 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,​H>
        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,​H>
        Parameters:
        vertex - the vertex to remove
        Returns:
        true if the removal is successful, false otherwise
      • removeEdge

        public boolean removeEdge​(H hyperedge)
        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,​H>
        Parameters:
        hyperedge - the edge to remove
        Returns:
        true if the removal is successful, false otherwise
      • isNeighbor

        public boolean isNeighbor​(V v1,
                                  V v2)
        Description copied from interface: Hypergraph
        Returns true if v1 and v2 share an incident edge. Equivalent to getNeighbors(v1).contains(v2).
        Specified by:
        isNeighbor in interface Hypergraph<V,​H>
        Parameters:
        v1 - the first vertex to test
        v2 - the second vertex to test
        Returns:
        true if v1 and v2 share an incident edge
      • isIncident

        public boolean isIncident​(V vertex,
                                  H edge)
        Description copied from interface: Hypergraph
        Returns true if vertex and edge are incident to each other. Equivalent to getIncidentEdges(vertex).contains(edge) and to getIncidentVertices(edge).contains(vertex).
        Specified by:
        isIncident in interface Hypergraph<V,​H>
        Parameters:
        vertex - vertex to test
        edge - edge to test
        Returns:
        true if vertex and edge are incident to each other
      • degree

        public int degree​(V vertex)
        Description copied from interface: Hypergraph
        Returns the number of edges incident to vertex. Special cases of interest:
        • Incident self-loops are counted once.
        • If there is only one edge that connects this vertex to each of its neighbors (and vice versa), then the value returned will also be equal to the number of neighbors that this vertex has (that is, the output of getNeighborCount).
        • If the graph is directed, then the value returned will be the sum of this vertex's indegree (the number of edges whose destination is this vertex) and its outdegree (the number of edges whose source is this vertex), minus the number of incident self-loops (to avoid double-counting).

        Equivalent to getIncidentEdges(vertex).size().

        Specified by:
        degree in interface Hypergraph<V,​H>
        Parameters:
        vertex - the vertex whose degree is to be returned
        Returns:
        the degree of this node
        See Also:
        Hypergraph.getNeighborCount(Object)
      • getNeighborCount

        public int getNeighborCount​(V vertex)
        Description copied from interface: Hypergraph
        Returns the number of vertices that are adjacent to vertex (that is, the number of vertices that are incident to edges in vertex's incident edge set).

        Equivalent to getNeighbors(vertex).size().

        Specified by:
        getNeighborCount in interface Hypergraph<V,​H>
        Parameters:
        vertex - the vertex whose neighbor count is to be returned
        Returns:
        the number of neighboring vertices
      • getIncidentCount

        public int getIncidentCount​(H edge)
        Description copied from interface: Hypergraph
        Returns the number of vertices that are incident to edge. For hyperedges, this can be any nonnegative integer; for edges this must be 2 (or 1 if self-loops are permitted).

        Equivalent to getIncidentVertices(edge).size().

        Specified by:
        getIncidentCount in interface Hypergraph<V,​H>
        Parameters:
        edge - the edge whose incident vertex count is to be returned
        Returns:
        the number of vertices that are incident to edge.
      • 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,​H>
        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
      • getEdges

        public java.util.Collection<H> getEdges​(EdgeType edge_type)
        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,​H>
        Parameters:
        edge_type - 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
      • getDefaultEdgeType

        public EdgeType getDefaultEdgeType()
        Description copied from interface: Hypergraph
        Returns the default edge type for this graph.
        Specified by:
        getDefaultEdgeType in interface Hypergraph<V,​H>
        Returns:
        the default edge type for this graph
      • getInEdges

        public java.util.Collection<H> getInEdges​(V vertex)
        Description copied from interface: Hypergraph
        Returns a Collection view of the incoming edges incident to vertex in this graph.
        Specified by:
        getInEdges in interface Hypergraph<V,​H>
        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<H> getOutEdges​(V vertex)
        Description copied from interface: Hypergraph
        Returns a Collection view of the outgoing edges incident to vertex in this graph.
        Specified by:
        getOutEdges in interface Hypergraph<V,​H>
        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
      • inDegree

        public int inDegree​(V vertex)
        Description copied from interface: Hypergraph
        Returns the number of incoming edges incident to vertex. Equivalent to getInEdges(vertex).size().
        Specified by:
        inDegree in interface Hypergraph<V,​H>
        Parameters:
        vertex - the vertex whose indegree is to be calculated
        Returns:
        the number of incoming edges incident to vertex
      • outDegree

        public int outDegree​(V vertex)
        Description copied from interface: Hypergraph
        Returns the number of outgoing edges incident to vertex. Equivalent to getOutEdges(vertex).size().
        Specified by:
        outDegree in interface Hypergraph<V,​H>
        Parameters:
        vertex - the vertex whose outdegree is to be calculated
        Returns:
        the number of outgoing edges incident to vertex
      • getDest

        public V getDest​(H directed_edge)
        Description copied from interface: Hypergraph
        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 Hypergraph<V,​H>
        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
      • getSource

        public V getSource​(H directed_edge)
        Description copied from interface: Hypergraph
        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 Hypergraph<V,​H>
        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
      • getPredecessors

        public java.util.Collection<V> getPredecessors​(V vertex)
        Description copied from interface: Hypergraph
        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 Hypergraph<V,​H>
        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: Hypergraph
        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 Hypergraph<V,​H>
        Parameters:
        vertex - the vertex whose predecessors are to be returned
        Returns:
        a Collection view of the successors of vertex in this graph