Package algs42
Class Digraph
java.lang.Object
algs42.Digraph
The
Digraph
class represents an directed graph of vertices
named 0 through V-1.
It supports the following operations: add an edge to the graph,
iterate over all of the neighbors incident to a vertex.
Parallel edges and self-loops are permitted.
For additional documentation, see Section 5.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addEdge
(int v, int w) Add the directed edgev->w
to the digraph.adj
(int v) Return the list of vertices pointed to from vertex v as an Iterable.int
E()
Return the number of edges in the digraph.int
indegree
(int v) Returns the number of directed edges incident to vertexv
.static void
Test client.int
outdegree
(int v) Returns the number of directed edges incident from vertexv
.reverse()
Return the reverse of the digraph.void
toGraphviz
(String filename) Save a graphviz representation of the graph.toString()
Return a string representation of the digraph.int
V()
Return the number of vertices in the digraph.
-
Constructor Details
-
Digraph
Create an empty digraph with V vertices. -
Digraph
Initializes a digraph from the specified input stream. The format is the number of vertices V, followed by the number of edges E, followed by E pairs of vertices, with each entry separated by whitespace.- Parameters:
in
- the input stream- Throws:
IllegalArgumentException
- ifin
isnull
IllegalArgumentException
- if the endpoints of any edge are not in prescribed rangeIllegalArgumentException
- if the number of vertices or edges is negativeIllegalArgumentException
- if the input stream is in the wrong format
-
Digraph
Initializes a new digraph that is a deep copy of the specified digraph.- Parameters:
G
- the digraph to copy- Throws:
IllegalArgumentException
- ifG
isnull
-
-
Method Details
-
V
Return the number of vertices in the digraph. -
E
Return the number of edges in the digraph. -
addEdge
Add the directed edgev->w
to the digraph.- Throws:
IndexOutOfBoundsException
- unless both0 <= v < V
and0 <= w < V
-
adj
Return the list of vertices pointed to from vertex v as an Iterable.- Throws:
IndexOutOfBoundsException
- unless0 <= v < V
-
outdegree
Returns the number of directed edges incident from vertexv
. This is known as the outdegree of vertexv
.- Parameters:
v
- the vertex- Returns:
- the outdegree of vertex
v
- Throws:
IllegalArgumentException
- unless{@code 0 <= v < V}
-
indegree
Returns the number of directed edges incident to vertexv
. This is known as the indegree of vertexv
.- Parameters:
v
- the vertex- Returns:
- the indegree of vertex
v
- Throws:
IllegalArgumentException
- unless0 <= v < V
-
reverse
Return the reverse of the digraph. -
toString
Return a string representation of the digraph. -
toGraphviz
Save a graphviz representation of the graph. See graphviz.org. -
main
Test client.
-