public class Graph extends java.lang.Object
This implementation uses an adjacency-lists representation, which is a vertex-indexed array of @link{Bag} objects. All operations take constant time (in the worst case) except iterating over the neighbors of a vertex, which takes time proportional to the number of neighbors.
For additional documentation, see Section 4.1 of Algorithms, 4th Edition, by Robert Sedgewick and Kevin Wayne.
| Constructor and Description |
|---|
Graph(Graph G)
Initializes a new graph that is a deep copy of G.
|
Graph(In in)
Initializes a graph from an input stream.
|
Graph(int V)
Initializes an empty graph with V vertices and 0 edges.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addEdge(int v,
int w)
Adds the undirected edge v-w to this graph.
|
java.lang.Iterable<java.lang.Integer> |
adj(int v)
Returns the vertices adjacent to vertex v.
|
int |
E()
Returns the number of edges in this graph.
|
static void |
main(java.lang.String[] args)
Unit tests the Graph data type.
|
java.lang.String |
toString()
Returns a string representation of the graph.
|
int |
V()
Returns the number of vertices in this graph.
|
public Graph(int V)
java.lang.IllegalArgumentException - if V < 0public Graph(In in)
in - the input streampublic Graph(Graph G)
G - the graph to copypublic int V()
public int E()
public void addEdge(int v,
int w)
v - one vertex in the edgew - the other vertex in the edgejava.lang.IndexOutOfBoundsException - unless both 0 <= v < V and 0 <= w < Vpublic java.lang.Iterable<java.lang.Integer> adj(int v)
v - the vertexjava.lang.IndexOutOfBoundsException - unless 0 <= v < Vpublic java.lang.String toString()
toString in class java.lang.Objectpublic static void main(java.lang.String[] args)