public class Digraph extends Object
Digraph class represents a directed graph of vertices
named 0 through V - 1.
It supports the following two primary operations: add an edge to the digraph,
iterate over all of the vertices adjacent from a given vertex.
It also provides
methods for returning the indegree or outdegree of a vertex,
the number of vertices V in the digraph,
the number of edges E in the digraph, and the reverse digraph.
Parallel edges and self-loops are permitted.
This implementation uses an adjacency-lists representation, which
is a vertex-indexed array of Bag objects.
It uses Θ(E + V) space, where E is
the number of edges and V is the number of vertices.
The reverse() method takes Θ(E + V) time
and space; all other instancce methods take Θ(1) time. (Though, iterating over
the vertices returned by adj(int) takes time proportional
to the outdegree of the vertex.)
Constructing an empty digraph with V vertices takes
Θ(V) time; constructing a digraph with E edges
and V vertices takes Θ(E + V) time.
For additional documentation, see Section 4.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
| Constructor and Description |
|---|
Digraph(Digraph G)
Initializes a new digraph that is a deep copy of the specified digraph.
|
Digraph(In in)
Initializes a digraph from the specified input stream.
|
Digraph(int V)
Initializes an empty digraph with V vertices.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addEdge(int v,
int w)
Adds the directed edge v→w to this digraph.
|
Iterable<Integer> |
adj(int v)
Returns the vertices adjacent from vertex
v in this digraph. |
int |
E()
Returns the number of edges in this digraph.
|
int |
indegree(int v)
Returns the number of directed edges incident to vertex
v. |
static void |
main(String[] args)
Unit tests the
Digraph data type. |
int |
outdegree(int v)
Returns the number of directed edges incident from vertex
v. |
Digraph |
reverse()
Returns the reverse of the digraph.
|
String |
toString()
Returns a string representation of the graph.
|
int |
V()
Returns the number of vertices in this digraph.
|
public Digraph(int V)
V - the number of verticesIllegalArgumentException - if V < 0public Digraph(In in)
in - the input streamIllegalArgumentException - if in is nullIllegalArgumentException - 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 formatpublic Digraph(Digraph G)
G - the digraph to copyIllegalArgumentException - if G is nullpublic int V()
public int E()
public void addEdge(int v,
int w)
v - the tail vertexw - the head vertexIllegalArgumentException - unless both 0 <= v < V and 0 <= w < Vpublic Iterable<Integer> adj(int v)
v in this digraph.v - the vertexv in this digraph, as an iterableIllegalArgumentException - unless 0 <= v < Vpublic int outdegree(int v)
v.
This is known as the outdegree of vertex v.v - the vertexvIllegalArgumentException - unless 0 <= v < Vpublic int indegree(int v)
v.
This is known as the indegree of vertex v.v - the vertexvIllegalArgumentException - unless 0 <= v < Vpublic Digraph reverse()
public String toString()
public static void main(String[] args)
Digraph data type.args - the command-line arguments