Class AbstractGraph

  • All Implemented Interfaces:
    Graph
    Direct Known Subclasses:
    AbstractBaseGraph, GraphDelegator, Subgraph

    public abstract class AbstractGraph
    extends java.lang.Object
    implements Graph
    A skeletal implementation of the Graph interface, to minimize the effort required to implement graph interfaces. This implementation is applicable to both: directed graphs and undirected graphs.
    Author:
    Barak Naveh
    See Also:
    Graph, DirectedGraph, UndirectedGraph
    • Constructor Summary

      Constructors 
      Constructor Description
      AbstractGraph()
      Construct a new empty graph object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean addAllEdges​(java.util.Collection edges)
      Adds all of the specified edges to this graph.
      boolean addAllVertices​(java.util.Collection vertices)
      Adds all of the specified vertices to this graph.
      protected boolean assertVertexExist​(java.lang.Object v)
      Ensures that the specified vertex exists in this graph, or else throws exception.
      boolean containsEdge​(java.lang.Object sourceVertex, java.lang.Object targetVertex)
      Returns true if and only if this graph contains an edge going from the source vertex to the target vertex.
      java.util.List removeAllEdges​(java.lang.Object sourceVertex, java.lang.Object targetVertex)
      Removes all the edges going from the specified source vertex to the specified target vertex, and returns a list of all removed edges.
      boolean removeAllEdges​(java.util.Collection edges)
      Removes all the edges in this graph that are also contained in the specified edge collection.
      protected boolean removeAllEdges​(Edge[] edges)
      Removes all the edges in this graph that are also contained in the specified edge array.
      boolean removeAllVertices​(java.util.Collection vertices)
      Removes all the vertices in this graph that are also contained in the specified vertex collection.
      java.lang.String toString()
      Returns a string of the parenthesized pair (V, E) representing this G=(V,E) graph.
      protected java.lang.String toStringFromSets​(java.util.Collection vertexSet, java.util.Collection edgeSet)
      Helper for subclass implementations of toString( ).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • AbstractGraph

        public AbstractGraph()
        Construct a new empty graph object.
    • Method Detail

      • addAllEdges

        public boolean addAllEdges​(java.util.Collection edges)
        Description copied from interface: Graph
        Adds all of the specified edges to this graph. The behavior of this operation is undefined if the specified vertex collection is modified while the operation is in progress. This method will invoke the Graph.addEdge(Edge) method.
        Specified by:
        addAllEdges in interface Graph
        Parameters:
        edges - the edges to be added to this graph.
        Returns:
        true if this graph changed as a result of the call
        See Also:
        Graph.addAllEdges(Collection)
      • addAllVertices

        public boolean addAllVertices​(java.util.Collection vertices)
        Description copied from interface: Graph
        Adds all of the specified vertices to this graph. The behavior of this operation is undefined if the specified vertex collection is modified while the operation is in progress. This method will invoke the Graph.addVertex(Object) method.
        Specified by:
        addAllVertices in interface Graph
        Parameters:
        vertices - the vertices to be added to this graph.
        Returns:
        true if this graph changed as a result of the call
        See Also:
        Graph.addAllVertices(Collection)
      • containsEdge

        public boolean containsEdge​(java.lang.Object sourceVertex,
                                    java.lang.Object targetVertex)
        Description copied from interface: Graph
        Returns true if and only if this graph contains an edge going from the source vertex to the target vertex. In undirected graphs the same result is obtained when source and target are inverted. If any of the specified vertices does not exist in the graph, or if is null, returns false.
        Specified by:
        containsEdge in interface Graph
        Parameters:
        sourceVertex - source vertex of the edge.
        targetVertex - target vertex of the edge.
        Returns:
        true if this graph contains the specified edge.
        See Also:
        Graph.containsEdge(Object, Object)
      • removeAllEdges

        public boolean removeAllEdges​(java.util.Collection edges)
        Description copied from interface: Graph
        Removes all the edges in this graph that are also contained in the specified edge collection. After this call returns, this graph will contain no edges in common with the specified edges. This method will invoke the Graph.removeEdge(Edge) method.
        Specified by:
        removeAllEdges in interface Graph
        Parameters:
        edges - edges to be removed from this graph.
        Returns:
        true if this graph changed as a result of the call
        See Also:
        Graph.removeAllEdges(Collection)
      • removeAllEdges

        public java.util.List removeAllEdges​(java.lang.Object sourceVertex,
                                             java.lang.Object targetVertex)
        Description copied from interface: Graph
        Removes all the edges going from the specified source vertex to the specified target vertex, and returns a list of all removed edges. Returns null if any of the specified vertices does exist in the graph. If both vertices exist but no edge found, returns an empty list. This method will either invoke the Graph.removeEdge(Edge) method, or the Graph.removeEdge(Object, Object) method.
        Specified by:
        removeAllEdges in interface Graph
        Parameters:
        sourceVertex - source vertex of the edge.
        targetVertex - target vertex of the edge.
        Returns:
        The removed edge, or null if no edge removed.
        See Also:
        Graph.removeAllEdges(Object, Object)
      • removeAllVertices

        public boolean removeAllVertices​(java.util.Collection vertices)
        Description copied from interface: Graph
        Removes all the vertices in this graph that are also contained in the specified vertex collection. After this call returns, this graph will contain no vertices in common with the specified vertices. This method will invoke the Graph.removeVertex(Object) method.
        Specified by:
        removeAllVertices in interface Graph
        Parameters:
        vertices - vertices to be removed from this graph.
        Returns:
        true if this graph changed as a result of the call
        See Also:
        Graph.removeAllVertices(Collection)
      • toString

        public java.lang.String toString()
        Returns a string of the parenthesized pair (V, E) representing this G=(V,E) graph. 'V' is the string representation of the vertex set, and 'E' is the string representation of the edge set.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of this graph.
      • assertVertexExist

        protected boolean assertVertexExist​(java.lang.Object v)
        Ensures that the specified vertex exists in this graph, or else throws exception.
        Parameters:
        v - vertex
        Returns:
        true if this assertion holds.
        Throws:
        java.lang.NullPointerException - if specified vertex is null.
        java.lang.IllegalArgumentException - if specified vertex does not exist in this graph.
      • removeAllEdges

        protected boolean removeAllEdges​(Edge[] edges)
        Removes all the edges in this graph that are also contained in the specified edge array. After this call returns, this graph will contain no edges in common with the specified edges. This method will invoke the Graph.removeEdge(Edge) method.
        Parameters:
        edges - edges to be removed from this graph.
        Returns:
        true if this graph changed as a result of the call.
        See Also:
        Graph.removeEdge(Edge), Graph.containsEdge(Edge)
      • toStringFromSets

        protected java.lang.String toStringFromSets​(java.util.Collection vertexSet,
                                                    java.util.Collection edgeSet)
        Helper for subclass implementations of toString( ).
        Parameters:
        vertexSet - the vertex set V to be printed
        edgeSet - the edge set E to be printed
        Returns:
        a string representation of (V,E)