Class StringGraph
java.lang.Object
StringGraph
- All Implemented Interfaces:
Graph
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intNumber of vertices that can be stored in this graph before the size of the labels and adjacency arrays needs to be increased.protected boolean[][]2D array that stores adjacencies between pairs of vertices.protected String[]1D array that stores the vertex labels.protected intNumber of edges in this graph (the "size" of this graph).protected intNumber of vertices in this graph (the "order" of this graph).Fields inherited from interface Graph
DEFAULT_CAPACITY -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor sets initial capacity to the value indicated by the constant value Graph.Default_Capacity.StringGraph(int initCapacity) Parameterized constructor sets initial capacity to given capacity -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new edge.final voidAdds multiple edges.voidAdds a new vertex to this graph.final voidaddVertices(String[] newVertices) 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.Return a string showing the vertices and edges of this graph.private intReturns the index at which a vertex label is stored.Returns a string containing the adjacency matrix for this graph.String[]Creates and returns a new array of Strings containing the vertices of this graph.private voidincreaseCapacity(int biggerCapacity) Increases the the size of labels array and adjacency matrix and sets the capacity variable to biggerCapacity.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
-
labels
1D array that stores the vertex labels. -
edgeMatrix
protected boolean[][] edgeMatrix2D array that stores adjacencies between pairs of vertices. -
numVertices
protected int numVerticesNumber of vertices in this graph (the "order" of this graph). -
numEdges
protected int numEdgesNumber of edges in this graph (the "size" of this graph). -
capacity
protected int capacityNumber of vertices that can be stored in this graph before the size of the labels and adjacency arrays needs to be increased.
-
-
Constructor Details
-
StringGraph
public StringGraph()Default constructor sets initial capacity to the value indicated by the constant value Graph.Default_Capacity. Note: This constructor calls the parameterized constructor that takes an initial capacity. -
StringGraph
public StringGraph(int initCapacity) Parameterized constructor sets initial capacity to given capacity- Parameters:
initCapacity- initial capacity for number of vertices- Throws:
GraphException- if initialCapacity is < 1
-
-
Method Details
-
numberOfVertices
public int numberOfVertices()Description copied from interface:GraphReturns the number of vertices in this graph (the "order" of this graph).- Specified by:
numberOfVerticesin interfaceGraph- Returns:
- the number of vertices in this graph
-
numberOfEdges
public int numberOfEdges()Description copied from interface:GraphReturns the number of edges in this graph (the "size" of this graph).- Specified by:
numberOfEdgesin interfaceGraph- Returns:
- the number of edges in this graph
-
capacity
public int capacity()Description copied from interface:GraphReturns the number of vertices that can be added to this graph before the array of vertices needs to be increased. -
resize
public void resize(int newCapacity) Description copied from interface:GraphResizes 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. -
increaseCapacity
private void increaseCapacity(int biggerCapacity) Increases the the size of labels array and adjacency matrix and sets the capacity variable to biggerCapacity.- Parameters:
biggerCapacity- The new, larger, capacity of this graph.
-
getIndex
Returns the index at which a vertex label is stored.- Parameters:
vertex- the vertex of interest- Returns:
- the index at which the vertex is stored. Returns -1 if not found
-
vertexExists
Description copied from interface:GraphDetermines if a vertex with the given label exists.- Specified by:
vertexExistsin interfaceGraph- Parameters:
vertex- the label of a vertex.- Returns:
- true of the vertex exist, false if it does not exist.
-
addVertex
-
addVertices
Description copied from interface:GraphAdds one or more vertices to this graph.- Specified by:
addVerticesin interfaceGraph- Parameters:
newVertices- the vertices to be added
-
edgeExists
Description copied from interface:GraphDetermines if an edge with the given end vertices exists.- Specified by:
edgeExistsin interfaceGraph- 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
-
addEdge
-
addEdges
Description copied from interface:GraphAdds 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. -
getVertices
Description copied from interface:GraphCreates 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.- Specified by:
getVerticesin interfaceGraph- Returns:
- a String array containing the vertex labels
-
getEdges
Description copied from interface:GraphCreates 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"} } -
getGraphString
Return a string showing the vertices and edges of this graph. The vertices are in sorted order. The two vertices of each edge are in sorted order and the list of all edges are shown in sorted order.- Returns:
- a string showing the vertices and edges of this graph
-
getMatrixString
Returns a string containing the adjacency matrix for this graph. The entire adjacency matrix is not necessarily included. Only rows and columns corresponding to vertices are provided. True values are shown as "T" and false values are shown as "-". The rows and columns are labeled with both the index as well as the vertex label.- Returns:
- A string showing the adjacency matrix for this graph.
-
toString
-