Class AdjListsDiGraph<T>

  • All Implemented Interfaces:
    DiGraph<T>

    public class AdjListsDiGraph<T>
    extends java.lang.Object
    implements DiGraph<T>
    AdjListsDiGraph.java Implements the DiGraph Interface. Uses a Vector of LinkedLists to keep track of the adjacent vertices. It does not handle operations involving non-existing vertices
    Version:
    (17/3/2021)
    Author:
    (SK)
    • Constructor Summary

      Constructors 
      Constructor Description
      AdjListsDiGraph()
      Constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addArc​(T source, T destination)
      Adds an arc to the graph, from source to destination.
      void addVertex​(T vertex)
      Adds the input vertex to the graph.
      static AdjListsDiGraph<java.lang.String> AdjListsGraphFromFile​(java.lang.String tgf_fName)
      Creates and returns a new graph of Strings using the data found in the input tgf file.
      int getNumArcs()
      Returns the number of arcs in the graph
      int getNumVertices()
      Returns the number of vertices in the graph
      boolean isArc​(T v1, T v2)
      Returns true iff a directed connection exists between the two input vertices
      boolean isEmpty()
      Returns true if the graph is empty and false otherwise.
      static void main​(java.lang.String[] args)
      Very Basic Driver program.
      void removeArc​(T v1, T v2)
      Removes the arc between v1 and v2.
      void removeVertex​(T vertex)
      Removes the input vertex from the graph.
      void saveToTGF​(java.lang.String fName)
      Saves the current graph into a .tgf file.
      java.lang.String toString()
      Returns a string representation of the graph.
      • Methods inherited from class java.lang.Object

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

      • AdjListsDiGraph

        public AdjListsDiGraph()
        Constructor. Creates an empty graph.
    • Method Detail

      • AdjListsGraphFromFile

        public static AdjListsDiGraph<java.lang.String> AdjListsGraphFromFile​(java.lang.String tgf_fName)
        Creates and returns a new graph of Strings using the data found in the input tgf file. If the file does not exist, a message is printed.
        Parameters:
        String - The name of the tgf file to read the graph from
        Returns:
        AdjListsDiGraph the graph of Strings created
      • isEmpty

        public boolean isEmpty()
        Returns true if the graph is empty and false otherwise.
        Specified by:
        isEmpty in interface DiGraph<T>
        Returns:
        boolean True iff the graph is empty
      • getNumVertices

        public int getNumVertices()
        Returns the number of vertices in the graph
        Specified by:
        getNumVertices in interface DiGraph<T>
        Returns:
        int The number of vertices in the graph
      • getNumArcs

        public int getNumArcs()
        Returns the number of arcs in the graph
        Specified by:
        getNumArcs in interface DiGraph<T>
        Returns:
        int The number of arcs in the graph
      • isArc

        public boolean isArc​(T v1,
                             T v2)
        Returns true iff a directed connection exists between the two input vertices
        Specified by:
        isArc in interface DiGraph<T>
        Parameters:
        T - the first vertex
        T - the second vertex
        Returns:
        boolean true iff a directed connection exists from the first vertex to the second
      • addVertex

        public void addVertex​(T vertex)
        Adds the input vertex to the graph. If the vertex already exists in the graph, the graph is not changed.
        Specified by:
        addVertex in interface DiGraph<T>
        Parameters:
        T - the vertex to be added to the graph
      • removeVertex

        public void removeVertex​(T vertex)
        Removes the input vertex from the graph. If the input vertex does not belong in the graph, the graph is not changed. Uses equals() for identidying the vertex to be removed.
        Specified by:
        removeVertex in interface DiGraph<T>
        Parameters:
        T - The vertex to be removed.
      • addArc

        public void addArc​(T source,
                           T destination)
        Adds an arc to the graph, from source to destination. If source or destination do not exist in the graph, the graph is not changed. Verifies that source and destination are valid vertices in the graph, and that the newly added arc does not already belong in the graph.
        Specified by:
        addArc in interface DiGraph<T>
        Parameters:
        T - the source of the arc
        T - the destination of the arc
      • removeArc

        public void removeArc​(T v1,
                              T v2)
        Removes the arc between v1 and v2. If v1 or v2, or the arc from v1 to v2 does not exist, the graph does not change.
        Specified by:
        removeArc in interface DiGraph<T>
        Parameters:
        T - the source of the arc to be removed
        T - the destination of the arc to be removed
      • toString

        public java.lang.String toString()
        Returns a string representation of the graph.
        Specified by:
        toString in interface DiGraph<T>
        Overrides:
        toString in class java.lang.Object
        Returns:
        String a string representation of this graph
      • saveToTGF

        public void saveToTGF​(java.lang.String fName)
        Saves the current graph into a .tgf file.
        Specified by:
        saveToTGF in interface DiGraph<T>
        Parameters:
        the - name of the file to write to
      • main

        public static void main​(java.lang.String[] args)
        Very Basic Driver program.