java.lang.ObjectGraph<E>
CS3Graph<E>
public class CS3Graph<E>
Implementation of concrete graph class for CS3.
| Constructor Summary | |
|---|---|
CS3Graph(boolean isDirected)
Boolean-parameter constructor. |
|
CS3Graph(Graph orig)
Copy constructor. |
|
| Method Summary | |
|---|---|
void |
addEdge(E startVertex,
E endVertex)
Add an edge to this graph. |
void |
addEdge(E startVertex,
E endVertex,
int weight)
Add a weighted edge to this graph. |
void |
addVertex(E vertex)
Add a vertex to this graph. |
List<E> |
breadthFirst(E startVertex)
Perform a breadth first traversal of the graph, starting at startVertex. |
void |
clear()
Remove all vertices and edges from this graph. |
int |
costOfPath(List<E> path)
Determine the cost of a path (the sum of the weights of the edges). |
List<E> |
depthFirst(E startVertex)
Perform a depth first traversal of the graph, starting at startVertex. |
boolean |
equals(Graph<E> graph)
Determine if this graph is equivalent to another graph. |
List<E> |
getNeighbors(E vertex)
Returns a list of the vertices that are adjacent to a given vertex. |
int |
getNumberOfEdges()
Returns the number of edges in this graph. |
int |
getNumberOfVertices()
Returns the number of vertices in this graph. |
List<E> |
getVertices()
Returns a list of the vertices in this graph. |
int |
getWeightOfEdge(E startVertex,
E endVertex)
Returns the weight associated with an edge in this graph. |
int |
inDegree(E vertex)
Returns the number of edges that have the given vertex as a destination. |
boolean |
isDirected()
Returns whether or not this graph is directed. |
Graph<E> |
minimumSpanningTree()
Determine a minimum spanning tree of this graph. |
int |
outDegree(E vertex)
Returns the number of edges that have the given vertex as a source. |
void |
removeEdge(E startVertex,
E endVertex)
Remove an edge from this graph. |
void |
removeVertex(E vertex)
Remove a vertex and all incident edges from this graph. |
List<E> |
topologicalSort()
Perform a topological sort of this graph. |
String |
toStringAdjacencyMatrix()
The graph represented as an adjacency matrix rendered as a String. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public CS3Graph(boolean isDirected)
isDirected - true if Graph is to be directed; false if undirectedpublic CS3Graph(Graph orig)
orig - the original Graph to be copied
NullPointerException - if the specified Graph is null| Method Detail |
|---|
public void addEdge(E startVertex,
E endVertex)
startVertex to endVertex.
If the graph is undirected, an undirected edge is added
between startVertex and endVertex.
If startVertex or endVertex is not
already in this graph, that vertex is added to the graph.
If an edge between startVertex and endVertex
already exists in this graph, no new edge is added and the
existing edge remains unchanged.
If a new edge is added to the graph, its weight is set to 1.
addEdge in class Graph<E>startVertex - the label that identifies the start vertexendVertex - the label that identifies the end vertex
NullPointerException - if one or both of the specified vertices is null
public void addEdge(E startVertex,
E endVertex,
int weight)
startVertex to endVertex.
If the graph is undirected, an undirected edge is added
between startVertex and endVertex.
If startVertex or endVertex is not
already in this graph, that vertex is added to the graph.
If an edge between startVertex and endVertex
already exists in this graph, no new edge is added. Rather,
the weight specified by the parameter replaces the weight
associated with the existing edge.
addEdge in class Graph<E>startVertex - the label that identifies the start vertexendVertex - the label that identifies the end vertexweight - the weight associated with the edge
NullPointerException - if one or both of the specified vertices is nullpublic void addVertex(E vertex)
addVertex in class Graph<E>vertex - the label that identifies the vertexpublic List<E> breadthFirst(E startVertex)
startVertex.
(Note that the entire graph is traversed even if it is unconnected.)
breadthFirst in class Graph<E>startVertex - the label that identifies the starting vertex
IllegalArgumentException - if vertex does not exist in the graph
NullPointerException - if the specified vertex is nullpublic void clear()
clear in class Graph<E>public int costOfPath(List<E> path)
costOfPath in class Graph<E>path - an ordered list of vertices that specify the path in this graph
IllegalArgumentException - if path is not a valid path in this graphpublic List<E> depthFirst(E startVertex)
startVertex.
(Note that the entire graph is traversed even if it is unconnected.)
depthFirst in class Graph<E>startVertex - the label that identifies the starting vertex
IllegalArgumentException - if vertex does not exist in the graph
NullPointerException - if the specified vertex is nullpublic boolean equals(Graph<E> graph)
equals in class Graph<E>graph - the Graph to be compared to this Graph
public List<E> getNeighbors(E vertex)
getNeighbors in class Graph<E>vertex - the label that identifies the vertex
null
IllegalArgumentException - if vertex does not exist in the graphpublic int getNumberOfEdges()
getNumberOfEdges in class Graph<E>public int getNumberOfVertices()
getNumberOfVertices in class Graph<E>public List<E> getVertices()
getVertices in class Graph<E>
public int getWeightOfEdge(E startVertex,
E endVertex)
getWeightOfEdge in class Graph<E>startVertex - the label that identifies the start vertexendVertex - the label that identifies the end vertex
startVertex to endVertex;
IllegalArgumentException - if no such edge exists in this graphpublic int inDegree(E vertex)
inDegree in class Graph<E>vertex - the label that identifies the vertex
null
IllegalArgumentException - if vertex does not exist in the graphpublic boolean isDirected()
isDirected in class Graph<E>true if this graph is directed; false
if this graph is undirectedpublic Graph<E> minimumSpanningTree()
minimumSpanningTree in class Graph<E>public int outDegree(E vertex)
outDegree in class Graph<E>vertex - the label that identifies the vertex
null
IllegalArgumentException - if vertex does not exist in the graph
public void removeEdge(E startVertex,
E endVertex)
null or not present in the graph.)
removeEdge in class Graph<E>startVertex - the label that identifies the start vertexendVertex - the label that identifies the end vertexpublic void removeVertex(E vertex)
removeVertex in class Graph<E>vertex - the label that identifies the vertexpublic List<E> topologicalSort()
topologicalSort in class Graph<E>UnsupportedOperationException - if invoked on an undirected graphpublic String toStringAdjacencyMatrix()
String.
For example,
| a | b | c | d | e | --+---+---+---+---+---+ a | | 2 | | 3 | | --+---+---+---+---+---+ b | | | 4 | | | --+---+---+---+---+---+ c | 1 | | | 3 | 2 | --+---+---+---+---+---+ d | | 5 | | | | --+---+---+---+---+---+ e | 6 | | | 1 | | --+---+---+---+---+---+
toStringAdjacencyMatrix in class Graph<E>