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 "BFS_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 capacityStringGraph(String[] initVertices) Parameterized constructor gives initial set of vertex labels.StringGraph(String[] initVertices, String[][] initEdges) Parameterized constructor gives initial set of vertex labels and edges. -
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.doubleReturns the average degree of the graphString[]Performs a Breadth First Search traversal starting at a given vertex.Performs a Breadth First Search traversal starting at a given vertex.intcapacity()Returns the number of vertices that can be added to this graph before the array of vertices needs to be increased.private voiddecreaseCapacity(int smallerCapacity) Decreases the the size of labels array and adjacency matrix and sets the capacity variable to smallerCapacity.private intdegree(int index) Returns the degree of the vertex whose label is stored at the given index in the labels array.intReturns the degree of a vertexint[]Return the degree sequence for this graph.private double[]Return maximum degree, minimum degree, and average degree of this graph.voiddeleteEdge(String vertex1, String vertex2) Deletes an edge.voiddeleteVertex(String vertex) Deletes a vertex and all of its incident edges.String[]Performs a Depth First Search traversal starting at a given vertex.Performs a Depth First Search traversal starting at a given vertex.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.private int[]getNeighborIndices(int index) Returns the indices of all neighbors of the vertex with the given index.String[]getNeighbors(String vertex) Returns the labels of all neighbors of a vertex.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.booleanDetermines if the graph is connected.intReturns the maximum degree of the graphintReturns the minimum degree of the 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
-
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 "BFS_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
-
StringGraph
Parameterized constructor gives initial set of vertex labels. Constructs A graph with capacity equal to the number of labels and adds a vertex for each given label. No edges are added.- Parameters:
initVertices- Initial vertex labels.
-
StringGraph
Parameterized constructor gives initial set of vertex labels and edges. Constructs a graph with capacity equal to the number of labels, adds a vertex for each given label, and adds the edges specified.- Parameters:
initVertices- An initial set of vertex labels.initEdges- An 2 x m array representing the edges of the graph.
-
-
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. newCapacity. -
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.
-
decreaseCapacity
private void decreaseCapacity(int smallerCapacity) Decreases the the size of labels array and adjacency matrix and sets the capacity variable to smallerCapacity.- Parameters:
smallerCapacity- The new, smaller, capacity of this graph.- Throws:
GraphException- if newCapacity less than number of vertices.
-
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
-
deleteVertex
Description copied from interface:GraphDeletes a vertex and all of its incident edges.- Specified by:
deleteVertexin interfaceGraph- Parameters:
vertex- the vertex to be deleted
-
deleteEdge
Description copied from interface:GraphDeletes an edge.- Specified by:
deleteEdgein interfaceGraph- Parameters:
vertex1- one end vertex of the edgevertex2- the other end vertex of the edge
-
degree
-
degree
private int degree(int index) Returns the degree of the vertex whose label is stored at the given index in the labels array.- Parameters:
index- The index of the vertex of interest- Returns:
- The degree of the vertex with the given index
- Throws:
GraphException- if the given index is not valid
-
maxDegree
-
minDegree
-
averageDegree
public double averageDegree()Description copied from interface:GraphReturns the average degree of the graph- Specified by:
averageDegreein interfaceGraph- Returns:
- the average degree
-
degreeStats
private double[] degreeStats()Return maximum degree, minimum degree, and average degree of this graph.- Returns:
- A three-element array of doubles containing max, min, avg degree.
-
degreeSequence
public int[] degreeSequence()Description copied from interface:GraphReturn the degree sequence for this graph. The degree sequence is a sequence consisting of the degree of each vertex, sorted from largest to smallest.- Specified by:
degreeSequencein interfaceGraph- Returns:
- an int array containing the degrees of all vertices, sorted from largest degree to smallest degree.
-
getNeighbors
Description copied from interface:GraphReturns the labels of all neighbors of a vertex. The order of the neighbors is unspecified.- Specified by:
getNeighborsin interfaceGraph- Parameters:
vertex- the vertex of interest- Returns:
- a String array holding the labels the neighbors.
-
getNeighborIndices
private int[] getNeighborIndices(int index) Returns the indices of all neighbors of the vertex with the given index.- Parameters:
index- The index of the vertex of interest- Returns:
- an array of int values containing the indices of the neighbors of the vertex of interest
- Throws:
GraphException- if the given index is not valid.
-
bfsOrder
Description copied from interface:GraphPerforms a Breadth First Search traversal starting at a given vertex. The label of each vertex visited is added to an array of Strings in the order visited. -
bfsTree
Description copied from interface:GraphPerforms a Breadth First Search traversal starting at a given vertex. Constructs and returns a BFS spanning tree of the connected component containing the specified vertex. Adjacent vertices are visited in the lexicographical order of their labels. -
dfsOrder
Description copied from interface:GraphPerforms a Depth First Search traversal starting at a given vertex. The label of each vertex visited is added to an array of Strings in the order visited. -
dfsTree
Description copied from interface:GraphPerforms a Depth First Search traversal starting at a given vertex. Constructs and returns a DFS spanning tree of the connected component containing the specified vertex. Adjacent vertices are visited in the lexicographical order of their labels. -
isConnected
public boolean isConnected()Description copied from interface:GraphDetermines if the graph is connected.- Specified by:
isConnectedin interfaceGraph- Returns:
- true if the graph is connected, false otherwise.
-