Interface Graph<V>

  • Type Parameters:
    V - the vertex type
    All Superinterfaces:
    Iterable<V>
    All Known Implementing Classes:
    DefaultGraphImpl

    public interface Graph<V>
    extends Iterable<V>

    Interface defining a graph data structure.

    Author:
    David B. Bracewell
    • Method Detail

      • create

        static <V> Graph<V> create​(EdgeFactory<V> edgeFactory)
        Creates a graph with vertices V and edges defined by the given EdgeFactory.
        Type Parameters:
        V - the vertex type parameter
        Parameters:
        edgeFactory - the edge factory to use for creating and validating edges
        Returns:
        the graph
      • directed

        static <V> Graph<V> directed()
        Creates a graph with vertices V and edges defined by DirectedEdgeFactory.
        Type Parameters:
        V - the vertex type parameter
        Returns:
        the graph
      • undirected

        static <V> Graph<V> undirected()
        Creates a graph with vertices V and edges defined by UndirectedEdgeFactory.
        Type Parameters:
        V - the vertex type parameter
        Returns:
        the graph
      • addEdge

        void addEdge​(Edge<V> edge)
        Adds an edge to the graph.
        Parameters:
        edge - the edge
      • addEdge

        Edge<V> addEdge​(V fromVertex,
                        V toVertex)
        Adds an edge to the graph
        Parameters:
        fromVertex - The first (from) vertex
        toVertex - The second (to) vertex
        Returns:
        The edge that was created
      • addEdge

        Edge<V> addEdge​(V fromVertex,
                        V toVertex,
                        double weight)
        Adds an edge to the graph
        Parameters:
        fromVertex - The first (from) vertex
        toVertex - The second (to) vertex
        weight - The weight of the edge
        Returns:
        The edge that was created
      • addEdges

        default void addEdges​(Collection<? extends Edge<V>> edges)
        Adds a collection of edges to the graph.
        Parameters:
        edges - the edges
      • addVertex

        boolean addVertex​(V vertex)
        Adds a vertex to the graph
        Parameters:
        vertex - The vertex
        Returns:
        True if the vertex was added, False if not
      • addVertices

        void addVertices​(Collection<V> vertices)
        Adds all vertices in a collection to the graph
        Parameters:
        vertices - The vertices to add
      • containsEdge

        boolean containsEdge​(V fromVertex,
                             V toVertex)
        Checks if an edge in the graph
        Parameters:
        fromVertex - The first (from) vertex
        toVertex - The second (to) vertex
        Returns:
        True if the edge is in the graph, false if not
      • containsEdge

        default boolean containsEdge​(Edge<V> edge)
        Checks if an edge in the graph
        Parameters:
        edge - The edge to check
        Returns:
        True if the edge is in the graph, false if not
      • containsVertex

        boolean containsVertex​(V vertex)
        Checks if a vertex in the graph
        Parameters:
        vertex - The vertex
        Returns:
        True if the vertex is in the graph, false if not
      • degree

        int degree​(V vertex)
        The number of neighbors
        Parameters:
        vertex - The vertex
        Returns:
        The degree
      • edges

        Set<? extends Edge<V>> edges()
        Edges set.
        Returns:
        The set of edges in the graph
      • getEdge

        Edge<V> getEdge​(V v1,
                        V v2)
        Gets the edge if one, between the two given vertices
        Parameters:
        v1 - vertex 1
        v2 - vertex 2
        Returns:
        The edge if one, null otherwise
      • getEdgeFactory

        EdgeFactory<V> getEdgeFactory()
        Gets the edge factory used in this grapy
        Returns:
        The edge factory
      • getEdges

        default Set<? extends Edge<V>> getEdges​(V vertex)
        Gets all edges incident to the given vertex.
        Parameters:
        vertex - The vertex
        Returns:
        The set of incident edges
      • getInEdges

        Set<? extends Edge<V>> getInEdges​(V vertex)
        Gets the edges coming in to the given vertex (i.e. in-links)
        Parameters:
        vertex - The vertex
        Returns:
        The set of incoming edges
      • getNeighbors

        default Set<V> getNeighbors​(V vertex)
        Gets the set of vertices that share an edge with the given vertex
        Parameters:
        vertex - The vertex
        Returns:
        The set of vertices which share an edge with the given vertex.
      • getOutEdges

        Set<? extends Edge<V>> getOutEdges​(V vertex)
        Gets the edges coming out out of the given vertex (i.e. out-links)
        Parameters:
        vertex - The vertex
        Returns:
        The set of outgoing edges
      • getPredecessors

        Set<V> getPredecessors​(V vertex)
        Gets the neighbors associated with the incoming edges for the given vertex.
        Parameters:
        vertex - The vertex
        Returns:
        The set of vertices which contain an outgoing edge to the given vertex.
      • getPredecessorsWeights

        default Counter<V> getPredecessorsWeights​(V vertex)
        Gets the weights associated with the edges to the predecessors of the given vertex.
        Parameters:
        vertex - The vertex
        Returns:
        The weights associated with the edges to the predecessors
      • getSuccessorWeights

        default Counter<V> getSuccessorWeights​(V vertex)
        Gets the weights associated with the edges to the successors of the given vertex.
        Parameters:
        vertex - The vertex
        Returns:
        The weights associated with the edges to the successors
      • getSuccessors

        Set<V> getSuccessors​(V vertex)
        Gets the neighbors associated with the outgoing edges for the given vertex.
        Parameters:
        vertex - The vertex
        Returns:
        The set of vertices which contain an incoming edge from the given vertex.
      • getWeight

        default double getWeight​(V v1,
                                 V v2)
        Gets the weight of the edge if one, between the two given vertices
        Parameters:
        v1 - vertex 1
        v2 - vertex 2
        Returns:
        The weight if one, 0 otherwise
      • getWeights

        default Counter<V> getWeights​(V vertex)
        Gets the weights associated with the edges of the given vertex.
        Parameters:
        vertex - The vertex
        Returns:
        The weights associated with the edges
      • inDegree

        int inDegree​(V vertex)
        The number of predecessors
        Parameters:
        vertex - The vertex
        Returns:
        The in degree
      • isDirected

        boolean isDirected()
        Is directed.
        Returns:
        True if the graph is directed, false if it is undirected
      • isEmpty

        boolean isEmpty()
        Determines if the graph is empty (no vertices no edges)
        Returns:
        True if the graph is empty
      • merge

        default void merge​(Graph<V> other)
        Merges another graph into this one ignoring any duplicate edges
        Parameters:
        other - The graph to merge
      • merge

        default void merge​(Graph<V> other,
                           EdgeMergeFunction<V> mergeFunction)
        Merges another graph into this one combining edges using the supplied merge function
        Parameters:
        other - The graph to merge
        mergeFunction - The function to use to merge duplicate edges
      • numberOfEdges

        int numberOfEdges()
        Number of edges.
        Returns:
        Number of edges in the graph
      • numberOfVertices

        int numberOfVertices()
        Number of vertices.
        Returns:
        Number of vertices in the graph
      • outDegree

        int outDegree​(V vertex)
        The number of successors
        Parameters:
        vertex - The vertex
        Returns:
        The out degree
      • parallelstream

        default Stream<V> parallelstream()
        Returns a stream of the vertices in this graph
        Returns:
        A stream of the vertices in this graph
      • removeEdge

        Edge<V> removeEdge​(V fromVertex,
                           V toVertex)
        Removes an edge from the graph
        Parameters:
        fromVertex - The first (from) vertex
        toVertex - The second (to) vertex
        Returns:
        The edge that was removed or null if there was no edge
      • removeEdge

        boolean removeEdge​(Edge<V> edge)
        Removes an edge from the graph
        Parameters:
        edge - The edge to remove
        Returns:
        True if the edge was removed, false if not
      • removeVertex

        boolean removeVertex​(V vertex)
        Removes a vertex from the graph
        Parameters:
        vertex - The vertex to remove
        Returns:
        True if the vertex was removed, false if not
      • stream

        default Stream<V> stream()
        Returns a stream of the vertices in this graph
        Returns:
        A stream of the vertices in this graph
      • vertices

        Set<V> vertices()
        Vertices set.
        Returns:
        The set of vertices in the graph