Interface Graph<T>

  • Type Parameters:
    T - Node type.

    public interface Graph<T>
    Graph.
    • Method Detail

      • directed

        @Contract(value="-> new",
                  pure=true)
        @NotNull
        static <T> @NotNull Graph.Builder<T> directed()
        Create a new directed graph builder.
        Type Parameters:
        T - Graph node type.
        Returns:
        new builder.
      • nodes

        @Contract(value="-> _",
                  pure=true)
        @NotNull
        @NotNull Collection<T> nodes()
        Returns all nodes in this graph.
        Returns:
        graph nodes.
      • edges

        @Contract(value="-> _",
                  pure=true)
        @NotNull
        @NotNull Collection<Edge<T>> edges()
        Returns all edges in this graph.
        Returns:
        graph edges.
      • adjacentNodes

        @Contract(value="_ -> _",
                  pure=true)
        @NotNull
        @NotNull Collection<T> adjacentNodes​(@NotNull
                                             T node)
        Returns the nodes which have an edge with node in this graph.

        This is equivalent to the union of predecessors(Object) and successors(Object)

        Parameters:
        node - Node.
        Returns:
        adjacent nodes to node.
      • predecessors

        @Contract(value="_ -> _",
                  pure=true)
        @NotNull
        @NotNull Collection<T> predecessors​(@NotNull
                                            T node)
        Returns the nodes in this graph that have an edge with node as a target.

        In an undirected graph this is equivalent to adjacentNodes(Object).

        Parameters:
        node - Node to get predecessors.
        Returns:
        predecessors to node.
      • successors

        @Contract(value="_ -> _",
                  pure=true)
        @NotNull
        @NotNull Collection<T> successors​(@NotNull
                                          T node)
        Returns the nodes in this graph that have an edge with node as a source.

        In an undirected graph this is equivalent to adjacentNodes(Object).

        Parameters:
        node - Node to get successors of.
        Returns:
        successors to node.
      • addNode

        @Contract("_ -> _")
        boolean addNode​(@NotNull
                        T node)
        Adds node to this graph, if it is not already present.
        Parameters:
        node - Node to be added.
        Returns:
        true if the graph changed as a result of this call.
      • removeNode

        @Contract("_ -> _")
        boolean removeNode​(@NotNull
                           T node)
        Removes node from this graph, if it is present.

        All edges connecting to node will also be removed.

        Parameters:
        node - Node to be removed.
        Returns:
        true if the graph changed as a result of this call.
      • putEdge

        @Contract("_, _ -> _")
        default boolean putEdge​(@NotNull
                                T source,
                                @NotNull
                                T target)
        Adds an edge connecting source and target, if not already present.

        If this graph is directed, the edge will also be directed; otherwise it will be undirected.

        Parameters:
        source - Source node.
        target - Target node.
        Returns:
        true if the graph changed as a result of this call.
        Throws:
        IllegalArgumentException - if this graph does not allow self-loops and this edge is a self-loop.
      • putEdge

        @Contract("_ -> _")
        boolean putEdge​(@NotNull
                        @NotNull Edge<T> edge)
        Adds an edge connecting Edge.source() and Edge.target(), if not already present.

        If this graph is directed, the edge will also be directed; otherwise it will be undirected.

        Parameters:
        edge - Edge to be added.
        Returns:
        true if the graph changed as a result of this call.
        Throws:
        IllegalArgumentException - if this graph does not allow self-loops and this edge is a self-loop.
      • removeEdge

        @Contract("_, _ -> _")
        default boolean removeEdge​(@NotNull
                                   T source,
                                   @NotNull
                                   T target)
        Removes the edge connecting source and target, if present.
        Parameters:
        source - Source node.
        target - Target node.
        Returns:
        true if the graph changed as a result of this call.
      • removeEdge

        @Contract("_ -> _")
        boolean removeEdge​(@NotNull
                           @NotNull Edge<T> edge)
        Removes the edge connecting Edge.source() and Edge.target(), if present.
        Parameters:
        edge - Edge to be removed.
        Returns:
        true if the graph changed as a result of this call.