V
- The type of data to associate with each vertex.E
- The type of data, if any, to store inside each edge.
Some graphs do not need to store any extra information in each edge,
in which case they can supply any type for E (such as Object)
and can avoid passing any extra information when adding edges
to the graph.public abstract class AbstractGraph<V,E> extends java.lang.Object implements Graph<V,E>
Constructor and Description |
---|
AbstractGraph()
Constructs a new empty undirected, unweighted graph.
|
AbstractGraph(boolean directed,
boolean weighted)
Constructs a new empty graph that can be directed or undirected,
weighted or unweighted.
|
Modifier and Type | Method and Description |
---|---|
void |
addEdge(V v1,
V v2)
Adds an edge between the given two vertices, if none already exists.
|
void |
addEdge(V v1,
V v2,
E e)
Adds an edge between the given two vertices, if none already exists,
storing the given information in the edge.
|
void |
addEdge(V v1,
V v2,
E e,
int weight)
Adds an edge with the given information and weight between the given two vertices,
storing the given information in the edge.
|
void |
addEdge(V v1,
V v2,
int weight)
Adds an edge between the given two vertices, if none already exists.
|
void |
addVertex(V v)
Adds the given element as a vertex in this graph.
|
protected static void |
checkForNegative(int value)
Tests the given integer to see whether it is negative.
|
protected static void |
checkForNull(java.lang.Object... args)
Tests the given arguments for null.
|
protected void |
checkVertex(V vertex)
Tests the given vertex for null and for membership in the graph.
|
protected void |
checkVertices(V v1,
V v2)
Tests the given vertices for null and for membership in the graph.
|
void |
clear()
Removes all vertices and edges from this graph.
|
void |
clearEdges()
Removes all edges from this graph.
|
protected void |
clearVertexInfo()
Resets all distance / previous / visited markings from vertex info
objects in this graph.
|
boolean |
containsEdge(E e)
Returns whether this graph contains at least one edge with the given
information stored in it.
|
boolean |
containsEdge(V v1,
V v2)
Returns whether this graph contains an edge between the given vertices.
|
boolean |
containsVertex(V v)
Returns whether this graph contains the given vertex.
|
int |
cost(java.util.List<V> path)
Returns the total cost of following the given path of vertices in this graph,
which is the sum of the edge weights between the path's vertices from start to end.
|
int |
degree(V v)
Returns the number of outgoing edges from the given vertex.
|
E |
edge(V v1,
V v2)
Returns the extra information stored in the edge between vertices v1 and v2, if one exists.
|
int |
edgeCount()
Returns the total number of edges in this graph.
|
java.util.Collection<E> |
edges()
Returns a collection of information about all unique edges in this graph.
|
int |
edgeWeight(V v1,
V v2)
Returns the weight of the edge between vertices v1 and v2, if one exists.
|
boolean |
equals(java.lang.Object o)
Returns true if o refers to a graph with the same vertices, edges, and other properties
(directed vs.
|
int |
hashCode()
Returns an integer code for placing this graph into a hash-based collection.
|
int |
inDegree(V v)
Returns the number of incoming edges to the given vertex.
|
boolean |
isDirected()
Returns true if this is a directed graph, and false if it is undirected.
|
boolean |
isEmpty()
Returns true if this graph does not contain any vertices or edges.
|
boolean |
isWeighted()
Returns true if this is a weighted graph and false if it is unweighted.
|
java.util.Set<V> |
neighbors(V v)
Returns a collection containing all neighbors, that is, all vertices that are
directly connected to the given vertex by an outgoing edge.
|
int |
outDegree(V v)
Returns the number of outgoing edges from the given vertex.
|
void |
removeEdge(E e)
Removes a single edge that exists with the given extra info stored in it.
|
void |
removeEdge(V v1,
V v2)
Removes any edge that exists from vertex v1 to vertex v2.
|
void |
removeVertex(V v)
Removes the given vertex from this graph, along with all edges that were
touching it.
|
java.lang.String |
toString()
Returns a string representation of this graph and its vertices and edges, such as
"{V={A, B, C}, E={(A, B), (A, C)}}".
|
java.lang.String |
toStringDetailed()
Returns a detailed multi-line string representation of this graph and its vertices, such as
"Graph{
vertices:
A -> neighbors: [B, C]
B -> neighbors: [A]
C -> neighbors: [A]
edges:
(A,B,weight=2)
(A,C)
}"
|
int |
vertexCount()
Returns the number of vertices in this graph.
|
protected Vertex<V> |
vertexInfo(V v)
Returns the Vertex object associated with the given vertex.
|
protected java.util.Map<V,Vertex<V>> |
vertexInfos()
Returns a read-only view of all internal data about vertex information.
|
java.util.Set<V> |
vertices()
Returns a collection of the vertices in this graph.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
isReachable, minimumWeightPath, shortestPath
public AbstractGraph()
public AbstractGraph(boolean directed, boolean weighted)
public void addEdge(V v1, V v2)
public void addEdge(V v1, V v2, E e)
public void addEdge(V v1, V v2, int weight)
addEdge
in interface Graph<V,E>
v1
- Starting vertex.v2
- Ending vertex.weight
- Edge's weight.java.lang.IllegalArgumentException
- If either vertex is not part of the graph,
if edge weight is negative,
or if this is an unweighted graph and an edge
weight other than 1 is passed.java.lang.NullPointerException
- If any object parameter is null.public void addEdge(V v1, V v2, E e, int weight)
addEdge
in interface Graph<V,E>
v1
- Starting vertex.v2
- Ending vertex.e
- Information to store in the edge.weight
- Edge's weight.java.lang.IllegalArgumentException
- If either vertex is not part of the graph,
if edge weight is negative,
or if this is an unweighted graph and an edge
weight other than 1 is passed.java.lang.NullPointerException
- If any object parameter is null.public void addVertex(V v)
public void clear()
public void clearEdges()
clearEdges
in interface Graph<V,E>
public boolean containsEdge(E e)
containsEdge
in interface Graph<V,E>
public boolean containsEdge(V v1, V v2)
containsEdge
in interface Graph<V,E>
public boolean containsVertex(V v)
containsVertex
in interface Graph<V,E>
v
- The vertex.public int cost(java.util.List<V> path)
public int degree(V v)
public E edge(V v1, V v2)
public int edgeCount()
public java.util.Collection<E> edges()
public int edgeWeight(V v1, V v2)
edgeWeight
in interface Graph<V,E>
v1
- The starting vertex.v2
- The ending vertex.java.lang.IllegalArgumentException
- If either vertex is not part of the graph.java.lang.NullPointerException
- If any object parameter is null.public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
o
- The object to compare against.public int hashCode()
hashCode
in class java.lang.Object
public int inDegree(V v)
public boolean isDirected()
isDirected
in interface Graph<V,E>
public boolean isEmpty()
public boolean isWeighted()
isWeighted
in interface Graph<V,E>
public java.util.Set<V> neighbors(V v)
public int outDegree(V v)
public void removeEdge(E e)
removeEdge
in interface Graph<V,E>
e
- The edge extra info of the edge to remove.public void removeEdge(V v1, V v2)
removeEdge
in interface Graph<V,E>
v1
- The starting vertex of the edge.v2
- The ending vertex of the edge.java.lang.IllegalArgumentException
- If either vertex is not part of the graph.java.lang.NullPointerException
- If any object parameter is null.public void removeVertex(V v)
removeVertex
in interface Graph<V,E>
v
- The vertex to remove.java.lang.NullPointerException
- If the vertex is null.public java.lang.String toString()
public java.lang.String toStringDetailed()
toStringDetailed
in interface Graph<V,E>
public int vertexCount()
vertexCount
in interface Graph<V,E>
public final java.util.Set<V> vertices()
protected static void checkForNegative(int value)
value
- The integer to examine.java.lang.IllegalArgumentException
- If the value is negative.protected static void checkForNull(java.lang.Object... args)
args
- The arguments to examine.java.lang.NullPointerException
- If any argument is null.protected void checkVertex(V vertex)
vertex
- The vertex to examine.java.lang.IllegalArgumentException
- If the vertex is not part of this graph.java.lang.NullPointerException
- If the vertex is null.protected void checkVertices(V v1, V v2)
v1
- The first vertex to examine.v2
- The second vertex to examine.java.lang.IllegalArgumentException
- If any vertex is not part of this graph.java.lang.NullPointerException
- If any vertex is null.protected void clearVertexInfo()
protected Vertex<V> vertexInfo(V v)
v
- The vertex to examine.java.lang.IllegalArgumentException
- If any vertex is not part of this graph.java.lang.NullPointerException
- If any vertex is null.