Package com.zybooks.dsaj.graph
Interface Graph<V,E>
- Type Parameters:
V
- type for associated element stored at each vertexE
- type for associated element stored at each edge
- All Known Implementing Classes:
AdjacencyMapGraph
public interface Graph<V,E>
An interface for a graph structure. A graph can be declared as either directed or undirected.
In the case of an undirected graph, methods outgoingEdges and incomingEdges return the same collection,
and outDegree and inDegree return the same value.
-
Method Summary
Modifier and TypeMethodDescriptionedges()
Returns the edges of the graph as an iterable collection.endVertices
(Edge<E> e) Returns the vertices of edge e as an array of length two.Returns the edge from u to v, or null if they are not adjacent.incomingEdges
(Vertex<V> v) Returns an iterable collection of edges for which vertex v is the destination.int
Returns the number of edges for which vertex v is the destination.Inserts and returns a new edge between vertices u and v, storing given element.insertVertex
(V element) Inserts and returns a new vertex with the given element.int
numEdges()
Returns the number of edges of the graph.int
Returns the number of vertices of the graph.Returns the vertex that is opposite to vertex v on edge e.int
Returns the number of edges leaving vertex v.outgoingEdges
(Vertex<V> v) Returns an iterable collection of edges for which vertex v is the origin.void
removeEdge
(Edge<E> e) Removes an edge from the graph.void
removeVertex
(Vertex<V> v) Removes a vertex and all its incident edges from the graph.vertices()
Returns the vertices of the graph as an iterable collection.
-
Method Details
-
numVertices
int numVertices()Returns the number of vertices of the graph.- Returns:
- the number of vertices of the graph
-
numEdges
int numEdges()Returns the number of edges of the graph.- Returns:
- the number of edges of the graph
-
vertices
Returns the vertices of the graph as an iterable collection.- Returns:
- the vertices of the graph as an iterable collection
-
edges
Returns the edges of the graph as an iterable collection.- Returns:
- the edges of the graph as an iterable collection
-
outDegree
Returns the number of edges leaving vertex v. Note that for an undirected graph, this is the same result returned by inDegree- Parameters:
v
- the vertex- Returns:
- the number of edges leaving vertex v
- Throws:
IllegalArgumentException
- if v is not a valid vertex
-
inDegree
Returns the number of edges for which vertex v is the destination. Note that for an undirected graph, this is the same result returned by outDegree- Parameters:
v
- the vertex- Returns:
- the number of edges for which vertex v is the destination
- Throws:
IllegalArgumentException
- if v is not a valid vertex
-
outgoingEdges
Returns an iterable collection of edges for which vertex v is the origin. Note that for an undirected graph, this is the same result returned by incomingEdges.- Parameters:
v
- the vertex- Returns:
- an iterable collection of edges for which vertex v is the origin
- Throws:
IllegalArgumentException
- if v is not a valid vertex
-
incomingEdges
Returns an iterable collection of edges for which vertex v is the destination. Note that for an undirected graph, this is the same result returned by outgoingEdges.- Parameters:
v
- the vertex- Returns:
- an iterable collection of edges for which vertex v is the destination
- Throws:
IllegalArgumentException
- if v is not a valid vertex
-
getEdge
Returns the edge from u to v, or null if they are not adjacent.- Parameters:
u
- origin vertexv
- destination vertex- Returns:
- the edge from u to v, or null if they are not adjacent.
- Throws:
IllegalArgumentException
-
endVertices
Returns the vertices of edge e as an array of length two. If the graph is directed, the first vertex is the origin, and the second is the destination. If the graph is undirected, the order is arbitrary.- Parameters:
e
- edge- Returns:
- an array of length two containing the vertices of edge e
- Throws:
IllegalArgumentException
-
opposite
Returns the vertex that is opposite to vertex v on edge e.- Parameters:
v
- an incident vertex to edge ee
- an edge incident to vertex v- Returns:
- the vertex that is opposite to vertex v on edge e
- Throws:
IllegalArgumentException
-
insertVertex
Inserts and returns a new vertex with the given element.- Parameters:
element
- data to be stored at newly created vertex- Returns:
- newly created Vertex instance
-
insertEdge
Inserts and returns a new edge between vertices u and v, storing given element.- Parameters:
u
- origin of new edgev
- destination of new edgeelement
- data to be stored at newly created edge- Returns:
- newly created Edge instance
- Throws:
IllegalArgumentException
- if u or v are invalid vertices, or if an edge already exists between u and v.
-
removeVertex
Removes a vertex and all its incident edges from the graph.- Parameters:
v
- the vertex to remove- Throws:
IllegalArgumentException
-
removeEdge
Removes an edge from the graph.- Parameters:
e
- the edge to remove- Throws:
IllegalArgumentException
-