Class Graph<T>

  • Type Parameters:
    T - the type of node elements.
    Direct Known Subclasses:
    UndirectedGraph


    public class Graph<T>
    extends Object
    A class to represent an unweighted directed graph.
    • Constructor Detail

      • Graph

        public Graph()
    • Method Detail

      • addNode

        public void addNode(T node)
        Add a node to the graph.
        Parameters:
        node - the node to add.
      • removeNode

        public void removeNode(T node)
        Remove a node from the graph.
        Parameters:
        node - the node to remove.
      • removeAllNodes

        public void removeAllNodes(Collection<T> toRemove)
        Remove a collection of nodes from the graph.
        Parameters:
        toRemove - the nodes to remove.
      • addNodes

        public void addNodes(Set<T> nodesToAdd)
        Add all these nodes to the graph.
        Parameters:
        nodesToAdd - a set of nodes to add.
      • addEdge

        public void addEdge(T a,
                            T b)
        Add this edge to the graph.
        Parameters:
        a - a node.
        b - another node.
      • removeEdge

        public void removeEdge(T a,
                               T b)
        Remove this edge from graph.
        Parameters:
        a - a node.
        b - another node.
      • getNodes

        public Collection<T> getNodes()
        Return the nodes of this graph.
        Returns:
        the set of nodes.
      • getEdgesOut

        public Set<T> getEdgesOut(T node)
        Get all outgoing edges (the nodes that can be reached via these edges).
        Parameters:
        node - the node whose edges to get.
        Returns:
        a set of reachable nodes.
      • getEdgesIn

        public Set<T> getEdgesIn(T node)
        Get all incoming edges (the nodes that can be reached via these edges).
        Parameters:
        node - the node whose incoming edges to get.
        Returns:
        a set of nodes that can reach this one.
      • degreeIn

        public int degreeIn(T node)
        Get the number of incoming edges.
        Parameters:
        node - the node.
        Returns:
        the number of incoming edges.
      • degreeOut

        public int degreeOut(T node)
        Get the number of outgoing edges.
        Parameters:
        node - the node.
        Returns:
        the number of outgoing edges.
      • getPartition

        public Set<Set<T>> getPartition()
        Get a partition of the graph. Each subset consist of a set of nodes that are connected. This currently expects the graph to be undirected.
        Returns:
        a partition of the graph.