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 you can supply a meaningless type for E (such as Void)
and can avoid passing any extra information when adding edges
to the graph.public interface Graph<V,E>
A Graph does not allow null vertices, but it does allow null edges, which corresponds to an edge with no extra information stored in it.
A Graph can have at most one edge with the same start and end vertex; in other words, it is not permitted to have two edges between the same vertices, except in a directed graph it is allowed to have an edge A,B and an edge B,A. Loops (edges from a vertex directly back to itself) are prohibited; an IllegalArgumentException will be thrown on an attempt to add a loop.
Implementations of the Graph interface do not allow edges with negative weights. If a negative edge weight is passed, an IllegalArgumentException is thrown. A weight of 0 is allowed.
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.
|
void |
clear()
Removes all vertices and edges from this graph.
|
void |
clearEdges()
Removes all edges from 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 all edges in this graph.
|
int |
edgeWeight(V v1,
V v2)
Returns the weight of the edge between vertices v1 and v2, if one exists.
|
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 |
isReachable(V v1,
V v2)
Returns true if there is any path in this graph that
leads from the given starting vertex v1 to the given ending vertex v2.
|
boolean |
isWeighted()
Returns true if this is a weighted graph and false if it is unweighted.
|
java.util.List<V> |
minimumWeightPath(V v1,
V v2)
Returns a list of vertices representing a minimum-weight path from vertex
v1 to vertex v2.
|
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 any edge(s) that exist with the given extra info stored in it/them.
|
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.util.List<V> |
shortestPath(V v1,
V v2)
Returns the path in this graph with the least number of vertices that
leads from the given starting vertex v1 to the given ending vertex v2.
|
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.
|
java.util.Set<V> |
vertices()
Returns a collection of the vertices in this graph.
|
void addEdge(V v1, V v2)
v1
- Starting vertex.v2
- Ending vertex.java.lang.IllegalArgumentException
- If either vertex is not part of the graph,
or if v1 and v2 are the same (a loop).java.lang.NullPointerException
- If any object parameter is null.void addEdge(V v1, V v2, E e)
v1
- Starting vertex.v2
- Ending vertex.e
- Information to store in the edge.java.lang.IllegalArgumentException
- If either vertex is not part of the graph,
or if v1 and v2 are the same (a loop).java.lang.NullPointerException
- If any object parameter is null.void addEdge(V v1, V v2, int weight)
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,
if v1 and v2 are the same (a loop),
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.void addEdge(V v1, V v2, E e, int weight)
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,
if v1 and v2 are the same (a loop),
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.void addVertex(V v)
v
- The vertex to add.java.lang.NullPointerException
- If the vertex is null.void clear()
void clearEdges()
boolean containsEdge(E e)
boolean containsEdge(V v1, V v2)
boolean containsVertex(V v)
v
- The vertex.int cost(java.util.List<V> path)
path
- The list of vertices in the path to examine.java.lang.IllegalArgumentException
- If any vertex in the path is not part of this graph.java.lang.NullPointerException
- If the path or any vertex in it is null.int degree(V v)
v
- The vertex.java.lang.IllegalArgumentException
- If the vertex is not part of the graph.java.lang.NullPointerException
- If any object parameter is null.E edge(V v1, V v2)
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.int edgeCount()
java.util.Collection<E> edges()
int edgeWeight(V v1, V v2)
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.int inDegree(V v)
v
- The vertex to examine.java.lang.IllegalArgumentException
- If the vertex is not part of the graph.java.lang.NullPointerException
- If any object parameter is null.boolean isDirected()
boolean isEmpty()
boolean isReachable(V v1, V v2)
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.boolean isWeighted()
java.util.List<V> minimumWeightPath(V v1, V v2)
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.java.util.Set<V> neighbors(V v)
v
- The vertex to examine.java.lang.IllegalArgumentException
- If the vertex is not part of this graph.java.lang.NullPointerException
- If the vertex is null.int outDegree(V v)
v
- The vertex to examine.void removeEdge(E e)
e
- The edge extra info of the edge to remove.void removeEdge(V v1, V v2)
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.void removeVertex(V v)
v
- The vertex to remove.java.lang.NullPointerException
- If the vertex is null.java.util.List<V> shortestPath(V v1, V v2)
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.java.lang.String toString()
toString
in class java.lang.Object
java.lang.String toStringDetailed()
int vertexCount()
java.util.Set<V> vertices()