Package com.zybooks.dsaj.graph
Class AdjacencyMapGraph<V,E>
java.lang.Object
com.zybooks.dsaj.graph.AdjacencyMapGraph<V,E>
- Type Parameters:
V
- Associated data stored with a vertexE
- Associated data stored with an edge
- All Implemented Interfaces:
Graph<V,
E>
An implementation for a graph structure using an adjacency map for each vertex.
Every vertex stores an element of type V.
Every edge stores an element of type E.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionedges()
Returns the edges of the graph as an iterable collectionendVertices
(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 graphint
Returns the number of vertices of the graphReturns the vertex that is opposite vertex v on edge e.int
Returns the number of edges for which vertex v is the origin.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.toString()
Returns a string representation of the graph.vertices()
Returns the vertices of the graph as an iterable collection
-
Constructor Details
-
AdjacencyMapGraph
public AdjacencyMapGraph(boolean directed) Constructs an empty graph. The parameter determines whether this is an undirected or directed graph.- Parameters:
directed
- true if a directed graph, false if an undirected graph
-
-
Method Details
-
numVertices
public int numVertices()Returns the number of vertices of the graph- Specified by:
numVertices
in interfaceGraph<V,
E> - Returns:
- the number of vertices of the graph
-
vertices
Returns the vertices of the graph as an iterable collection -
numEdges
public int numEdges()Returns the number of edges of the graph -
edges
Returns the edges of the graph as an iterable collection -
outDegree
Returns the number of edges for which vertex v is the origin. Note that for an undirected graph, this is the same result returned by inDegree(v).- Specified by:
outDegree
in interfaceGraph<V,
E> - Parameters:
v
- the vertex- Returns:
- the number of edges leaving vertex v
- 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(v).- Specified by:
outgoingEdges
in interfaceGraph<V,
E> - 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
-
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(v).- Specified by:
inDegree
in interfaceGraph<V,
E> - 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
-
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(v).- Specified by:
incomingEdges
in interfaceGraph<V,
E> - 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.- Specified by:
getEdge
in interfaceGraph<V,
E> - 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.- Specified by:
endVertices
in interfaceGraph<V,
E> - Parameters:
e
- edge- Returns:
- an array of length two containing the vertices of edge e
- Throws:
IllegalArgumentException
-
opposite
Returns the vertex that is opposite vertex v on edge e.- Specified by:
opposite
in interfaceGraph<V,
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.- Specified by:
insertVertex
in interfaceGraph<V,
E> - 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.- Specified by:
insertEdge
in interfaceGraph<V,
E> - 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.- Specified by:
removeVertex
in interfaceGraph<V,
E> - Parameters:
v
- the vertex to remove- Throws:
IllegalArgumentException
-
removeEdge
Description copied from interface:Graph
Removes an edge from the graph.- Specified by:
removeEdge
in interfaceGraph<V,
E> - Parameters:
e
- the edge to remove- Throws:
IllegalArgumentException
-
toString
Returns a string representation of the graph. This is used only for debugging; do not rely on the string representation.
-