java.lang.ObjectGraph<E>
public abstract class Graph<E>
Abstract class for directed and undirected graphs.
Graphs are comprised of vertices and edges.
Each vertex is identified by its label, which is a reference to an object of the element type E. Every vertex label must be unique.
In undirected graphs, all edges are undirected. In directed graphs, all edges are directed. At most one edge may exist with the same start and end vertices.
Every edge has a weight associated with it. If no weight is specified, the edge has "unit" weight (weight equal to 1).
Implementation classes that extend Graph
need
to provide a boolean-parameter constructor and a copy constructor.
The boolean-parameter constructor accepts a boolean
parameter that
indicates if the graph is directed (true
) or undirected (false
);
for example, public ConcreteGraph(boolean directed)
.
The copy constructor accepts a Graph
parameter and creates a
copy of the graph, utilizing the same label references.
Constructor Summary | |
---|---|
Graph()
|
Method Summary | |
---|---|
abstract void |
addEdge(E startVertex,
E endVertex)
Add an edge to this graph. |
abstract void |
addEdge(E startVertex,
E endVertex,
int weight)
Add a weighted edge to this graph. |
abstract void |
addVertex(E vertex)
Add a vertex to this graph. |
abstract List<E> |
breadthFirst(E startVertex)
Perform a breadth first traversal of the graph, starting at startVertex . |
abstract void |
clear()
Remove all vertices and edges from this graph. |
abstract int |
costOfPath(List<E> path)
Determine the cost of a path (the sum of the weights of the edges). |
abstract List<E> |
depthFirst(E startVertex)
Perform a depth first traversal of the graph, starting at startVertex . |
abstract boolean |
equals(Graph<E> graph)
Determine if this graph is equivalent to another graph. |
abstract List<E> |
getNeighbors(E vertex)
Returns a list of the vertices that are adjacent to a given vertex. |
abstract int |
getNumberOfEdges()
Returns the number of edges in this graph. |
abstract int |
getNumberOfVertices()
Returns the number of vertices in this graph. |
abstract List<E> |
getVertices()
Returns a list of the vertices in this graph. |
abstract int |
getWeightOfEdge(E startVertex,
E endVertex)
Returns the weight associated with an edge in this graph. |
abstract int |
inDegree(E vertex)
Returns the number of edges that have the given vertex as a destination. |
abstract boolean |
isDirected()
Returns whether or not this graph is directed. |
abstract Graph<E> |
minimumSpanningTree()
Determine a minimum spanning tree of this graph. |
abstract int |
outDegree(E vertex)
Returns the number of edges that have the given vertex as a source. |
abstract void |
removeEdge(E startVertex,
E endVertex)
Remove an edge from this graph. |
abstract void |
removeVertex(E vertex)
Remove a vertex and all incident edges from this graph. |
abstract List<E> |
topologicalSort()
Perform a topological sort of this graph. |
abstract 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 Graph()
Method Detail |
---|
public abstract 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.
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 abstract 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.
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 null
public abstract void addVertex(E vertex)
vertex
- the label that identifies the vertexpublic abstract List<E> breadthFirst(E startVertex)
startVertex
.
(Note that the entire graph is traversed even if it is unconnected.)
startVertex
- the label that identifies the starting vertex
IllegalArgumentException
- if vertex
does not exist in the graph
NullPointerException
- if the specified vertex is null
public abstract void clear()
public abstract int costOfPath(List<E> path)
path
- an ordered list of vertices that specify the path in this graph
IllegalArgumentException
- if path
is not a valid path in this graphpublic abstract List<E> depthFirst(E startVertex)
startVertex
.
(Note that the entire graph is traversed even if it is unconnected.)
startVertex
- the label that identifies the starting vertex
IllegalArgumentException
- if vertex
does not exist in the graph
NullPointerException
- if the specified vertex is null
public abstract boolean equals(Graph<E> graph)
graph
- the Graph
to be compared to this Graph
public abstract List<E> getNeighbors(E vertex)
vertex
- the label that identifies the vertex
null
IllegalArgumentException
- if vertex
does not exist in the graphpublic abstract int getNumberOfEdges()
public abstract int getNumberOfVertices()
public abstract List<E> getVertices()
public abstract int getWeightOfEdge(E startVertex, E endVertex)
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 abstract int inDegree(E vertex)
vertex
- the label that identifies the vertex
null
IllegalArgumentException
- if vertex
does not exist in the graphpublic abstract boolean isDirected()
true
if this graph is directed; false
if this graph is undirectedpublic abstract Graph<E> minimumSpanningTree()
public abstract int outDegree(E vertex)
vertex
- the label that identifies the vertex
null
IllegalArgumentException
- if vertex
does not exist in the graphpublic abstract void removeEdge(E startVertex, E endVertex)
null
or not present in the graph.)
startVertex
- the label that identifies the start vertexendVertex
- the label that identifies the end vertexpublic abstract void removeVertex(E vertex)
vertex
- the label that identifies the vertexpublic abstract List<E> topologicalSort()
UnsupportedOperationException
- if invoked on an undirected graphpublic abstract String toStringAdjacencyMatrix()
String
.
For example,
| a | b | c | d | e | --+---+---+---+---+---+ a | | 2 | | 3 | | --+---+---+---+---+---+ b | | | 4 | | | --+---+---+---+---+---+ c | 1 | | | 3 | 2 | --+---+---+---+---+---+ d | | 5 | | | | --+---+---+---+---+---+ e | 6 | | | 1 | | --+---+---+---+---+---+