Interface Graph
- All Known Implementing Classes:
StringGraph
public interface Graph
A graph whose vertices are strings.
- Author:
- Dr. Lillis
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default capacity used when the no-argument constructor is called. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new edge.voidAdds multiple edges.voidAdds a new vertex to this graph.voidaddVertices(String[] vertices) Adds one or more vertices to this graph.intcapacity()Returns the number of vertices that can be added to this graph before the array of vertices needs to be increased.booleanedgeExists(String vertex1, String vertex2) Determines if an edge with the given end vertices exists.String[][]getEdges()Creates and returns a new 2D array of strings containing the edges of this graph.String[]Creates and returns a new array of Strings containing the vertices of this graph.intReturns the number of edges in this graph (the "size" of this graph).intReturns the number of vertices in this graph (the "order" of this graph).voidresize(int newCapacity) Resizes the array of labels and the adjacency matrix to a new capacity.toString()Returns a string representation of this Graph.booleanvertexExists(String vertex) Determines if a vertex with the given label exists.
-
Field Details
-
DEFAULT_CAPACITY
static final int DEFAULT_CAPACITYThe 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
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
Adds a new vertex to this graph.- Parameters:
vertex- the new vertex to be added- Throws:
GraphException- if the specified vertex already exists.
-
addVertices
Adds one or more vertices to this graph.- Parameters:
vertices- the vertices to be added
-
edgeExists
Determines if an edge with the given end vertices exists.- Parameters:
vertex1- the label of one end of the edgevertex2- 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
Adds a new edge.- Parameters:
vertex1- the label of one end of the new edgevertex2- 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
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
-