Interface Graph

All Known Implementing Classes:
StringGraph

public interface Graph
A graph whose vertices are strings.
Author:
Dr. Lillis
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The default capacity used when the no-argument constructor is called.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addEdge(String vertex1, String vertex2)
    Adds a new edge.
    void
    addEdges(String[][] edges)
    Adds multiple edges.
    void
    addVertex(String vertex)
    Adds a new vertex to this graph.
    void
    addVertices(String[] vertices)
    Adds one or more vertices to this graph.
    int
    Returns the number of vertices that can be added to this graph before the array of vertices needs to be increased.
    boolean
    edgeExists(String vertex1, String vertex2)
    Determines if an edge with the given end vertices exists.
    String[][]
    Creates and returns a new 2D array of strings containing the edges of this graph.
    Creates and returns a new array of Strings containing the vertices of this graph.
    int
    Returns the number of edges in this graph (the "size" of this graph).
    int
    Returns the number of vertices in this graph (the "order" of this graph).
    void
    resize(int newCapacity)
    Resizes the array of labels and the adjacency matrix to a new capacity.
    Returns a string representation of this Graph.
    boolean
    Determines if a vertex with the given label exists.
  • Field Details

    • DEFAULT_CAPACITY

      static final int DEFAULT_CAPACITY
      The default capacity used when the no-argument constructor is called.
      See Also:
  • Method Details

    • numberOfVertices

      int numberOfVertices()
      Returns the number of vertices in this graph (the "order" of this graph).
      Returns:
      the number of vertices in this graph
    • numberOfEdges

      int numberOfEdges()
      Returns the number of edges in this graph (the "size" of this graph).
      Returns:
      the number of edges in this graph
    • getVertices

      String[] getVertices()
      Creates and returns a new array of Strings containing the vertices of this graph. The length of the returned array equals the number of vertices. The order of the vertices is unspecified.
      Returns:
      a String array containing the vertex labels
    • getEdges

      String[][] getEdges()
      Creates and returns a new 2D array of strings containing the edges of this graph. The size of the returned array is m x 2. In other words, there is one row for each edge. The order of the edges is unspecified and the order of the vertices in each edge is also unspecified. For example, a graph with the three edges {"A", "B"}, {"C", "A"}, and {"F", "D"} may return: {"A", "B"}, {"C", "A"}, {"F", "D"} }
      Returns:
      an m x 2 array in which each row represents and edge in this graph.
    • capacity

      int capacity()
      Returns the number of vertices that can be added to this graph before the array of vertices needs to be increased.
      Returns:
      the number of vertices that can be added to this graph before the array of vertices needs to be increased.
    • resize

      void resize(int newCapacity)
      Resizes the array of labels and the adjacency matrix to a new capacity. If the new capacity is larger than the current capacity, the capacity is increased. Note: In future assignment the capacity may also be made smaller.
      Parameters:
      newCapacity - The new capacity
    • vertexExists

      boolean vertexExists(String vertex)
      Determines if a vertex with the given label exists.
      Parameters:
      vertex - the label of a vertex.
      Returns:
      true of the vertex exist, false if it does not exist.
    • addVertex

      void addVertex(String vertex) throws GraphException
      Adds a new vertex to this graph.
      Parameters:
      vertex - the new vertex to be added
      Throws:
      GraphException - if the specified vertex already exists.
    • addVertices

      void addVertices(String[] vertices)
      Adds one or more vertices to this graph.
      Parameters:
      vertices - the vertices to be added
    • edgeExists

      boolean edgeExists(String vertex1, String vertex2) throws GraphException
      Determines if an edge with the given end vertices exists.
      Parameters:
      vertex1 - the label of one end of the edge
      vertex2 - the label of the other end of the edge
      Returns:
      true edge {vertex1, vertex2} is in this graph, returns false if the edge is not in this graph
      Throws:
      GraphException - if either vertex1 or vertex2 is not in this graph.
    • addEdge

      void addEdge(String vertex1, String vertex2) throws GraphException
      Adds a new edge.
      Parameters:
      vertex1 - the label of one end of the new edge
      vertex2 - the label of the other end of the new edge
      Throws:
      GraphException - if the edge already exists or if either end vertex does not exist.
    • addEdges

      void addEdges(String[][] edges)
      Adds multiple edges. Each row i in the edges parameter represents the pair of vertices edges[i][0] and edges[i][1] for a new edge.
      Parameters:
      edges - a 2 x w array of vertices, w ≥ 0
    • toString

      String toString()
      Returns a string representation of this Graph.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this Graph.