public class Digraph extends Object
This implementation uses an adjacency-lists representation, which
is a vertex-indexed array of Bag objects.
All operations take constant time (in the worst case) except
iterating over the vertices adjacent from a given vertex, which takes
time proportional to the number of such vertices.
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 G.
|
Digraph(edu.princeton.cs.introcs.In in)
Initializes a digraph from an 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 the digraph.
|
Iterable<Integer> |
adj(int v)
Returns the vertices adjacent from vertex v in the digraph.
|
int |
E()
Returns the number of edges in the digraph.
|
static void |
main(String[] args)
Unit tests the Digraph data type.
|
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 the digraph.
|
public Digraph(int V)
V - the number of verticesIllegalArgumentException - if V < 0public Digraph(edu.princeton.cs.introcs.In in)
in - the input streamIndexOutOfBoundsException - if the endpoints of any edge are not in prescribed rangeIllegalArgumentException - if the number of vertices or edges is negativepublic Digraph(Digraph G)
G - the digraph to copypublic int V()
public int E()
public void addEdge(int v,
int w)
v - the tail vertexw - the head vertexIndexOutOfBoundsException - unless both 0 <= v < V and 0 <= w < Vpublic Iterable<Integer> adj(int v)
v - the vertexIndexOutOfBoundsException - unless 0 <= v < Vpublic Digraph reverse()
public String toString()
public static void main(String[] args)
Copyright © 2014. All Rights Reserved.